UNPKG

ice.fo.utils

Version:

131 lines (117 loc) 3.68 kB
export function waitForElementOnScreen (el) { return new Promise((resolve) => { const observer = new IntersectionObserver((entries) => { // Use `intersectionRatio` because of Edge 15's // lack of support for `isIntersecting`. // See: https://github.com/w3c/IntersectionObserver/issues/211 if (entries[0].intersectionRatio <= 0) { return } // Cleanup the observer when it's not // needed anymore. observer.unobserve(el) return resolve() }) // Begin observer try { observer.observe(el) } catch (error) { return resolve() } }) } export function execScript (source, { isolate = true, context = { $axios: window.$nuxt.$axios, $modal: window.$nuxt.$modal } } = {}) { return new Promise((resolve) => { if (isolate) { const frame = document.createElement('iframe') document.body.append(frame) /** * primitive value * function * promise * multi-function */ const script = frame.contentWindow.document.createElement('script') script.type = 'text/javascript' script.innerHTML = ` Promise.resolve(eval(\`${source}\`)).then((result) => { window.postMessage({ event: 'result', data: result }) })` Object.assign(frame.contentWindow, context) frame.contentWindow.document.body.appendChild(script) frame.contentWindow.onmessage = function ({ data: { event, data } }) { if (event == 'result') { document.body.removeChild(frame) resolve() } } } }) } export const EditorSignal = { get signaler () { return window.parent.___globalEventSystem }, get EDITOR_ID () { return window.$nuxt.$route.query.__editorId }, _events: {}, /** * This is required. Must run before all */ init () { console.log('[ WindowUtils - EditorSignal ]', 'Initializing Builder Signal Communication') const domainTokens = window.location.hostname.split('.').reverse() let domainScope = '' if (domainTokens.length > 3 && (domainTokens[1] === 'co' || domainTokens[1] === 'or' || domainTokens[1] === 'pe')) { domainScope = [domainTokens[2], domainTokens[1], domainTokens[0]].join('.') } else if (domainTokens.length === 1) { domainScope = domainTokens[0] } else { domainScope = [domainTokens[1], domainTokens[0]].join('.') } try { document.domain = domainScope } catch (e) { console.error('Fail nuxt domain setting', e) } if (!this.signaler) { return false } return true }, listen (eventName, func) { this._events[eventName] = func this.signaler.on(`${eventName}[${this.EDITOR_ID}]`, function () { console.log('[ WindowUtils - EditorSignal ]', 'Incomming Event:', eventName, arguments) func(...arguments) }) }, emit (eventName, data) { if (this._events[eventName]) { this._events[eventName](data) } return this._emitToParentWindow(this.makeMainEventName(this.EDITOR_ID), eventName, data) }, makeMainEventName (pageId) { return `__editor_main_${pageId}` }, /** * 초기 도메인 세팅 후에 일시적으로 크로스도메인문제로 블럭되는데 성공할 때 까지 시도한다. * @param editorId * @param eventName * @param data * @constructor */ _emitToParentWindow (editorId, eventName, data) { return new Promise((resolve) => { const tryDomain = () => { try { this.signaler.emit(editorId, eventName, data) resolve() } catch (e) { setTimeout(tryDomain, 1) } } tryDomain() }) }, }