diff --git a/src/index.js b/src/index.js index c1fa597..408d967 100644 --- a/src/index.js +++ b/src/index.js @@ -23,13 +23,13 @@ function CoCreateModal(id) { } else { this.pageId = localStorage.getItem('pageId') let viewPort = window.top.CoCreate.modal.viewPorts - if (viewPort) + if (viewPort) viewPort.set(this.pageId, this) - + // TODO: can be depreciated if we find a better way to pass rootid and parentid this.parentId = localStorage.getItem('parentId') this.rootId = window.top.CoCreate.modal.rootId - } + } this._createViewPort(); @@ -38,39 +38,39 @@ function CoCreateModal(id) { action.init({ name: "openModal", endEvent: "openModal", - callback: (btn, data) => { - this.open(btn); + callback: (data) => { + this.open(data.element); }, }); action.init({ name: "closeModal", endEvent: "closeModal", - callback: (btn, data) => { - this.modalAction(btn, 'close'); + callback: (data) => { + this.modalAction(data.element, 'close'); }, }); action.init({ name: "minMaxModal", endEvent: "minMaxModal", - callback: (btn, data) => { - this.modalAction(btn, 'minMax'); + callback: (data) => { + this.modalAction(data.element, 'minMax'); }, }); action.init({ name: "parkModal", endEvent: "parkModal", - callback: (btn, data) => { - this.modalAction(btn, 'park'); + callback: (data) => { + this.modalAction(data.element, 'park'); }, }); } CoCreateModal.prototype = { - _checkRoot: function() { + _checkRoot: function () { try { return window === window.top; } catch (e) { @@ -78,8 +78,8 @@ CoCreateModal.prototype = { } }, - _createViewPort: function() { - if (this.viewPort) + _createViewPort: function () { + if (this.viewPort) return true; let el = document.getElementById(this.id); @@ -91,27 +91,27 @@ CoCreateModal.prototype = { } }, - _initSocket: function() { - const self = this; - message.listen('modalAction', function(response) { + _initSocket: function () { + const self = this; + message.listen('modalAction', function (response) { self.runModalAction(response.data) }) }, - open: function(aTag) { + open: function (aTag) { let attributes = []; for (let attribute of aTag.attributes) attributes[attribute.name] = attribute.value let data = { - src: aTag.getAttribute('modal-src'), - x: aTag.getAttribute('modal-x'), - y: aTag.getAttribute('modal-y'), - width: aTag.getAttribute('modal-width'), + src: aTag.getAttribute('modal-src'), + x: aTag.getAttribute('modal-x'), + y: aTag.getAttribute('modal-y'), + width: aTag.getAttribute('modal-width'), height: aTag.getAttribute('modal-height'), - color: aTag.getAttribute('modal-color'), - header: aTag.getAttribute('modal-header'), - iframe: aTag.getAttribute('modal-iframe'), + color: aTag.getAttribute('modal-color'), + header: aTag.getAttribute('modal-header'), + iframe: aTag.getAttribute('modal-iframe'), attributes: attributes } @@ -124,38 +124,38 @@ CoCreateModal.prototype = { } let openIn = aTag.getAttribute('modal-open') || 'root'; - + let openId; switch (openIn) { - case 'parent': - openId = this.parentId; - break; - case 'page': - openId = this.pageId; - break; - case 'root': - openId = this.rootId; - break; - default: - openId = openIn; - break; + case 'parent': + openId = this.parentId; + break; + case 'page': + openId = this.pageId; + break; + case 'root': + openId = this.rootId; + break; + default: + openId = openIn; + break; } localStorage.setItem('parentId', openId) - + data.type = 'open'; data.parentId = openId; if (this.isRoot) { if (this._createViewPort()) - this.viewPort._createModal(data); + this.viewPort._createModal(data); } else { let viewPort = window.top.CoCreate.modal.viewPorts.get(openId); if (viewPort.pageId == data.parentId) { viewPort.viewPort._createModal(data); document.dispatchEvent(new CustomEvent('openModal', { - detail: {} + detail: {} })) } else { @@ -164,7 +164,7 @@ CoCreateModal.prototype = { } }, - modalAction: function(btn, type, data) { + modalAction: function (btn, type, data) { let json = { key: CoCreateConfig.key, organization_id: CoCreateConfig.organization_id, @@ -178,57 +178,57 @@ CoCreateModal.prototype = { } else { let modalEl = btn.closest('.modal') - let modal - if (modalEl) - modal = this.viewPort.modals.get(modalEl.id || this.pageId) - if (!modal) { - modal = window.top.CoCreate.modal.modals.get(this.pageId); - } - json.data = { - parentId: this.parentId, - pageId: this.pageId, - modal, - type - } + let modal + if (modalEl) + modal = this.viewPort.modals.get(modalEl.id || this.pageId) + if (!modal) { + modal = window.top.CoCreate.modal.modals.get(this.pageId); + } + json.data = { + parentId: this.parentId, + pageId: this.pageId, + modal, + type + } - if (modal) - this.runModalAction(json.data) - else - message.send(json); + if (modal) + this.runModalAction(json.data) + else + message.send(json); } }, - runModalAction: function(data) { + runModalAction: function (data) { if (data.modal || data.parentId == this.pageId) { - + let pageId = data.pageId; let type = data.type; let modal = data.modal if (!modal) modal = this.viewPort.modals.get(pageId); - + if (modal) { switch (type) { - case 'open': - if (data.parentId == this.pageId) - this.viewPort._createModal(data) - break; - case 'close': - modal.viewPort._removeModal(modal) - break; - case 'minMax': - modal.el.position.minMax(); - break; - case 'park': - modal.togglePark(); - break; - } + case 'open': + if (data.parentId == this.pageId) + this.viewPort._createModal(data) + break; + case 'close': + modal.viewPort._removeModal(modal) + break; + case 'minMax': + modal.el.position.minMax(); + break; + case 'park': + modal.togglePark(); + break; + } document.dispatchEvent(new CustomEvent(`${type}Modal`, { detail: {} })) - + } }