PluginProbe
Tiered Pricing Table for WooCommerce / 6.1.0
Tiered Pricing Table for WooCommerce v6.1.0
8.0.2 7.1.7 7.1.5 7.1.4 7.1.1 6.5.0 6.4.0 6.1.0 trunk 1.0 1.1 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2 2.2.3 2.3.0 2.3.1 2.3.2 2.3.3 All 91 releases
tier-pricing-table / assets / admin / main.js

main.js in Tiered Pricing Table for WooCommerce 6.1.0, at assets/admin/main.js

269 lines 9.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 jQuery(document).ready(function ($) {
2
3 jQuery(document).on('click', '[data-add-new-price-rule]', function (e) {
4 e.preventDefault();
5
6 var newRuleInputs = jQuery(e.target).parent().find('[data-price-rules-input-wrapper]').first().clone();
7
8 jQuery('<span data-price-rules-container></span>').insertBefore(jQuery(e.target))
9 .append(newRuleInputs)
10 .append('<span class="notice-dismiss remove-price-rule" data-remove-price-rule style="vertical-align: middle"></span>')
11 .append('<br><br>');
12
13 newRuleInputs.children('input').val('');
14
15 recalculateIndexes(jQuery(e.target).closest('[data-price-rules-wrapper]'));
16 });
17
18 jQuery('body').on('click', '.remove-price-rule', function (e) {
19 e.preventDefault();
20
21 var element = jQuery(e.target.parentElement);
22 var wrapper = element.parent('[data-price-rules-wrapper]');
23 var containers = wrapper.find('[data-price-rules-container]');
24
25 if ((containers.length) < 2) {
26 containers.find('input').val('');
27 return;
28 }
29
30 jQuery('[data-price-rules-wrapper] .wc_input_price').trigger('change');
31
32 element.remove();
33
34 recalculateIndexes(wrapper);
35 });
36
37 function recalculateIndexes(container) {
38
39 var fieldsName = [
40 'tiered_price_percent_quantity',
41 'tiered_price_percent_discount',
42 'tiered_price_fixed_quantity',
43 'tiered_price_fixed_price'
44 ];
45
46 for (var key in fieldsName) {
47 if (fieldsName.hasOwnProperty(key)) {
48 var name = fieldsName[key];
49
50 jQuery.each(jQuery(container.find('input[name^="' + name + '"]')), function (index, el) {
51 var currentName = jQuery(el).attr('name');
52
53 var newName = currentName.replace(/\[\d*\]$/, '[' + index + ']');
54
55 jQuery(el).attr('name', newName);
56 });
57 }
58 }
59
60 }
61
62 var RoleBasedBlock = function () {
63
64 $(document).on('change', '[data-role-tiered-price-type-select]', function (e) {
65
66 var $container = $(e.target).closest('div');
67
68 $container.find('[data-role-tiered-price-type]').css('display', 'none');
69 $container.find('[data-role-tiered-price-type-' + this.value + ']').css('display', 'block');
70 });
71
72 this.$block = null;
73 this.initializedBlocks = [];
74
75 this.init = function (id) {
76
77 this.variationCanBeChangedAlreadyTriggered = false;
78 this.id = id;
79 this.$block = jQuery('#' + id);
80
81 if (this.initializedBlocks[id] !== undefined) {
82 this.unbindEvents();
83 }
84
85 this.bindEvents();
86
87 this.initializedBlocks[id] = this;
88 };
89
90 this.bindEvents = function () {
91 $('body').on('click', '#' + this.id + ' .tpt-role-based-role-action--delete', this.removeRole.bind(this));
92 $('body').on('click', '#' + this.id + ' .tpt-role-based-role__header', this.toggleRoleView.bind(this));
93 $('body').on('click', '#' + this.id + ' .tpt-role-based-adding-form__add-button', this.addRole.bind(this));
94 }
95
96 this.unbindEvents = function () {
97 $('body').off('click', '#' + this.id + ' .tpt-role-based-role-action--delete');
98 $('body').off('click', '#' + this.id + ' .tpt-role-based-role__header');
99 $('body').off('click', '#' + this.id + ' .tpt-role-based-adding-form__add-button');
100 }
101
102 this.toggleRoleView = function (event) {
103
104 var $element = $(event.target);
105
106 if ($element.hasClass('tpt-role-based-role-action--delete')) {
107 return;
108 }
109
110 var role = $element.closest('.tpt-role-based-role');
111
112 if (role.data('visible')) {
113 this.hideRole(role);
114 } else {
115 this.showRole(role);
116 }
117 };
118
119 this.showRole = function ($role) {
120 $role.find('.tpt-role-based-role__content').stop().slideDown(400);
121 $role.find('.tpt-role-based-role__action-toggle-view')
122 .removeClass('tpt-role-based-role__action-toggle-view--open')
123 .addClass('tpt-role-based-role__action-toggle-view--close');
124
125 $role.data('visible', true);
126 };
127
128 this.hideRole = function ($role) {
129 $role.find('.tpt-role-based-role__content').stop().slideUp(400);
130 $role.find('.tpt-role-based-role__action-toggle-view')
131 .removeClass('tpt-role-based-role__action-toggle-view--close')
132 .addClass('tpt-role-based-role__action-toggle-view--open');
133 $role.data('visible', false);
134 };
135
136 this.removeRole = function (e) {
137 e.preventDefault();
138
139 if (confirm("Are you sure?")) {
140
141 var $roleToRemove = $(e.target).closest('.tpt-role-based-role');
142 var roleSlug = $roleToRemove.data('role-slug');
143
144 this.$block.find('.tpt-role-based-adding-form__role-selector').append('<option value="' + roleSlug + '">' + $roleToRemove.data('role-name') + '</option>');
145 this.$block.find('.tiered_price_rules_roles_to_delete').find('[value=' + roleSlug + ']').prop('selected', true);
146
147 $roleToRemove.slideUp(400, function () {
148 $roleToRemove.remove();
149 });
150
151 this.triggerVariationCanBeUpdated();
152 }
153 };
154
155 this.block = function () {
156 this.$block.block({
157 message: null,
158 overlayCSS: {
159 background: '#fff',
160 opacity: 0.6
161 }
162 });
163 };
164
165 this.unblock = function () {
166 this.$block.unblock();
167 };
168
169 this.addRole = function (event) {
170
171 event.preventDefault();
172
173 var selectedRole = this.$block.find('.tpt-role-based-adding-form__role-selector').val();
174
175 if (selectedRole) {
176
177 var action = this.$block.data('add-action');
178 var nonce = this.$block.data('add-action-nonce');
179 var productId = this.$block.data('product-id');
180 var loop = this.$block.data('loop');
181
182 $.ajax({
183 method: 'GET',
184 url: ajaxurl,
185 data: {
186 action: action,
187 nonce: nonce,
188 role: selectedRole,
189 product_id: productId,
190 loop: loop,
191 },
192 beforeSend: (function () {
193 this.block();
194 }).bind(this)
195 }).done((function (response) {
196 if (response.success && response.role_row_html) {
197 this.$block.find('.tpt-role-based-roles').append(response.role_row_html);
198 this.$block.find('.tpt-role-based-no-roles').css('display', 'none');
199
200 $.each(this.$block.find('.tpt-role-based-role'), (function (i, el) {
201 this.hideRole($(el));
202 }).bind(this));
203
204 this.showRole(this.$block.find('.tpt-role-based-role').last());
205
206 this.$block.find('.tpt-role-based-adding-form__role-selector').find('[value=' + selectedRole + ']').remove();
207 this.$block.find('.tiered_price_rules_roles_to_delete').find('[value=' + selectedRole + ']').prop('selected', false);
208
209 $('.woocommerce-help-tip').tipTip({
210 'attribute': 'data-tip',
211 'fadeIn': 50,
212 'fadeOut': 50,
213 'delay': 200
214 });
215
216 this.triggerVariationCanBeUpdated();
217 } else {
218 response.error_message && alert(response.error_message);
219 }
220 this.unblock();
221 }).bind(this));
222 }
223 }
224
225 this.triggerVariationCanBeUpdated = function () {
226
227 if (!this.variationCanBeChangedAlreadyTriggered) {
228
229 this.$block
230 .closest('.woocommerce_variation')
231 .addClass('variation-needs-update');
232
233 jQuery('button.cancel-variation-changes, button.save-variation-changes').removeAttr('disabled');
234 jQuery('#variable_product_options').trigger('woocommerce_variations_defaults_changed');
235
236 this.variationCanBeChangedAlreadyTriggered = true;
237 }
238
239 }
240 };
241
242 jQuery.each($('.tpt-role-based-block'), function (i, el) {
243 (new RoleBasedBlock()).init(jQuery(el).attr('id'));
244 });
245
246 jQuery(document).on('woocommerce_variations_loaded', function ($) {
247 jQuery.each(jQuery('.tpt-role-based-block'), function (i, el) {
248
249 var $el = jQuery(el);
250
251 if ($el.data('product-type') === 'variation') {
252 (new RoleBasedBlock()).init($el.attr('id'));
253 }
254 });
255 });
256
257 $(document).on('change', '.variable_roles_tiered_pricing', function () {
258 if ($(this).is(':checked')) {
259 $(this).closest('.data').find('.show_if_variable_roles_tiered_pricing').show();
260 } else {
261 $(this).closest('.data').find('.show_if_variable_roles_tiered_pricing').hide();
262 }
263
264 });
265
266 if (jQuery('.tpt-select-woo').length) {
267 jQuery('.tpt-select-woo').selectWoo();
268 }
269 });