Bug 34180: Template variable in JavaScript prevents authority MARC preview from displ...
[koha.git] / koha-tmpl / intranet-tmpl / prog / js / marc_modification_templates.js
1 /* global __ KohaTable table_settings */
2 $(document).ready(function() {
3     window.modaction_legend_innerhtml = $("#modaction_legend").text();
4     window.action_submit_value = $("#action_submit").val();
5
6     $('#select_template').find("input:submit").hide();
7     $('#select_template').change(function() {
8         $('#select_template').submit();
9     });
10     $("span.match_regex_prefix" ).hide();
11     $("span.match_regex_suffix" ).hide();
12
13     $("#add_action").submit(function(){
14         var action = $("#action").val();
15         if ( action == 'move_field' || action == 'copy_field' || action == 'copy_and_replace_field') {
16             if ( $("#from_subfield").val().length != $("#to_subfield").val().length ) {
17                 alert( __("Both subfield values should be filled or empty.") );
18                 return false;
19             }
20             if ( $("#to_field").val().length <= 0 ) {
21                 alert( __("The destination should be filled.") );
22                 return false;
23             }
24             if ( ( $("#to_field").val()   < 10 && $("#to_subfield").val().length   > 0 ) ||
25                 ( $("#from_field").val() < 10 && $("#from_subfield").val().length > 0 ) ) {
26                 alert( __("If the field is a control field, the subfield should be empty") );
27                 return false;
28             }
29             if ( ( $("#from_field").val() < 10 && $("#to_field").val()   >= 10 ) ||
30                  ( $("#to_field").val()   < 10 && $("#from_field").val() >= 10 ) ) {
31                 alert( __("A control field cannot be used with a regular field.") );
32                 return false;
33             }
34         }
35         if ( action == 'update_field' ) {
36             if ( $("#from_subfield").val().length <= 0 ) {
37                 alert( __("The source subfield should be filled for update.") );
38                 return false;
39             }
40         }
41         if ( $("#from_field").val().length <= 0 ) {
42             alert( __("The source field should be filled.") );
43             return false;
44         }
45         if ( $("#conditional").val() == 'if' || $("#conditional").val() == 'unless' ) {
46             if ( $("#conditional_field").val() == '' ) {
47                 alert( __("The conditional field should be filled.") );
48                 return false;
49             }
50             if ( $("#conditional_comparison").val() == '' ) {
51                 alert( __("The conditional comparison operator should be filled.") );
52                 return false;
53             }
54             if ( $("#conditional_value").val() == '' &&
55                  ( $("#conditional_comparison").val() == 'equals' || $("#conditional_comparison").val() == 'not_equals' ) ) {
56                 if ( document.getElementById('conditional_regex').checked == true ) {
57                     alert( __("The conditional regular expression should be filled.") );
58                     return false;
59                 } else {
60                     alert( __("The conditional value should be filled.") );
61                     return false;
62                 }
63             }
64         }
65     });
66
67     $("#conditional_field,#from_field").change(function(){
68         updateAllEvery();
69     });
70
71     $(".new_action").on("click",function(e){
72         e.preventDefault();
73         cancelEditAction();
74         $("#no_defined_actions").hide();
75         $("#add_action").show();
76         $("#action").focus();
77     });
78
79     $(".duplicate_template").on("click",function(e){
80         e.preventDefault();
81         var template_id = $(this).data("template_id");
82         $("#duplicate_a_template").val(template_id);
83         $("#duplicate_current_template").val(1);
84     });
85
86     $('#createTemplate').on('shown.bs.modal', function (e) {
87         e.preventDefault();
88         $("#template_name").focus();
89     });
90
91     $("#duplicate_a_template").on("change",function(e){
92         e.preventDefault();
93         if( this.value === '' ){
94             $("#duplicate_current_template").val("");
95         } else {
96             $("#duplicate_current_template").val(1);
97         }
98     });
99
100     $(".delete_template").on("click",function(){
101         return confirmDelete();
102     });
103
104     $(".edit_action").on("click", function(){
105         var mmta_id = $(this).data("mmta_id");
106         var mmta = $.grep(mmtas, function(elt, id) {
107             return elt['mmta_id'] == mmta_id;
108         });
109         editAction( mmta[0] );
110         updateAllEvery();
111     });
112
113     KohaTable("templatest", {
114     }, table_settings);
115
116 });
117
118 function updateAllEvery(){
119     if ( $("#conditional_field").is(":visible") ) {
120         if ( $("#conditional_field").val() == $("#from_field").val() && $("#from_field").val().length > 0 ) {
121             $("#field_number option[value='0']").html( __("Every") );
122         } else {
123             $("#field_number option[value='0']").html( __("All") );
124         }
125     }
126 }
127
128 function onActionChange(selectObj) {
129     // get the index of the selected option
130     var idx = selectObj.selectedIndex;
131
132     // get the value of the selected option
133     var action = selectObj.options[idx].value;
134
135     switch( action ) {
136         case 'delete_field':
137             show('field_number_block');
138             hide('with_value_block');
139             hide('to_field_block');
140             break;
141
142         case 'add_field':
143             hide('field_number_block');
144             show('with_value_block');
145             hide('to_field_block');
146             break;
147
148         case 'update_field':
149             hide('field_number_block');
150             show('with_value_block');
151             hide('to_field_block');
152             break;
153
154         case 'move_field':
155             show('field_number_block');
156             hide('with_value_block');
157             show('to_field_block');
158             break;
159
160         case 'copy_field':
161             show('field_number_block');
162             hide('with_value_block');
163             show('to_field_block');
164             break;
165
166         case 'copy_and_replace_field':
167             show('field_number_block');
168             hide('with_value_block');
169             show('to_field_block');
170             break;
171
172     }
173 }
174
175 function onConditionalChange(selectObj) {
176     // get the index of the selected option
177     var idx = selectObj.selectedIndex;
178
179     // get the value of the selected option
180     var action = selectObj.options[idx].value;
181
182     switch( action ) {
183         case '':
184             hide('conditional_block');
185             break;
186
187         case 'if':
188         case 'unless':
189             show('conditional_block');
190             break;
191     }
192 }
193
194 function onConditionalComparisonChange(selectObj) {
195     // get the index of the selected option
196     var idx = selectObj.selectedIndex;
197
198     // get the value of the selected option
199     var action = selectObj.options[idx].value;
200
201     switch( action ) {
202         case 'equals':
203         case 'not_equals':
204             show('conditional_comparison_block');
205             break;
206
207         default:
208             hide('conditional_comparison_block');
209             break;
210     }
211 }
212
213 function onToFieldRegexChange( checkboxObj ) {
214     if ( checkboxObj.checked ) {
215         show('to_field_regex_value_block');
216     } else {
217         hide('to_field_regex_value_block');
218     }
219 }
220
221 function onConditionalRegexChange( checkboxObj ) {
222     if ( checkboxObj.checked ) {
223         $("span.match_regex_prefix" ).show();
224         $("span.match_regex_suffix" ).show();
225     } else {
226         $("span.match_regex_prefix" ).hide();
227         $("span.match_regex_suffix" ).hide();
228     }
229 }
230
231 function show(eltId) {
232     elt = document.getElementById( eltId );
233     elt.style.display='inline';
234 }
235
236 function hide(eltId) {
237     clearFormElements( eltId );
238     elt = document.getElementById( eltId );
239     elt.style.display='none';
240 }
241
242 function clearFormElements(divId) {
243     myBlock = document.getElementById( divId );
244
245     var inputElements = myBlock.getElementsByTagName( "input" );
246     for (var i = 0; i < inputElements.length; i++) {
247         switch( inputElements[i].type ) {
248             case "text":
249                 inputElements[i].value = '';
250                 break;
251             case "checkbox":
252                 inputElements[i].checked = false;
253                 break;
254         }
255     }
256
257     var selectElements = myBlock.getElementsByTagName( "select" );
258     for (var i = 0; i < selectElements.length; i++) {
259         selectElements[i].selectedIndex = 0;
260     }
261
262 }
263
264 function confirmDeleteAction() {
265     return confirm( __("Are you sure you wish to delete this template action?") );
266 }
267
268 function confirmDelete() {
269     return confirm( __("Are you sure you wish to delete this template?") );
270 }
271
272 var modaction_legend_innerhtml;
273 var action_submit_value;
274
275 function editAction( mmta ) {
276     $("#add_action").show();
277     document.getElementById('mmta_id').value = mmta['mmta_id'];
278
279     setSelectByValue( 'action', mmta['action'] );
280     $('#action').change();
281
282     setSelectByValue( 'field_number', mmta['field_number'] );
283
284     document.getElementById('from_field').value = mmta['from_field'];
285     document.getElementById('from_subfield').value = mmta['from_subfield'];
286     document.getElementById('field_value').value = mmta['field_value'];
287     document.getElementById('to_field').value = mmta['to_field'];
288     document.getElementById('to_subfield').value = mmta['to_subfield'];
289     if ( mmta['regex_search'] == '' && mmta['to_regex_replace'] == '' && mmta['to_regex_modifiers'] == '' ) {
290         $('#to_field_regex').prop('checked', false).change();
291     } else {
292         $('#to_field_regex').prop('checked', true).change();
293         $("#to_regex_search").val(mmta['to_regex_search']);
294         $("#to_regex_replace").val(mmta['to_regex_replace']);
295         $("#to_regex_modifiers").val(mmta['to_regex_modifiers']);
296     }
297
298     setSelectByValue( 'conditional', mmta['conditional'] );
299     $('#conditional').change();
300
301     document.getElementById('conditional_field').value = mmta['conditional_field'];
302     document.getElementById('conditional_subfield').value = mmta['conditional_subfield'];
303
304     setSelectByValue( 'conditional_comparison', mmta['conditional_comparison'] );
305     $('#conditional_comparison').change();
306
307     document.getElementById('conditional_value').value = mmta['conditional_value'];
308
309     document.getElementById('conditional_regex').checked = parseInt( mmta['conditional_regex'] );
310     $('#conditional_regex').change();
311
312     document.getElementById('description').value = mmta['description'];
313
314     window.modaction_legend_innerhtml = document.getElementById('modaction_legend').innerHTML;
315     document.getElementById('modaction_legend').innerHTML = __("Edit action %s").format(mmta['ordering']);
316
317     window.action_submit_value = document.getElementById('action_submit').value;
318     document.getElementById('action_submit').value = __("Update action");
319 }
320
321 function cancelEditAction() {
322     document.getElementById('mmta_id').value = '';
323
324     setSelectByValue( 'action', 'delete_field' );
325     $('#action').change();
326
327     document.getElementById('from_field').value = '';
328     document.getElementById('from_subfield').value = '';
329     document.getElementById('field_value').value = '';
330     document.getElementById('to_field').value = '';
331     document.getElementById('to_subfield').value = '';
332     $("#to_regex_search").val("");
333     $("#to_regex_replace").val("");
334     $("#to_regex_modifiers").val("");
335     $("#description").val("");
336
337     $('#to_field_regex').prop('checked', false).change();
338
339     setSelectByValue( 'conditional', '' );
340     $('#conditional').change();
341
342     document.getElementById('conditional_field').value = '';
343     document.getElementById('conditional_subfield').value = '';
344
345     setSelectByValue( 'conditional_comparison', '' );
346     $('#conditional_comparison').change();
347
348     document.getElementById('conditional_value').value = '';
349
350     document.getElementById('conditional_regex').checked = false;
351
352     document.getElementById('modaction_legend').innerHTML = window.modaction_legend_innerhtml;
353     document.getElementById('action_submit').value = window.action_submit_value;
354
355     if( $("#template_actions").length < 1 ){
356         $("#no_defined_actions").show();
357     }
358
359     $("#add_action").hide();
360 }
361
362 function setSelectByValue( selectId, value ) {
363     s = document.getElementById( selectId );
364
365     for ( i = 0; i < s.options.length; i++ ) {
366         if ( s.options[i].value == value ) {
367             s.selectedIndex = i;
368         }
369     }
370 }