PluginProbe
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant / 2.3.1
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant v2.3.1
2.3.2 2.3.1 2.3.0 2.2.8 2.2.7 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.11.0 1.11.1 1.11.2 1.6 1.7 1.8 1.8.1 1.8.2 1.8.3 1.9.0 1.9.1 1.9.10 1.9.11 All 60 releases
merchant / assets / js / admin / merchant-preview.js

merchant-preview.js in Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant 2.3.1, at assets/js/admin/merchant-preview.js

418 lines 19.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 "use strict";
2
3 function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
4 function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
5 function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
6 function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
7 function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
8 function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t.return || t.return(); } finally { if (u) throw o; } } }; }
9 function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
10 function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
11 ;
12 (function ($, window, document, undefined) {
13 'use strict';
14
15 $(document).ready(function () {
16 /**
17 * @param merchantPreviewManipulators.css
18 * @param merchantPreviewManipulators.text
19 */
20 var manipulators = merchantPreviewManipulators;
21 var hasManipulators = function hasManipulators(object) {
22 return typeof object !== 'undefined' && Object.keys(object).length > 0;
23 };
24 var updateElements = function updateElements() {
25 if (hasManipulators(manipulators.css)) {
26 var _loop = function _loop(key) {
27 if (manipulators.css.hasOwnProperty(key)) {
28 var elements = document.querySelectorAll(manipulators.css[key].selector);
29 var value = $('[name="merchant[' + manipulators.css[key].setting + ']"]').val() + manipulators.css[key].unit;
30 var input = $('[name="merchant[' + manipulators.css[key].setting + ']"]');
31 var inputType = input.attr('type');
32 if (inputType === 'radio') {
33 value = $('[name="merchant[' + manipulators.css[key].setting + ']"]' + ':checked').val() + manipulators.css[key].unit;
34 }
35 elements.forEach(function (element) {
36 element.style.setProperty(manipulators.css[key].variable, value);
37 });
38 }
39 };
40 for (var key in manipulators.css) {
41 _loop(key);
42 }
43 }
44 if (hasManipulators(manipulators.text)) {
45 for (var _key in manipulators.text) {
46 if (manipulators.text.hasOwnProperty(_key)) {
47 var inputText = $('[name="merchant[' + manipulators.text[_key].setting + ']"]').val();
48 if (manipulators.text[_key].hasOwnProperty('replacements') && inputText) {
49 inputText = setReplacements(inputText, manipulators.text[_key]);
50 }
51 $(manipulators.text[_key].selector).html(inputText);
52 }
53 }
54 }
55 if (hasManipulators(manipulators.attributes)) {
56 for (var _key2 in manipulators.attributes) {
57 if (manipulators.attributes.hasOwnProperty(_key2)) {
58 var input = $('[name="merchant[' + manipulators.attributes[_key2].setting + ']"]');
59 var inputType = input.attr('type');
60 if (inputType === 'radio') {
61 input = $('[name="merchant[' + manipulators.attributes[_key2].setting + ']"]' + ':checked');
62 }
63 var inputValue = input.val();
64 if (manipulators.attributes[_key2].hasOwnProperty('replacements')) {
65 inputValue = setReplacements(inputValue, manipulators.attributes[_key2]);
66 }
67 $(manipulators.attributes[_key2].selector).attr(manipulators.attributes[_key2].attribute, inputValue);
68 }
69 }
70 }
71 if (hasManipulators(manipulators.classes)) {
72 for (var _key3 in manipulators.classes) {
73 if (manipulators.classes.hasOwnProperty(_key3)) {
74 if (manipulators.classes[_key3].hasOwnProperty('remove')) {
75 var _iterator = _createForOfIteratorHelper(manipulators.classes[_key3].remove),
76 _step;
77 try {
78 for (_iterator.s(); !(_step = _iterator.n()).done;) {
79 var classToRemove = _step.value;
80 $(manipulators.classes[_key3].selector).removeClass(classToRemove);
81 }
82 } catch (err) {
83 _iterator.e(err);
84 } finally {
85 _iterator.f();
86 }
87 }
88 var _input = $('[name="merchant[' + manipulators.classes[_key3].setting + ']"]');
89 var _inputType = _input.attr('type');
90 if (_inputType === 'radio') {
91 _input = $('[name="merchant[' + manipulators.classes[_key3].setting + ']"]' + ':checked');
92 }
93 if (_inputType === 'checkbox') {
94 if (_input.is(':checked')) {
95 $(manipulators.classes[_key3].selector).addClass(manipulators.classes[_key3].add);
96 } else {
97 $(manipulators.classes[_key3].selector).removeClass(manipulators.classes[_key3].add);
98 }
99 } else {
100 var _inputValue = _input.val();
101 if (manipulators.classes[_key3].hasOwnProperty('add')) {
102 $(manipulators.classes[_key3].selector).toggleClass(manipulators.classes[_key3].add);
103 } else {
104 $(manipulators.classes[_key3].selector).addClass(_inputValue);
105 }
106 }
107 }
108 }
109 }
110 if (hasManipulators(manipulators.icons)) {
111 for (var _key4 in manipulators.icons) {
112 if (manipulators.icons.hasOwnProperty(_key4)) {
113 var radioElement = $('[name="merchant[' + manipulators.icons[_key4].setting + ']"]' + ':checked');
114 var iconSrc = radioElement.parent().find('figure img').attr('src');
115 var iconSelector = $(manipulators.icons[_key4].selector);
116 if (radioElement.val() === 'none') {
117 iconSelector.hide();
118 } else {
119 iconSelector.show();
120 iconSelector.attr('src', iconSrc);
121 }
122 }
123 }
124 }
125 if (hasManipulators(manipulators.svg_icons)) {
126 for (var _key5 in manipulators.svg_icons) {
127 if (manipulators.svg_icons.hasOwnProperty(_key5)) {
128 var _radioElement = $('[name="merchant[' + manipulators.svg_icons[_key5].setting + ']"]' + ':checked');
129 var iconsLib = manipulators.svg_icons[_key5].icons_lib;
130 var icon = iconsLib[_radioElement.val()];
131 var _iconSelector = $(manipulators.svg_icons[_key5].selector);
132 if (_radioElement.val() === 'none') {
133 _iconSelector.hide();
134 } else {
135 _iconSelector.show();
136 _iconSelector.html(icon);
137 }
138 }
139 }
140 }
141 if (hasManipulators(manipulators.repeater_content)) {
142 for (var _key6 in manipulators.repeater_content) {
143 if (manipulators.repeater_content.hasOwnProperty(_key6)) {
144 var repeaterElement = $('[name="merchant[' + manipulators.repeater_content[_key6].setting + ']"]');
145 var repeaterValue = repeaterElement.val() ? JSON.parse(repeaterElement.val()) : [];
146 var repeaterItemSelector = $(manipulators.repeater_content[_key6].selector);
147 if (repeaterValue.length) {
148 // Update content.
149 var _iterator2 = _createForOfIteratorHelper(repeaterValue.entries()),
150 _step2;
151 try {
152 for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
153 var _step2$value = _slicedToArray(_step2.value, 2),
154 index = _step2$value[0],
155 repeaterItem = _step2$value[1];
156 if (repeaterItemSelector.length) {
157 repeaterItemSelector.eq(index).html(repeaterItem);
158 }
159 }
160
161 // Update content when removing.
162 } catch (err) {
163 _iterator2.e(err);
164 } finally {
165 _iterator2.f();
166 }
167 if (repeaterItemSelector.length > repeaterValue.length) {
168 for (var i = repeaterValue.length; i < repeaterItemSelector.length; i++) {
169 repeaterItemSelector.eq(i).parent().remove();
170 }
171 }
172
173 // Update content when adding.
174 if (repeaterItemSelector.length < repeaterValue.length) {
175 for (var _i = repeaterItemSelector.length; _i < repeaterValue.length; _i++) {
176 repeaterItemSelector.eq(0).parent().clone().appendTo(repeaterItemSelector.eq(0).parent().parent());
177 }
178 }
179 }
180 }
181 }
182 }
183 if (hasManipulators(manipulators.flexible_content)) {
184 var _loop2 = function _loop2(_key7) {
185 var flexibleContentSelector = $(manipulators.flexible_content[_key7].selector);
186 var flexibleContentItems = flexibleContentSelector.children();
187 var flexibleContentTemplate = flexibleContentItems.eq(0);
188 var flexibleContentSettings = $('.merchant-flexible-content-control[data-id=' + manipulators.flexible_content[_key7].setting + ']').find('.merchant-flexible-content').children();
189
190 /**
191 * We're going to remove all items in the selector except for the template
192 * and regenerate the items based on the settings.
193 */
194
195 flexibleContentItems.each(function () {
196 if (!$(this).hasClass('flexible-content-template')) {
197 $(this).remove();
198 }
199 });
200 flexibleContentSettings.each(function () {
201 // Skip deferred (lazy-loaded) rows — they have no rendered field inputs.
202 if ($(this).attr('data-deferred') === '1') {
203 return;
204 }
205 var item = flexibleContentTemplate.clone();
206 var layout = $(this).data('type');
207 for (var variable in manipulators.flexible_content[_key7].variables[layout]) {
208 var setting = manipulators.flexible_content[_key7].variables[layout][variable];
209 var field = $(this).find('.merchant-module-page-setting-field[data-id=' + setting + ']');
210 var fieldType = field.data('type');
211 var fieldInput = field.find('input').val();
212
213 // Handle variable replacement for text fields
214 if (fieldType === 'text') {
215 item.html(item.html().replace(variable, fieldInput));
216 }
217
218 // Handle variable replacement for upload fields
219 if (fieldType === 'upload') {
220 var image = field.find('.merchant-upload-image img').prop('outerHTML');
221 if (typeof image !== 'undefined') {
222 item.html(item.html().replace(variable, image));
223 }
224 }
225
226 // Handle variable replacement for choices fields
227 if (fieldType === 'choices') {
228 var selectedChoice = field.find('input:checked');
229
230 // If choices uses SVG then we want to replace the variable with the selected SVG
231 if (field.find('.merchant-svg').length) {
232 var SVG = selectedChoice.parent().find('svg').prop('outerHTML');
233 if (typeof SVG !== 'undefined') {
234 item.html(item.html().replace(variable, SVG));
235 }
236 }
237 }
238
239 // Append item to selector
240 item.html(item.html().replace(variable, '')) // First remove variable if it's not replaced yet
241 .removeClass('flexible-content-template') // Remove template class
242 .appendTo(flexibleContentSelector); // Append
243 }
244 });
245 };
246 for (var _key7 in manipulators.flexible_content) {
247 _loop2(_key7);
248 }
249 }
250 };
251 var triggerChangeOnInput = function triggerChangeOnInput(input) {
252 var inputType = input.attr('type');
253
254 // Text inputs
255 if (inputType === 'text' || input.prop('tagName') === 'TEXTAREA') {
256 input.on('keyup input', function () {
257 return updateElements();
258 });
259 }
260
261 // Number + range inputs
262 if (inputType === 'number') {
263 input.on('keyup input', function () {
264 return updateElements();
265 });
266 if (input.parent().find('input[type=range]').length) {
267 input.parent().find('input[type=range]').on('input change', function () {
268 return updateElements();
269 });
270 }
271 }
272 // Radio inputs
273 if (inputType === 'radio') {
274 input.on('input', function () {
275 return updateElements();
276 });
277 }
278
279 // Select
280 if (inputType === 'checkbox' || input.is('select')) {
281 input.on('change', function () {
282 return updateElements();
283 });
284 }
285
286 // Repeater
287 if (input.hasClass('merchant-sortable-repeater-input')) {
288 input.on('change', function () {
289 return updateElements();
290 });
291 }
292
293 // Upload input
294 if (input.hasClass('merchant-upload-input')) {
295 input.on('change', function () {
296 return updateElements();
297 });
298 }
299 };
300 if (typeof manipulators !== 'undefined') {
301 if (hasManipulators(manipulators.css)) {
302 for (var key in manipulators.css) {
303 triggerChangeOnInput($('[name="merchant[' + manipulators.css[key].setting + ']"]'));
304 }
305 }
306 if (hasManipulators(manipulators.text)) {
307 for (var _key8 in manipulators.text) {
308 triggerChangeOnInput($('[name="merchant[' + manipulators.text[_key8].setting + ']"]'));
309 }
310 }
311 if (hasManipulators(manipulators.attributes)) {
312 for (var _key9 in manipulators.attributes) {
313 triggerChangeOnInput($('[name="merchant[' + manipulators.attributes[_key9].setting + ']"]'));
314 }
315 }
316 if (hasManipulators(manipulators.classes)) {
317 for (var _key0 in manipulators.classes) {
318 triggerChangeOnInput($('[name="merchant[' + manipulators.classes[_key0].setting + ']"]'));
319 }
320 }
321 if (hasManipulators(manipulators.icons)) {
322 for (var _key1 in manipulators.icons) {
323 triggerChangeOnInput($('[name="merchant[' + manipulators.icons[_key1].setting + ']"]'));
324 }
325 }
326 if (hasManipulators(manipulators.svg_icons)) {
327 for (var _key10 in manipulators.svg_icons) {
328 triggerChangeOnInput($('[name="merchant[' + manipulators.svg_icons[_key10].setting + ']"]'));
329 }
330 }
331 if (hasManipulators(manipulators.repeater_content)) {
332 for (var _key11 in manipulators.repeater_content) {
333 triggerChangeOnInput($('[name="merchant[' + manipulators.repeater_content[_key11].setting + ']"]'));
334 }
335 }
336 if (hasManipulators(manipulators.flexible_content)) {
337 for (var _key12 in manipulators.flexible_content) {
338 var field = $('.merchant-flexible-content-control[data-id=' + manipulators.flexible_content[_key12].setting + ']');
339 triggerChangeOnInput(field.find('input'));
340 field.find('.customize-control-flexible-content-delete').on('click', function () {
341 return updateElements();
342 });
343 field.on('merchant.sorted', function () {
344 return updateElements();
345 });
346 }
347 }
348 if (hasManipulators(manipulators.update)) {
349 for (var _key13 in manipulators.update) {
350 triggerChangeOnInput($('[name="merchant[' + manipulators.update[_key13].setting + ']"]'));
351 }
352 }
353
354 // On color picker interaction
355 for (var _i2 = 0, _arr = ['.pcr-color-palette', '.pcr-swatches button', '.pcr-color-chooser', '.pcr-color-opacity']; _i2 < _arr.length; _i2++) {
356 var selector = _arr[_i2];
357 $(document).on('click', selector, function () {
358 return updateElements();
359 });
360 }
361 }
362
363 // Replacement helpers
364 var setReplacements = function setReplacements(inputText, manipulator) {
365 var searches = manipulator['replacements'][0]; // Searches
366 var replacements = manipulator['replacements'][1]; // Replacements
367
368 // Do search replacements
369 var _iterator3 = _createForOfIteratorHelper(searches.entries()),
370 _step3;
371 try {
372 for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
373 var _step3$value = _slicedToArray(_step3.value, 2),
374 index = _step3$value[0],
375 search = _step3$value[1];
376 var replacement = replacements[index];
377 if (typeof replacement === 'string') {
378 inputText = inputText.replace(search, replacement);
379 }
380 if (_typeof(replacement) === 'object') {
381 if (replacement.hasOwnProperty('conditions')) {
382 var conditionalElement = $('[name="merchant[' + replacement.setting + ']"]');
383 for (var conditionValue in replacement.conditions) {
384 if (replacement.conditions.hasOwnProperty(conditionValue)) {
385 if (conditionalElement.val() === conditionValue) {
386 if (typeof replacement.conditions[conditionValue] === 'string') {
387 inputText = inputText.replace(search, replacement.conditions[conditionValue]);
388 }
389 if (_typeof(replacement.conditions[conditionValue]) === 'object') {
390 inputText = replaceTextBySettingsValue(inputText, search, replacement.conditions[conditionValue]);
391 }
392 }
393 }
394 }
395 } else {
396 inputText = replaceTextBySettingsValue(inputText, search, replacement);
397 }
398 }
399 }
400 } catch (err) {
401 _iterator3.e(err);
402 } finally {
403 _iterator3.f();
404 }
405 return inputText;
406 };
407 var replaceTextBySettingsValue = function replaceTextBySettingsValue(inputText, search, replacement) {
408 var replacementValue = $('input[name="merchant[' + replacement.setting + ']"]').val();
409 if (replacement.hasOwnProperty('format')) {
410 replacementValue = replacement.format.replace('{string}', replacementValue);
411 }
412 return inputText.replace(search, replacementValue);
413 };
414 $(document).on('change', function () {
415 return updateElements();
416 });
417 });
418 })(jQuery, window, document);