Bug 20115: Trigger "modified" when sort is changed
[koha.git] / koha-tmpl / intranet-tmpl / prog / js / pages / preferences.js
blobf7ed5e112118b4917c64a12121a06ce5c81ef2b8
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' ).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     }
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     } );
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     $("dl.sortable").sortable();
86     $("dl.sortable").on( "sortchange", function( event, ui ) {
87         // This is not exact but we just need to trigger a change
88         $(ui.item.find('input:first')).change();
89     } );
91     window.onbeforeunload = function () {
92         if ( KOHA.Preferences.Modified ) {
93             return MSG_MADE_CHANGES;
94         }
95     }
97     $( '.prefs-tab .action .cancel' ).click( function () { KOHA.Preferences.Modified = false } );
99     $( '.prefs-tab .save-all' ).prop('disabled', true).click( function () {
100         KOHA.Preferences.Save( this.form );
101         return false;
102     } );
104     $( '.prefs-tab .expand-textarea' ).show().click( function () {
105         $( this ).hide().nextAll( 'textarea, input[type=submit], a' )
106             .animate( { height: 'show', queue: false } )
107             .animate( { opacity: 1 } );
109         return false;
110     } ).nextAll( 'textarea, input[type=submit]' ).hide().css( { opacity: 0 } );
112     $( '.prefs-tab .collapse-textarea' ).hide().click( function () {
113         $( this ).show().prevAll( 'textarea, input[type=submit]' )
114             .animate( { height: 'hide', queue: false } )
115             .animate( { opacity: 0 } );
117         $( this ).hide().prevAll( 'a' ).show();
118         return false;
119     });
122     $("h3").attr("class","expanded").attr("title",MSG_CLICK_TO_EXPAND);
123     var collapsible = $(".collapsed,.expanded");
125     $(collapsible).on("click",function(){
126         var panel = $(this).next("div");
127         if(panel.is(":visible")){
128             $(this).addClass("collapsed").removeClass("expanded").attr("title",MSG_CLICK_TO_EXPAND);
129             panel.hide();
130         } else {
131             $(this).addClass("expanded").removeClass("collapsed").attr("title",MSG_CLICK_TO_COLLAPSE);
132             panel.show();
133         }
134     });
136     if ( to_highlight ) {
137         var words = to_highlight.split( ' ' );
138         $( '.prefs-tab table' ).find( 'td, th' ).not( '.name-cell' ).each( function ( i, td ) {
139             $.each( words, function ( i, word ) { $( td ).highlight( word ) } );
140         } ).find( 'option, textarea' ).removeHighlight();
141     }
143     if ( search_jumped ) {
144         document.location.hash = "jumped";
145     }
146 } );