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 $("#add_action").show();
78 $(".duplicate_template").on("click",function(e){
80 var template_id = $(this).data("template_id");
81 $("#duplicate_a_template").val(template_id);
82 $("#duplicate_current_template").val(1);
85 $('#createTemplate').on('shown.bs.modal', function (e) {
87 $("#template_name").focus();
90 $("#duplicate_a_template").on("change",function(e){
92 if( this.value === '' ){
93 $("#duplicate_current_template").val("");
95 $("#duplicate_current_template").val(1);
99 $(".delete_template").on("click",function(){
100 return confirmDelete();
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;
108 editAction( mmta[0] );
112 KohaTable("templatest", {
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") );
122 $("#field_number option[value='0']").html( __("All") );
127 function onActionChange(selectObj) {
128 // get the index of the selected option
129 var idx = selectObj.selectedIndex;
131 // get the value of the selected option
132 var action = selectObj.options[idx].value;
136 show('field_number_block');
137 hide('with_value_block');
138 hide('to_field_block');
142 hide('field_number_block');
143 show('with_value_block');
144 hide('to_field_block');
148 hide('field_number_block');
149 show('with_value_block');
150 hide('to_field_block');
154 show('field_number_block');
155 hide('with_value_block');
156 show('to_field_block');
160 show('field_number_block');
161 hide('with_value_block');
162 show('to_field_block');
165 case 'copy_and_replace_field':
166 show('field_number_block');
167 hide('with_value_block');
168 show('to_field_block');
174 function onConditionalChange(selectObj) {
175 // get the index of the selected option
176 var idx = selectObj.selectedIndex;
178 // get the value of the selected option
179 var action = selectObj.options[idx].value;
183 hide('conditional_block');
188 show('conditional_block');
193 function onConditionalComparisonChange(selectObj) {
194 // get the index of the selected option
195 var idx = selectObj.selectedIndex;
197 // get the value of the selected option
198 var action = selectObj.options[idx].value;
203 show('conditional_comparison_block');
207 hide('conditional_comparison_block');
212 function onToFieldRegexChange( checkboxObj ) {
213 if ( checkboxObj.checked ) {
214 show('to_field_regex_value_block');
216 hide('to_field_regex_value_block');
220 function onConditionalRegexChange( checkboxObj ) {
221 if ( checkboxObj.checked ) {
222 $("span.match_regex_prefix" ).show();
223 $("span.match_regex_suffix" ).show();
225 $("span.match_regex_prefix" ).hide();
226 $("span.match_regex_suffix" ).hide();
230 function show(eltId) {
231 elt = document.getElementById( eltId );
232 elt.style.display='inline';
235 function hide(eltId) {
236 clearFormElements( eltId );
237 elt = document.getElementById( eltId );
238 elt.style.display='none';
241 function clearFormElements(divId) {
242 myBlock = document.getElementById( divId );
244 var inputElements = myBlock.getElementsByTagName( "input" );
245 for (var i = 0; i < inputElements.length; i++) {
246 switch( inputElements[i].type ) {
248 inputElements[i].value = '';
251 inputElements[i].checked = false;
256 var selectElements = myBlock.getElementsByTagName( "select" );
257 for (var i = 0; i < selectElements.length; i++) {
258 selectElements[i].selectedIndex = 0;
263 function confirmDeleteAction() {
264 return confirm( __("Are you sure you wish to delete this template action?") );
267 function confirmDelete() {
268 return confirm( __("Are you sure you wish to delete this template?") );
271 var modaction_legend_innerhtml;
272 var action_submit_value;
274 function editAction( mmta ) {
275 $("#add_action").show();
276 document.getElementById('mmta_id').value = mmta['mmta_id'];
278 setSelectByValue( 'action', mmta['action'] );
279 $('#action').change();
281 setSelectByValue( 'field_number', mmta['field_number'] );
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();
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']);
297 setSelectByValue( 'conditional', mmta['conditional'] );
298 $('#conditional').change();
300 document.getElementById('conditional_field').value = mmta['conditional_field'];
301 document.getElementById('conditional_subfield').value = mmta['conditional_subfield'];
303 setSelectByValue( 'conditional_comparison', mmta['conditional_comparison'] );
304 $('#conditional_comparison').change();
306 document.getElementById('conditional_value').value = mmta['conditional_value'];
308 document.getElementById('conditional_regex').checked = parseInt( mmta['conditional_regex'] );
309 $('#conditional_regex').change();
311 document.getElementById('description').value = mmta['description'];
313 window.modaction_legend_innerhtml = document.getElementById('modaction_legend').innerHTML;
314 document.getElementById('modaction_legend').innerHTML = __("Edit action %s").format(mmta['ordering']);
316 window.action_submit_value = document.getElementById('action_submit').value;
317 document.getElementById('action_submit').value = __("Update action");
320 function cancelEditAction() {
321 document.getElementById('mmta_id').value = '';
323 setSelectByValue( 'action', 'delete_field' );
324 $('#action').change();
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("");
336 $('#to_field_regex').prop('checked', false).change();
338 setSelectByValue( 'conditional', '' );
339 $('#conditional').change();
341 document.getElementById('conditional_field').value = '';
342 document.getElementById('conditional_subfield').value = '';
344 setSelectByValue( 'conditional_comparison', '' );
345 $('#conditional_comparison').change();
347 document.getElementById('conditional_value').value = '';
349 document.getElementById('conditional_regex').checked = false;
351 document.getElementById('modaction_legend').innerHTML = window.modaction_legend_innerhtml;
352 document.getElementById('action_submit').value = window.action_submit_value;
353 $("#add_action").hide();
356 function setSelectByValue( selectId, value ) {
357 s = document.getElementById( selectId );
359 for ( i = 0; i < s.options.length; i++ ) {
360 if ( s.options[i].value == value ) {