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