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