Bug 11464: Prevent wrong "nothing to save" message on saving syspref
[koha.git] / koha-tmpl / intranet-tmpl / prog / en / js / pages / preferences.js
blob04135ef4f5df6f80dc428d9e6b3df657787b89a1
1 // We can assume 'KOHA' exists, as we depend on KOHA.AJAX
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);
39         $( form )
40             .find( '.modified-warning' ).remove().end()
41             .find( '.modified' ).removeClass('modified');
42         KOHA.Preferences.Modified = false;
43     }
46 $( document ).ready( function () {
48     $("table.preferences").dataTable($.extend(true, {}, dataTablesDefaults, {
49         "sDom": 't',
50         "aoColumnDefs": [
51             { "aTargets": [ -1 ], "bSortable": false, "bSearchable": false }
52         ],
53         "bPaginate": false
54     }));
56     function mark_modified() {
57         $( this.form ).find( '.save-all' ).removeAttr( 'disabled' );
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     }
65     $( '.prefs-tab' )
66         .find( 'input.preference, textarea.preference' ).on('change', 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     } );
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     });
85     window.onbeforeunload = function () {
86         if ( KOHA.Preferences.Modified ) {
87             return MSG_MADE_CHANGES;
88         }
89     }
91     $( '.prefs-tab .action .cancel' ).click( function () { KOHA.Preferences.Modified = false } );
93     $( '.prefs-tab .save-all' ).attr( 'disabled', true ).click( function () {
94         KOHA.Preferences.Save( this.form );
95         return false;
96     } );
98     $( '.prefs-tab .expand-textarea' ).show().click( function () {
99         $( this ).hide().nextAll( 'textarea, input[type=submit]' )
100             .animate( { height: 'show', queue: false } )
101             .animate( { opacity: 1 } );
103         return false;
104     } ).nextAll( 'textarea, input[type=submit]' ).hide().css( { opacity: 0 } );
106     $("h3").attr("class","expanded").attr("title",MSG_CLICK_TO_EXPAND);
107     var collapsible = $(".collapsed,.expanded");
109     $(collapsible).on("click",function(){
110         var panel = $(this).next("div");
111         if(panel.is(":visible")){
112             $(this).addClass("collapsed").removeClass("expanded").attr("title",MSG_CLICK_TO_EXPAND);
113             panel.hide();
114         } else {
115             $(this).addClass("expanded").removeClass("collapsed").attr("title",MSG_CLICK_TO_COLLAPSE);
116             panel.show();
117         }
118     });
120     if ( to_highlight ) {
121         var words = to_highlight.split( ' ' );
122         $( '.prefs-tab table' ).find( 'td, th' ).not( '.name-cell' ).each( function ( i, td ) {
123             $.each( words, function ( i, word ) { $( td ).highlight( word ) } );
124         } ).find( 'option, textarea' ).removeHighlight();
125     }
127     if ( search_jumped ) {
128         document.location.hash = "jumped";
129     }
130 } );