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] );
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") );
118 $("#field_number option[value='0']").html( __("All") );
123 function onActionChange(selectObj) {
124 // get the index of the selected option
125 var idx = selectObj.selectedIndex;
127 // get the value of the selected option
128 var action = selectObj.options[idx].value;
132 show('field_number_block');
133 hide('with_value_block');
134 hide('to_field_block');
138 hide('field_number_block');
139 show('with_value_block');
140 hide('to_field_block');
144 hide('field_number_block');
145 show('with_value_block');
146 hide('to_field_block');
150 show('field_number_block');
151 hide('with_value_block');
152 show('to_field_block');
156 show('field_number_block');
157 hide('with_value_block');
158 show('to_field_block');
161 case 'copy_and_replace_field':
162 show('field_number_block');
163 hide('with_value_block');
164 show('to_field_block');
170 function onConditionalChange(selectObj) {
171 // get the index of the selected option
172 var idx = selectObj.selectedIndex;
174 // get the value of the selected option
175 var action = selectObj.options[idx].value;
179 hide('conditional_block');
184 show('conditional_block');
189 function onConditionalComparisonChange(selectObj) {
190 // get the index of the selected option
191 var idx = selectObj.selectedIndex;
193 // get the value of the selected option
194 var action = selectObj.options[idx].value;
199 show('conditional_comparison_block');
203 hide('conditional_comparison_block');
208 function onToFieldRegexChange( checkboxObj ) {
209 if ( checkboxObj.checked ) {
210 show('to_field_regex_value_block');
212 hide('to_field_regex_value_block');
216 function onConditionalRegexChange( checkboxObj ) {
217 if ( checkboxObj.checked ) {
218 $("span.match_regex_prefix" ).show();
219 $("span.match_regex_suffix" ).show();
221 $("span.match_regex_prefix" ).hide();
222 $("span.match_regex_suffix" ).hide();
226 function show(eltId) {
227 elt = document.getElementById( eltId );
228 elt.style.display='inline';
231 function hide(eltId) {
232 clearFormElements( eltId );
233 elt = document.getElementById( eltId );
234 elt.style.display='none';
237 function clearFormElements(divId) {
238 myBlock = document.getElementById( divId );
240 var inputElements = myBlock.getElementsByTagName( "input" );
241 for (var i = 0; i < inputElements.length; i++) {
242 switch( inputElements[i].type ) {
244 inputElements[i].value = '';
247 inputElements[i].checked = false;
252 var selectElements = myBlock.getElementsByTagName( "select" );
253 for (var i = 0; i < selectElements.length; i++) {
254 selectElements[i].selectedIndex = 0;
259 function confirmDeleteAction() {
260 return confirm( __("Are you sure you wish to delete this template action?") );
263 function confirmDelete() {
264 return confirm( __("Are you sure you wish to delete this template?") );
267 var modaction_legend_innerhtml;
268 var action_submit_value;
270 function editAction( mmta ) {
271 $("#add_action").show();
272 document.getElementById('mmta_id').value = mmta['mmta_id'];
274 setSelectByValue( 'action', mmta['action'] );
275 $('#action').change();
277 setSelectByValue( 'field_number', mmta['field_number'] );
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();
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']);
293 setSelectByValue( 'conditional', mmta['conditional'] );
294 $('#conditional').change();
296 document.getElementById('conditional_field').value = mmta['conditional_field'];
297 document.getElementById('conditional_subfield').value = mmta['conditional_subfield'];
299 setSelectByValue( 'conditional_comparison', mmta['conditional_comparison'] );
300 $('#conditional_comparison').change();
302 document.getElementById('conditional_value').value = mmta['conditional_value'];
304 document.getElementById('conditional_regex').checked = parseInt( mmta['conditional_regex'] );
305 $('#conditional_regex').change();
307 document.getElementById('description').value = mmta['description'];
309 window.modaction_legend_innerhtml = document.getElementById('modaction_legend').innerHTML;
310 document.getElementById('modaction_legend').innerHTML = __("Edit action %s").format(mmta['ordering']);
312 window.action_submit_value = document.getElementById('action_submit').value;
313 document.getElementById('action_submit').value = __("Update action");
316 function cancelEditAction() {
317 document.getElementById('mmta_id').value = '';
319 setSelectByValue( 'action', 'delete_field' );
320 $('#action').change();
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("");
332 $('#to_field_regex').prop('checked', false).change();
334 setSelectByValue( 'conditional', '' );
335 $('#conditional').change();
337 document.getElementById('conditional_field').value = '';
338 document.getElementById('conditional_subfield').value = '';
340 setSelectByValue( 'conditional_comparison', '' );
341 $('#conditional_comparison').change();
343 document.getElementById('conditional_value').value = '';
345 document.getElementById('conditional_regex').checked = false;
347 document.getElementById('modaction_legend').innerHTML = window.modaction_legend_innerhtml;
348 document.getElementById('action_submit').value = window.action_submit_value;
349 $("#add_action").hide();
352 function setSelectByValue( selectId, value ) {
353 s = document.getElementById( selectId );
355 for ( i = 0; i < s.options.length; i++ ) {
356 if ( s.options[i].value == value ) {