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