{"version":3,"file":"Snackbar.js","names":["Snackbar","a","setters","Component","default","deepMerge","Event","on","off","execute","constructor","element","options","arguments","length","sustain","timeoutMs","preventStacking","initState","state","currentNotification","stack","timeoutId","bindEvents","_onNotify","bind","_onClose","onGlobalUpdate","window","clearTimeout","requestAnimationFrame","removeNotification","data","addNotification","_removeNotificationEvents","querySelector","clearAllNotifications","push","createNotification","content","notification","document","createElement","classList","add","innerHTML","type","appendChild","setTimeout","removeChild","shift","snackbarNotifications","forEach","l","i","destroy","removeListener"],"sources":["components/global/Snackbar.js"],"sourcesContent":["import Component from 'core/Component';\nimport { deepMerge } from 'toolbox/deepMerge';\nimport { Event } from 'services/EventEmitter';\nimport { on, off } from 'toolbox/event';\n\n/**\n * This is a description of the Snackbar constructor function.\n * @class\n * @classdesc This is a description of the Snackbar class. (must be edited)\n * @extends Component\n */\nexport default class Snackbar extends Component {\n    /**\n     * Constructor of the class that mainly merge the options of the components\n     * @param {HTMLElement} element HTMLElement of the component\n     * @param {Object} options options that belongs to the component\n     */\n    constructor(element, options = {}) {\n        super(element, deepMerge({\n            sustain: 700, // delay between two notifications\n            timeoutMs: 5000, // default duration of notifications\n            preventStacking: false, // by default snackbars can be stacked\n        }, options));\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.currentNotification = null;\n        this.state.stack = [];\n        this.state.timeoutId = null;\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('snackbar.notify', this._onNotify.bind(this), this);\n        Event.on('snackbar.close', this._onClose.bind(this), this);\n\n        // Get notifications on global update\n        Event.on('global.update', this.onGlobalUpdate.bind(this));\n    }\n\n    /**\n     * Handler when closing a notification\n     */\n    _onClose() {\n        if (this.state.timeoutId !== null) {\n            window.clearTimeout(this.state.timeoutId);\n            window.requestAnimationFrame(this.removeNotification.bind(this, false));\n        }\n    }\n\n    /**\n     * Handler of a new notification\n     * @param {Object} data notification data retrieved from emit\n     * @param {boolean} preventStacking clears notification stack and show current notification\n     */\n    _onNotify(data, preventStacking = false) {\n        this.addNotification(data, preventStacking);\n    }\n\n\n    /**\n     * Remove notification events\n     */\n    _removeNotificationEvents() {\n        if (this.state.currentNotification !== null) {\n            off('click', this.state.currentNotification.querySelector('[data-js-close]'));\n        }\n    }\n\n    /**\n     * Store notification to the stack\n     * @param {Object} data notification data retrieved from emit\n     * @param {boolean} preventStacking clears notification stack and show current notification\n     */\n    addNotification(data, preventStacking) {\n        if (preventStacking) {\n            this.clearAllNotifications();\n        }\n\n        // store current notification data to the state\n        this.state.stack.push(data);\n\n        // if there is no other notification currently displayed then display the current one\n        if (this.state.stack.length === 1) {\n            this.createNotification(this.state.stack[0]);\n        }\n    }\n\n    /**\n     * Display a notification\n     * @param {Object} data notification data retrieved from emit\n     */\n    createNotification(data) {\n        const timeoutMs = data.timeoutMs || this.options.timeoutMs;\n        const content = `\n            <span class=\"c-snackbar__label\"><span class=\"c-snackbar__content\" data-js-content>${data.content}</span></span>\n            <i class=\"c-snackbar__close\" data-js-close></i>\n        `;\n\n        const notification = document.createElement('div');\n        notification.classList.add('c-snackbar__surface');\n        notification.innerHTML = content;\n\n        switch (data.type) {\n        case 'success':\n            notification.classList.add('m-success');\n            break;\n        case 'warning':\n            notification.classList.add('m-warning');\n            break;\n        case 'error':\n            notification.classList.add('m-error');\n            break;\n        default:\n            break;\n        }\n\n        this.element.appendChild(notification);\n        this.state.currentNotification = notification;\n\n        this.state.timeoutId = window.setTimeout(() => {\n            window.requestAnimationFrame(this.removeNotification.bind(this, false));\n        }, timeoutMs);\n\n        on('click', this.state.currentNotification.querySelector('[data-js-close]'), this._onClose.bind(this));\n    }\n\n    /**\n     * Remove current notification\n     * @param {boolean} preventStacking - Ensure that no new notification get stacked\n     */\n    removeNotification(preventStacking = false) {\n        this._removeNotificationEvents();\n        if (this.state.currentNotification) {\n            this.element.removeChild(this.state.currentNotification);\n        }\n        this.state.currentNotification = null;\n        this.state.stack.shift();\n        this.state.timeoutId = null;\n\n        if (this.state.stack.length && !preventStacking) {\n            window.setTimeout(() => {\n                window.requestAnimationFrame(this.createNotification.bind(this, this.state.stack[0]));\n            }, this.options.sustain);\n        }\n    }\n\n    /**\n     * Handler for global update\n     * @param {Object} content - Analytics data of the virtual page\n     */\n    onGlobalUpdate(content) {\n        if (content.snackbarNotifications) {\n            content.snackbarNotifications.forEach((notification) => {\n                this.addNotification(notification);\n            });\n        }\n    }\n\n    /**\n     * remove all notifications\n     */\n    clearAllNotifications() {\n        // clear timed out call if any\n        if (this.state.timeoutId !== null) {\n            window.clearTimeout(this.state.timeoutId);\n        }\n\n        // run with simple cycle to make sure that removeNotification will be called\n        // exact numbe of times as state.stack length, as removeNotification modifies (reduce length) of stack\n        const l = this.state.stack.length;\n        for (let i = 0; i < l; i++) {\n            this.removeNotification(true);\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('snackbar.notify', this._onNotify.bind(this), this);\n        Event.removeListener('snackbar.close', this._onClose.bind(this), this);\n        this._removeNotificationEvents();\n    }\n}\n"],"mappings":"mJAWqBA,CAAQ,QAAAC,CAAA,oBAAAC,OAAA,WAAAD,CAAA,EAXtBE,CAAS,CAAAF,CAAA,CAAAG,OAAA,WAAAH,CAAA,EACPI,CAAS,CAAAJ,CAAA,CAATI,SAAS,WAAAJ,CAAA,EACTK,CAAK,CAAAL,CAAA,CAALK,KAAK,WAAAL,CAAA,EACLM,CAAE,CAAAN,CAAA,CAAFM,EAAE,CAAEC,CAAG,CAAAP,CAAA,CAAHO,GAAG,GAAAC,OAAA,SAAAA,CAAA,EAAAR,CAAA,WAQKD,CAAQ,CAAd,aAAuB,CAAAG,CAAU,CAM5CO,WAAWA,CAACC,CAAO,CAAgB,IAAd,CAAAC,CAAO,GAAAC,SAAA,CAAAC,MAAA,WAAAD,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