1 $(document).ready(function() {
2 window.modaction_legend_innerhtml = $("#modaction_legend").text();
3 window.action_submit_value = $("#action_submit").val();
5 $('#select_template').find("input:submit").hide();
6 $('#select_template').change(function() {
7 $('#select_template').submit();
9 $("span.match_regex_prefix" ).hide();
10 $("span.match_regex_suffix" ).hide();
12 $("#add_action").submit(function(){
13 var action = $("#action").val();
14 if ( action == 'move_field' || action == 'copy_field' || action == 'copy_and_replace_field') {
15 if ( $("#from_subfield").val().length != $("#to_subfield").val().length ) {
16 alert( MSG_MMT_SUBFIELDS_MATCH );
19 if ( $("#to_field").val().length <= 0 ) {
20 alert( MSG_MMT_DESTINATION_REQUIRED );
23 if ( ( $("#to_field").val() < 10 && $("#to_subfield").val().length > 0 ) ||
24 ( $("#from_field").val() < 10 && $("#from_subfield").val().length > 0 ) ) {
25 alert( MSG_MMT_CONTROL_FIELD_EMPTY );
28 if ( ( $("#from_field").val() < 10 && $("#to_subfield").val().length === 0 ) ||
29 ( $("#to_field").val() < 10 && $("#from_subfield").val().length === 0 ) ) {
30 alert( MSG_MMT_CONTROL_FIELD );
34 if ( action == 'update_field' ) {
35 if ( $("#from_subfield").val().length <= 0 ) {
36 alert( MSG_MMT_SOURCE_SUBFIELD );
40 if ( $("#from_field").val().length <= 0 ) {
41 alert( MSG_MMT_SOURCE_FIELD );
44 if ( $("#conditional").val() == 'if' || $("#conditional").val() == 'unless' ) {
45 if ( $("#conditional_field").val() == '' ) {
46 alert( MSG_MMT_CONDITIONAL_FIELD_REQUIRED );
49 if ( $("#conditional_comparison").val() == '' ) {
50 alert( MSG_MMT_CONDITIONAL_COMPARISON_REQUIRED );
53 if ( $("#conditional_value").val() == '' &&
54 ( $("#conditional_comparison").val() == 'equals' || $("#conditional_comparison").val() == 'not_equals' ) ) {
55 if ( document.getElementById('conditional_regex').checked == true ) {
56 alert( MSG_MMT_CONDITIONAL_VALUE_REGEX_REQUIRED );
59 alert( MSG_MMT_CONDITIONAL_VALUE_REQUIRED );
66 $("#conditional_field,#from_field").change(function(){
70 $("#new_action").on("click",function(e){
73 $("#add_action").show();
77 $(".duplicate_template").on("click",function(e){
79 var template_id = $(this).data("template_id");
80 $("#duplicate_a_template").val(template_id);
81 $("#duplicate_current_template").val(1);
84 $('#createTemplate').on('shown.bs.modal', function (e) {
86 $("#template_name").focus();
89 $("#duplicate_a_template").on("change",function(e){
91 if( this.value === '' ){
92 $("#duplicate_current_template").val("");
94 $("#duplicate_current_template").val(1);
98 $(".delete_template").on("click",function(){
99 return confirmDelete();
102 $(".edit_action").on("click", function(){
103 var mmta_id = $(this).data("mmta_id");
104 var mmta = $.grep(mmtas, function(elt, id) {
105 return elt['mmta_id'] == mmta_id;
107 editAction( mmta[0] );
112 function updateAllEvery(){
113 if ( $("#conditional_field").is(":visible") ) {
114 if ( $("#conditional_field").val() == $("#from_field").val() && $("#from_field").val().length > 0 ) {
115 $("#field_number option[value='0']").html( MSG_MMT_EVERY );
117 $("#field_number option[value='0']").html( MSG_MMT_ALL );
122 function onActionChange(selectObj) {
123 // get the index of the selected option
124 var idx = selectObj.selectedIndex;
126 // get the value of the selected option
127 var action = selectObj.options[idx].value;
131 show('field_number_block');
132 hide('with_value_block');
133 hide('to_field_block');
137 hide('field_number_block');
138 show('with_value_block');
139 hide('to_field_block');
143 hide('field_number_block');
144 show('with_value_block');
145 hide('to_field_block');
149 show('field_number_block');
150 hide('with_value_block');
151 show('to_field_block');
155 show('field_number_block');
156 hide('with_value_block');
157 show('to_field_block');
160 case 'copy_and_replace_field':
161 show('field_number_block');
162 hide('with_value_block');
163 show('to_field_block');
169 function onConditionalChange(selectObj) {
170 // get the index of the selected option
171 var idx = selectObj.selectedIndex;
173 // get the value of the selected option
174 var action = selectObj.options[idx].value;
178 hide('conditional_block');
183 show('conditional_block');
188 function onConditionalComparisonChange(selectObj) {
189 // get the index of the selected option
190 var idx = selectObj.selectedIndex;
192 // get the value of the selected option
193 var action = selectObj.options[idx].value;
198 show('conditional_comparison_block');
202 hide('conditional_comparison_block');
207 function onToFieldRegexChange( checkboxObj ) {
208 if ( checkboxObj.checked ) {
209 show('to_field_regex_value_block');
211 hide('to_field_regex_value_block');
215 function onConditionalRegexChange( checkboxObj ) {
216 if ( checkboxObj.checked ) {
217 $("span.match_regex_prefix" ).show();
218 $("span.match_regex_suffix" ).show();
220 $("span.match_regex_prefix" ).hide();
221 $("span.match_regex_suffix" ).hide();
225 function show(eltId) {
226 elt = document.getElementById( eltId );
227 elt.style.display='inline';
230 function hide(eltId) {
231 clearFormElements( eltId );
232 elt = document.getElementById( eltId );
233 elt.style.display='none';
236 function clearFormElements(divId) {
237 myBlock = document.getElementById( divId );
239 var inputElements = myBlock.getElementsByTagName( "input" );
240 for (var i = 0; i < inputElements.length; i++) {
241 switch( inputElements[i].type ) {
243 inputElements[i].value = '';
246 inputElements[i].checked = false;
251 var selectElements = myBlock.getElementsByTagName( "select" );
252 for (var i = 0; i < selectElements.length; i++) {
253 selectElements[i].selectedIndex = 0;
258 function confirmDeleteAction() {
259 return confirm( MSG_MMT_CONFIRM_DEL_TEMPLATE_ACTION );
262 function confirmDelete() {
263 return confirm( MSG_MMT_CONFIRM_DEL_TEMPLATE );
266 var modaction_legend_innerhtml;
267 var action_submit_value;
269 function editAction( mmta ) {
270 $("#add_action").show();
271 document.getElementById('mmta_id').value = mmta['mmta_id'];
273 setSelectByValue( 'action', mmta['action'] );
274 $('#action').change();
276 setSelectByValue( 'field_number', mmta['field_number'] );
278 document.getElementById('from_field').value = mmta['from_field'];
279 document.getElementById('from_subfield').value = mmta['from_subfield'];
280 document.getElementById('field_value').value = mmta['field_value'];
281 document.getElementById('to_field').value = mmta['to_field'];
282 document.getElementById('to_subfield').value = mmta['to_subfield'];
283 if ( mmta['regex_search'] == '' && mmta['to_regex_replace'] == '' && mmta['to_regex_modifiers'] == '' ) {
284 $('#to_field_regex').prop('checked', false).change();
286 $('#to_field_regex').prop('checked', true).change();
287 $("#to_regex_search").val(mmta['to_regex_search']);
288 $("#to_regex_replace").val(mmta['to_regex_replace']);
289 $("#to_regex_modifiers").val(mmta['to_regex_modifiers']);
292 setSelectByValue( 'conditional', mmta['conditional'] );
293 $('#conditional').change();
295 document.getElementById('conditional_field').value = mmta['conditional_field'];
296 document.getElementById('conditional_subfield').value = mmta['conditional_subfield'];
298 setSelectByValue( 'conditional_comparison', mmta['conditional_comparison'] );
299 $('#conditional_comparison').change();
301 document.getElementById('conditional_value').value = mmta['conditional_value'];
303 document.getElementById('conditional_regex').checked = parseInt( mmta['conditional_regex'] );
304 $('#conditional_regex').change();
306 document.getElementById('description').value = mmta['description'];
308 window.modaction_legend_innerhtml = document.getElementById('modaction_legend').innerHTML;
309 document.getElementById('modaction_legend').innerHTML = MSG_MMT_EDIT_ACTION.format(mmta['ordering']);
311 window.action_submit_value = document.getElementById('action_submit').value;
312 document.getElementById('action_submit').value = MSG_MMT_UPDATE_ACTION;
315 function cancelEditAction() {
316 document.getElementById('mmta_id').value = '';
318 setSelectByValue( 'action', 'delete_field' );
319 $('#action').change();
321 document.getElementById('from_field').value = '';
322 document.getElementById('from_subfield').value = '';
323 document.getElementById('field_value').value = '';
324 document.getElementById('to_field').value = '';
325 document.getElementById('to_subfield').value = '';
326 $("#to_regex_search").val("");
327 $("#to_regex_replace").val("");
328 $("#to_regex_modifiers").val("");
329 $("#description").val("");
331 $('#to_field_regex').prop('checked', false).change();
333 setSelectByValue( 'conditional', '' );
334 $('#conditional').change();
336 document.getElementById('conditional_field').value = '';
337 document.getElementById('conditional_subfield').value = '';
339 setSelectByValue( 'conditional_comparison', '' );
340 $('#conditional_comparison').change();
342 document.getElementById('conditional_value').value = '';
344 document.getElementById('conditional_regex').checked = false;
346 document.getElementById('modaction_legend').innerHTML = window.modaction_legend_innerhtml;
347 document.getElementById('action_submit').value = window.action_submit_value;
348 $("#add_action").hide();
351 function setSelectByValue( selectId, value ) {
352 s = document.getElementById( selectId );
354 for ( i = 0; i < s.options.length; i++ ) {
355 if ( s.options[i].value == value ) {