Bug 15814: Handle correctly MMTA edit button
[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     $(".edit_action").on("click", function(){
103         var mmta_id = $(this).data("mmta_id");
104         var mmta = $.grep(mmtas, function(elt, id) {
105             return elt['mmta_id'] == mmta_id;
106         });
107         editAction( mmta[0] );
108         updateAllEvery();
109     });
110 });
111
112 function updateAllEvery(){
113     if ( $("#conditional_field").is(":visible") ) {
114         if ( $("#conditional_field").val() == $("#from_field").val() && $("#from_field").val().length > 0 ) {
115             $("#field_number option[value='0']").html( MSG_MMT_EVERY );
116         } else {
117             $("#field_number option[value='0']").html( MSG_MMT_ALL );
118         }
119     }
120 }
121
122 function onActionChange(selectObj) {
123     // get the index of the selected option
124     var idx = selectObj.selectedIndex;
125
126     // get the value of the selected option
127     var action = selectObj.options[idx].value;
128
129     switch( action ) {
130         case 'delete_field':
131             show('field_number_block');
132             hide('with_value_block');
133             hide('to_field_block');
134             break;
135
136         case 'add_field':
137             hide('field_number_block');
138             show('with_value_block');
139             hide('to_field_block');
140             break;
141
142         case 'update_field':
143             hide('field_number_block');
144             show('with_value_block');
145             hide('to_field_block');
146             break;
147
148         case 'move_field':
149             show('field_number_block');
150             hide('with_value_block');
151             show('to_field_block');
152             break;
153
154         case 'copy_field':
155             show('field_number_block');
156             hide('with_value_block');
157             show('to_field_block');
158             break;
159
160         case 'copy_and_replace_field':
161             show('field_number_block');
162             hide('with_value_block');
163             show('to_field_block');
164             break;
165
166     }
167 }
168
169 function onConditionalChange(selectObj) {
170     // get the index of the selected option
171     var idx = selectObj.selectedIndex;
172
173     // get the value of the selected option
174     var action = selectObj.options[idx].value;
175
176     switch( action ) {
177         case '':
178             hide('conditional_block');
179             break;
180
181         case 'if':
182         case 'unless':
183             show('conditional_block');
184             break;
185     }
186 }
187
188 function onConditionalComparisonChange(selectObj) {
189     // get the index of the selected option
190     var idx = selectObj.selectedIndex;
191
192     // get the value of the selected option
193     var action = selectObj.options[idx].value;
194
195     switch( action ) {
196         case 'equals':
197         case 'not_equals':
198             show('conditional_comparison_block');
199             break;
200
201         default:
202             hide('conditional_comparison_block');
203             break;
204     }
205 }
206
207 function onToFieldRegexChange( checkboxObj ) {
208     if ( checkboxObj.checked ) {
209         show('to_field_regex_value_block');
210     } else {
211         hide('to_field_regex_value_block');
212     }
213 }
214
215 function onConditionalRegexChange( checkboxObj ) {
216     if ( checkboxObj.checked ) {
217         $("span.match_regex_prefix" ).show();
218         $("span.match_regex_suffix" ).show();
219     } else {
220         $("span.match_regex_prefix" ).hide();
221         $("span.match_regex_suffix" ).hide();
222     }
223 }
224
225 function show(eltId) {
226     elt = document.getElementById( eltId );
227     elt.style.display='inline';
228 }
229
230 function hide(eltId) {
231     clearFormElements( eltId );
232     elt = document.getElementById( eltId );
233     elt.style.display='none';
234 }
235
236 function clearFormElements(divId) {
237     myBlock = document.getElementById( divId );
238
239     var inputElements = myBlock.getElementsByTagName( "input" );
240     for (var i = 0; i < inputElements.length; i++) {
241         switch( inputElements[i].type ) {
242             case "text":
243                 inputElements[i].value = '';
244                 break;
245             case "checkbox":
246                 inputElements[i].checked = false;
247                 break;
248         }
249     }
250
251     var selectElements = myBlock.getElementsByTagName( "select" );
252     for (var i = 0; i < selectElements.length; i++) {
253         selectElements[i].selectedIndex = 0;
254     }
255
256 }
257
258 function confirmDeleteAction() {
259     return confirm( MSG_MMT_CONFIRM_DEL_TEMPLATE_ACTION );
260 }
261
262 function confirmDelete() {
263     return confirm( MSG_MMT_CONFIRM_DEL_TEMPLATE );
264 }
265
266 var modaction_legend_innerhtml;
267 var action_submit_value;
268
269 function editAction( mmta ) {
270     $("#add_action").show();
271     document.getElementById('mmta_id').value = mmta['mmta_id'];
272
273     setSelectByValue( 'action', mmta['action'] );
274     $('#action').change();
275
276     setSelectByValue( 'field_number', mmta['field_number'] );
277
278     document.getElementById('from_field').value = mmta['from_field'];
279     document.getElementById('from_subfield').value = mmta['from_subfield'];
280     document.getElementById('field_value').value = mmta['field_value'];
281     document.getElementById('to_field').value = mmta['to_field'];
282     document.getElementById('to_subfield').value = mmta['to_subfield'];
283     if ( mmta['regex_search'] == '' && mmta['to_regex_replace'] == '' && mmta['to_regex_modifiers'] == '' ) {
284         $('#to_field_regex').prop('checked', false).change();
285     } else {
286         $('#to_field_regex').prop('checked', true).change();
287         $("#to_regex_search").val(mmta['to_regex_search']);
288         $("#to_regex_replace").val(mmta['to_regex_replace']);
289         $("#to_regex_modifiers").val(mmta['to_regex_modifiers']);
290     }
291
292     setSelectByValue( 'conditional', mmta['conditional'] );
293     $('#conditional').change();
294
295     document.getElementById('conditional_field').value = mmta['conditional_field'];
296     document.getElementById('conditional_subfield').value = mmta['conditional_subfield'];
297
298     setSelectByValue( 'conditional_comparison', mmta['conditional_comparison'] );
299     $('#conditional_comparison').change();
300
301     document.getElementById('conditional_value').value = mmta['conditional_value'];
302
303     document.getElementById('conditional_regex').checked = parseInt( mmta['conditional_regex'] );
304     $('#conditional_regex').change();
305
306     document.getElementById('description').value = mmta['description'];
307
308     window.modaction_legend_innerhtml = document.getElementById('modaction_legend').innerHTML;
309     document.getElementById('modaction_legend').innerHTML = MSG_MMT_EDIT_ACTION.format(mmta['ordering']);
310
311     window.action_submit_value = document.getElementById('action_submit').value;
312     document.getElementById('action_submit').value = MSG_MMT_UPDATE_ACTION;
313 }
314
315 function cancelEditAction() {
316     document.getElementById('mmta_id').value = '';
317
318     setSelectByValue( 'action', 'delete_field' );
319     $('#action').change();
320
321     document.getElementById('from_field').value = '';
322     document.getElementById('from_subfield').value = '';
323     document.getElementById('field_value').value = '';
324     document.getElementById('to_field').value = '';
325     document.getElementById('to_subfield').value = '';
326     $("#to_regex_search").val("");
327     $("#to_regex_replace").val("");
328     $("#to_regex_modifiers").val("");
329     $("#description").val("");
330
331     $('#to_field_regex').prop('checked', false).change();
332
333     setSelectByValue( 'conditional', '' );
334     $('#conditional').change();
335
336     document.getElementById('conditional_field').value = '';
337     document.getElementById('conditional_subfield').value = '';
338
339     setSelectByValue( 'conditional_comparison', '' );
340     $('#conditional_comparison').change();
341
342     document.getElementById('conditional_value').value = '';
343
344     document.getElementById('conditional_regex').checked = false;
345
346     document.getElementById('modaction_legend').innerHTML = window.modaction_legend_innerhtml;
347     document.getElementById('action_submit').value = window.action_submit_value;
348     $("#add_action").hide();
349 }
350
351 function setSelectByValue( selectId, value ) {
352     s = document.getElementById( selectId );
353
354     for ( i = 0; i < s.options.length; i++ ) {
355         if ( s.options[i].value == value ) {
356             s.selectedIndex = i;
357         }
358     }
359 }