{"version":3,"file":"VariationsDropdown.js","names":["VariationsDropdown","a","setters","Component","default","on","off","trigger","deepMerge","execute","constructor","element","options","arguments","length","templateName","dataModelId","events","variationUpdate","reloadOnChange","initCache","selectors","selectedOption","querySelector","initState","state","loading","pid","getSelectedVariationId","bindEvents","e","onVariationSelected","onProductUpdated","bind","getAttribute","preventDefault","url","target","value","test","select","previousPid","selectedIndex","blur","source","id","bubbles","variationAttributeId","displayValue","text","trim","detail","data","model","getModel","render","containerDataModelId","destroy"],"sources":["components/product/VariationsDropdown.js"],"sourcesContent":["import Component from 'core/Component';\nimport { on, off, trigger } from 'toolbox/event';\nimport { deepMerge } from 'toolbox/deepMerge';\n\n/**\n * This is a description of the VariationsDropdown constructor function.\n * @class\n * @classdesc This is a description of the VariationsDropdown class.\n * @extends Component\n */\nexport default class VariationsDropdown extends Component {\n /**\n * @typedef {Object} optionsObject\n * @property {String} dataModelId - the data model name needed to render the component on client side.\n */\n\n /**\n * @constructor\n * @param {HTMLElement} element Component DOM node\n * @param {optionsObject} options - Component Options\n */\n constructor(element, options = {}) {\n super(element, deepMerge({\n templateName: 'product/variationsdropdown',\n dataModelId: 'variationsdropdown',\n events: {\n variationUpdate: 'product.variation.update',\n },\n reloadOnChange: false,\n }, options));\n }\n\n /**\n * All selectors must be cached. Never cache elements that are out of the component scope\n */\n initCache() {\n this.selectors.selectedOption = this.element.querySelector('option[selected]');\n }\n\n /**\n * Init the different state of the component\n * It helps to avoid heavy DOM manipulation\n */\n initState() {\n this.state.loading = false;\n this.state.pid = this.getSelectedVariationId();\n }\n\n /**\n * Should contain only event listeners and nothing else\n * All the event handlers should be into a separated function. No usage of anonymous function\n */\n bindEvents() {\n on('change', this.element, e => this.onVariationSelected(e));\n on('product.updated', this.element, this.onProductUpdated.bind(this));\n }\n\n /**\n * Get ID of selected variation\n *\n * @returns {Promise} object\n */\n getSelectedVariationId() {\n return this.selectors.selectedOption ? this.selectors.selectedOption.getAttribute('data-js-pid') : '';\n }\n\n /**\n * Event handler for variation selection dropdown\n *\n * @param {Object} e - Event object\n * @returns {Boolean} true or false\n */\n onVariationSelected(e) {\n e.preventDefault();\n const url = e.target.value;\n const hostRegExp = /^(http:\\/\\/www\\.|https:\\/\\/www\\.|http:\\/\\/|https:\\/\\/)?[a-z0-9]+([-.]{1}[a-z0-9]+)*\\.[a-z]{2,5}(:[0-9]{1,5})?(\\/.*)?$/g; // Url checker with or without http:// or https://\n\n if (url && hostRegExp.test(url)) {\n const select = e.target;\n const previousPid = this.state.pid;\n const pid = select.options ? select.options[select.selectedIndex].getAttribute('data-js-pid') : '';\n this.state.loading = true;\n this.state.pid = pid;\n select.blur();\n trigger(this.options.events.variationUpdate, this.element, {\n url,\n pid,\n source: this.id,\n bubbles: true,\n previousPid,\n variationAttributeId: this.options.variationAttributeId,\n displayValue: select.options ? select.options[select.selectedIndex].text.trim() : '',\n });\n\n return true;\n }\n return false;\n }\n\n /**\n * Update variation attributes list when product updated\n *\n * @param {Object} e - event data\n */\n onProductUpdated(e) {\n if (!e || !e.detail) {\n return;\n }\n // do not update dropdown if this component is an initiator of product update.\n if (e.detail.source === this.id && !this.options.reloadOnChange) {\n return;\n }\n\n const { data } = e.detail;\n if (!data) {\n return;\n }\n const model = this.getModel(data);\n if (model) {\n this.render(model);\n }\n }\n\n /**\n * Find model for swatch by dataModelId\n * @param {Object} data array of components models\n * @returns {Object|null} model object\n */\n getModel(data) {\n const { containerDataModelId } = this.options;\n\n if (data[containerDataModelId] && data[containerDataModelId][this.options.dataModelId]) {\n return data[containerDataModelId][this.options.dataModelId];\n }\n if (data[this.options.dataModelId]) {\n return data[this.options.dataModelId];\n }\n\n return null;\n }\n\n /**\n * Destroy is called automatically after the component is being removed from the DOM\n * You must always destroy the listeners attached to an element to avoid any memory leaks\n */\n destroy() {\n off(this.element);\n off('product.updated', this.element);\n }\n}\n"],"mappings":"2HAUqBA,CAAkB,QAAAC,CAAA,oBAAAC,OAAA,WAAAD,CAAA,EAVhCE,CAAS,CAAAF,C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