Bug 14576: Allow arbitrary automatic update of location on checkin
[koha-equinox.git] / koha-tmpl / intranet-tmpl / prog / js / pages / preferences.js
1 /* global KOHA MSG_MADE_CHANGES CodeMirror MSG_CLICK_TO_EXPAND MSG_CLICK_TO_COLLAPSE to_highlight search_jumped humanMsg MSG_NOTHING_TO_SAVE MSG_MODIFIED MSG_SAVING MSG_SAVED_PREFERENCE dataTablesDefaults */
2 // We can assume 'KOHA' exists, as we depend on KOHA.AJAX
3
4 KOHA.Preferences = {
5     Save: function ( form ) {
6         modified_prefs = $( form ).find( '.modified' );
7         // $.serialize removes empty value, we need to keep them.
8         // If a multiple select has all its entries unselected
9         var unserialized = new Array();
10         $(modified_prefs).each(function(){
11             if ( $(this).attr('multiple') && $(this).val() == null ) {
12                 unserialized.push($(this));
13             }
14         });
15         data = modified_prefs.serialize();
16         $(unserialized).each(function(){
17             data += '&' + $(this).attr('name') + '=';
18         });
19         if ( !data ) {
20             humanMsg.displayAlert( MSG_NOTHING_TO_SAVE );
21             return;
22         }
23         KOHA.AJAX.MarkRunning( $( form ).find( '.save-all' ), MSG_SAVING );
24         KOHA.AJAX.Submit( {
25             data: data,
26             url: '/cgi-bin/koha/svc/config/systempreferences/',
27             success: function ( data ) { KOHA.Preferences.Success( form ) },
28             complete: function () { KOHA.AJAX.MarkDone( $( form ).find( '.save-all' ) ) }
29         } );
30     },
31     Success: function ( form ) {
32         var msg = "";
33         modified_prefs.each(function(){
34             var modified_pref = $(this).attr("id");
35             modified_pref = modified_pref.replace("pref_","");
36             msg += "<strong>"+ MSG_SAVED_PREFERENCE.format(modified_pref) + "</strong>\n";
37         });
38         humanMsg.displayAlert(msg);
39
40         $( form )
41             .find( '.modified-warning' ).remove().end()
42             .find( '.modified' ).removeClass('modified');
43         KOHA.Preferences.Modified = false;
44     }
45 };
46
47 $( document ).ready( function () {
48
49     $("table.preferences").dataTable($.extend(true, {}, dataTablesDefaults, {
50         "sDom": 't',
51         "aoColumnDefs": [
52             { "aTargets": [ -1 ], "bSortable": false, "bSearchable": false }
53         ],
54         "bPaginate": false
55     }));
56
57     function mark_modified() {
58         $( this.form ).find( '.save-all' ).prop('disabled', false);
59         $( this ).addClass( 'modified' );
60         var name_cell = $( this ).parents( '.name-row' ).find( '.name-cell' );
61         if ( !name_cell.find( '.modified-warning' ).length )
62             name_cell.append( '<em class="modified-warning">('+MSG_MODIFIED+')</em>' );
63         KOHA.Preferences.Modified = true;
64     }
65
66     $( '.prefs-tab' )
67         .find( 'input.preference, textarea.preference' ).on('input', function () {
68             if ( this.defaultValue === undefined || this.value != this.defaultValue ) mark_modified.call( this );
69         } ).end()
70         .find( 'select.preference' ).change( mark_modified );
71     $('.preference-checkbox').change( function () {
72         $('.preference-checkbox').addClass('modified');
73         mark_modified.call(this);
74     } );
75
76     $(".set_syspref").click(function() {
77         var s = $(this).attr('data-syspref');
78         var v = $(this).attr('data-value');
79         // populate the input with the value in data-value
80         $("#pref_"+s).val(v);
81         // pass the DOM element to trigger "modified" to enable submit button
82         mark_modified.call($("#pref_"+s)[0]);
83         return false;
84     });
85
86     $(".sortable").sortable();
87     $(".sortable").on( "sortchange", function( event, ui ) {
88         // This is not exact but we just need to trigger a change
89         $(ui.item.find('input:first')).change();
90     } );
91
92     window.onbeforeunload = function () {
93         if ( KOHA.Preferences.Modified ) {
94             return MSG_MADE_CHANGES;
95         }
96     };
97
98     $( '.prefs-tab .action .cancel' ).click( function () { KOHA.Preferences.Modified = false } );
99
100     $( '.prefs-tab .save-all' ).prop('disabled', true).click( function () {
101         KOHA.Preferences.Save( this.form );
102         return false;
103     } );
104
105     $( ".expand-textarea" ).on("click", function(e){
106         e.preventDefault();
107         $(this).hide();
108         var target = $(this).data("target");
109         var syntax = $(this).data("syntax");
110         $("#collapse_" + target ).show();
111         if( syntax ){
112             var editor = CodeMirror.fromTextArea( document.getElementById( "pref_" + target ), {
113                 lineNumbers: true,
114                 mode: syntax,
115                 lineWrapping: true
116             });
117             editor.on("change", function(){
118                 mark_modified.call( $("#pref_" + target )[0]);
119             });
120             editor.on("blur", function(){
121                 editor.save();
122             });
123         } else {
124             $("#pref_" + target ).show();
125         }
126     });
127
128     $( ".collapse-textarea" ).on("click", function(e){
129         e.preventDefault();
130         $(this).hide();
131         var target = $(this).data("target");
132         var syntax = $(this).data("syntax");
133         $("#expand_" + target ).show();
134         if( syntax ){
135             var editor = $("#pref_" + target ).next(".CodeMirror")[0].CodeMirror;
136             editor.toTextArea();
137         }
138         $("#pref_" + target ).hide();
139     });
140
141     $("h3").attr("class","expanded").attr("title",MSG_CLICK_TO_EXPAND);
142     var collapsible = $(".collapsed,.expanded");
143
144     $(collapsible).on("click",function(){
145         var panel = $(this).next("div");
146         if(panel.is(":visible")){
147             $(this).addClass("collapsed").removeClass("expanded").attr("title",MSG_CLICK_TO_EXPAND);
148             panel.hide();
149         } else {
150             $(this).addClass("expanded").removeClass("collapsed").attr("title",MSG_CLICK_TO_COLLAPSE);
151             panel.show();
152         }
153     });
154
155     if ( to_highlight ) {
156         var words = to_highlight.split( ' ' );
157         $( '.prefs-tab table' ).find( 'td, th' ).not( '.name-cell' ).each( function ( i, td ) {
158             $.each( words, function ( i, word ) { $( td ).highlight( word ) } );
159         } ).find( 'option, textarea' ).removeHighlight();
160     }
161
162     if ( search_jumped ) {
163         document.location.hash = "jumped";
164     }
165
166     $("#pref_UpdateItemLocationOnCheckin").change(function(){
167         var the_text = $(this).val();
168         var alert_text = '';
169         if ( the_text.indexOf('_ALL_:') != -1 ) alert_text = MSG_ALL_VALUE_WARN + '\n';
170         var split_text  =the_text.split("\n");
171         var alert_issues = '';
172         var issue_count = 0;
173         var reg_check = /.*:\s.*/;
174         for (var i=0; i < split_text.length; i++){
175             if ( !split_text[i].match(reg_check) && split_text[i].length ) {
176                 alert_issues+=split_text[i]+"\n";
177                 issue_count++;
178             }
179         }
180         if (issue_count) alert_text += "\n"+ MSG_UPD_LOC_FORMAT_WARN  +"\n"+alert_issues;
181         if ( alert_text.length )  alert(alert_text);
182     });
183
184 } );