Bug 9444 [Revised] Use DataTables in system preferences editor
[koha_fer] / koha-tmpl / intranet-tmpl / prog / en / 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         data = modified_prefs.serialize();
7         if ( !data ) {
8             humanMsg.displayAlert( MSG_NOTHING_TO_SAVE );
9             return;
10         }
11         KOHA.AJAX.MarkRunning( $( form ).find( '.save-all' ), _( MSG_SAVING ) );
12         KOHA.AJAX.Submit( {
13             data: data,
14             url: '/cgi-bin/koha/svc/config/systempreferences/',
15             success: function ( data ) { KOHA.Preferences.Success( form ) },
16             complete: function () { KOHA.AJAX.MarkDone( $( form ).find( '.save-all' ) ) }
17         } );
18     },
19     Success: function ( form ) {
20         var msg = "";
21         modified_prefs.each(function(){
22             var modified_pref = $(this).attr("id");
23             modified_pref = modified_pref.replace("pref_","");
24             msg += '<strong>Saved preference '+modified_pref+'</strong>\n';
25         });
26         humanMsg.displayAlert(msg);
27
28         $( form )
29             .find( '.modified-warning' ).remove().end()
30             .find( '.modified' ).removeClass('modified');
31         KOHA.Preferences.Modified = false;
32     }
33 };
34
35 $( document ).ready( function () {
36
37     $("table.preferences").dataTable($.extend(true, {}, dataTablesDefaults, {
38         "sDom": 't',
39         "aoColumnDefs": [
40             { "aTargets": [ -1 ], "bSortable": false, "bSearchable": false }
41         ],
42         "bPaginate": false
43     }));
44
45     function mark_modified() {
46         $( this.form ).find( '.save-all' ).removeAttr( 'disabled' );
47         $( this ).addClass( 'modified' );
48         var name_cell = $( this ).parents( '.name-row' ).find( '.name-cell' );
49                 if ( !name_cell.find( '.modified-warning' ).length )
50             name_cell.append( '<em class="modified-warning">('+MSG_MODIFIED+')</em>' );
51         KOHA.Preferences.Modified = true;
52     }
53
54     $( '.prefs-tab' )
55         .find( 'input.preference, textarea.preference' ).keyup( function () {
56             if ( this.defaultValue === undefined || this.value != this.defaultValue ) mark_modified.call( this );
57         } ).end()
58         .find( 'select.preference' ).change( mark_modified );
59     $('.preference-checkbox').change( function () {
60         $('.preference-checkbox').addClass('modified');
61         mark_modified.call(this);
62     } );
63
64     $(".set_syspref").click(function() {
65         var s = $(this).attr('data-syspref');
66         var v = $(this).attr('data-value');
67         // populate the input with the value in data-value
68         $("#pref_"+s).val(v);
69         // pass the DOM element to trigger "modified" to enable submit button
70         mark_modified.call($("#pref_"+s)[0]);
71         return false;
72     });
73
74     window.onbeforeunload = function () {
75         if ( KOHA.Preferences.Modified ) {
76             return MSG_MADE_CHANGES;
77         }
78     }
79
80     $( '.prefs-tab .action .cancel' ).click( function () { KOHA.Preferences.Modified = false } );
81
82     $( '.prefs-tab .save-all' ).attr( 'disabled', true ).click( function () {
83         KOHA.Preferences.Save( this.form );
84         return false;
85     } );
86
87     $( '.prefs-tab .expand-textarea' ).show().click( function () {
88         $( this ).hide().nextAll( 'textarea, input[type=submit]' )
89             .animate( { height: 'show', queue: false } )
90             .animate( { opacity: 1 } );
91
92         return false;
93     } ).nextAll( 'textarea, input[type=submit]' ).hide().css( { opacity: 0 } );
94
95     $("h3").attr("class","expanded").attr("title",MSG_CLICK_TO_EXPAND);
96     var collapsible = $(".collapsed,.expanded");
97
98     $(collapsible).toggle(
99         function () {
100             $(this).addClass("collapsed").removeClass("expanded").attr("title",MSG_CLICK_TO_EXPAND);
101             $(this).next("div").hide();
102         },
103         function () {
104             $(this).addClass("expanded").removeClass("collapsed").attr("title",MSG_CLICK_TO_COLLAPSE);
105             $(this).next("div").show();
106         }
107     );
108
109     if ( to_highlight ) {
110         var words = to_highlight.split( ' ' );
111         $( '.prefs-tab table' ).find( 'td, th' ).not( '.name-cell' ).each( function ( i, td ) {
112             $.each( words, function ( i, word ) { $( td ).highlight( word ) } );
113         } ).find( 'option, textarea' ).removeHighlight();
114     }
115
116     if ( search_jumped ) {
117         document.location.hash = "jumped";
118     }
119 } );