Bug 29695: Make "modalselect" syspref's values translatable
[koha.git] / koha-tmpl / intranet-tmpl / prog / js / pages / preferences.js
1 /* global KOHA CodeMirror to_highlight search_jumped humanMsg dataTablesDefaults themelang */
2 // We can assume 'KOHA' exists, as we depend on KOHA.AJAX
3
4 KOHA.Preferences = {
5     Save: function ( form ) {
6         if ( ! $(form).valid() ) {
7             humanMsg.displayAlert( __("Error: presence of invalid data prevent saving. Please make the corrections and try again.") );
8             return;
9         }
10
11         modified_prefs = $( form ).find( '.modified' );
12         // $.serialize removes empty value, we need to keep them.
13         // If a multiple select has all its entries unselected
14         var unserialized = new Array();
15         $(modified_prefs).each(function(){
16             if ( $(this).attr('multiple') && $(this).val() == null ) {
17                 unserialized.push($(this));
18             }
19         });
20         data = modified_prefs.serialize();
21         $(unserialized).each(function(){
22             data += '&' + $(this).attr('name') + '=';
23         });
24         if ( !data ) {
25             humanMsg.displayAlert( __("Nothing to save") );
26             return;
27         }
28         KOHA.AJAX.MarkRunning($(form).find('.save-all'), __("Saving...") );
29         KOHA.AJAX.Submit( {
30             data: data,
31             url: '/cgi-bin/koha/svc/config/systempreferences/',
32             success: function ( data ) { KOHA.Preferences.Success( form ) },
33             complete: function () { KOHA.AJAX.MarkDone( $( form ).find( '.save-all' ) ) }
34         } );
35     },
36     Success: function ( form ) {
37         var msg = "";
38         modified_prefs.each(function(){
39             var modified_pref = $(this).attr("id");
40             modified_pref = modified_pref.replace("pref_","");
41             msg += "<strong>" + __("Saved preference %s").format(modified_pref) + "</strong>\n";
42         });
43         humanMsg.displayAlert(msg);
44
45         $( form )
46             .find( '.modified-warning' ).remove().end()
47             .find( '.modified' ).removeClass('modified');
48         KOHA.Preferences.Modified = false;
49     }
50 };
51
52 function mark_modified() {
53     $( this.form ).find( '.save-all' ).prop('disabled', false);
54     $( this ).addClass( 'modified' );
55     var name_cell = $( this ).parents( '.name-row' ).find( '.name-cell' );
56     if ( !name_cell.find( '.modified-warning' ).length )
57         name_cell.append('<em class="modified-warning">(' + __("modified") + ')</em>');
58     KOHA.Preferences.Modified = true;
59 }
60
61 window.onbeforeunload = function () {
62     if ( KOHA.Preferences.Modified ) {
63         return __("You have made changes to system preferences.");
64     }
65 };
66
67 $( document ).ready( function () {
68
69     $("table.preferences").dataTable($.extend(true, {}, dataTablesDefaults, {
70         "sDom": 't',
71         "aoColumnDefs": [
72             { "aTargets": [ -1 ], "bSortable": false, "bSearchable": false }
73         ],
74         "bPaginate": false
75     }));
76
77     $( '.prefs-tab' )
78         .find( 'input.preference, textarea.preference' ).on('input', function () {
79             if ( this.defaultValue === undefined || this.value != this.defaultValue ) mark_modified.call( this );
80         } ).end()
81         .find( 'select.preference' ).change( mark_modified );
82     $('.preference-checkbox').change( function () {
83         $('.preference-checkbox').addClass('modified');
84         mark_modified.call(this);
85     } );
86
87     $(".set_syspref").click(function() {
88         var s = $(this).attr('data-syspref');
89         var v = $(this).attr('data-value');
90         // populate the input with the value in data-value
91         $("#pref_"+s).val(v);
92         // pass the DOM element to trigger "modified" to enable submit button
93         mark_modified.call($("#pref_"+s)[0]);
94         return false;
95     });
96
97     $(".sortable").sortable();
98     $(".sortable").on( "sortchange", function( event, ui ) {
99         // This is not exact but we just need to trigger a change
100         $(ui.item.find('input:first')).change();
101     } );
102
103     $( '.prefs-tab .action .cancel' ).click( function () { KOHA.Preferences.Modified = false } );
104
105     $( '.prefs-tab .save-all' ).prop('disabled', true).click( function () {
106         KOHA.Preferences.Save( this.form );
107         return false;
108     });
109
110     $( ".expand-textarea" ).on("click", function(e){
111         e.preventDefault();
112         $(this).hide();
113         var target = $(this).data("target");
114         var syntax = $(this).data("syntax");
115         $("#collapse_" + target ).show();
116         if( syntax ){
117             var editor = CodeMirror.fromTextArea( document.getElementById( "pref_" + target ), {
118                 lineNumbers: true,
119                 mode: syntax,
120                 lineWrapping: true,
121                 viewportMargin: Infinity,
122                 gutters: ["CodeMirror-lint-markers"],
123                 lint: true
124             });
125             editor.on("change", function(){
126                 mark_modified.call( $("#pref_" + target )[0]);
127             });
128             editor.on("blur", function(){
129                 editor.save();
130             });
131         } else {
132             $("#pref_" + target ).show();
133         }
134     });
135
136     $( ".collapse-textarea" ).on("click", function(e){
137         e.preventDefault();
138         $(this).hide();
139         var target = $(this).data("target");
140         var syntax = $(this).data("syntax");
141         $("#expand_" + target ).show();
142         if( syntax ){
143             var editor = $("#pref_" + target ).next(".CodeMirror")[0].CodeMirror;
144             editor.toTextArea();
145         }
146         $("#pref_" + target ).hide();
147     });
148
149     $("h3").attr("class", "expanded").attr("title", __("Click to collapse this section"));
150     var collapsible = $(".collapsed,.expanded");
151
152     $(collapsible).on("click",function(){
153         var h3Id = $(this).attr("id");
154         var panel = $("#collapse_" + h3Id);
155         if(panel.is(":visible")){
156             $(this).addClass("collapsed").removeClass("expanded").attr("title", __("Click to expand this section") );
157             panel.hide();
158         } else {
159             $(this).addClass("expanded").removeClass("collapsed").attr("title", __("Click to collapse this section") );
160             panel.show();
161         }
162     });
163
164     $(".pref_sublink").on("click", function(){
165         /* If the user clicks a sub-menu link in the sidebar,
166            check to see if it is collapsed. If so, expand it */
167         var href = $(this).attr("href");
168         href = href.replace("#","");
169         var panel = $("#collapse_" + href );
170         if( panel.is(":hidden") ){
171             $("#" + href).addClass("expanded").removeClass("collapsed").attr("title", __("Click to collapse this section") );
172             panel.show();
173         }
174     });
175
176     if ( to_highlight ) {
177         var words = to_highlight.split( ' ' );
178         $( '.prefs-tab table' ).find( 'td, th' ).not( '.name-cell' ).each( function ( i, td ) {
179             $.each( words, function ( i, word ) { $( td ).highlight( word ) } );
180         } ).find( 'option, textarea' ).removeHighlight();
181     }
182
183     if ( search_jumped ) {
184         document.location.hash = "jumped";
185     }
186
187     $("#pref_UpdateItemLocationOnCheckin").change(function(){
188         var the_text = $(this).val();
189         var alert_text = '';
190         if (the_text.indexOf('_ALL_:') != -1) alert_text = __("Note: _ALL_ value will override all other values") + '\n';
191         var split_text  =the_text.split("\n");
192         var alert_issues = '';
193         var issue_count = 0;
194         var reg_check = /.*:\s.*/;
195         for (var i=0; i < split_text.length; i++){
196             if ( !split_text[i].match(reg_check) && split_text[i].length ) {
197                 alert_issues+=split_text[i]+"\n";
198                 issue_count++;
199             }
200         }
201         if (issue_count) alert_text += "\n" + __("The following values are not formatted correctly:") + "\n" + alert_issues;
202         if ( alert_text.length )  alert(alert_text);
203     });
204
205     $(".prefs-tab form").each(function () {
206         $(this).validate({
207             rules: { },
208             errorPlacement: function(error, element) {
209                 var placement = $(element).parent();
210                 if (placement) {
211                     $(placement).append(error)
212                 } else {
213                     error.insertAfter(element);
214                 }
215             }
216         });
217     });
218
219     $(".preference-email").each(function() {
220         $(this).rules("add", {
221             email: true
222         });
223     });
224
225
226     $(".modalselect").on("click", function(){
227         var datasource = $(this).data("source");
228         var exclusions = $(this).data("exclusions").split('|');
229         var required = $(this).data("required").split('|');
230         var pref_name = this.id.replace(/pref_/, '');
231         var pref_value = this.value;
232         var prefs = pref_value.split("|");
233
234         let data = db_columns[datasource];
235         var items = [];
236         var checked = "";
237         var readonly = "";
238         var disabled = "";
239         var style = "";
240         $.each( Object.keys(data).sort(), function( i, key ){
241             if( prefs.indexOf( key ) >= 0 ){
242                 checked = ' checked="checked" ';
243             } else {
244                 checked = "";
245             }
246             if( required.indexOf( key ) >= 0 ){
247                 style = "required";
248                 checked  = ' checked="checked" ';
249             } else if( exclusions.indexOf( key ) >= 0 ){
250                 style = "disabled";
251                 disabled = ' disabled="disabled" ';
252                 checked  = "";
253             } else {
254                 style = "";
255                 disabled = "";
256             }
257             items.push('<label class="' + style +'"><input class="dbcolumn_selection" type="checkbox" id="' + key + '"' + checked + disabled + ' name="pref" value="' + key + '" /> ' + data[key]+ ' (' + key + ')</label>');
258         });
259         $("<div/>", {
260             "class": "columns-2",
261             html: items.join("")
262         }).appendTo("#prefModalForm");
263
264         $("#saveModalPrefs").data("target", this.id );
265         $("#prefModalLabel").text( pref_name );
266         $("#prefModal").modal("show");
267     });
268
269     $("#saveModalPrefs").on("click", function(){
270         var formfieldid = $("#" + $(this).data("target") );
271         var prefs = [];
272         $("#prefModal input[type='checkbox']").each(function(){
273             if( $(this).prop("checked") ){
274                 prefs.push( this.value );
275             }
276         });
277
278         formfieldid.val( prefs.join("|") )
279             .addClass("modified");
280         mark_modified.call( formfieldid );
281         KOHA.Preferences.Save( formfieldid.closest("form") );
282         $("#prefModal").modal("hide");
283     });
284
285     $("#prefModal").on("hide.bs.modal", function(){
286         $("#prefModalLabel,#prefModalForm").html("");
287         $("#saveModalPrefs").data("target", "" );
288     });
289
290     $("#select_all").on("click",function(e){
291         e.preventDefault();
292         $("label:not(.required) .dbcolumn_selection:not(:disabled)").prop("checked", true);
293     });
294     $("#clear_all").on("click",function(e){
295         e.preventDefault();
296         $("label:not(.required) .dbcolumn_selection").prop("checked", false);
297     });
298
299     $("body").on("click", "label.required input.dbcolumn_selection", function(e){
300         e.preventDefault();
301     });
302
303 } );