Bug 26988: Add API route to fetch hold pickup locations and use it in the holds table
[koha.git] / koha-tmpl / intranet-tmpl / prog / js / marc_modification_templates.js
1 /* global __ */
2 $(document).ready(function() {
3     window.modaction_legend_innerhtml = $("#modaction_legend").text();
4     window.action_submit_value = $("#action_submit").val();
5
6     $('#select_template').find("input:submit").hide();
7     $('#select_template').change(function() {
8         $('#select_template').submit();
9     });
10     $("span.match_regex_prefix" ).hide();
11     $("span.match_regex_suffix" ).hide();
12
13     $("#add_action").submit(function(){
14         var action = $("#action").val();
15         if ( action == 'move_field' || action == 'copy_field' || action == 'copy_and_replace_field') {
16             if ( $("#from_subfield").val().length != $("#to_subfield").val().length ) {
17                 alert( __("Both subfield values should be filled or empty.") );
18                 return false;
19             }
20             if ( $("#to_field").val().length <= 0 ) {
21                 alert( __("The destination should be filled.") );
22                 return false;
23             }
24             if ( ( $("#to_field").val()   < 10 && $("#to_subfield").val().length   > 0 ) ||
25                 ( $("#from_field").val() < 10 && $("#from_subfield").val().length > 0 ) ) {
26                 alert( __("If the field is a control field, the subfield should be empty") );
27                 return false;
28             }
29             if ( ( $("#from_field").val() < 10 && $("#to_field").val()   >= 10 ) ||
30                  ( $("#to_field").val()   < 10 && $("#from_field").val() >= 10 ) ) {
31                 alert( __("A control field cannot be used with a regular field.") );
32                 return false;
33             }
34         }
35         if ( action == 'update_field' ) {
36             if ( $("#from_subfield").val().length <= 0 ) {
37                 alert( __("The source subfield should be filled for update.") );
38                 return false;
39             }
40         }
41         if ( $("#from_field").val().length <= 0 ) {
42             alert( __("The source field should be filled.") );
43             return false;
44         }
45         if ( $("#conditional").val() == 'if' || $("#conditional").val() == 'unless' ) {
46             if ( $("#conditional_field").val() == '' ) {
47                 alert( __("The conditional field should be filled.") );
48                 return false;
49             }
50             if ( $("#conditional_comparison").val() == '' ) {
51                 alert( __("The conditional comparison operator should be filled.") );
52                 return false
53             }
54             if ( $("#conditional_value").val() == '' &&
55                  ( $("#conditional_comparison").val() == 'equals' || $("#conditional_comparison").val() == 'not_equals' ) ) {
56                 if ( document.getElementById('conditional_regex').checked == true ) {
57                     alert( __("The conditional regular expression should be filled.") );
58                     return false;
59                 } else {
60                     alert( __("The conditional value should be filled.") );
61                     return false;
62                 }
63             }
64         }
65     });
66
67     $("#conditional_field,#from_field").change(function(){
68         updateAllEvery();
69     });
70
71     $("#new_action").on("click",function(e){
72         e.preventDefault();
73         cancelEditAction();
74         $("#add_action").show();
75         $("#action").focus();
76     });
77
78     $(".duplicate_template").on("click",function(e){
79         e.preventDefault();
80         var template_id = $(this).data("template_id");
81         $("#duplicate_a_template").val(template_id);
82         $("#duplicate_current_template").val(1);
83     });
84
85     $('#createTemplate').on('shown.bs.modal', function (e) {
86         e.preventDefault();
87         $("#template_name").focus();
88     });
89
90     $("#duplicate_a_template").on("change",function(e){
91         e.preventDefault();
92         if( this.value === '' ){
93             $("#duplicate_current_template").val("");
94         } else {
95             $("#duplicate_current_template").val(1);
96         }
97     });
98
99     $(".delete_template").on("click",function(){
100         return confirmDelete();
101     });
102
103     $(".edit_action").on("click", function(){
104         var mmta_id = $(this).data("mmta_id");
105         var mmta = $.grep(mmtas, function(elt, id) {
106             return elt['mmta_id'] == mmta_id;
107         });
108         editAction( mmta[0] );
109         updateAllEvery();
110     });
111 });
112
113 function updateAllEvery(){
114     if ( $("#conditional_field").is(":visible") ) {
115         if ( $("#conditional_field").val() == $("#from_field").val() && $("#from_field").val().length > 0 ) {
116             $("#field_number option[value='0']").html( __("Every") );
117         } else {
118             $("#field_number option[value='0']").html( __("All") );
119         }
120     }
121 }
122
123 function onActionChange(selectObj) {
124     // get the index of the selected option
125     var idx = selectObj.selectedIndex;
126
127     // get the value of the selected option
128     var action = selectObj.options[idx].value;
129
130     switch( action ) {
131         case 'delete_field':
132             show('field_number_block');
133             hide('with_value_block');
134             hide('to_field_block');
135             break;
136
137         case 'add_field':
138             hide('field_number_block');
139             show('with_value_block');
140             hide('to_field_block');
141             break;
142
143         case 'update_field':
144             hide('field_number_block');
145             show('with_value_block');
146             hide('to_field_block');
147             break;
148
149         case 'move_field':
150             show('field_number_block');
151             hide('with_value_block');
152             show('to_field_block');
153             break;
154
155         case 'copy_field':
156             show('field_number_block');
157             hide('with_value_block');
158             show('to_field_block');
159             break;
160
161         case 'copy_and_replace_field':
162             show('field_number_block');
163             hide('with_value_block');
164             show('to_field_block');
165             break;
166
167     }
168 }
169
170 function onConditionalChange(selectObj) {
171     // get the index of the selected option
172     var idx = selectObj.selectedIndex;
173
174     // get the value of the selected option
175     var action = selectObj.options[idx].value;
176
177     switch( action ) {
178         case '':
179             hide('conditional_block');
180             break;
181
182         case 'if':
183         case 'unless':
184             show('conditional_block');
185             break;
186     }
187 }
188
189 function onConditionalComparisonChange(selectObj) {
190     // get the index of the selected option
191     var idx = selectObj.selectedIndex;
192
193     // get the value of the selected option
194     var action = selectObj.options[idx].value;
195
196     switch( action ) {
197         case 'equals':
198         case 'not_equals':
199             show('conditional_comparison_block');
200             break;
201
202         default:
203             hide('conditional_comparison_block');
204             break;
205     }
206 }
207
208 function onToFieldRegexChange( checkboxObj ) {
209     if ( checkboxObj.checked ) {
210         show('to_field_regex_value_block');
211     } else {
212         hide('to_field_regex_value_block');
213     }
214 }
215
216 function onConditionalRegexChange( checkboxObj ) {
217     if ( checkboxObj.checked ) {
218         $("span.match_regex_prefix" ).show();
219         $("span.match_regex_suffix" ).show();
220     } else {
221         $("span.match_regex_prefix" ).hide();
222         $("span.match_regex_suffix" ).hide();
223     }
224 }
225
226 function show(eltId) {
227     elt = document.getElementById( eltId );
228     elt.style.display='inline';
229 }
230
231 function hide(eltId) {
232     clearFormElements( eltId );
233     elt = document.getElementById( eltId );
234     elt.style.display='none';
235 }
236
237 function clearFormElements(divId) {
238     myBlock = document.getElementById( divId );
239
240     var inputElements = myBlock.getElementsByTagName( "input" );
241     for (var i = 0; i < inputElements.length; i++) {
242         switch( inputElements[i].type ) {
243             case "text":
244                 inputElements[i].value = '';
245                 break;
246             case "checkbox":
247                 inputElements[i].checked = false;
248                 break;
249         }
250     }
251
252     var selectElements = myBlock.getElementsByTagName( "select" );
253     for (var i = 0; i < selectElements.length; i++) {
254         selectElements[i].selectedIndex = 0;
255     }
256
257 }
258
259 function confirmDeleteAction() {
260     return confirm( __("Are you sure you wish to delete this template action?") );
261 }
262
263 function confirmDelete() {
264     return confirm( __("Are you sure you wish to delete this template?") );
265 }
266
267 var modaction_legend_innerhtml;
268 var action_submit_value;
269
270 function editAction( mmta ) {
271     $("#add_action").show();
272     document.getElementById('mmta_id').value = mmta['mmta_id'];
273
274     setSelectByValue( 'action', mmta['action'] );
275     $('#action').change();
276
277     setSelectByValue( 'field_number', mmta['field_number'] );
278
279     document.getElementById('from_field').value = mmta['from_field'];
280     document.getElementById('from_subfield').value = mmta['from_subfield'];
281     document.getElementById('field_value').value = mmta['field_value'];
282     document.getElementById('to_field').value = mmta['to_field'];
283     document.getElementById('to_subfield').value = mmta['to_subfield'];
284     if ( mmta['regex_search'] == '' && mmta['to_regex_replace'] == '' && mmta['to_regex_modifiers'] == '' ) {
285         $('#to_field_regex').prop('checked', false).change();
286     } else {
287         $('#to_field_regex').prop('checked', true).change();
288         $("#to_regex_search").val(mmta['to_regex_search']);
289         $("#to_regex_replace").val(mmta['to_regex_replace']);
290         $("#to_regex_modifiers").val(mmta['to_regex_modifiers']);
291     }
292
293     setSelectByValue( 'conditional', mmta['conditional'] );
294     $('#conditional').change();
295
296     document.getElementById('conditional_field').value = mmta['conditional_field'];
297     document.getElementById('conditional_subfield').value = mmta['conditional_subfield'];
298
299     setSelectByValue( 'conditional_comparison', mmta['conditional_comparison'] );
300     $('#conditional_comparison').change();
301
302     document.getElementById('conditional_value').value = mmta['conditional_value'];
303
304     document.getElementById('conditional_regex').checked = parseInt( mmta['conditional_regex'] );
305     $('#conditional_regex').change();
306
307     document.getElementById('description').value = mmta['description'];
308
309     window.modaction_legend_innerhtml = document.getElementById('modaction_legend').innerHTML;
310     document.getElementById('modaction_legend').innerHTML = __("Edit action %s").format(mmta['ordering']);
311
312     window.action_submit_value = document.getElementById('action_submit').value;
313     document.getElementById('action_submit').value = __("Update action");
314 }
315
316 function cancelEditAction() {
317     document.getElementById('mmta_id').value = '';
318
319     setSelectByValue( 'action', 'delete_field' );
320     $('#action').change();
321
322     document.getElementById('from_field').value = '';
323     document.getElementById('from_subfield').value = '';
324     document.getElementById('field_value').value = '';
325     document.getElementById('to_field').value = '';
326     document.getElementById('to_subfield').value = '';
327     $("#to_regex_search").val("");
328     $("#to_regex_replace").val("");
329     $("#to_regex_modifiers").val("");
330     $("#description").val("");
331
332     $('#to_field_regex').prop('checked', false).change();
333
334     setSelectByValue( 'conditional', '' );
335     $('#conditional').change();
336
337     document.getElementById('conditional_field').value = '';
338     document.getElementById('conditional_subfield').value = '';
339
340     setSelectByValue( 'conditional_comparison', '' );
341     $('#conditional_comparison').change();
342
343     document.getElementById('conditional_value').value = '';
344
345     document.getElementById('conditional_regex').checked = false;
346
347     document.getElementById('modaction_legend').innerHTML = window.modaction_legend_innerhtml;
348     document.getElementById('action_submit').value = window.action_submit_value;
349     $("#add_action").hide();
350 }
351
352 function setSelectByValue( selectId, value ) {
353     s = document.getElementById( selectId );
354
355     for ( i = 0; i < s.options.length; i++ ) {
356         if ( s.options[i].value == value ) {
357             s.selectedIndex = i;
358         }
359     }
360 }