{"version":3,"file":"Sticky.js","names":["Sticky","a","setters","Component","default","mix","Accessibility","Event","deepMerge","execute","with","constructor","element","options","arguments","length","classNames","active","timeout","initCache","selectors","container","bar","querySelector","stickyObserver","initState","state","isSticky","stickyInit","afterInit","observer","window","IntersectionObserver","onIntersection","bind","threshold","observe","bindEvents","on","onUpdatePosition","entries","forEach","entry","emit","height","getBoundingClientRect","intersectionRatio","boundingClientRect","bottom","delay","then","stick","unstick","event","updatePosition","style","top","concat","offsetHeight","classList","add","remove","miliseconds","Promise","resolve","setTimeout","destroy","unobserve","disconnect","removeListener","updateHeight"],"sources":["components/global/Sticky.js"],"sourcesContent":["import Component from 'core/Component';\nimport { mix } from 'core/mixwith';\nimport Accessibility from 'mixins/Accessibility';\nimport { Event } from 'services/EventEmitter';\nimport { deepMerge } from 'toolbox/deepMerge';\n\n/**\n * This is a description of the Sticky constructor function.\n * @class\n * @classdesc This is a description of the Sticky class.\n * @extends Component\n */\nexport default class Sticky extends mix(Component).with(Accessibility) {\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 classNames: {\n active: 'm-sticked',\n },\n timeout: 100,\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.container = this.element;\n this.selectors.bar = this.element.querySelector('[data-js-sticky-bar]');\n\n // place any empty div with `data-js-sticky-limit` attribute to define where the sticky start to be active\n this.selectors.stickyObserver = this.element.querySelector('[data-js-sticky-limit]');\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.isSticky = false;\n this.state.stickyInit = false;\n }\n\n /**\n * Called after component initialization and save original tab index\n */\n afterInit() {\n this.observer = new window.IntersectionObserver(this.onIntersection.bind(this), { threshold: 1 });\n this.observer.observe(this.selectors.stickyObserver);\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 anonyous function\n */\n bindEvents() {\n Event.on('header.updateHeight', this.onUpdatePosition, this);\n }\n\n /**\n * Inter section event handler\n * @param {Object} entries - Entries object\n */\n onIntersection(entries) {\n entries.forEach((entry) => {\n // display only if limit point is out of viewport and should only appear if it is below that limit point\n Event.emit('sticky.intersection', {\n height: this.selectors.container.getBoundingClientRect().height,\n });\n if (entry.intersectionRatio === 0 && entry.boundingClientRect.bottom < 0) {\n this.delay(this.options.timeout).then(this.stick.bind(this));\n } else {\n this.unstick();\n }\n });\n }\n\n /**\n * Update top position of the sticky header\n *\n * @param {Object} event of the header\n */\n onUpdatePosition(event) {\n if (!this.state.isSticky) {\n return;\n }\n this.updatePosition(event.height);\n }\n\n /**\n * Update top position of the sticky header\n *\n * @param {Number} height of the header\n */\n updatePosition(height) {\n this.selectors.bar.style.top = `${height}px`;\n }\n\n /**\n * Move product bar to stick state\n */\n stick() {\n if (this.state.isSticky) {\n return;\n }\n this.selectors.container.style.height = `${this.selectors.container.offsetHeight}px`;\n this.selectors.bar.classList.add(this.options.classNames.active);\n this.state.isSticky = true;\n\n // force register sticked components if not registered yet.\n // in some browsers, intersection observer polyfill does not see entering viewport\n // of items with fixed position.\n if (!this.state.stickyInit) {\n Event.emit('registry.registerChildren', this.element);\n }\n this.state.stickyInit = true;\n Event.emit('sticky.stick');\n }\n\n /**\n * Move product bar to stale state\n */\n unstick() {\n if (!this.state.isSticky) {\n return;\n }\n this.selectors.container.style.height = 'auto';\n this.updatePosition(0);\n this.selectors.bar.classList.remove(this.options.classNames.active);\n this.state.isSticky = false;\n }\n\n /**\n * delay\n * used to delay prommise\n * @param {Number} miliseconds - time in miliseconds\n * @returns {Promise} promise\n */\n delay(miliseconds) {\n return new Promise((resolve) => {\n setTimeout(resolve, miliseconds);\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 this.unstick();\n this.observer.unobserve(this.selectors.stickyObserver);\n this.observer.disconnect();\n Event.removeListener('header.updateHeight', this.updateHeight, this);\n }\n}\n"],"mappings":"yKAYqBA,CAAM,QAAAC,CAAA,oBAAAC,OAAA,WAAAD,CAAA,EAZpBE,CAAS,CAAAF,CAAA,CAAAG,OAAA,WAAAH,CAAA,EACPI,CAAG,CAAAJ,CAAA,CAAHI,GAAG,WAAAJ,CAAA,EACLK,CAAa,CAAAL,CAAA,CAAAG,OAAA,WAAAH,CAAA,EACXM,CAAK,CAAAN,CAAA,CAALM,KAAK,WAAAN,CAAA,EACLO,CAAS,CAAAP,CAAA,CAATO,SAAS,GAAAC,OAAA,SAAAA,CAAA,EAAAR,CAAA,WAQGD,CAAM,CAAZ,aAAqB,CAAAK,CAAG,CAACF,CAAS,CAAC,CAACO,IAAI,CAACJ,CAAa,CAAE,CAOnEK,WAAWA,CAACC,CAAO,CAAgB,IAAd,CAAAC,CAAO,GAAAC,SAAA,CAAAC,MAAA,WAAAD,SAAA,IAAAA,SAAA,IAAG,CAAC,CAAC,CAC7B,KAAK,CAACF,CAAO,CAAEJ,CAAS,CAAC,CACrBQ,UAAU,CAAE,CACRC,MAAM,CAAE,WACZ,CAAC,CACDC,OAAO,CAAE,GACb,CAAC,CAAEL,CAAO,CAAC,CACf,CAKAM,SAASA,CAAA,CAAG,CACR,IAAI,CAACC,SAAS,CAACC,SAAS,CAAG,IAAI,CAACT,OAAO,CACvC,IAAI,CAACQ,SAAS,CAACE,GAAG,CAAG,IAAI,CAACV,OAAO,CAACW,aAAa,CAAC,sBAAsB,CAAC,CAGvE,IAAI,CAACH,SAAS,CAACI,cAAc,CAAG,IAAI,CAACZ,OAAO,CAACW,aAAa,CAAC,wBAAwB,CACvF,CAMAE,SAASA,CAAA,CAAG,CACR,IAAI,CAACC,KAAK,CAACC,QAAQ,GAAQ,CAC3B,IAAI,CAACD,KAAK,CAACE,UAAU,GACzB,CAKAC,SAASA,CAAA,CAAG,CACR,IAAI,CAACC,QAAQ,CAAG,GAAI,C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