Source: plugins/contentWindows/contentWindows.js

'use strict'

import bindEvent from '../../helpers/bindEvent.js'
import Target from '../../EpmlCore/Target.js'

const targetWindows = []

class contentWindowTarget extends Target {
    //
}

/**
 * Epml content windows plugin. Enables communication with iframes and popup windows
 */
export default {
    init: function (Epml) {
        // const proto = Epml.prototype

        bindEvent(window, 'message', event => {
            console.log(event)
            Epml.handleMessage(event.data, message => {
                event.source.postMessage(message, event.origin)
            })
        })

        Epml.addTargetHandler({
            targetType: 'WINDOW', // Unique type for each target type
            name: 'Content window',
            description: 'Allows Epml to communicate with iframes and popup windows',
            isMatchingTargetSource: function (source) {
                if (typeof source !== 'object') return false

                source = source.contentWindow || source
            },
            createTarget: function (source) {
                targetWindows.push({
                    source,
                    eventIsFromSource: function (event) {
                        if (event.source === source) return true
                        return false
                    }
                })
                return {
                    sendMessage: function (data) {
                        return source.postMessage(data, source.origin)
                    }
                }
            }
        })
    }
}