Bug 16238 [Follow-up] Upgrade jQuery in staff client: use .prop() instead of .attr()
[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 confirmDeleteAction() {
189     return confirm( MSG_MMT_CONFIRM_DEL_TEMPLATE_ACTION );
190 }
191
192 function confirmDelete() {
193     return confirm( MSG_MMT_CONFIRM_DEL_TEMPLATE );
194 }
195
196 var modaction_legend_innerhtml;
197 var action_submit_value;
198
199 function editAction( mmta_id, ordering, action, field_number, from_field, from_subfield, field_value, to_field,
200     to_subfield, to_regex_search, to_regex_replace, to_regex_modifiers, conditional, conditional_field, conditional_subfield,
201     conditional_comparison, conditional_value, conditional_regex, description
202 ) {
203     document.getElementById('mmta_id').value = mmta_id;
204
205     setSelectByValue( 'action', action );
206     document.getElementById('action').onchange();
207
208     setSelectByValue( 'field_number', field_number );
209
210     document.getElementById('from_field').value = from_field;
211     document.getElementById('from_subfield').value = from_subfield;
212     document.getElementById('field_value').value = field_value;
213     document.getElementById('to_field').value = to_field;
214     document.getElementById('to_subfield').value = to_subfield;
215     $("#to_regex_search").val(to_regex_search);
216     $("#to_regex_replace").val(to_regex_replace);
217     $("#to_regex_modifiers").val(to_regex_modifiers);
218
219     document.getElementById('to_field_regex').checked = conditional_regex.length;
220     document.getElementById('to_field_regex').onchange();
221
222     setSelectByValue( 'conditional', conditional );
223     document.getElementById('conditional').onchange();
224
225     document.getElementById('conditional_field').value = conditional_field;
226     document.getElementById('conditional_subfield').value = conditional_subfield;
227
228     setSelectByValue( 'conditional_comparison', conditional_comparison );
229     document.getElementById('conditional_comparison').onchange();
230
231     document.getElementById('conditional_value').value = conditional_value;
232
233     document.getElementById('conditional_regex').checked = parseInt( conditional_regex );
234
235     document.getElementById('description').value = description;
236
237     window.modaction_legend_innerhtml = document.getElementById('modaction_legend').innerHTML;
238     document.getElementById('modaction_legend').innerHTML = MSG_MMT_EDIT_ACTION.format(ordering);
239
240     window.action_submit_value = document.getElementById('action_submit').value;
241     document.getElementById('action_submit').value = MSG_MMT_UPDATE_ACTION;
242
243     show('cancel_edit');
244 }
245
246 function cancelEditAction() {
247     document.getElementById('mmta_id').value = '';
248
249     setSelectByValue( 'action', 'delete_field' );
250     document.getElementById('action').onchange();
251
252     document.getElementById('from_field').value = '';
253     document.getElementById('from_subfield').value = '';
254     document.getElementById('field_value').value = '';
255     document.getElementById('to_field').value = '';
256     document.getElementById('to_subfield').value = '';
257     $("#to_regex_search").val("");
258     $("#to_regex_replace").val("");
259     $("#to_regex_modifiers").val("");
260
261     document.getElementById('to_field_regex').checked = false;
262     document.getElementById('to_field_regex').onchange();
263
264     setSelectByValue( 'conditional', '' );
265     document.getElementById('conditional').onchange();
266
267     document.getElementById('conditional_field').value = '';
268     document.getElementById('conditional_subfield').value = '';
269
270     setSelectByValue( 'conditional_comparison', '' );
271     document.getElementById('conditional_comparison').onchange();
272
273     document.getElementById('conditional_value').value = '';
274
275     document.getElementById('conditional_regex').checked = false;
276
277     document.getElementById('modaction_legend').innerHTML = window.modaction_legend_innerhtml;
278     document.getElementById('action_submit').value = window.action_submit_value;
279
280     hide('cancel_edit');
281 }
282
283 function setSelectByValue( selectId, value ) {
284     s = document.getElementById( selectId );
285
286     for ( i = 0; i < s.options.length; i++ ) {
287         if ( s.options[i].value == value ) {
288             s.selectedIndex = i;
289         }
290     }
291 }