Bug 15867: Move MARC modification templates JavaScript into separate file
[koha.git] / koha-tmpl / intranet-tmpl / prog / en / js / marc_modification_templates.js
1 $(document).ready(function() {
2     $('#select_template').find("input:submit").hide();
3     $('#select_template').change(function() {
4         $('#select_template').submit();
5     });
6     $("span.match_regex_prefix" ).hide();
7     $("span.match_regex_suffix" ).hide();
8
9     $("#add_action").submit(function(){
10         var action = $("#action").val();
11         if ( action == 'move_field' || action == 'copy_field' || action == 'copy_and_replace_field') {
12             if ( $("#from_subfield").val().length != $("#to_subfield").val().length ) {
13                 alert( MSG_MMT_SUBFIELDS_MATCH );
14                 return false;
15             }
16             if ( $("#to_field").val().length <= 0 ) {
17                 alert( MSG_MMT_DESTINATION_REQUIRED );
18                 return false;
19             }
20             if ( ( $("#to_field").val()   < 10 && $("#to_subfield").val().length   > 0 ) ||
21                  ( $("#from_field").val() < 10 && $("#from_subfield").val().length > 0 ) ) {
22                  alert( MSG_MMT_CONTROL_FIELD_EMPTY );
23                  return false;
24             }
25             if ( ( $("#from_field").val() < 10 && $("#to_subfield").val().length   === 0 ) ||
26                  ( $("#to_field").val()   < 10 && $("#from_subfield").val().length === 0 ) ) {
27                 alert( MSG_MMT_CONTROL_FIELD );
28                 return false;
29              }
30         }
31         if ( action == 'update_field' ) {
32             if ( $("#from_subfield").val().length <= 0 ) {
33                 alert( MSG_MMT_SOURCE_SUBFIELD );
34                 return false;
35             }
36         }
37         if ( $("#from_field").val().length <= 0 ) {
38             alert( MSG_MMT_SOURCE_FIELD );
39             return false;
40         }
41     });
42
43     $("#conditional_field,#from_field").change(function(){
44         updateAllEvery();
45     });
46 });
47
48 function updateAllEvery(){
49     if ( $("#conditional_field").is(":visible") ) {
50         if ( $("#conditional_field").val() == $("#from_field").val() && $("#from_field").val().length > 0 ) {
51             $("#field_number option[value='0']").html( MSG_MMT_EVERY );
52         } else {
53             $("#field_number option[value='0']").html( MSG_MMT_ALL );
54         }
55     }
56 }
57
58 function onActionChange(selectObj) {
59     // get the index of the selected option
60     var idx = selectObj.selectedIndex;
61
62     // get the value of the selected option
63     var action = selectObj.options[idx].value;
64
65     switch( action ) {
66         case 'delete_field':
67             show('field_number_block');
68             hide('with_value_block');
69             hide('to_field_block');
70             break;
71
72         case 'update_field':
73             hide('field_number_block');
74             show('with_value_block');
75             hide('to_field_block');
76             break;
77
78         case 'move_field':
79             show('field_number_block');
80             hide('with_value_block');
81             show('to_field_block');
82             break;
83
84         case 'copy_field':
85             show('field_number_block');
86             hide('with_value_block');
87             show('to_field_block');
88             break;
89
90         case 'copy_and_replace_field':
91             show('field_number_block');
92             hide('with_value_block');
93             show('to_field_block');
94             break;
95
96     }
97 }
98
99 function onConditionalChange(selectObj) {
100     // get the index of the selected option
101     var idx = selectObj.selectedIndex;
102
103     // get the value of the selected option
104     var action = selectObj.options[idx].value;
105
106     switch( action ) {
107         case '':
108             hide('conditional_block');
109             break;
110
111         case 'if':
112         case 'unless':
113             show('conditional_block');
114             break;
115     }
116 }
117
118 function onConditionalComparisonChange(selectObj) {
119     // get the index of the selected option
120     var idx = selectObj.selectedIndex;
121
122     // get the value of the selected option
123     var action = selectObj.options[idx].value;
124
125     switch( action ) {
126         case 'equals':
127         case 'not_equals':
128             show('conditional_comparison_block');
129             break;
130
131         default:
132             hide('conditional_comparison_block');
133             break;
134     }
135 }
136
137 function onToFieldRegexChange( checkboxObj ) {
138     if ( checkboxObj.checked ) {
139         show('to_field_regex_value_block');
140     } else {
141         hide('to_field_regex_value_block');
142     }
143 }
144
145 function onConditionalRegexChange( checkboxObj ) {
146     if ( checkboxObj.checked ) {
147         $("span.match_regex_prefix" ).show();
148         $("span.match_regex_suffix" ).show();
149     } else {
150         $("span.match_regex_prefix" ).hide();
151         $("span.match_regex_suffix" ).hide();
152     }
153 }
154
155 function show(eltId) {
156     elt = document.getElementById( eltId );
157     elt.style.display='inline';
158 }
159
160 function hide(eltId) {
161     clearFormElements( eltId );
162     elt = document.getElementById( eltId );
163     elt.style.display='none';
164 }
165
166 function clearFormElements(divId) {
167     myBlock = document.getElementById( divId );
168
169     var inputElements = myBlock.getElementsByTagName( "input" );
170     for (var i = 0; i < inputElements.length; i++) {
171         switch( inputElements[i].type ) {
172             case "text":
173                 inputElements[i].value = '';
174                 break;
175             case "checkbox":
176                 inputElements[i].checked = false;
177                 break;
178         }
179     }
180
181     var selectElements = myBlock.getElementsByTagName( "select" );
182     for (var i = 0; i < selectElements.length; i++) {
183         selectElements[i].selectedIndex = 0;
184     }
185
186 }
187
188 function confirmDelete() {
189     var agree = confirm( MSG_MMT_CONFIRM_DEL_TEMPLATE );
190     return agree;
191 }
192
193 var modaction_legend_innerhtml;
194 var action_submit_value;
195
196 function editAction( mmta_id, ordering, action, field_number, from_field, from_subfield, field_value, to_field,
197     to_subfield, to_regex_search, to_regex_replace, to_regex_modifiers, conditional, conditional_field, conditional_subfield,
198     conditional_comparison, conditional_value, conditional_regex, description
199 ) {
200     document.getElementById('mmta_id').value = mmta_id;
201
202     setSelectByValue( 'action', action );
203     document.getElementById('action').onchange();
204
205     setSelectByValue( 'field_number', field_number );
206
207     document.getElementById('from_field').value = from_field;
208     document.getElementById('from_subfield').value = from_subfield;
209     document.getElementById('field_value').value = field_value;
210     document.getElementById('to_field').value = to_field;
211     document.getElementById('to_subfield').value = to_subfield;
212     $("#to_regex_search").val(to_regex_search);
213     $("#to_regex_replace").val(to_regex_replace);
214     $("#to_regex_modifiers").val(to_regex_modifiers);
215
216     document.getElementById('to_field_regex').checked = conditional_regex.length;
217     document.getElementById('to_field_regex').onchange();
218
219     setSelectByValue( 'conditional', conditional );
220     document.getElementById('conditional').onchange();
221
222     document.getElementById('conditional_field').value = conditional_field;
223     document.getElementById('conditional_subfield').value = conditional_subfield;
224
225     setSelectByValue( 'conditional_comparison', conditional_comparison );
226     document.getElementById('conditional_comparison').onchange();
227
228     document.getElementById('conditional_value').value = conditional_value;
229
230     document.getElementById('conditional_regex').checked = parseInt( conditional_regex );
231
232     document.getElementById('description').value = description;
233
234     window.modaction_legend_innerhtml = document.getElementById('modaction_legend').innerHTML;
235     document.getElementById('modaction_legend').innerHTML = MSG_MMT_EDIT_ACTION.format(ordering);
236
237     window.action_submit_value = document.getElementById('action_submit').value;
238     document.getElementById('action_submit').value = MSG_MMT_UPDATE_ACTION;
239
240     show('cancel_edit');
241 }
242
243 function cancelEditAction() {
244     document.getElementById('mmta_id').value = '';
245
246     setSelectByValue( 'action', 'delete_field' );
247     document.getElementById('action').onchange();
248
249     document.getElementById('from_field').value = '';
250     document.getElementById('from_subfield').value = '';
251     document.getElementById('field_value').value = '';
252     document.getElementById('to_field').value = '';
253     document.getElementById('to_subfield').value = '';
254     $("#to_regex_search").val("");
255     $("#to_regex_replace").val("");
256     $("#to_regex_modifiers").val("");
257
258     document.getElementById('to_field_regex').checked = false;
259     document.getElementById('to_field_regex').onchange();
260
261     setSelectByValue( 'conditional', '' );
262     document.getElementById('conditional').onchange();
263
264     document.getElementById('conditional_field').value = '';
265     document.getElementById('conditional_subfield').value = '';
266
267     setSelectByValue( 'conditional_comparison', '' );
268     document.getElementById('conditional_comparison').onchange();
269
270     document.getElementById('conditional_value').value = '';
271
272     document.getElementById('conditional_regex').checked = false;
273
274     document.getElementById('modaction_legend').innerHTML = window.modaction_legend_innerhtml;
275     document.getElementById('action_submit').value = window.action_submit_value;
276
277     hide('cancel_edit');
278 }
279
280 function setSelectByValue( selectId, value ) {
281     s = document.getElementById( selectId );
282
283     for ( i = 0; i < s.options.length; i++ ) {
284         if ( s.options[i].value == value ) {
285             s.selectedIndex = i;
286         }
287     }
288 }