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