|
9 | 9 | * @license https://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) |
10 | 10 | */ |
11 | 11 | ?> |
12 | | -<script type="text/javascript"> |
13 | | -//<![CDATA[ |
14 | | -var couponTypeSpecific = '<?= Mage_SalesRule_Model_Rule::COUPON_TYPE_SPECIFIC ?>'; |
15 | | -var tmpButtonsActionsStorage = []; |
| 12 | +<script> |
| 13 | +const couponTypeSpecific = '<?= Mage_SalesRule_Model_Rule::COUPON_TYPE_SPECIFIC ?>'; |
16 | 14 |
|
17 | 15 | function disableEnableCouponsTabContent(disable) { |
18 | | - var containerId = 'promo_catalog_edit_tabs_coupons_section_content'; |
19 | | - if($(containerId)){ |
20 | | - var dataFields = $(containerId).select('input', 'select', 'textarea', 'button'); |
21 | | - for(var i = 0; i < dataFields.length; i++) { |
22 | | - disable ? dataFields[i].disable().addClassName('disabled') |
23 | | - : dataFields[i].enable().removeClassName('disabled'); |
24 | | - } |
| 16 | + const containerEl = document.getElementById('promo_catalog_edit_tabs_coupons_section_content'); |
| 17 | + if (!containerEl) { |
| 18 | + return; |
| 19 | + } |
| 20 | + for (const el of containerEl.querySelectorAll('input, select, textarea, button')) { |
| 21 | + setElementDisable(el, disable); |
25 | 22 | } |
26 | | - disable ? $('rule_coupon_code').enable() : $('rule_coupon_code').disable(); |
27 | 23 | } |
28 | 24 |
|
29 | 25 | function handleCouponsTabContentActivity() { |
30 | | - disableEnableCouponsTabContent(!$('rule_use_auto_generation').checked); |
31 | | -} |
32 | | - |
33 | | -function handleCouponTypeChange() { |
34 | | - $('rule_coupon_type').observe('change', function() { |
35 | | - var disable = $('rule_coupon_type').value != couponTypeSpecific; |
36 | | - if (!disable) { |
37 | | - disable = !$('rule_use_auto_generation').checked; |
| 26 | + const couponType = document.getElementById('rule_coupon_type').value; |
| 27 | + if (couponType == couponTypeSpecific) { |
| 28 | + if (document.getElementById('rule_use_auto_generation').checked) { |
| 29 | + disableEnableCouponsTabContent(false); |
| 30 | + setElementDisable(document.getElementById('rule_coupon_code'), true); |
| 31 | + } else { |
| 32 | + disableEnableCouponsTabContent(true); |
| 33 | + setElementDisable(document.getElementById('rule_coupon_code'), false); |
38 | 34 | } |
39 | | - disableEnableCouponsTabContent(disable); |
40 | | - }); |
| 35 | + } else { |
| 36 | + disableEnableCouponsTabContent(true); |
| 37 | + } |
41 | 38 | } |
42 | 39 |
|
43 | | -function refreshCouponCodesGrid(grid, gridMassAction, transport) { |
| 40 | +function refreshCouponCodesGrid(grid, gridMassAction) { |
44 | 41 | grid.reload(); |
45 | 42 | gridMassAction.unselectAll(); |
46 | 43 | } |
47 | 44 |
|
48 | | -function generateCouponCodes(idPrefix, generateUrl, grid) { |
49 | | - $(idPrefix + 'information_fieldset').removeClassName('ignore-validate'); |
50 | | - var validationResult = $(idPrefix + 'information_fieldset').select('input', |
51 | | - 'select', 'textarea').collect( function(elm) { |
52 | | - return Validation.validate(elm, { |
53 | | - useTitle :false, |
54 | | - onElementValidate : function() { |
55 | | - } |
| 45 | +async function generateCouponCodes(idPrefix, generateUrl, grid) { |
| 46 | + const fieldsetEl = document.getElementById(idPrefix + 'information_fieldset'); |
| 47 | + const elements = fieldsetEl.querySelectorAll('input, select, textarea'); |
| 48 | + let validationResult = true; |
| 49 | + |
| 50 | + fieldsetEl.classList.remove('ignore-validate'); |
| 51 | + for (const el of elements) { |
| 52 | + validationResult &&= Validation.validate(el, { |
| 53 | + useTitle: false, |
| 54 | + onElementValidate: function() {}, |
56 | 55 | }); |
57 | | - }).all(); |
58 | | - $(idPrefix + 'information_fieldset').addClassName('ignore-validate'); |
| 56 | + } |
| 57 | + fieldsetEl.classList.add('ignore-validate'); |
59 | 58 |
|
60 | 59 | if (!validationResult) { |
61 | 60 | return; |
62 | 61 | } |
63 | | - var elements = $(idPrefix + 'information_fieldset').select('input', 'select', 'textarea'); |
64 | 62 |
|
65 | | - elements = elements.concat( |
66 | | - $$('#rule_uses_per_coupon'), |
67 | | - $$('#rule_uses_per_customer'), |
68 | | - $$('#rule_to_date') |
69 | | - ); |
| 63 | + const formData = new FormData(); |
| 64 | + for (const el of elements) { |
| 65 | + formData.append(el.name, el.value); |
| 66 | + } |
| 67 | + for (const extra of ['rule_uses_per_coupon', 'rule_uses_per_customer', 'rule_to_date']) { |
| 68 | + const el = document.getElementById(extra); |
| 69 | + formData.append(el.name, el.value); |
| 70 | + } |
| 71 | + |
| 72 | + clearMessagesDiv(); |
70 | 73 |
|
71 | | - var params = Form.serializeElements(elements, true); |
72 | | - params.form_key = FORM_KEY; |
73 | | - $('messages').update(); |
74 | | - var couponCodesGrid = eval(grid); |
75 | | - new Ajax.Request(generateUrl, { |
76 | | - parameters :params, |
77 | | - method :'post', |
78 | | - onComplete : function (transport, param){ |
79 | | - var response = transport.responseJSON || transport.responseText.evalJSON(true) || {}; |
| 74 | + try { |
| 75 | + const result = await mahoFetch(generateUrl, { |
| 76 | + method: 'POST', |
| 77 | + body: formData, |
| 78 | + }); |
80 | 79 |
|
81 | | - if (couponCodesGrid) { |
82 | | - couponCodesGrid.reload(); |
83 | | - } |
84 | | - if (response && response.messages) { |
85 | | - $('messages').update(response.messages); |
86 | | - } |
87 | | - if (response && response.error) { |
88 | | - alert(response.error.stripTags().toString()); |
89 | | - } |
| 80 | + if (window[grid]) { |
| 81 | + window[grid].reload(); |
90 | 82 | } |
91 | | - }); |
92 | | -} |
93 | | - |
94 | | -Ajax.Responders.register({ |
95 | | - onComplete: function() { |
96 | | - if ($('promo_catalog_edit_tabs_coupons_section_content') |
97 | | - && $('promo_catalog_edit_tabs_coupons_section_content').visible() |
98 | | - && Ajax.activeRequestCount == 0 |
99 | | - ) { |
100 | | - handleCouponsTabContentActivity(); |
| 83 | + if (result?.messages) { |
| 84 | + setMessagesDivHtml(result.messages); |
101 | 85 | } |
| 86 | + } catch (error) { |
| 87 | + setMessagesDiv(error.message, 'error'); |
102 | 88 | } |
103 | | -}); |
104 | 89 |
|
105 | | -document.observe("dom:loaded", handleCouponsTabContentActivity); |
106 | | -document.observe("dom:loaded", handleCouponTypeChange); |
107 | | -//]]> |
| 90 | + handleCouponsTabContentActivity(); |
| 91 | +} |
| 92 | + |
| 93 | +document.addEventListener('DOMContentLoaded', () => { |
| 94 | + handleCouponsTabContentActivity(); |
| 95 | + document.getElementById('rule_coupon_type')?.addEventListener('change', handleCouponsTabContentActivity); |
| 96 | + document.getElementById('rule_use_auto_generation')?.addEventListener('change', handleCouponsTabContentActivity); |
| 97 | +}); |
108 | 98 | </script> |
0 commit comments