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