1 /* global __ KohaTable table_settings */
2 $(document).ready(function() {
3 window.modaction_legend_innerhtml = $("#modaction_legend").text();
4 window.action_submit_value = $("#action_submit").val();
6 $('#select_template').find("input:submit").hide();
7 $('#select_template').change(function() {
8 $('#select_template').submit();
10 $("span.match_regex_prefix" ).hide();
11 $("span.match_regex_suffix" ).hide();
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.") );
20 if ( $("#to_field").val().length <= 0 ) {
21 alert( __("The destination should be filled.") );
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") );
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.") );
35 if ( action == 'update_field' ) {
36 if ( $("#from_subfield").val().length <= 0 ) {
37 alert( __("The source subfield should be filled for update.") );
41 if ( $("#from_field").val().length <= 0 ) {
42 alert( __("The source field should be filled.") );
45 if ( $("#conditional").val() == 'if' || $("#conditional").val() == 'unless' ) {
46 if ( $("#conditional_field").val() == '' ) {
47 alert( __("The conditional field should be filled.") );
50 if ( $("#conditional_comparison").val() == '' ) {
51 alert( __("The conditional comparison operator should be filled.") );
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.") );
60 alert( __("The conditional value should be filled.") );
67 $("#conditional_field,#from_field").change(function(){
71 $(".new_action").on("click",function(e){
74 $("#no_defined_actions").hide();
75 $("#add_action").show();
79 $(".duplicate_template").on("click",function(e){
81 var template_id = $(this).data("template_id");
82 $("#duplicate_a_template").val(template_id);
83 $("#duplicate_current_template").val(1);
86 $('#createTemplate').on('shown.bs.modal', function (e) {
88 $("#template_name").focus();
91 $("#duplicate_a_template").on("change",function(e){
93 if( this.value === '' ){
94 $("#duplicate_current_template").val("");
96 $("#duplicate_current_template").val(1);
100 $(".delete_template").on("click",function(){
101 return confirmDelete();
104 $(".edit_action").on("click", function(){
105 var mmta_id = $(this).data("mmta_id");
106 var mmta = $.grep(mmtas, function(elt, id) {
107 return elt['mmta_id'] == mmta_id;
109 editAction( mmta[0] );
113 KohaTable("templatest", {
118 function updateAllEvery(){
119 if ( $("#conditional_field").is(":visible") ) {
120 if ( $("#conditional_field").val() == $("#from_field").val() && $("#from_field").val().length > 0 ) {
121 $("#field_number option[value='0']").html( __("Every") );
123 $("#field_number option[value='0']").html( __("All") );
128 function onActionChange(selectObj) {
129 // get the index of the selected option
130 var idx = selectObj.selectedIndex;
132 // get the value of the selected option
133 var action = selectObj.options[idx].value;
137 show('field_number_block');
138 hide('with_value_block');
139 hide('to_field_block');
143 hide('field_number_block');
144 show('with_value_block');
145 hide('to_field_block');
149 hide('field_number_block');
150 show('with_value_block');
151 hide('to_field_block');
155 show('field_number_block');
156 hide('with_value_block');
157 show('to_field_block');
161 show('field_number_block');
162 hide('with_value_block');
163 show('to_field_block');
166 case 'copy_and_replace_field':
167 show('field_number_block');
168 hide('with_value_block');
169 show('to_field_block');
175 function onConditionalChange(selectObj) {
176 // get the index of the selected option
177 var idx = selectObj.selectedIndex;
179 // get the value of the selected option
180 var action = selectObj.options[idx].value;
184 hide('conditional_block');
189 show('conditional_block');
194 function onConditionalComparisonChange(selectObj) {
195 // get the index of the selected option
196 var idx = selectObj.selectedIndex;
198 // get the value of the selected option
199 var action = selectObj.options[idx].value;
204 show('conditional_comparison_block');
208 hide('conditional_comparison_block');
213 function onToFieldRegexChange( checkboxObj ) {
214 if ( checkboxObj.checked ) {
215 show('to_field_regex_value_block');
217 hide('to_field_regex_value_block');
221 function onConditionalRegexChange( checkboxObj ) {
222 if ( checkboxObj.checked ) {
223 $("span.match_regex_prefix" ).show();
224 $("span.match_regex_suffix" ).show();
226 $("span.match_regex_prefix" ).hide();
227 $("span.match_regex_suffix" ).hide();
231 function show(eltId) {
232 elt = document.getElementById( eltId );
233 elt.style.display='inline';
236 function hide(eltId) {
237 clearFormElements( eltId );
238 elt = document.getElementById( eltId );
239 elt.style.display='none';
242 function clearFormElements(divId) {
243 myBlock = document.getElementById( divId );
245 var inputElements = myBlock.getElementsByTagName( "input" );
246 for (var i = 0; i < inputElements.length; i++) {
247 switch( inputElements[i].type ) {
249 inputElements[i].value = '';
252 inputElements[i].checked = false;
257 var selectElements = myBlock.getElementsByTagName( "select" );
258 for (var i = 0; i < selectElements.length; i++) {
259 selectElements[i].selectedIndex = 0;
264 function confirmDeleteAction() {
265 return confirm( __("Are you sure you wish to delete this template action?") );
268 function confirmDelete() {
269 return confirm( __("Are you sure you wish to delete this template?") );
272 var modaction_legend_innerhtml;
273 var action_submit_value;
275 function editAction( mmta ) {
276 $("#add_action").show();
277 document.getElementById('mmta_id').value = mmta['mmta_id'];
279 setSelectByValue( 'action', mmta['action'] );
280 $('#action').change();
282 setSelectByValue( 'field_number', mmta['field_number'] );
284 document.getElementById('from_field').value = mmta['from_field'];
285 document.getElementById('from_subfield').value = mmta['from_subfield'];
286 document.getElementById('field_value').value = mmta['field_value'];
287 document.getElementById('to_field').value = mmta['to_field'];
288 document.getElementById('to_subfield').value = mmta['to_subfield'];
289 if ( mmta['regex_search'] == '' && mmta['to_regex_replace'] == '' && mmta['to_regex_modifiers'] == '' ) {
290 $('#to_field_regex').prop('checked', false).change();
292 $('#to_field_regex').prop('checked', true).change();
293 $("#to_regex_search").val(mmta['to_regex_search']);
294 $("#to_regex_replace").val(mmta['to_regex_replace']);
295 $("#to_regex_modifiers").val(mmta['to_regex_modifiers']);
298 setSelectByValue( 'conditional', mmta['conditional'] );
299 $('#conditional').change();
301 document.getElementById('conditional_field').value = mmta['conditional_field'];
302 document.getElementById('conditional_subfield').value = mmta['conditional_subfield'];
304 setSelectByValue( 'conditional_comparison', mmta['conditional_comparison'] );
305 $('#conditional_comparison').change();
307 document.getElementById('conditional_value').value = mmta['conditional_value'];
309 document.getElementById('conditional_regex').checked = parseInt( mmta['conditional_regex'] );
310 $('#conditional_regex').change();
312 document.getElementById('description').value = mmta['description'];
314 window.modaction_legend_innerhtml = document.getElementById('modaction_legend').innerHTML;
315 document.getElementById('modaction_legend').innerHTML = __("Edit action %s").format(mmta['ordering']);
317 window.action_submit_value = document.getElementById('action_submit').value;
318 document.getElementById('action_submit').value = __("Update action");
321 function cancelEditAction() {
322 document.getElementById('mmta_id').value = '';
324 setSelectByValue( 'action', 'delete_field' );
325 $('#action').change();
327 document.getElementById('from_field').value = '';
328 document.getElementById('from_subfield').value = '';
329 document.getElementById('field_value').value = '';
330 document.getElementById('to_field').value = '';
331 document.getElementById('to_subfield').value = '';
332 $("#to_regex_search").val("");
333 $("#to_regex_replace").val("");
334 $("#to_regex_modifiers").val("");
335 $("#description").val("");
337 $('#to_field_regex').prop('checked', false).change();
339 setSelectByValue( 'conditional', '' );
340 $('#conditional').change();
342 document.getElementById('conditional_field').value = '';
343 document.getElementById('conditional_subfield').value = '';
345 setSelectByValue( 'conditional_comparison', '' );
346 $('#conditional_comparison').change();
348 document.getElementById('conditional_value').value = '';
350 document.getElementById('conditional_regex').checked = false;
352 document.getElementById('modaction_legend').innerHTML = window.modaction_legend_innerhtml;
353 document.getElementById('action_submit').value = window.action_submit_value;
355 if( $("#template_actions").length < 1 ){
356 $("#no_defined_actions").show();
359 $("#add_action").hide();
362 function setSelectByValue( selectId, value ) {
363 s = document.getElementById( selectId );
365 for ( i = 0; i < s.options.length; i++ ) {
366 if ( s.options[i].value == value ) {