|
| 1 | +import EventEmitter from 'eventemitter3'; |
| 2 | +import ekoPlatform from '../ekoPlatform'; |
| 3 | +import { stringifyQueryParams } from '../queryParamsUtils'; |
| 4 | +import deepmerge from 'deepmerge'; |
| 5 | + |
| 6 | +class EkoEmbedDeliveryBase { |
| 7 | + constructor(embedapi, embedpath, iframe) { |
| 8 | + this.embedapi = embedapi || '2.0'; |
| 9 | + this.embedpath = embedpath || '/'; |
| 10 | + this.iframe = iframe; |
| 11 | + this.eventEmitter = new EventEmitter(); |
| 12 | + this.onEkoEventFired = this.onEkoEventFired.bind(this); |
| 13 | + this.addIframeListeners(); |
| 14 | + |
| 15 | + return { |
| 16 | + load: this.load.bind(this), |
| 17 | + invoke: this.invoke.bind(this), |
| 18 | + on: this.on.bind(this), |
| 19 | + off: this.off.bind(this), |
| 20 | + once: this.once.bind(this), |
| 21 | + dispose: this.dispose.bind(this) |
| 22 | + }; |
| 23 | + } |
| 24 | + |
| 25 | + |
| 26 | + load(id, options) { |
| 27 | + let embedParams = { |
| 28 | + id, |
| 29 | + embedapi: this.embedapi, |
| 30 | + embedid: this.iframe.id, |
| 31 | + events: options.events.join(',') |
| 32 | + }; |
| 33 | + embedParams = deepmerge.all([embedParams, options.params]); |
| 34 | + |
| 35 | + let clientSideParams = options.clientSideParams; |
| 36 | + if (clientSideParams && (typeof clientSideParams === 'object' || typeof clientSideParams === 'function')) { |
| 37 | + // Normalize clientSideParams - if it's an object, convert it into a function that returns that object |
| 38 | + if (typeof clientSideParams === 'object') { |
| 39 | + const cspObj = clientSideParams; |
| 40 | + clientSideParams = () => cspObj; |
| 41 | + } |
| 42 | + |
| 43 | + embedParams.csp = true; |
| 44 | + this.once('loader.csp.ready', () => { |
| 45 | + Promise.resolve() |
| 46 | + .then(clientSideParams) |
| 47 | + .then((csp) => { |
| 48 | + this.iframe.contentWindow.postMessage({ target: 'loader', csp }, '*'); |
| 49 | + }); |
| 50 | + }); |
| 51 | + } |
| 52 | + |
| 53 | + // Finally, let's set the iframe's src to begin loading the project |
| 54 | + this.iframe.setAttribute( |
| 55 | + 'src', |
| 56 | + `https://${options.env || ''}embed.eko.com${this.embedpath}?${stringifyQueryParams(embedParams)}` |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + invoke(method, args) { |
| 61 | + if (typeof method !== 'string') { |
| 62 | + throw new Error('Expected required argument method to have type string'); |
| 63 | + } |
| 64 | + |
| 65 | + const action = { |
| 66 | + target: 'embedapi', |
| 67 | + playerProperty: `${method}`, |
| 68 | + args: args |
| 69 | + }; |
| 70 | + |
| 71 | + this.iframe.contentWindow.postMessage(action, '*'); |
| 72 | + } |
| 73 | + on(eventName, callback) { |
| 74 | + this.eventEmitter.on(eventName, callback); |
| 75 | + } |
| 76 | + |
| 77 | + off(eventName, callback) { |
| 78 | + this.eventEmitter.off(eventName, callback); |
| 79 | + } |
| 80 | + |
| 81 | + once(eventName, callback) { |
| 82 | + this.eventEmitter.once(eventName, callback); |
| 83 | + } |
| 84 | + |
| 85 | + dispose() { |
| 86 | + this.removeIframeListeners(); |
| 87 | + } |
| 88 | + |
| 89 | + /////////////////////////// |
| 90 | + // PRIVATE FUNCTIONS |
| 91 | + ////////////////////////// |
| 92 | + addIframeListeners() { |
| 93 | + window.addEventListener('message', this.onEkoEventFired); |
| 94 | + } |
| 95 | + |
| 96 | + removeIframeListeners() { |
| 97 | + window.removeEventListener('message', this.onEkoEventFired); |
| 98 | + } |
| 99 | + |
| 100 | + onEkoEventFired(event) { |
| 101 | + if (!ekoPlatform.isEkoDomain(event.origin)) { |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + const msg = event.data; |
| 106 | + |
| 107 | + // Do nothing if this message was not intended for us |
| 108 | + if (!msg.event || msg.embedid !== this.iframe.id || msg.embedapi !== this.embedapi) { |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + this.eventEmitter.emit(msg.event, ...msg.args); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +export default EkoEmbedDeliveryBase; |
0 commit comments