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