| 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 _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } |
| 5 |
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } |
| 6 |
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; } |
| 7 |
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } |
| 8 |
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } |
| 9 |
(function ($) { |
| 10 |
'use strict'; |
| 11 |
|
| 12 |
/** |
| 13 |
* MerchantSideCartAdminPreview |
| 14 |
* Add tweaks to control the side cart upsell groups label in the module settings page. |
| 15 |
*/ |
| 16 |
var MerchantSideCartAdminPreview = /*#__PURE__*/function () { |
| 17 |
function MerchantSideCartAdminPreview() { |
| 18 |
_classCallCheck(this, MerchantSideCartAdminPreview); |
| 19 |
this.init(); |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Initialize the logic. |
| 24 |
*/ |
| 25 |
return _createClass(MerchantSideCartAdminPreview, [{ |
| 26 |
key: "init", |
| 27 |
value: function init() { |
| 28 |
this.preview(); |
| 29 |
this.events(); |
| 30 |
this.flexibleContentLabel(); |
| 31 |
} |
| 32 |
}, { |
| 33 |
key: "preview", |
| 34 |
value: function preview() { |
| 35 |
$(document).on('change', '.merchant-field-slide_direction input', function () { |
| 36 |
if ($(this).is(':checked')) { |
| 37 |
$('.merchant-side-cart').removeClass('slide-left slide-right').addClass('slide-' + $(this).val()); |
| 38 |
} |
| 39 |
}); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Events. |
| 44 |
*/ |
| 45 |
}, { |
| 46 |
key: "events", |
| 47 |
value: function events() { |
| 48 |
$(document).on('change keyup merchant.change', '.merchant-module-page-setting-fields', this.flexibleContentLabel.bind(this)); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Update the label of the upsell groups in the side cart settings page based on the selected options. |
| 53 |
*/ |
| 54 |
}, { |
| 55 |
key: "flexibleContentLabel", |
| 56 |
value: function flexibleContentLabel() { |
| 57 |
var self = this; |
| 58 |
if (this.cartUpsellsToggleState() && this.cartUpsellType() === 'custom_upsell') { |
| 59 |
var upsellsGroups = $('.merchant-field-custom_upsells .merchant-flexible-content .layout'); |
| 60 |
upsellsGroups.each(function () { |
| 61 |
var group = $(this), |
| 62 |
trigger = self.customUpsellTrigger(group), |
| 63 |
categories = self.customUpsellCategories(group), |
| 64 |
products = self.customUpsellProducts(group); |
| 65 |
if ('categories' === trigger) { |
| 66 |
self.updateGroupLabel(group, 'categories', categories.length, categories); |
| 67 |
} |
| 68 |
if ('products' === trigger) { |
| 69 |
self.updateGroupLabel(group, 'products', products.length, products); |
| 70 |
} |
| 71 |
if ('all' === trigger) { |
| 72 |
self.updateGroupLabel(group, 'all', 10, []); |
| 73 |
} |
| 74 |
}); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Get the upsells count label badge. |
| 80 |
* @param group {jQuery} The upsell group. |
| 81 |
* @returns {string} |
| 82 |
*/ |
| 83 |
}, { |
| 84 |
key: "upsellsLabelBadge", |
| 85 |
value: function upsellsLabelBadge(group) { |
| 86 |
var upsellsType = group.find('.merchant-field-custom_upsell_type select').val(); |
| 87 |
if ('products' === upsellsType) { |
| 88 |
var products = group.find('.merchant-field-upsells_product_ids .merchant-selected-products-preview li'); |
| 89 |
if (products.length) { |
| 90 |
return "<span class=\"merchant-upsells-badge\">Upsells: ".concat(products.length, "</span>"); |
| 91 |
} |
| 92 |
} |
| 93 |
return ''; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Update the group label based on the selected options. |
| 98 |
* @param group {jQuery} The upsell group. |
| 99 |
* @param type {string} The type of the upsell group |
| 100 |
* @param count {number} The count of the selected items |
| 101 |
* @param data {Array} The selected items data |
| 102 |
*/ |
| 103 |
}, { |
| 104 |
key: "updateGroupLabel", |
| 105 |
value: function updateGroupLabel(group, type, count, data) { |
| 106 |
var groupLabel = group.find('.layout-title'); |
| 107 |
if ('categories' === type) { |
| 108 |
if (count > 1) { |
| 109 |
groupLabel.html(merchant_side_cart_params.keywords.multi_categories + this.upsellsLabelBadge(group)); |
| 110 |
} else if (count === 1) { |
| 111 |
groupLabel.html(merchant_side_cart_params.keywords.category_trigger + ' ' + data[0] + this.upsellsLabelBadge(group)); |
| 112 |
} else { |
| 113 |
groupLabel.html(merchant_side_cart_params.keywords.no_cats_selected + this.upsellsLabelBadge(group)); |
| 114 |
} |
| 115 |
} |
| 116 |
if ('products' === type) { |
| 117 |
if (count > 1) { |
| 118 |
groupLabel.html(merchant_side_cart_params.keywords.multi_products + this.upsellsLabelBadge(group)); |
| 119 |
} else if (count === 1) { |
| 120 |
groupLabel.html(data[0].name + ' (#' + data[0].id + ')' + this.upsellsLabelBadge(group)); |
| 121 |
} else { |
| 122 |
groupLabel.html(merchant_side_cart_params.keywords.no_products_selected + this.upsellsLabelBadge(group)); |
| 123 |
} |
| 124 |
} |
| 125 |
if ('all' === type) { |
| 126 |
groupLabel.html(merchant_side_cart_params.keywords.all_products + this.upsellsLabelBadge(group)); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Get the selected trigger for the upsell group. |
| 132 |
* @param group {jQuery} The upsell group. |
| 133 |
* @returns {string} The selected trigger value. |
| 134 |
*/ |
| 135 |
}, { |
| 136 |
key: "customUpsellTrigger", |
| 137 |
value: function customUpsellTrigger(group) { |
| 138 |
return group.find('.merchant-field-upsell_based_on select').val(); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Get the selected products for the upsell group. |
| 143 |
* @param group {jQuery} The upsell group. |
| 144 |
* @returns {Array} The selected products data. |
| 145 |
*/ |
| 146 |
}, { |
| 147 |
key: "customUpsellProducts", |
| 148 |
value: function customUpsellProducts(group) { |
| 149 |
var foundProducts = group.find('.merchant-field-product_ids .merchant-selected-products-preview li'); |
| 150 |
var products = []; |
| 151 |
foundProducts.each(function () { |
| 152 |
products.push({ |
| 153 |
id: $(this).data('id'), |
| 154 |
name: $(this).data('name'), |
| 155 |
element: $(this) |
| 156 |
}); |
| 157 |
}); |
| 158 |
return products; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Get the selected categories for the upsell group. |
| 163 |
* @param group {jQuery} The upsell group. |
| 164 |
* @returns {Array} The selected categories. |
| 165 |
*/ |
| 166 |
}, { |
| 167 |
key: "customUpsellCategories", |
| 168 |
value: function customUpsellCategories(group) { |
| 169 |
var upsellDropdown = group.find('.merchant-field-category_slugs select'); |
| 170 |
return upsellDropdown.find('option:selected').map(function () { |
| 171 |
return $(this).text().trim(); |
| 172 |
}).get(); |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Check the state of the upsells main toggle. |
| 177 |
* @returns {boolean} True if the toggle is checked otherwise false. |
| 178 |
*/ |
| 179 |
}, { |
| 180 |
key: "cartUpsellsToggleState", |
| 181 |
value: function cartUpsellsToggleState() { |
| 182 |
var toggle = $('.merchant-field-use_upsells input'); |
| 183 |
return !!toggle.is(':checked'); |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Get the selected upsell type. |
| 188 |
* @returns {string} The selected upsell type. |
| 189 |
*/ |
| 190 |
}, { |
| 191 |
key: "cartUpsellType", |
| 192 |
value: function cartUpsellType() { |
| 193 |
// custom_upsell |
| 194 |
var type = $('.merchant-field-upsells_type select'); |
| 195 |
return type.val(); |
| 196 |
} |
| 197 |
}]); |
| 198 |
}(); // Instantiate the class when the DOM is ready |
| 199 |
$(document).ready(function () { |
| 200 |
new MerchantSideCartAdminPreview(); |
| 201 |
}); |
| 202 |
})(jQuery); |