vendor: 4,345 bytes, lines 1-25
1/*! 2 * Variation Swatches for WooCommerce 3 * 4 * Author: Emran Ahmed ( [email protected] ) 5 * Date: 2/16/2025, 5:38:38 PM 6 * Released under the GPLv3 license. 7 */ 8/******/ (function() { // webpackBootstrap 9var __webpack_exports__ = {}; 10// This entry need to be wrapped in an IIFE because it need to be isolated against other entry modules. 11!function() { 12function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } 13function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; } 14function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } 15function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e2) { throw _e2; }, 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 normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e3) { didErr = true; err = _e3; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; } 16function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } 17function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } 18function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } 19function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } 20function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } 21function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } 22function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } 23function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } 24function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } 25function _nonIterableRest() { throw new TypeError("Invalid attempt to de
25structure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } 26function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } 27function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } 28function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } 29function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } 30// ================================================================ 31// WooCommerce Variation Swatches 32// ================================================================ 33/*global wp, _, wc_add_to_cart_variation_params, woo_variation_swatches_options */ 34 35(function (window) { 36 'use strict'; 37 38 /** 39 * Converts various string/number/boolean inputs to their boolean equivalent 40 * Mimics PHP's filter_var($var, FILTER_VALIDATE_BOOLEAN) behavior 41 * 42 * Returns true for: 43 * - true 44 * - "true", "yes", "on", "1" (case-insensitive) 45 * - 1, "1" 46 * 47 * Returns false for: 48 * - false 49 * - "false", "no", "off", "0" (case-insensitive) 50 * - 0, "0" 51 * - null, undefined 52 * - empty string 53 * - any other value 54 * 55 * @param {*} value - The value to convert to boolean 56 * @returns {boolean} - The boolean equivalent of the input 57 */ 58 function filterBoolean(value) { 59 // Handle null, undefined, and empty string 60 if (!value) { 61 return false; 62 } 63 64 // Handle actual boolean values 65 if (typeof value === 'boolean') { 66 return value; 67 } 68 69 // Convert to string and lowercase for consistent comparison 70 var strValue = String(value).toLowerCase().trim(); 71 72 // Values that should return true 73 var trueValues = ['1', 'true', 'yes', 'on']; 74 if (trueValues.includes(strValue)) { 75 return true; 76 } 77 78 // Handle numeric 1 79 if (value === 1) { 80 return true; 81 } 82 83 // All other values return false 84 return false; 85 } 86 function isWooVariationSwatchesAPIRequest(options) { 87 return !!options.path && options.path.indexOf('woo-variation-swatches') !== -1 || !!options.url && options.url.indexOf('woo-variation-swatches') !== -1; 88 } 89 window.createMiddlewareForExtraQueryParams = function () { 90 var args = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; 91 return function (options, next) { 92 if (isWooVariationSwatchesAPIRequest(options) && Object.keys(args).length > 0) { 93 for (var _i = 0, _Object$entries = Object.entries(args); _i < _Object$entries.length; _i++) { 94 var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2), 95 key = _Object$entries$_i[0], 96 value = _Object$entries$_i[1]; 97 if (typeof options.url === 'string' && !wp.url.hasQueryArg(options.url, key)) { 98 options.url = wp.url.addQueryArgs(options.url, _defineProperty({}, key, value)); 99 } 100 if (typeof options.path === 'string' && !wp.url.hasQueryArg(options.path, key)) { 101 options.path = wp.url.addQueryArgs(options.path, _defineProperty({}, key, value)); 102 } 103 } 104 } 105 return next(options); 106 }; 107 }; 108 109 /* 110 wp.apiFetch.use((options, next) => createMiddlewareForExtraQueryParams({'lang':'en'})); 111 */ 112 113 var Plugin = function ($) { 114 return /*#__PURE__*/function () { 115 function _class2(element, options, name) { 116 _classCallCheck(this, _class2); 117 _defineProperty(this, "defaults", {}); 118 // Assign 119 this.name = name; 120 this.element = element; 121 this.$element = $(element); 122 this.settings = $.extend(true, {}, this.defaults, options); 123 this.product_variations = this.$element.data('product_variations') || []; 124 this.is_ajax_variation = this.product_variations.length < 1; 125 this.product_id = this.$element.data('product_id'); 126 this.reset_variations = this.$element.find('.reset_variations'); 127 this.attributeFields = this.$element.find('.variations select'); 128 this.attributeSwatchesFields = this.$element.find('ul.variable-items-wrapper'); 129 this.selected_item_template = "<span class=\"woo-selected-variation-item-name\" data-default=\"\"></span>"; 130 this.$element.addClass('wvs-loaded'); 131 132 // Call 133 this.init(); 134 this.update(); 135 136 // Trigger 137 $(document).trigger('woo_variation_swatches_loaded', this); 138 } 139 _createClass(_class2, [{ 140 key: "isAjaxVariation", 141 value: function isAjaxVariation() { 142 //this.product_variations = this.$element.data('product_variations') || [] 143 return this.is_ajax_variation; // = this.product_variations.length < 1 144 } 145 }, { 146 key: "init",
147 value: function init() { 148 this.prepareLabel(); 149 this.prepareItems(); 150 this.setupItems(); 151 this.setupEvents(); 152 this.setUpStockInfo(); 153 this.deselectNonAvailable(); 154 } 155 }, { 156 key: "prepareLabel", 157 value: function prepareLabel() { 158 var _this = this; 159 // Append Selected Item Template 160 if (filterBoolean(woo_variation_swatches_options.show_variation_label)) { 161 this.$element.find('.variations .label').each(function (index, el) { 162 $(el).append(_this.selected_item_template); 163 }); 164 } 165 } 166 }, { 167 key: "prepareItems", 168 value: function prepareItems() { 169 this.attributeSwatchesFields.each(function (i, el) { 170 $(el).parent().addClass('woo-variation-items-wrapper'); 171 }); 172 } 173 }, { 174 key: "setupItems", 175 value: function setupItems() { 176 var _this2 = this; 177 var self = this; 178 this.attributeSwatchesFields.each(function (i, element) { 179 var selected = ''; 180 var select = $(element).parent().find('select.woo-variation-raw-select'); 181 var available_options = select.find('option'); 182 var disabled_out_of_stock_options = select.find('option:disabled'); 183 var enabled_out_of_stock_options = select.find('option.enabled.out-of-stock'); 184 var current = select.find('option:selected'); 185 var eq = select.find('option').eq(1); 186 var attribute_name = $(element).data('attribute_name'); 187 var all_options = $(element).data('attribute_values'); 188 var current_options = []; 189 var available_out_of_stocks = []; 190 var disabled_out_of_stocks = []; 191 192 // Available Options 193 available_options.each(function () { 194 if ($(this).val() !== '') { 195 current_options.push($(this).val()); 196 selected = current.length === 0 ? eq.val() : current.val(); 197 } 198 }); 199 200 // Settings: Disabled Out Of Stock Item 201 disabled_out_of_stock_options.each(function () { 202 if ($(this).val() !== '') { 203 disabled_out_of_stocks.push($(this).val()); 204 } 205 }); 206 207 // Settings: Enabled Out Of Stock Item 208 enabled_out_of_stock_options.each(function () { 209 if ($(this).val() !== '') { 210 available_out_of_stocks.push($(this).val()); 211 } 212 }); 213 var in_stocks = _.difference(current_options, disabled_out_of_stocks); 214 var disabled_values = [].concat(_toConsumableArray(_.difference(all_options, current_options)), disabled_out_of_stocks); 215 _this2.setupItem(element, selected, in_stocks, available_out_of_stocks, disabled_values); 216 }); 217 } 218 }, { 219 key: "setupItem", 220 value: function setupItem(element, selected, in_stocks, out_of_stocks, disabled_values) { 221 var _this3 = this; 222 var $selected_variation_item = $(element).parent().prev().find('.woo-selected-variation-item-name'); 223 224 // Mark Selected 225 $(element).find('li.variable-item').each(function (index, el) { 226 var attribute_value = $(el).attr('data-value'); 227 var attribute_title = $(el).attr('data-title'); 228 229 // Resetting LI 230 $(el).removeClass('selected disabled no-stock').addClass('disabled'); 231 $(el).attr('aria-checked', 'false'); 232 $(el).attr('tabindex', '-1'); 233 $(el).attr('data-wvstooltip-out-of-stock', ''); 234 $(el).find('input.variable-item-radio-input:radio').prop('disabled', true).prop('checked', false); 235 236 // To Prevent blink 237 if (selected.length < 1 && filterBoolean(woo_variation_swatches_options.show_variation_label)) { 238 $selected_variation_item.text(''); 239 } 240 241 // Ajax variation 242 if (_this3.isAjaxVariation()) { 243 $(el).find('input.variable-item-radio-input:radio').prop('disabled', false); 244 $(el).removeClass('selected disabled no-stock'); 245 246 // Selected 247 if (attribute_value === selected) { 248 $(el).addClass('selected'); 249 $(el).attr('aria-checked', 'true'); 250 $(el).attr('tabindex', '0'); 251 $(el).find('input.variable-item-radio-input:radio').prop('disabled', false).prop('checked', true); 252 if (filterBoolean(woo_variation_swatches_options.show_variation_label)) { 253 $selected_variation_item.text("".concat(woo_variation_swatches_options.variation_label_separator, " ").concat(attribute_title)); 254 } 255 $(el).trigger('wvs-item-updated', [selected, attribute_value]); 256 } 257 } else { 258 // Default Selected 259 // We can't use es6 includes for IE11 260 // in_stocks.includes(attribute_value) 261 // _.contains(in_stocks, attribute_value) 262 // _.includes(in_stocks, attribute_value) 263 264 if (_.includes(in_stocks, attribute_value)) { 265 $(el).removeClass('selected disabled'); 266 $(el).removeAttr('aria-hidden'); 267 $(el).attr('tabindex', '0'); 268 $(el).find('input.variable-item-radio-input:radio').prop('disabled', false); 269 270 // Selected 271 if (attribute_value === selected) { 272 $(el).addClass('selected'); 273 $(el).attr('aria-checked', 'true'); 274 $(el).find('input.variable-item-radio-input:radio').prop('checked', true); 275 if (filterBoolean(woo_variation_swatches_options.show_variation_label)) { 276 $selected_variation_item.text("".concat(woo_variation_swatches_options.variation_label_separator, " ").concat(attribute_title)); 277 } 278 $(el).trigger('wvs-item-updated', [selected, attribute_value]); 279 } 280 } 281 282 // Out of Stock 283 if (_.includes(out_of_stocks, attribute_value)) {
284 $(el).attr('data-wvstooltip-out-of-stock', woo_variation_swatches_options.out_of_stock_tooltip_text); 285 if (filterBoolean(woo_variation_swatches_options.clickable_out_of_stock)) { 286 $(el).removeClass('disabled').addClass('no-stock'); 287 } 288 } 289 290 // Disabled 291 if (_.includes(disabled_values, attribute_value)) { 292 $(el).attr('data-wvstooltip-out-of-stock', woo_variation_swatches_options.unavailable_tooltip_text); 293 } 294 } 295 }); 296 } 297 }, { 298 key: "setupEvents", 299 value: function setupEvents() { 300 var $element = this.$element; 301 this.attributeSwatchesFields.each(function (i, element) { 302 var select = $(element).parent().find('select.woo-variation-raw-select'); 303 304 // Trigger Select event based on list 305 if (filterBoolean(woo_variation_swatches_options.clear_on_reselect)) { 306 // Non Selected Item Should Select 307 $(element).on('click.wvs', 'li.variable-item:not(.selected):not(.radio-variable-item)', function (event) { 308 event.preventDefault(); 309 event.stopPropagation(); 310 var attribute_name = $(this).data('attribute_name'); 311 var attribute_value = $(this).data('value'); 312 select.val(attribute_value).trigger('change'); 313 select.trigger('click'); 314 $(this).trigger('wvs-selected-item', [attribute_name, attribute_value, select, $element]); // Custom Event for li 315 }); 316 317 // Selected Item Should un Select 318 $(element).on('click.wvs', 'li.variable-item.selected:not(.radio-variable-item)', function (event) { 319 event.preventDefault(); 320 event.stopPropagation(); 321 var attribute_name = $(this).data('attribute_name'); 322 var attribute_value = $(this).data('value'); 323 select.val('').trigger('change'); 324 select.trigger('click'); 325 $(this).trigger('wvs-unselected-item', [attribute_name, attribute_value, select, $element]); // Custom Event for li 326 }); 327 328 // RADIO 329 330 // On Click trigger change event on Radio button 331 $(element).on('click.wvs', 'input.variable-item-radio-input:radio', function (event) { 332 event.stopPropagation(); 333 $(this).trigger('change.wvs', { 334 radioChange: true 335 }); 336 }); 337 $(element).on('change.wvs', 'input.variable-item-radio-input:radio', function (event, params) { 338 event.preventDefault(); 339 event.stopPropagation(); 340 if (params && params.radioChange) { 341 var attribute_name = $(this).data('attribute_name'); 342 var attribute_value = $(this).val(); 343 var is_selected = $(this).parent('li.radio-variable-item').hasClass('selected'); 344 if (is_selected) { 345 select.val('').trigger('change'); 346 $(this).closest('li.radio-variable-item').trigger('wvs-unselected-item', [attribute_name, attribute_value, select, $element]); // Custom Event for li 347 } else { 348 select.val(attribute_value).trigger('change'); 349 $(this).closest('li.radio-variable-item').trigger('wvs-selected-item', [attribute_name, attribute_value, select, $element]); // Custom Event for li 350 } 351 select.trigger('click'); 352 } 353 }); 354 } else { 355 $(element).on('click.wvs', 'li.variable-item:not(.radio-variable-item)', function (event) { 356 event.preventDefault(); 357 event.stopPropagation(); 358 var attribute_name = $(this).data('attribute_name'); 359 var attribute_value = $(this).data('value'); 360 select.val(attribute_value).trigger('change'); 361 select.trigger('click'); 362 $(this).trigger('wvs-selected-item', [attribute_name, attribute_value, select, $element]); // Custom Event for li 363 }); 364 365 // Radio 366 $(element).on('change.wvs', 'input.variable-item-radio-input:radio', function (event) { 367 event.preventDefault(); 368 event.stopPropagation(); 369 var attribute_name = $(this).data('attribute_name'); 370 var attribute_value = $(this).val(); 371 select.val(attribute_value).trigger('change'); 372 select.trigger('click'); 373 374 // Radio 375 $(this).closest('li.radio-variable-item').removeClass('selected disabled no-stock').addClass('selected'); 376 $(this).closest('li.radio-variable-item').trigger('wvs-selected-item', [attribute_name, attribute_value, select, $element]); // Custom Event for li 377 }); 378 } 379 380 // Keyboard Access 381 $(element).on('keydown.wvs', 'li.variable-item:not(.disabled)', function (event) { 382 if (event.keyCode && 32 === event.keyCode || event.key && ' ' === event.key || event.keyCode && 13 === event.keyCode || event.key && 'enter' === event.key.toLowerCase()) { 383 event.preventDefault(); 384 $(this).trigger('click'); 385 } 386 }); 387 }); 388 this.$element.on('click.wvs', '.woo-variation-swatches-variable-item-more', function (event) { 389 event.preventDefault(); 390 $(this).parent().removeClass('enabled-display-limit-mode enabled-catalog-display-limit-mode'); 391 $(this).remove(); 392 }); 393 this.$element.find('[data-wvstooltip]').each(function (i, element) { 394 $(element).on('mouseenter', function (event) { 395 var rect = element.getBoundingClientRect(); 396 var tooltip = window.getComputedStyle(element, ':before'); 397 var arrow = window.getComputedStyle(element, ':after'); 398 var arrowHeight = parseInt(arrow.getPropertyValue('border-top-width'), 10); 399 var tooltipHeight = parseInt(tooltip.getPropertyValue('height'), 10); 400 var tooltipWidth = parseInt(tooltip.getPropertyValue('width'), 10); 401 var offset = 2; 402 var calculateTooltipPosition = tooltipHeight + arrowHeight + offset; 403 element.classList.toggle('wvs-tooltip-position-bottom', rect.top < calculateTooltipPosition); 404 var width = tooltipWidth / 2; 405 var position = rect.left + rect.width / 2; 406 407 // Left 408 var left = width - position; 409 var isLeft = width > position; 410 var computedRight = width + position; 411 var isRight = document.body.clientWidth < computedRight; 412 var right = document.body.clientWidth - computedRight; 413 element.style.setProperty('--horizontal-position', "0px"); 414 if (isLeft) { 415 element.style.setProperty('--horizontal-position', "".concat(left + offset, "px")); 416 } 417 if (isRight) { 418 element.style.setProperty('--horizontal-position', "".concat(right - offset, "px")); 419 } 420 // 421 }); 422 }); 423 } 424 }, { 425 key: "extractAttributes", 426 value: function extractAttributes(selected) { 427 var result = new Set(); 428 var _iterator = _createForOfIteratorHelper(this.product_variations), 429 _step; 430 try { 431 for (_iterator.s();
431 !(_step = _iterator.n()).done;) { 432 var variation = _step.value; 433 var attributes = variation.attributes; 434 for (var attribute_name in attributes) { 435 if (attributes[attribute_name].length > 0) { 436 result.add(attribute_name); 437 } 438 } 439 } 440 } catch (err) { 441 _iterator.e(err); 442 } finally { 443 _iterator.f(); 444 } 445 result["delete"](selected); 446 return Array.from(result); 447 } 448 }, { 449 key: "getUnavailableAttributes", 450 value: function getUnavailableAttributes(currentChosen, selected) { 451 var availableVariations = this.findMatchingVariations(this.product_variations, currentChosen).filter(function (variation) { 452 if (filterBoolean(woo_variation_swatches_options.disable_out_of_stock)) { 453 return variation.is_in_stock; 454 } 455 return true; 456 }); 457 if (availableVariations.length === 0) { 458 return this.extractAttributes(selected); 459 } 460 return []; 461 } 462 }, { 463 key: "deselectNonAvailable", 464 value: function deselectNonAvailable() { 465 var _this4 = this; 466 if (!filterBoolean(woo_variation_swatches_options.deselect_unavailable)) { 467 return; 468 } 469 this.$element.on('wvs-selected-item.wvs', function (event, attribute_name, attribute_value) { 470 var _this4$getChosenAttri = _this4.getChosenAttributes(), 471 data = _this4$getChosenAttri.data; 472 var currentAttribute = _objectSpread(_objectSpread({}, data), {}, _defineProperty({}, attribute_name, attribute_value)); 473 var unavailableAttributes = _this4.getUnavailableAttributes(currentAttribute, attribute_name); 474 if (unavailableAttributes.length > 0) { 475 var _iterator2 = _createForOfIteratorHelper(unavailableAttributes), 476 _step2; 477 try { 478 for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) { 479 var unavailable = _step2.value; 480 // Unselect unmatched 481 _this4.attributeSwatchesFields.find("li[data-attribute_name=\"".concat(unavailable, "\"]")).removeClass('selected'); 482 _this4.attributeFields.filter("[data-attribute_name=\"".concat(unavailable, "\"]")).each(function (i, el) { 483 $(el).val('').trigger('change'); 484 }); 485 } 486 487 // Select current 488 } catch (err) { 489 _iterator2.e(err); 490 } finally { 491 _iterator2.f(); 492 } 493 _this4.attributeSwatchesFields.filter("[data-attribute_name=\"".concat(attribute_name, "\"]")).each(function () { 494 $(this).find("[data-value=\"".concat(attribute_value, "\"]")).removeClass('disabled').addClass('selected'); 495 }); 496 _this4.attributeFields.filter("[data-attribute_name=\"".concat(attribute_name, "\"]")).each(function (i, el) { 497 $(el).val(attribute_value).trigger('change'); 498 }); 499 } 500 }); 501 } 502 }, { 503 key: "update", 504 value: function update() { 505 var _this5 = this; 506 // this.$element.off('woocommerce_variation_has_changed.wvs') 507 this.$element.on('woocommerce_variation_has_changed.wvs', function (event) { 508 // Don't use any propagation. It will disable composite product functionality 509 // event.stopPropagation(); 510 511 _this5.setupItems(); 512 }); 513 } 514 }, { 515 key: "setUpStockInfo", 516 value: function setUpStockInfo() { 517 var _this6 = this; 518 if (filterBoolean(woo_variation_swatches_options.show_variation_stock)) { 519 var max_stock_label = parseInt(woo_variation_swatches_options.stock_label_threshold, 10); 520 this.$element.on('wvs-selected-item.wvs', function (event) { 521 var attributes = _this6.getChosenAttributes(); 522 var variations = _this6.findStockVariations(_this6.product_variations, attributes); 523 if (attributes.count > 1 && attributes.count === attributes.chosenCount) { 524 _this6.resetStockInfo(); 525 } 526 if (attributes.count > 1 && attributes.count === attributes.mayChosenCount) { 527 variations.forEach(function (data) { 528 var stockInfoSelector = "[data-attribute_name=\"".concat(data.attribute_name, "\"] > [data-value=\"").concat(data.attribute_value, "\"]"); 529 if (data.variation.is_in_stock && data.variation.max_qty && data.variation.variation_stock_left && data.variation.max_qty <= max_stock_label) { 530 _this6.$element.find("".concat(stockInfoSelector, " .wvs-stock-left-info")).attr('data-wvs-stock-info', data.variation.variation_stock_left); 531 _this6.$element.find(stockInfoSelector).addClass('wvs-show-stock-left-info'); 532 } else { 533 _this6.$element.find(stockInfoSelector).removeClass('wvs-show-stock-left-info'); 534 _this6.$element.find("".concat(stockInfoSelector, " .wvs-stock-left-info")).attr('data-wvs-stock-info', ''); 535 } 536 }); 537 } 538 }); 539 this.$element.on('hide_variation.wvs', function () { 540 _this6.resetStockInfo(); 541 }); 542 } 543 } 544 }, { 545 key: "resetStockInfo", 546 value: function resetStockInfo() { 547 this.$element.find('.variable-item').removeClass('wvs-show-stock-left-info'); 548 this.$element.find('.wvs-stock-left-info').attr('data-wvs-stock-info', ''); 549 } 550 }, { 551 key: "getChosenAttributes", 552 value: function getChosenAttributes() { 553 var data = {}; 554 var count = 0; 555 var chosen = 0; 556 this.attributeFields.each(function () { 557 var attribute_name = $(this).data('attribute_name') || $(this).attr('name'); 558 var value = $(this).val() || ''; 559 if (value.length > 0) { 560 chosen++; 561 } 562 count++; 563 data[attribute_name] = value; 564 }); 565 return { 566 'count': count, 567 'chosenCount': chosen, 568 'mayChosenCount': chosen + 1, 569 'data': data 570 }; 571 } 572 }, { 573 key: "findStockVariations", 574 value: function findStockVariations(allVariations, selectedAttributes) { 575 var found = []; 576 for (var _i2 = 0, _Object$entries2 = Object.entries(selectedAttributes.data); _i2 < _Object$entries2.length; _i2++) { 577 var _Object$entries2$_i = _slicedToArray(_Object$entries2[_i2], 2), 578 attribute_name = _Object$entries2$_i[0], 579 attribute_value = _Object$entries2$_i[1]; 580 if (attribute_value.length === 0) { 581 var values = this.$element.find("ul[data-attribute_name='".concat(attribute_name, "']")).data('attribute_values') || []; 582 var _iterator3 = _createForOfIteratorHelper(values), 583 _step3; 584 try { 585 for (_iterator3.s();
585 !(_step3 = _iterator3.n()).done;) { 586 var value = _step3.value; 587 var compare = _.extend(selectedAttributes.data, _defineProperty({}, attribute_name, value)); 588 var matched_variation = this.findMatchingVariations(allVariations, compare); 589 if (matched_variation.length > 0) { 590 var variation = matched_variation.shift(); 591 var data = {}; 592 data['attribute_name'] = attribute_name; 593 data['attribute_value'] = value; 594 data['variation'] = variation; 595 found.push(data); 596 } 597 } 598 } catch (err) { 599 _iterator3.e(err); 600 } finally { 601 _iterator3.f(); 602 } 603 } 604 } 605 return found; 606 } 607 }, { 608 key: "findMatchingVariations", 609 value: function findMatchingVariations(variations, attributes) { 610 var matching = []; 611 for (var i = 0; i < variations.length; i++) { 612 var variation = variations[i]; 613 if (this.isMatch(variation.attributes, attributes)) { 614 matching.push(variation); 615 } 616 } 617 return matching; 618 } 619 }, { 620 key: "findMatchingVariations2", 621 value: function findMatchingVariations2(variations, attributes) { 622 return variations.filter(function (variation) { 623 return Object.entries(attributes).every(function (_ref) { 624 var _ref2 = _slicedToArray(_ref, 2), 625 attr_name = _ref2[0], 626 attr_value = _ref2[1]; 627 var var_attr_value = variation.attributes[attr_name]; 628 var notMatch = var_attr_value !== undefined && attr_value !== undefined && var_attr_value.length !== 0 && attr_value.length !== 0 && var_attr_value !== attr_value; 629 return !notMatch; 630 }); 631 }); 632 } 633 }, { 634 key: "isMatch", 635 value: function isMatch(variation_attributes, attributes) { 636 var match = true; 637 for (var attr_name in variation_attributes) { 638 if (variation_attributes.hasOwnProperty(attr_name)) { 639 var val1 = variation_attributes[attr_name]; 640 var val2 = attributes[attr_name]; 641 if (val1 !== undefined && val2 !== undefined && val1.length !== 0 && val2.length !== 0 && val1 !== val2) { 642 match = false; 643 } 644 } 645 } 646 return match; 647 } 648 }, { 649 key: "destroy", 650 value: function destroy() { 651 this.$element.removeClass('wvs-loaded'); 652 this.$element.removeData(this.name); 653 } 654 }]); 655 return _class2; 656 }(); 657 }(jQuery); 658 var jQueryPlugin = function ($) { 659 return function (PluginName, ClassName) { 660 $.fn[PluginName] = function (options) { 661 var _this7 = this; 662 for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { 663 args[_key - 1] = arguments[_key]; 664 } 665 return this.each(function (index, element) { 666 var $element = $(element); 667 // let $element = $(this) 668 669 var data = $element.data(PluginName); 670 if (!data) { 671 data = new ClassName($element, $.extend({}, options), PluginName); 672 $element.data(PluginName, data); 673 } 674 if (typeof options === 'string') { 675 if (_typeof(data[options]) === 'object') { 676 return data[options]; 677 } 678 if (typeof data[options] === 'function') { 679 var _data; 680 return (_data = data)[options].apply(_data, args); 681 } 682 } 683 return _this7; 684 }); 685 }; 686 687 // Constructor 688 $.fn[PluginName].Constructor = ClassName; 689 690 // Short hand 691 $[PluginName] = function (options) { 692 var _$; 693 for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) { 694 args[_key2 - 1] = arguments[_key2]; 695 } 696 return (_$ = $({}))[PluginName].apply(_$, [options].concat(args)); 697 }; 698 699 // No Conflict 700 $.fn[PluginName].noConflict = function () { 701 return $.fn[PluginName]; 702 }; 703 }; 704 }(jQuery); 705 jQueryPlugin('WooVariationSwatches', Plugin); 706})(window); 707}(); 708// This entry need to be wrapped in an IIFE because it need to be isolated against other entry modules. 709!function() { 710/*global wp, _, wc_add_to_cart_variation_params, woo_variation_swatches_options */ 711jQuery(function ($) { 712 try { 713 $(document).on('woo_variation_swatches_init', function () { 714 $('.variations_form:not(.wvs-loaded)').WooVariationSwatches(); // Any custom product with .woo_variation_swatches_variations_form class to support 715 716 $('.woo_variation_swatches_variations_form:not(.wvs-loaded)').WooVariationSwatches(); // Yith Composite Product 717 718 $('.ywcp_inner_selected_container:not(.wvs-loaded)').WooVariationSwatches(); 719 }); //.trigger('woo_variation_swatches_init') 720 } catch (err) { 721 // If failed (conflict?) log the error but don't stop other scripts breaking. 722 window.console.log('Variation Swatches:', err); 723 } // Init WooVariationSwatches after wc_variation_form script loaded. 724 725
726 $(document).on('wc_variation_form.wvs', function (event) { 727 $(document).trigger('woo_variation_swatches_init'); 728 }); // Try to cover global ajax data complete 729 730 $(document).ajaxComplete(function (event, request, settings) { 731 _.delay(function () { 732 $('.variations_form:not(.wvs-loaded)').each(function () { 733 $(this).wc_variation_form(); 734 }); 735 }, 1000); 736 }); // Composite Product Load 737 // JS API: https://docs.woocommerce.com/document/composite-products/composite-products-js-api-reference/ 738 739 $(document.body).on('wc-composite-initializing', '.composite_data', function (event, composite) { 740 composite.actions.add_action('component_options_state_changed', function (self) { 741 $(self.$component_content).find('.variations_form').WooVariationSwatches('destroy'); 742 }); 743 /* composite.actions.add_action('active_scenarios_updated', (self) => { 744 console.log('active_scenarios_updated') 745 $(self.$component_content).find('.variations_form').removeClass('wvs-loaded wvs-pro-loaded') 746 })*/ 747 }); 748}); 749}(); 750/******/ })() 751;
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.