{"version":3,"file":"ProductPrice.js","names":["ProductPrice","a","setters","Component","default","Event","on","off","deepMerge","execute","constructor","element","options","arguments","length","templateName","pricesMap","pid","productQuantities","hideStandardPrice","reloadData","configid","section","id","classNames","hidden","dataModelId","initState","state","initCache","selectors","salePrice","querySelector","standardPrice","priceLabels","querySelectorAll","bindEvents","onQuantityChanged","onProductUpdated","bind","onProductPriceUpdated","getParamsMap","params","Object","keys","reduce","obj","el","i","concat","e","updateData","detail","data","productType","productsMap","updatePrice","update","quantity","reload","assign","dataType","then","onPriceUpdated","catch","onPriceFailed","price","strikethroughPrice","updateHTML","componentOptions","renderHidden","innerHTML","classList","toggle","forEach","remove","add","previousPid","renderComponent","model","render","destroy","removeListener"],"sources":["components/product/ProductPrice.js"],"sourcesContent":["import Component from 'core/Component';\nimport { Event } from 'services/EventEmitter';\nimport { on, off } from 'toolbox/event';\nimport { deepMerge } from 'toolbox/deepMerge';\n\n/**\n * This is a description of the ProductPrice constructor function.\n * @class\n * @classdesc This is a description of the ProductPrice class.\n * @extends Component\n */\nexport default class ProductPrice extends Component {\n /**\n * Constructor of the class that mainly merge the options of the components\n *\n * @param {HTMLElement} element HTMLElement of the component\n * @param {Object} options That belongs to the component\n */\n constructor(element, options = {}) {\n super(element, deepMerge({\n templateName: 'product/productprice',\n pricesMap: {},\n pid: null,\n productQuantities: {},\n hideStandardPrice: false,\n reloadData: {\n configid: null, // component config ID\n section: 'product',\n id: 'productprice',\n },\n classNames: {\n hidden: 'h-hidden',\n },\n dataModelId: 'productprice',\n }, options));\n }\n\n /**\n * Init the different state of the component\n */\n initState() {\n this.state.productQuantities = this.options.productQuantities;\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.salePrice = this.element.querySelector('[data-js-saleprice]');\n this.selectors.standardPrice = this.element.querySelector('[data-js-standardprice]');\n this.selectors.priceLabels = this.element.querySelectorAll('[data-js-pricelabel]');\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 Event.on('product.quantity.updated', this.onQuantityChanged, this);\n on('product.updated', this.element, this.onProductUpdated.bind(this));\n on('productprice.updated', this.element, this.onProductPriceUpdated.bind(this));\n }\n\n /**\n * Transform productsMap object to key-value map usable for query string\n *\n * Example:\n * productQuantities = {\n * \"3605531812213\": 1,\n * \"3605532987453\": 1\n * };\n *\n * output = {\n * \"pid_0\": \"3605531812213\",\n * \"qty0\": 1,\n * \"pid_1\": \"3605532987453\",\n * \"qty1\": 1\n * };\n * @returns {Object} - Params object\n */\n getParamsMap() {\n const params = Object.keys(this.state.productQuantities).reduce((obj, el, i) => {\n obj[`pid_${i}`] = el;\n obj[`qty${i}`] = this.state.productQuantities[el] || 1;\n return obj;\n }, {});\n\n params.pid = this.options.pid;\n return params;\n }\n\n /**\n * 'product.quantity.updated' event handler\n *\n * @param {Object} e Event object\n */\n onQuantityChanged(e) {\n this.updateData(e);\n }\n\n /**\n * productprice.updated event handler\n * @param {Object} e Event object\n */\n onProductPriceUpdated(e) {\n if (!e || !e.detail) {\n return;\n }\n\n this.updateData(e.detail);\n }\n\n /**\n * Update product price component when product updated\n *\n * @param {Object} data Data\n */\n updateData(data) {\n if (data.pid === this.options.pid) {\n if (data.productType === 'ROUTINE') {\n if (data.productsMap) {\n this.state.productQuantities = data.productsMap;\n this.updatePrice();\n }\n } else {\n this.update(data.quantity);\n }\n }\n }\n\n /**\n * Update the price for routine.\n *\n * Because of each products of the routine can have it own quantity we need to send the price calculation\n * request to the server. To improve performance the method should be called with the debounce functionality.\n * We can not use the reload method because there can be case when a new request will be invoked while the previous\n * is not completed and one of the requests will be complited in context of destroyed component object.\n *\n */\n updatePrice() {\n const params = this.getParamsMap();\n\n this.reload(Object.assign({\n pid: this.options.pid,\n dataType: 'json',\n }, params))\n .then(this.onPriceUpdated.bind(this))\n .catch(this.onPriceFailed.bind(this));\n }\n\n /**\n * Trigger an update of a routine price\n * @param {Object} data Data\n */\n onPriceUpdated(data) {\n if (data && data.price) {\n const { price, strikethroughPrice } = data;\n this.updateHTML(price, strikethroughPrice);\n this.options.reloadData = Object.assign({\n configid: null,\n section: 'product',\n id: 'productprice',\n }, data.componentOptions.reloadData);\n }\n }\n\n /**\n * Callback when a routine price update failed\n */\n onPriceFailed() {\n // Error handler\n }\n\n /**\n * Update the price\n *\n * @param {Number} quantity New value of the price\n */\n update(quantity) {\n if (quantity && this.options.pricesMap[quantity]) {\n const { salePrice, standardPrice, renderHidden } = this.options.pricesMap[quantity];\n if (!renderHidden) {\n this.updateHTML(salePrice, standardPrice);\n } else {\n this.updateHTML('', '');\n }\n }\n }\n\n /**\n * Render prices\n *\n * @param {String} salePrice Sale price\n * @param {String} standardPrice Standard price\n */\n updateHTML(salePrice, standardPrice) {\n this.selectors.salePrice.innerHTML = salePrice;\n\n if (this.selectors.standardPrice) {\n this.selectors.standardPrice.innerHTML = standardPrice || '';\n\n // eslint-disable-next-line max-len\n this.selectors.standardPrice.classList.toggle(this.options.classNames.hidden, !standardPrice || this.options.hideStandardPrice);\n\n if (this.selectors.priceLabels && this.selectors.priceLabels.length) {\n this.selectors.priceLabels.forEach((el) => {\n if (standardPrice) {\n el.classList.remove(this.options.classNames.hidden);\n } else {\n el.classList.add(this.options.classNames.hidden);\n }\n });\n }\n }\n\n if (!salePrice && !standardPrice) {\n this.element.classList.add(this.options.classNames.hidden);\n } else {\n this.element.classList.remove(this.options.classNames.hidden);\n }\n }\n\n /**\n * Update product price component when product updated\n *\n * @param {Object} e - event data\n */\n onProductUpdated(e) {\n if (!e || !e.detail) {\n return;\n }\n\n const pid = (e.detail.productType === 'DEFAULT') ? e.detail.previousPid : e.detail.pid;\n\n if (pid === this.options.pid) {\n this.renderComponent(e.detail.data);\n }\n }\n\n /**\n * Update product\n * @param {Object} data event data\n */\n renderComponent(data) {\n const model = data && data[this.options.dataModelId];\n\n if (model) {\n this.render(model);\n }\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 Event.removeListener('product.quantity.updated', this.onQuantityChanged, this);\n off('product.updated', this.element);\n off('productprice.updated', this.element);\n }\n}\n"],"mappings":"mJAWqBA,CAAY,QAAAC,CAAA,oBAAAC,OAAA,WAAAD,CAAA,EAX1BE,CAAS,CAAAF,CAAA,CAAAG,OAAA,WAAAH,CAAA,EACPI,CAAK,CAAAJ,CAAA,CAALI,KAAK,WAAAJ,CAAA,EACLK,CAAE,CAAAL,CAAA,CAAFK,EAAE,CAAEC,CAAG,CAAAN,CAAA,CAAHM,GAAG,WAAAN,CAAA,EACPO,CAAS,CAAAP,CAAA,CAATO,SAAS,GAAAC,OAAA,SAAAA,CAAA,EAAAR,CAAA,WAQGD,CAAY,CAAlB,aAA2B,CAAAG,CAAU,CAOhDO,WAAWA,CAACC,CAAO,CAAgB,IAAd,CAAAC,CAAO,GAAAC,SAAA,CAAAC,MAAA,WAAAD,SAAA,IAAAA,SAAA,IAAG,CAAC,CAAC,CAC7B,KAAK,CAACF,CAAO,CAAEH,CAAS,CAAC,CACrBO,YAAY,CAAE,sBAAsB,CACpCC,SAAS,CAAE,CAAC,CAAC,CACbC,GAAG,CAAE,IAAI,CACTC,iBAAiB,CAAE,CAAC,CAAC,CACrBC,iBAAiB,GAAO,CACxBC,UAAU,CAAE,CACRC,QAAQ,CAAE,IAAI,CACdC,OAAO,CAAE,SAAS,CAClBC,EAAE,CAAE,cACR,CAAC,CACDC,UAAU,CAAE,CACRC,MAAM,CAAE,UACZ,CAAC,CACDC,WAAW,CAAE,cACjB,CAAC,CAAEd,CAAO,CAAC,CACf,CAKAe,SAASA,CAAA,CAAG,CACR,IAAI,CAACC,KAAK,CAACV,iBAAiB,CAAG,IAAI,CAACN,OAAO,CAACM,iBAChD,CAKAW,SAASA,CAAA,CAAG,CACR,IAAI,CAACC,SAAS,CAACC,SAAS,CAAG,IAAI,CAACpB,OAAO,CAACqB,aAAa,CAAC,qBAAqB,CAAC,CAC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