import { Inject, Injectable } from '@angular/core'; import { DOCUMENT } from '@angular/common'; import { ScriptList } from '../../constants/scripts.constant'; import { Scripts, LoadScript } from '../../models/scripts.interface'; import { production } from '../../tokens'; @Injectable() export class ScriptsService { scripts: LoadScript = { loaded: null, source: null }; production: boolean; constructor(@Inject(DOCUMENT) private doc: any, @Inject(production) productionDi) { this.production = productionDi; ScriptList.forEach((script: Scripts) => { this.scripts[script.name] = { loaded: false, source: script.source }; }); } load(callBackFn, ...scriptProperties: any[]) { const scriptList: any[] = []; scriptProperties.forEach((scriptSource) => scriptList.push(this.loadScript(scriptSource, callBackFn))); return Promise.all(scriptList); } loadScript(scriptSource: Scripts, callBackFn) { return new Promise((resolve) => { if (this.scripts[scriptSource.name].loaded) { resolve({ name: scriptSource.name, loaded: true, status: 'Loaded' }); // callback function if (callBackFn instanceof Function) { callBackFn(); } } else { const sourceArgs: string[] = (scriptSource.source) ? Object.keys(scriptSource.source).map(function (key) { return scriptSource.source[key]; }) : []; const scriptSrc = (scriptSource.source) ? this.scripts[scriptSource.name].source.apply(this, sourceArgs) : this.scripts[scriptSource.name].source; const scriptEle = this.doc.createElement('script'); scriptEle.src = scriptSrc; scriptEle.type = 'text/javascript'; // Populate the id' for white listed scripts this.whiteListScriptId(scriptEle, scriptSource.name, 'certona_resonance.js', 'certonaScriptTag'); // IE if (scriptEle.readyState) { scriptEle.onreadystatechange = () => { if (scriptEle.readyState === 'loaded' || scriptEle.readyState === 'complete') { scriptEle.onreadystatechange = null; this.scripts[scriptSource.name].loaded = true; resolve({ name: scriptSource.name, loaded: true, status: 'Loaded' }); // callback function if (callBackFn instanceof Function) { callBackFn(); } } }; } else { scriptEle.onload = () => { this.scripts[scriptSource.name].loaded = true; resolve({ name: scriptSource.name, loaded: true, status: 'Loaded' }); // callback function if (callBackFn instanceof Function) { callBackFn(); } }; } scriptEle.onerror = (error) => resolve({ name: scriptSource.name, loaded: false, status: 'Error loaded', error: error }); this.doc.body.appendChild(scriptEle); } }); } isDevEnvironment(): boolean { return (!this.production); } whiteListScriptId(scriptElement, scriptName, inputSourceName, inputSourceId) { // Populate id for certona if (scriptName === inputSourceName) { scriptElement.id = inputSourceId; } } /** * how to use */ /** * loadFingerPrintProfileScript(sessionId) { try { const orgId = this.scriptService.isDevEnvironment() ? fingerPrintInfos.ORG_ID_DEV : fingerPrintInfos.ORG_ID_PROD; this.scriptService.load(callBackFn, { name: fingerPrintInfos.NAME, source: { org_id: orgId, merchant_id: fingerPrintInfos.MERCHANT_ID, session_id: sessionId } }).then(data => { // DO something here // for now just comment lines // console.log('Load script >> ', data); }).catch(err => console.log(err)); } catch (err) { console.log(fingerPrintInfos.NAME, 'caused error >> ', err); } } */ }