Bug 16242 - Move staff client JavaScript out of language directory
[koha.git] / koha-tmpl / intranet-tmpl / prog / js / pages / preferences.js
1 // We can assume 'KOHA' exists, as we depend on KOHA.AJAX
2
3 KOHA.Preferences = {
4     Save: function ( form ) {
5         modified_prefs = $( form ).find( '.modified' );
6         // $.serialize removes empty value, we need to keep them.
7         // If a multiple select has all its entries unselected
8         var unserialized = new Array();
9         $(modified_prefs).each(function(){
10             if ( $(this).attr('multiple') && $(this).val() == null ) {
11                 unserialized.push($(this));
12             }
13         });
14         data = modified_prefs.serialize();
15         $(unserialized).each(function(){
16             data += '&' + $(this).attr('name') + '=';
17         });
18         if ( !data ) {
19             humanMsg.displayAlert( MSG_NOTHING_TO_SAVE );
20             return;
21         }
22         KOHA.AJAX.MarkRunning( $( form ).find( '.save-all' ), MSG_SAVING );
23         KOHA.AJAX.Submit( {
24             data: data,
25             url: '/cgi-bin/koha/svc/config/systempreferences/',
26             success: function ( data ) { KOHA.Preferences.Success( form ) },
27             complete: function () { KOHA.AJAX.MarkDone( $( form ).find( '.save-all' ) ) }
28         } );
29     },
30     Success: function ( form ) {
31         var msg = "";
32         modified_prefs.each(function(){
33             var modified_pref = $(this).attr("id");
34             modified_pref = modified_pref.replace("pref_","");
35             msg += "<strong>"+ MSG_SAVED_PREFERENCE.format(modified_pref) + "</strong>\n";
36         });
37         humanMsg.displayAlert(msg);
38
39         $( form )
40             .find( '.modified-warning' ).remove().end()
41             .find( '.modified' ).removeClass('modified');
42         KOHA.Preferences.Modified = false;
43     }
44 };
45
46 $( document ).ready( function () {
47
48     $("table.preferences").dataTable($.extend(true, {}, dataTablesDefaults, {
49         "sDom": 't',
50         "aoColumnDefs": [
51             { "aTargets": [ -1 ], "bSortable": false, "bSearchable": false }
52         ],
53         "bPaginate": false
54     }));
55
56     function mark_modified() {
57         $( this.form ).find( '.save-all' ).prop('disabled', false);
58         $( this ).addClass( 'modified' );
59         var name_cell = $( this ).parents( '.name-row' ).find( '.name-cell' );
60         if ( !name_cell.find( '.modified-warning' ).length )
61             name_cell.append( '<em class="modified-warning">('+MSG_MODIFIED+')</em>' );
62         KOHA.Preferences.Modified = true;
63     }
64
65     $( '.prefs-tab' )
66         .find( 'input.preference, textarea.preference' ).on('input', function () {
67             if ( this.defaultValue === undefined || this.value != this.defaultValue ) mark_modified.call( this );
68         } ).end()
69         .find( 'select.preference' ).change( mark_modified );
70     $('.preference-checkbox').change( function () {
71         $('.preference-checkbox').addClass('modified');
72         mark_modified.call(this);
73     } );
74
75     $(".set_syspref").click(function() {
76         var s = $(this).attr('data-syspref');
77         var v = $(this).attr('data-value');
78         // populate the input with the value in data-value
79         $("#pref_"+s).val(v);
80         // pass the DOM element to trigger "modified" to enable submit button
81         mark_modified.call($("#pref_"+s)[0]);
82         return false;
83     });
84
85     window.onbeforeunload = function () {
86         if ( KOHA.Preferences.Modified ) {
87             return MSG_MADE_CHANGES;
88         }
89     }
90
91     $( '.prefs-tab .action .cancel' ).click( function () { KOHA.Preferences.Modified = false } );
92
93     $( '.prefs-tab .save-all' ).prop('disabled', true).click( function () {
94         KOHA.Preferences.Save( this.form );
95         return false;
96     } );
97
98     $( '.prefs-tab .expand-textarea' ).show().click( function () {
99         $( this ).hide().nextAll( 'textarea, input[type=submit], a' )
100             .animate( { height: 'show', queue: false } )
101             .animate( { opacity: 1 } );
102
103         return false;
104     } ).nextAll( 'textarea, input[type=submit]' ).hide().css( { opacity: 0 } );
105
106     $( '.prefs-tab .collapse-textarea' ).hide().click( function () {
107         $( this ).show().prevAll( 'textarea, input[type=submit]' )
108             .animate( { height: 'hide', queue: false } )
109             .animate( { opacity: 0 } );
110
111         $( this ).hide().prevAll( 'a' ).show();
112         return false;
113     });
114
115
116     $("h3").attr("class","expanded").attr("title",MSG_CLICK_TO_EXPAND);
117     var collapsible = $(".collapsed,.expanded");
118
119     $(collapsible).on("click",function(){
120         var panel = $(this).next("div");
121         if(panel.is(":visible")){
122             $(this).addClass("collapsed").removeClass("expanded").attr("title",MSG_CLICK_TO_EXPAND);
123             panel.hide();
124         } else {
125             $(this).addClass("expanded").removeClass("collapsed").attr("title",MSG_CLICK_TO_COLLAPSE);
126             panel.show();
127         }
128     });
129
130     if ( to_highlight ) {
131         var words = to_highlight.split( ' ' );
132         $( '.prefs-tab table' ).find( 'td, th' ).not( '.name-cell' ).each( function ( i, td ) {
133             $.each( words, function ( i, word ) { $( td ).highlight( word ) } );
134         } ).find( 'option, textarea' ).removeHighlight();
135     }
136
137     if ( search_jumped ) {
138         document.location.hash = "jumped";
139     }
140 } );