{"version":3,"file":"ThirdPartyLoader.js","names":["ThirdPartyLoader","a","setters","Component","default","deepMerge","stringify","loadjs","execute","constructor","element","options","arguments","length","url","bundleId","params","dataAttributes","loadScriptTimeout","path","initState","state","loaded","loading","data","loadScript","Promise","reject","Error","resolve","isDefined","ready","onScriptLoaded","bind","callbacks","success","error","onScriptLoadFailed","before","scriptEl","onBeforeScriptLoad","console","message","Object","keys","forEach","key","setAttribute","afterInit","setTimeout"],"sources":["components/global/ThirdPartyLoader.js"],"sourcesContent":["import Component from 'core/Component';\nimport { deepMerge } from 'toolbox/deepMerge';\nimport { stringify } from 'toolbox/queryString';\nimport loadjs from 'loadjs';\n\n/**\n * @class\n * @classdesc This component is intented to be used as third party component wrapper\n * @extends Component\n */\nexport default class ThirdPartyLoader 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 url: null, // url of the third-party service to call\n bundleId: null, // id used to avoid duplicate scripts. If null, no duplicate check will be done.\n params: null, // params object to be transformed into a query string\n dataAttributes: null, // Extra data attributes to be passed to the thirdParty script\n loadScriptTimeout: 0, // timeout for the loadScript function call\n }, options));\n }\n\n /**\n * Get path\n */\n get path() {\n const { url, params } = this.options;\n let path = url;\n\n // If params is set and has to be an object\n if (params && typeof params === 'object') {\n // we stringify the params and concat with the url\n path += `?${stringify(params)}`;\n }\n\n return path;\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.loaded = false;\n this.state.loading = false;\n this.state.data = null;\n }\n\n /**\n * Load the third-party script\n * @returns {Promise} - promise object\n */\n loadScript() {\n if (!this.path) {\n return Promise.reject(new Error('URL is missing'));\n }\n\n if (this.state.loading) {\n return Promise.reject(new Error('Script already loading'));\n }\n\n if (this.state.loaded) {\n return Promise.resolve(this.state.data);\n }\n\n if (this.options.bundleId && loadjs.isDefined(this.options.bundleId)) {\n loadjs.ready(this.options.bundleId, this.onScriptLoaded.bind(this));\n return Promise.resolve(this.state.data);\n }\n\n this.state.loading = true;\n\n return new Promise((resolve, reject) => {\n const callbacks = {\n success: (data) => {\n this.onScriptLoaded(data);\n resolve(data);\n },\n error: (error) => {\n this.onScriptLoadFailed(error);\n reject(error);\n },\n before: (path, scriptEl) => this.onBeforeScriptLoad(path, scriptEl),\n };\n if (this.options.bundleId) {\n loadjs(this.path, this.options.bundleId, callbacks);\n } else {\n loadjs(this.path, callbacks);\n }\n });\n }\n\n /**\n * On script load handler, called when the third-party is loaded\n * @param {Object} data - Response of the provider\n */\n onScriptLoaded(data) {\n this.state.loaded = true;\n this.state.loading = false;\n this.state.data = data;\n }\n\n /**\n * On script loading failed handler\n * @param { Object } error - error object\n */\n onScriptLoadFailed(error) {\n this.state.loading = false;\n\n if (error) {\n console.error(error.message);\n }\n }\n\n /**\n * Before load script\n * @param {String} path script path\n * @param {Object} scriptEl script dom element\n * @returns {Boolean} callback result\n */\n onBeforeScriptLoad(path, scriptEl) {\n if (this.options.dataAttributes) {\n Object.keys(this.options.dataAttributes).forEach((key) => {\n scriptEl.setAttribute(key, this.options.dataAttributes[key]);\n });\n }\n\n return true;\n }\n\n /**\n * After init\n * Run any script after the component is fully initialized\n */\n afterInit() {\n if (this.options.loadScriptTimeout) {\n setTimeout(() => {\n this.loadScript();\n }, this.options.loadScriptTimeout);\n } else {\n this.loadScript();\n }\n }\n}\n"],"mappings":"wIAUqBA,CAAgB,QAAAC,CAAA,oBAAAC,OAAA,WAAAD,CAAA,EAV9BE,CAAS,CAAAF,CAAA,CAAAG,OAAA,WAAAH,CAAA,EACPI,CAAS,CAAAJ,CAAA,CAATI,SAAS,WAAAJ,CAAA,EACTK,CAAS,CAAAL,CAAA,CAATK,SAAS,WAAAL,CAAA,EACXM,CAAM,CAAAN,CAAA,CAAAG,OAAA,GAAAI,OAAA,SAAAA,CAAA,EAAAP,CAAA,WAOQD,CAAgB,CAAtB,aAA+B,CAA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