Bug 29408: Add JSDoc documentation for kohaTable function
[koha.git] / koha-tmpl / intranet-tmpl / prog / js / marc_modification_templates.js
1 /* global __ KohaTable columns_settings */
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     KohaTable("templatest", {
113     }, columns_settings);
114
115 });
116
117 function updateAllEvery(){
118     if ( $("#conditional_field").is(":visible") ) {
119         if ( $("#conditional_field").val() == $("#from_field").val() && $("#from_field").val().length > 0 ) {
120             $("#field_number option[value='0']").html( __("Every") );
121         } else {
122             $("#field_number option[value='0']").html( __("All") );
123         }
124     }
125 }
126
127 function onActionChange(selectObj) {
128     // get the index of the selected option
129     var idx = selectObj.selectedIndex;
130
131     // get the value of the selected option
132     var action = selectObj.options[idx].value;
133
134     switch( action ) {
135         case 'delete_field':
136             show('field_number_block');
137             hide('with_value_block');
138             hide('to_field_block');
139             break;
140
141         case 'add_field':
142             hide('field_number_block');
143             show('with_value_block');
144             hide('to_field_block');
145             break;
146
147         case 'update_field':
148             hide('field_number_block');
149             show('with_value_block');
150             hide('to_field_block');
151             break;
152
153         case 'move_field':
154             show('field_number_block');
155             hide('with_value_block');
156             show('to_field_block');
157             break;
158
159         case 'copy_field':
160             show('field_number_block');
161             hide('with_value_block');
162             show('to_field_block');
163             break;
164
165         case 'copy_and_replace_field':
166             show('field_number_block');
167             hide('with_value_block');
168             show('to_field_block');
169             break;
170
171     }
172 }
173
174 function onConditionalChange(selectObj) {
175     // get the index of the selected option
176     var idx = selectObj.selectedIndex;
177
178     // get the value of the selected option
179     var action = selectObj.options[idx].value;
180
181     switch( action ) {
182         case '':
183             hide('conditional_block');
184             break;
185
186         case 'if':
187         case 'unless':
188             show('conditional_block');
189             break;
190     }
191 }
192
193 function onConditionalComparisonChange(selectObj) {
194     // get the index of the selected option
195     var idx = selectObj.selectedIndex;
196
197     // get the value of the selected option
198     var action = selectObj.options[idx].value;
199
200     switch( action ) {
201         case 'equals':
202         case 'not_equals':
203             show('conditional_comparison_block');
204             break;
205
206         default:
207             hide('conditional_comparison_block');
208             break;
209     }
210 }
211
212 function onToFieldRegexChange( checkboxObj ) {
213     if ( checkboxObj.checked ) {
214         show('to_field_regex_value_block');
215     } else {
216         hide('to_field_regex_value_block');
217     }
218 }
219
220 function onConditionalRegexChange( checkboxObj ) {
221     if ( checkboxObj.checked ) {
222         $("span.match_regex_prefix" ).show();
223         $("span.match_regex_suffix" ).show();
224     } else {
225         $("span.match_regex_prefix" ).hide();
226         $("span.match_regex_suffix" ).hide();
227     }
228 }
229
230 function show(eltId) {
231     elt = document.getElementById( eltId );
232     elt.style.display='inline';
233 }
234
235 function hide(eltId) {
236     clearFormElements( eltId );
237     elt = document.getElementById( eltId );
238     elt.style.display='none';
239 }
240
241 function clearFormElements(divId) {
242     myBlock = document.getElementById( divId );
243
244     var inputElements = myBlock.getElementsByTagName( "input" );
245     for (var i = 0; i < inputElements.length; i++) {
246         switch( inputElements[i].type ) {
247             case "text":
248                 inputElements[i].value = '';
249                 break;
250             case "checkbox":
251                 inputElements[i].checked = false;
252                 break;
253         }
254     }
255
256     var selectElements = myBlock.getElementsByTagName( "select" );
257     for (var i = 0; i < selectElements.length; i++) {
258         selectElements[i].selectedIndex = 0;
259     }
260
261 }
262
263 function confirmDeleteAction() {
264     return confirm( __("Are you sure you wish to delete this template action?") );
265 }
266
267 function confirmDelete() {
268     return confirm( __("Are you sure you wish to delete this template?") );
269 }
270
271 var modaction_legend_innerhtml;
272 var action_submit_value;
273
274 function editAction( mmta ) {
275     $("#add_action").show();
276     document.getElementById('mmta_id').value = mmta['mmta_id'];
277
278     setSelectByValue( 'action', mmta['action'] );
279     $('#action').change();
280
281     setSelectByValue( 'field_number', mmta['field_number'] );
282
283     document.getElementById('from_field').value = mmta['from_field'];
284     document.getElementById('from_subfield').value = mmta['from_subfield'];
285     document.getElementById('field_value').value = mmta['field_value'];
286     document.getElementById('to_field').value = mmta['to_field'];
287     document.getElementById('to_subfield').value = mmta['to_subfield'];
288     if ( mmta['regex_search'] == '' && mmta['to_regex_replace'] == '' && mmta['to_regex_modifiers'] == '' ) {
289         $('#to_field_regex').prop('checked', false).change();
290     } else {
291         $('#to_field_regex').prop('checked', true).change();
292         $("#to_regex_search").val(mmta['to_regex_search']);
293         $("#to_regex_replace").val(mmta['to_regex_replace']);
294         $("#to_regex_modifiers").val(mmta['to_regex_modifiers']);
295     }
296
297     setSelectByValue( 'conditional', mmta['conditional'] );
298     $('#conditional').change();
299
300     document.getElementById('conditional_field').value = mmta['conditional_field'];
301     document.getElementById('conditional_subfield').value = mmta['conditional_subfield'];
302
303     setSelectByValue( 'conditional_comparison', mmta['conditional_comparison'] );
304     $('#conditional_comparison').change();
305
306     document.getElementById('conditional_value').value = mmta['conditional_value'];
307
308     document.getElementById('conditional_regex').checked = parseInt( mmta['conditional_regex'] );
309     $('#conditional_regex').change();
310
311     document.getElementById('description').value = mmta['description'];
312
313     window.modaction_legend_innerhtml = document.getElementById('modaction_legend').innerHTML;
314     document.getElementById('modaction_legend').innerHTML = __("Edit action %s").format(mmta['ordering']);
315
316     window.action_submit_value = document.getElementById('action_submit').value;
317     document.getElementById('action_submit').value = __("Update action");
318 }
319
320 function cancelEditAction() {
321     document.getElementById('mmta_id').value = '';
322
323     setSelectByValue( 'action', 'delete_field' );
324     $('#action').change();
325
326     document.getElementById('from_field').value = '';
327     document.getElementById('from_subfield').value = '';
328     document.getElementById('field_value').value = '';
329     document.getElementById('to_field').value = '';
330     document.getElementById('to_subfield').value = '';
331     $("#to_regex_search").val("");
332     $("#to_regex_replace").val("");
333     $("#to_regex_modifiers").val("");
334     $("#description").val("");
335
336     $('#to_field_regex').prop('checked', false).change();
337
338     setSelectByValue( 'conditional', '' );
339     $('#conditional').change();
340
341     document.getElementById('conditional_field').value = '';
342     document.getElementById('conditional_subfield').value = '';
343
344     setSelectByValue( 'conditional_comparison', '' );
345     $('#conditional_comparison').change();
346
347     document.getElementById('conditional_value').value = '';
348
349     document.getElementById('conditional_regex').checked = false;
350
351     document.getElementById('modaction_legend').innerHTML = window.modaction_legend_innerhtml;
352     document.getElementById('action_submit').value = window.action_submit_value;
353     $("#add_action").hide();
354 }
355
356 function setSelectByValue( selectId, value ) {
357     s = document.getElementById( selectId );
358
359     for ( i = 0; i < s.options.length; i++ ) {
360         if ( s.options[i].value == value ) {
361             s.selectedIndex = i;
362         }
363     }
364 }