UNPKG

iportal

Version:

web-portal

1,107 lines (1,087 loc) 123 kB
/*! * iportal v0.0.9 * (c) 2021 @ioing * Released under the MIT License. */ (function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : typeof define === 'function' && define.amd ? define(['exports'], factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.ioing = {})); })(this, (function (exports) { 'use strict'; /*! ***************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ /* global Reflect, Promise */ var extendStatics = function(d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; function __extends(d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); } function __awaiter(thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); } function __generator(thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } } function __spreadArray(to, from) { for (var i = 0, il = from.length, j = to.length; i < il; i++, j++) to[j] = from[i]; return to; } var EventProvider = /** @class */ (function () { function EventProvider() { this._events = {}; } EventProvider.prototype.on = function (type, fn) { var _this = this; var types = type.split(' '); types.forEach(function (typ) { if (!_this._events[typ]) { _this._events[typ] = [fn]; } else { _this._events[typ].push(fn); } }); return this; }; EventProvider.prototype.one = function (type, fn) { var _this = this; var once = function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } fn.apply(null, args); _this.off(type, once); }; var types = type.split(' '); types.forEach(function (typ) { if (!_this._events[typ]) { _this._events[typ] = [once]; } else { _this._events[typ].push(once); } }); return this; }; EventProvider.prototype.off = function (type, fn) { var _this = this; var types = type.split(' '); types.forEach(function (typ) { if (!_this._events[typ]) return; var index = _this._events[typ].indexOf(fn); if (index > -1) { _this._events[typ].splice(index, 1); } }); return this; }; EventProvider.prototype.trigger = function (type) { var _this = this; var args = []; for (var _i = 1; _i < arguments.length; _i++) { args[_i - 1] = arguments[_i]; } if (!this._events[type]) return; this._events[type].forEach(function (fn) { try { fn.apply(null, args); } catch (e) { _this.off(type, fn); _this.trigger('error', "Error by event[" + type + "]"); } }); return this; }; return EventProvider; }()); var ModuleProptey = /** @class */ (function (_super) { __extends(ModuleProptey, _super); function ModuleProptey(id, model, application) { var _this = _super.call(this) || this; _this.param = {}; _this.view = null; _this.events = { transformStart: function () { return undefined; }, transformEnd: function () { return undefined; }, start: function () { return undefined; }, load: function () { return undefined; }, loadError: function () { return undefined; }, preload: function () { return undefined; }, destroy: function () { return undefined; } }; _this.darkTask = []; _this.createTime = Date.now(); _this.transient = false; _this.config = { title: '', rel: 'module', level: 0, source: {}, prerender: [], apply: ['smart-setTimeout', 'link-in-new-window', ['tap-highlight', 'tap-highlight data-appeared']], free: true, background: 'auto' }; _this.components = []; _this.resources = { script: [], image: [], worker: [], video: [], audio: [], font: [], style: [] }; _this.elements = { container: document.body }; _this.id = id; _this.param = {}; _this.model = model; _this.application = application; var _a = _this.setDefaultConfig(model), config = _a.config, resources = _a.resources, events = _a.events, components = _a.components; Object.assign(_this.config, config); Object.assign(_this.resources, resources); Object.assign(_this.events, events); if (components) { _this.components = components; } return _this; } ModuleProptey.prototype.setDefaultConfig = function (manifest) { var _a, _b, _c, _d; if (((_a = manifest.config) === null || _a === void 0 ? void 0 : _a.rel) === 'frameworks') ; if ((_b = manifest.config) === null || _b === void 0 ? void 0 : _b.portal) { if (!manifest.config.free) { console.error('[Module.config[free & portal]] conflit! [free] must be true when [portal] sets true'); } } if (((_d = (_c = manifest.config) === null || _c === void 0 ? void 0 : _c.level) !== null && _d !== void 0 ? _d : 0) > 10000) { console.error('[Module.config.level] needs to be less than 9999!'); } return manifest; }; return ModuleProptey; }(EventProvider)); var ModuleState = /** @class */ (function (_super) { __extends(ModuleState, _super); function ModuleState(id, model, application) { var _a; var _this = _super.call(this, id, model, application) || this; _this.viewTypeCache = null; _this.visibility = false; _this.status = { init: false, preload: false, prefetch: false, prerender: false }; _this.nowViewIndex = (_a = _this.config.level) !== null && _a !== void 0 ? _a : 0; return _this; } Object.defineProperty(ModuleState.prototype, "sameOrigin", { get: function () { if (!this.uri) { if (this.config.sandbox === undefined) return true; if (this.config.sandbox.includes('allow-same-origin')) return true; return false; } var link = new URL(this.uri, window.location.toString()); var isSameOrigin = link.host === location.host; return isSameOrigin; }, enumerable: false, configurable: true }); Object.defineProperty(ModuleState.prototype, "level", { get: function () { var _a; return (_a = this.config.level) !== null && _a !== void 0 ? _a : 0; }, enumerable: false, configurable: true }); Object.defineProperty(ModuleState.prototype, "viewIndex", { get: function () { var _a, _b; var level = (((_a = this.application.preActiveModule) === null || _a === void 0 ? void 0 : _a.nowViewIndex) || 1) + ((_b = this.config.level) !== null && _b !== void 0 ? _b : 1); this.nowViewIndex = level; var baseLevel = this.config.free ? 20000 : 10000; return baseLevel + level; }, enumerable: false, configurable: true }); Object.defineProperty(ModuleState.prototype, "rel", { get: function () { if (this.id === 'system') return 'system'; if (this.id === 'frameworks') return 'frameworks'; return this.config.rel || 'module'; }, enumerable: false, configurable: true }); Object.defineProperty(ModuleState.prototype, "uri", { get: function () { var _a, _b; return ((_b = (_a = this.config) === null || _a === void 0 ? void 0 : _a.source) === null || _b === void 0 ? void 0 : _b.src) || ''; }, enumerable: false, configurable: true }); Object.defineProperty(ModuleState.prototype, "viewType", { get: function () { var _a; if (this.viewTypeCache) return this.viewTypeCache; if (this.rel !== 'module') return this.viewTypeCache = 'shadow'; if (!((_a = this.config) === null || _a === void 0 ? void 0 : _a.portal) || !this.uri) return this.viewTypeCache = 'iframe'; return this.viewTypeCache = ('HTMLPortalElement' in window) && this.sameOrigin ? 'portal' : 'iframe'; }, enumerable: false, configurable: true }); ModuleState.prototype.setActionOrigin = function (origin) { this.actionOriginMap = origin; }; ModuleState.prototype.getActionOrigin = function () { return this.actionOriginMap; }; return ModuleState; }(ModuleProptey)); var ModuleMount = /** @class */ (function (_super) { __extends(ModuleMount, _super); function ModuleMount(id, model, application) { return _super.call(this, id, model, application) || this; } ModuleMount.prototype.timeTick = function () { if (Date.now() - this.createTime > (this.config.timeout || 3600000)) { if (this.view) this.destroy(); } }; ModuleMount.prototype.show = function () { var _a; if (this.viewType === 'portal') { (_a = this.view) === null || _a === void 0 ? void 0 : _a.activate(); return; } if (this.viewType !== 'iframe') return; for (var _i = 0, _b = this.darkTask; _i < _b.length; _i++) { var task = _b[_i]; task(); } this.darkTask = []; this.visibility = true; this.trigger('visible'); this.triggerWindow('module-visible', 'moduleVisibilityState', 'visible'); }; ModuleMount.prototype.hide = function () { this.visibility = false; this.trigger('hidden'); this.triggerWindow('module-hidden', 'moduleVisibilityState', 'hidden'); }; ModuleMount.prototype.willShow = function () { this.trigger('willShow'); this.triggerWindow('module-will-show', 'moduleVisibilityState', 'willVisible'); }; ModuleMount.prototype.willHide = function () { this.trigger('willHidden'); this.triggerWindow('module-will-hidden', 'moduleVisibilityState', 'willHidden'); }; ModuleMount.prototype.triggerWindow = function (type, attributeName, attributeValue) { var _a; if (this.viewType !== 'iframe') return; var sandbox = this.view; var contentWindow = (_a = sandbox === null || sandbox === void 0 ? void 0 : sandbox.contentWindow) === null || _a === void 0 ? void 0 : _a.window; if (contentWindow) { contentWindow.postMessage({ type: type, historyDirection: this.application.transform.historyDirection }, '*'); if (this.sameOrigin && attributeName) { contentWindow[attributeName] = attributeValue; } } }; ModuleMount.prototype.mediaGuard = function () { var _this = this; return new Promise(function (resolve, reject) { var _a; try { if (_this.viewType !== 'iframe') return resolve(); if (_this.sandbox === undefined) return resolve(); var view = _this.view; var contentDocumentElement = (_a = view.contentDocument) === null || _a === void 0 ? void 0 : _a.documentElement; if (!contentDocumentElement) return reject(); if (contentDocumentElement.getElementsByTagName('video')[0]) { var videos = contentDocumentElement.querySelectorAll('video'); var _loop_1 = function (index) { var video = videos[index]; if (!(video === null || video === void 0 ? void 0 : video.paused)) { video.pause(); _this.darkTask.push(function () { video.play(); }); } }; for (var index in videos) { _loop_1(index); } } if (contentDocumentElement.getElementsByTagName('audio')[0]) { var audios = contentDocumentElement.querySelectorAll('audio'); var _loop_2 = function (index) { var audio = audios[index]; if (!(audio === null || audio === void 0 ? void 0 : audio.paused)) { audio.pause(); _this.darkTask.push(function () { audio.play(); }); } }; for (var index in audios) { _loop_2(index); } } } catch (error) { reject(); } }); }; ModuleMount.prototype.destroy = function () { var _this = this; return new Promise(function (resolve, reject) { var _a, _b, _c; if (_this.rel !== 'module') return reject(); if (((_a = _this.application.transform) === null || _a === void 0 ? void 0 : _a.id) === _this.id) return reject(); if (_this.viewType === 'iframe') _this.unload().catch(reject); (_b = _this.elements.container.parentElement) === null || _b === void 0 ? void 0 : _b.removeChild(_this.elements.container); _this.status.prefetch = _this.status.preload = _this.status.prerender = false; _this.view = null; _this.status.init = false; _this.darkTask = []; (_c = _this.events) === null || _c === void 0 ? void 0 : _c.destroy.bind(_this)(); _this.trigger('destroy'); resolve(); }); }; ModuleMount.prototype.observer = function (change) { var _a; var target = this.sandbox ? (_a = this.sandbox.contentDocument) === null || _a === void 0 ? void 0 : _a.documentElement : this.view; if (!target) return; var observer = new MutationObserver(function (record) { change(record); }); observer.observe(target, { subtree: true, attributes: true, childList: true, characterData: true, attributeOldValue: true, characterDataOldValue: true }); return observer; }; ModuleMount.prototype.fate = function () { var _this = this; return new Promise(function (resolve, reject) { var _a; if (_this.rel !== 'module') return reject(); if (_this.config.background === true) return reject(); if (_this.config.background === false) return resolve(); if (_this.viewType !== 'iframe') return reject(); if (_this.sandbox === undefined) return reject(); if (_this.sameOrigin === false) return resolve(); var view = _this.view; try { var contentDocumentElement = (_a = view.contentDocument) === null || _a === void 0 ? void 0 : _a.documentElement; if (!contentDocumentElement) return reject(); if (_this.config.mediaGuard !== false) { _this.mediaGuard().catch(resolve); } if (contentDocumentElement.getElementsByTagName('object')[0]) return resolve(); if (contentDocumentElement.getElementsByTagName('embed')[0]) return resolve(); if (contentDocumentElement.getElementsByTagName('applet')[0]) return resolve(); if (contentDocumentElement.getElementsByTagName('iframe')[0]) return resolve(); if (contentDocumentElement) { var counter_1 = { times: 0 }; var observer = _this.observer(function () { counter_1.times++; if (counter_1.times > 1000) { resolve(); _this.mutationObserver.disconnect(); } }); if (!observer) return; _this.mutationObserver = observer; setTimeout(function () { if (counter_1.times > 10) resolve(); }, 3000); } else { reject(); } } catch (error) { reject(); } }); }; ModuleMount.prototype.unfate = function () { var _a; (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.disconnect(); }; ModuleMount.prototype.unload = function () { var _this = this; return new Promise(function (resolve, reject) { var _a, _b; _this.unfate(); var sandbox = _this.view; if (!sandbox) return resolve(); sandbox.style.display = 'none'; sandbox.src = 'about:blank'; try { var contentWindow = (_a = sandbox.contentWindow) === null || _a === void 0 ? void 0 : _a.window; var contentDocument = sandbox.contentDocument; contentWindow === null || contentWindow === void 0 ? void 0 : contentWindow.location.reload(); contentDocument === null || contentDocument === void 0 ? void 0 : contentDocument.open(); contentDocument === null || contentDocument === void 0 ? void 0 : contentDocument.write(''); contentDocument === null || contentDocument === void 0 ? void 0 : contentDocument.close(); } catch (error) { reject(); } (_b = sandbox.parentElement) === null || _b === void 0 ? void 0 : _b.removeChild(sandbox); resolve(); }); }; return ModuleMount; }(ModuleState)); var ModulePrefetch = /** @class */ (function (_super) { __extends(ModulePrefetch, _super); function ModulePrefetch(id, model, application) { return _super.call(this, id, model, application) || this; } ModulePrefetch.prototype.prefetch = function () { var _this = this; return new Promise(function (resolve, reject) { Promise.all([ _this.prefetchStatic(_this.resources.script, 'script'), _this.prefetchStatic(_this.resources.image, 'image'), _this.prefetchStatic(_this.resources.worker, 'worker'), _this.prefetchStatic(_this.resources.video, 'video'), _this.prefetchStatic(_this.resources.audio, 'audio'), _this.prefetchStatic(_this.resources.font, 'font'), _this.prefetchStatic(_this.resources.style, 'style') ]).then(function () { _this.status.prefetch = true; resolve(true); }).catch(reject); }); }; ModulePrefetch.prototype.prefetchStatic = function (list, as) { var _this = this; if (list === void 0) { list = []; } if (as === void 0) { as = 'script'; } return new Promise(function (resolve, reject) { Promise.all([].concat(list).map(function (url) { return _this.beforehandLink(url, 'preload', as); })).then(resolve).catch(reject); }); }; ModulePrefetch.prototype.beforehandLink = function (url, rel, as) { if (rel === void 0) { rel = 'preload'; } if (as === void 0) { as = 'worker | video | audio | font | script | style | image | document'; } if (!url) return Promise.resolve(); return new Promise(function (resolve, reject) { var _a; var link = document.createElement('link'); link.rel = rel; link.href = url; link.as = as; link.onload = resolve; link.onerror = reject; if (!((_a = link.relList) === null || _a === void 0 ? void 0 : _a.supports(rel))) { return resolve('notSupports'); } document.getElementsByTagName('head')[0].appendChild(link); resolve(); }); }; return ModulePrefetch; }(ModuleMount)); var Sandbox = /** @class */ (function () { function Sandbox(src, setting) { var sandbox = this.sandbox = document.createElement('iframe'); sandbox.src = src || 'about:blank'; sandbox.style.display = 'none'; if (setting !== undefined) { this.set(setting); } return this; } Object.defineProperty(Sandbox.prototype, "src", { set: function (src) { this.sandbox.src = src; }, enumerable: false, configurable: true }); Object.defineProperty(Sandbox.prototype, "unload", { set: function (beforeunload) { this.contentWindow.onbeforeunload = beforeunload; }, enumerable: false, configurable: true }); Object.defineProperty(Sandbox.prototype, "onload", { set: function (onload) { this.sandbox.onload = onload; }, enumerable: false, configurable: true }); Object.defineProperty(Sandbox.prototype, "onerror", { set: function (onerror) { this.sandbox.onerror = onerror; }, enumerable: false, configurable: true }); Sandbox.prototype.set = function (allow) { this.sandbox.setAttribute('sandbox', allow); }; Sandbox.prototype.reset = function (allow) { this.exit(); this.set(allow); return this; }; Sandbox.prototype.open = function () { this.contentDocument.open(); return this; }; Sandbox.prototype.write = function (context) { if (context === void 0) { context = '<head><meta charset="utf-8"></head>'; } this.contentDocument.write(context); return this; }; Sandbox.prototype.close = function () { this.contentDocument.close(); return this; }; Sandbox.prototype.append = function (context, container) { this.enter(container); this.attach(); this.open(); this.write(context); this.close(); }; Sandbox.prototype.enter = function (container) { container.appendChild(this.sandbox); }; Sandbox.prototype.attach = function () { var contentWindow = this.sandbox.contentWindow; var contentDocument = this.sandbox.contentDocument; this.contentWindow = contentWindow; this.contentDocument = contentDocument; }; Sandbox.prototype.exit = function () { var parentNode = this.sandbox.parentNode; parentNode && parentNode.removeChild(this.sandbox); }; return Sandbox; }()); var windowOpen = (function (moduleWindow, application) { var realOpen = moduleWindow.open; var blockClick = function (event) { var _a; var path = event['path'] || ((_a = event.composedPath) === null || _a === void 0 ? void 0 : _a.call(event)) || []; var anchor = (function () { for (var _i = 0, path_1 = path; _i < path_1.length; _i++) { var el = path_1[_i]; if (el.tagName === 'A') return el; } })(); if (!anchor) return false; var href = anchor.href || String(anchor); if (href && anchor.target !== 'self') { var title = anchor.title || ''; var preset = anchor.getAttribute('preset-effect'); var cloneAs = anchor.getAttribute('clone-as'); event.stopPropagation(); event.preventDefault(); application.pushWindow(href, title, preset, cloneAs, event).catch(function () { realOpen(href); }); } return false; }; moduleWindow.addEventListener('click', blockClick); moduleWindow.open = function (url, target, features) { if (typeof url !== 'string' || target || features) { return realOpen(url, target, features); } if (typeof url === 'string') { application.pushWindow(url, '').catch(function () { realOpen(url); }); } return null; }; }); var getTimerHandler = function (handler, moduleWindow, stretch) { return function () { var _args = []; for (var _i = 0; _i < arguments.length; _i++) { _args[_i] = arguments[_i]; } var run = function () { if (typeof handler === 'function') { handler.apply(void 0, _args); } else if (typeof handler === 'string') { var evalHander = new moduleWindow['Function']("return " + handler); evalHander.apply(void 0, _args); } }; if (moduleWindow['moduleVisibilityState'] === 'visible') { run(); } else if (stretch) { moduleWindow.addEventListener('module-visible', function () { run(); }); } }; }; var smartSetTimeout = function (moduleWindow) { var realSetTimeout = moduleWindow.setTimeout; moduleWindow.setTimeout = function (handler, timeout) { var args = []; for (var _i = 2; _i < arguments.length; _i++) { args[_i - 2] = arguments[_i]; } var fn = getTimerHandler(handler, moduleWindow, true); var intervalId = realSetTimeout.apply(void 0, __spreadArray([fn, timeout], args)); return intervalId; }; }; var smartSetInterval = function (moduleWindow) { var realSetInterval = moduleWindow.setInterval; moduleWindow.setInterval = function (handler, timeout) { var args = []; for (var _i = 2; _i < arguments.length; _i++) { args[_i - 2] = arguments[_i]; } var fn = getTimerHandler(handler, moduleWindow); var intervalId = realSetInterval.apply(void 0, __spreadArray([fn, timeout], args)); return intervalId; }; }; var tapHighlight = (function (moduleWindow, capture) { var touchActive = { element: null, oldStyle: '' }; var addHighlight = function (event) { var captureList = capture ? typeof capture === 'string' ? capture.split(' ') : capture : null; var path = event['path'] || event.composedPath() || []; path.splice(-3); var anchor = (function () { var _a, _b, _c; for (var _i = 0, path_1 = path; _i < path_1.length; _i++) { var el = path_1[_i]; if (!((_a = el.children) === null || _a === void 0 ? void 0 : _a.length)) continue; if (el.tagName === 'A') return el; if (captureList) { for (var _d = 0, captureList_1 = captureList; _d < captureList_1.length; _d++) { var attr = captureList_1[_d]; if (el === null || el === void 0 ? void 0 : el.getAttribute(attr)) return el; } } } return (((_c = (_b = path[0]) === null || _b === void 0 ? void 0 : _b.children) === null || _c === void 0 ? void 0 : _c.length) ? path[0] : path[1]) || event.target; })(); if (!anchor) return; touchActive.element = anchor; touchActive.oldStyle = anchor.style.filter; setTimeout(function () { if (touchActive.element === anchor) anchor.style.filter = touchActive.oldStyle + ' brightness(.8)'; var continueCheck = function () { setTimeout(function () { if (touchActive.element !== anchor) { anchor.style.filter = touchActive.oldStyle; } else { continueCheck(); } }, 600); }; continueCheck(); }, 60); }; var cancelHighlight = function () { var _a, _b; if ((_b = (_a = touchActive.element) === null || _a === void 0 ? void 0 : _a.style) === null || _b === void 0 ? void 0 : _b.filter) { touchActive.element.style.filter = touchActive.oldStyle; } touchActive.element = null; touchActive.oldStyle = ''; }; var delayCancelHighlight = function () { setTimeout(function () { cancelHighlight(); }, 600); }; moduleWindow.document.addEventListener('touchstart', addHighlight); moduleWindow.addEventListener('touchstart', addHighlight); moduleWindow.addEventListener('touchmove', cancelHighlight); moduleWindow.addEventListener('touchcancel', cancelHighlight); moduleWindow.addEventListener('touchend', delayCancelHighlight); }); var cssVar = (function (moduleWindow, module) { var config = module.config, application = module.application; var globalCSSVariables = application.config.globalCSSVariables; var docStyle = moduleWindow.document.documentElement.style; var setGlobalCSSVariables = function (variables) { for (var key in variables) { docStyle.setProperty(key, variables[key]); } }; var setCSSSafeAreaValue = function (data) { var _a, _b, _c, _d; setGlobalCSSVariables({ '--application-safe-area-top': (_a = data[0]) !== null && _a !== void 0 ? _a : data, '--application-safe-area-right': (_b = data[1]) !== null && _b !== void 0 ? _b : data, '--application-safe-area-bottom': (_c = data[2]) !== null && _c !== void 0 ? _c : data, '--application-safe-area-left': (_d = data[3]) !== null && _d !== void 0 ? _d : data }); }; if (config.safeArea) { setCSSSafeAreaValue(config.safeArea); } else if (application.config.safeArea) { setCSSSafeAreaValue(application.config.safeArea); } if (globalCSSVariables) { setGlobalCSSVariables(globalCSSVariables); } application.on('safeAreaChange', function (data) { setCSSSafeAreaValue(data); }); application.on('globalCSSVariablesChange', function (data) { setGlobalCSSVariables(data); }); }); var inject = (function (moduleWindow, module) { var config = module.config, application = module.application; var apply = config.apply || []; if (Array.isArray(apply)) { for (var _i = 0, apply_1 = apply; _i < apply_1.length; _i++) { var item = apply_1[_i]; switch (item) { case 'link-in-new-window': windowOpen(moduleWindow, application); break; case 'smart-setTimeout': smartSetTimeout(moduleWindow); break; case 'smart-setInterval': smartSetInterval(moduleWindow); break; case 'tap-highlight': tapHighlight(moduleWindow); break; default: if (Array.isArray(item)) { switch (item[0]) { case 'tap-highlight': tapHighlight(moduleWindow, item[1]); break; } } break; } } } if (typeof config.inject === 'function') { config.inject(moduleWindow, module); } if (module.components) { for (var _a = 0, _b = module.components; _a < _b.length; _a++) { var mountComponent = _b[_a]; moduleWindow.customElements.define('code-highlight', mountComponent(moduleWindow)); } } cssVar(moduleWindow, module); }); var ModuleView = /** @class */ (function (_super) { __extends(ModuleView, _super); function ModuleView(id, model, application) { return _super.call(this, id, model, application) || this; } ModuleView.prototype.attach = function (element) { if (!element) return; this.status.init = true; this.elements.container = element; this.addPanMoveHolder(element); }; ModuleView.prototype.addPanMoveHolder = function (container) { var touchBorder = this.config.touchBorder; if (!touchBorder) return; var threshold = 12; var topHolder = document.createElement('div'); var rightHolder = document.createElement('div'); var bottomHolder = document.createElement('div'); var leftHolder = document.createElement('div'); var mainHolder = document.createElement('div'); topHolder.style.cssText = "position: absolute; top: 0; right: 0; left: 0; z-index: 3; height: " + threshold + "px;"; rightHolder.style.cssText = "position: absolute; top: 0; right: 0; bottom: 0; z-index: 3; width: " + threshold + "px;"; bottomHolder.style.cssText = "position: absolute; right: 0; bottom: 0; left: 0; z-index: 3; height: " + threshold + "px;"; leftHolder.style.cssText = "position: absolute; top: 0; bottom: 0; left: 0; z-index: 3; width: " + threshold + "px;"; mainHolder.style.cssText = "display: none; position: absolute; top: 0; right: 0; bottom: 0; left: 0; z-index: 4;"; container.appendChild(topHolder); container.appendChild(rightHolder); container.appendChild(bottomHolder); container.appendChild(leftHolder); container.appendChild(mainHolder); touchBorder({ topHolder: topHolder, rightHolder: rightHolder, bottomHolder: bottomHolder, leftHolder: leftHolder, mainHolder: mainHolder }, this, this.application); }; ModuleView.prototype.createSandbox = function (uri, config) { this.sandbox = new Sandbox(uri, config); return this.sandbox.sandbox; }; ModuleView.prototype.createShadowbox = function (render) { var shadowbox = document.createElement('shadow-view'); var shadowboxInner = document.createElement('shadow-inner'); shadowbox === null || shadowbox === void 0 ? void 0 : shadowbox.attachShadow({ mode: 'open' }); var shadowRoot = shadowbox.shadowRoot ? shadowbox.shadowRoot : shadowbox; shadowRoot.appendChild(shadowboxInner); render(shadowboxInner); this.elements.container.appendChild(shadowbox); return shadowbox; }; ModuleView.prototype.createPortals = function (uri) { var portal = document.createElement('portal'); portal.src = uri; return portal; }; ModuleView.prototype.createView = function () { if (this.view) return this.view; if (this.config.render) { return this.view = this.createShadowbox(this.config.render); } if (this.rel !== 'module') return; if (this.viewType === 'portal') { this.view = this.createPortals(this.uri); } else { this.view = this.createSandbox(this.uri, this.config.sandbox); } return this.view; }; ModuleView.prototype.loadContent = function () { var _a, _b, _c; var container = this.elements.container; var shadowView = this.view; if (!shadowView) return; switch (this.viewType) { case 'portal': container.appendChild(shadowView); break; case 'iframe': default: if (!this.sandbox) { if (this.rel === 'frameworks') { inject(window, this); } return; } if (this.uri) { this.sandbox.enter(container); var contentWindow = shadowView.contentWindow; if (contentWindow && this.sameOrigin) { inject(contentWindow === null || contentWindow === void 0 ? void 0 : contentWindow.window, this); contentWindow === null || contentWindow === void 0 ? void 0 : contentWindow.window.postMessage({ type: 'container-ready' }, '*'); } } else { this.sandbox.append((_b = (_a = this.config) === null || _a === void 0 ? void 0 : _a.source) === null || _b === void 0 ? void 0 : _b.html, container); var update = this.loadContent.bind(this); this.sandbox.unload = update; inject((_c = this.sandbox.contentWindow) === null || _c === void 0 ? void 0 : _c.window, this); } break; } }; ModuleView.prototype.mountPresetView = function () { var _a, _b, _c; var view = (_b = (_a = window === null || window === void 0 ? void 0 : window.__application_preset) === null || _a === void 0 ? void 0 : _a.modules) === null || _b === void 0 ? void 0 : _b[this.id]; if (view) { this.view = view; this.elements.container.appendChild(view); (_c = window === null || window === void 0 ? void 0 : window.__application_preset) === null || _c === void 0 ? true : delete _c[this.id]; } }; ModuleView.prototype.create = function (prepare) { var _this = this; if (prepare === void 0) { prepare = true; } this.unfate(); this.timeTick(); this.mountPresetView(); if (this.status.prerender) { return Promise.resolve('prerender'); } return new Promise(function (resolve, reject) { _this.createTime = Date.now(); var shadowView = _this.createView(); if (!shadowView) { return resolve('null'); } if (_this.viewType === 'shadow') { return resolve('shadow'); } shadowView.style.cssText = "\n position: absolute;\n z-index: 0;\n width: 100%;\n height: 100%;\n border: 0;\n transform: translate3d(0, 0, 0);\n "; shadowView.onload = function (e) { var _a; _this.status.prerender = true; (_a = _this.events) === null || _a === void 0 ? void 0 : _a.load.bind(_this)(); _this.trigger('load'); setTimeout(function () { resolve(e); }, 50); }; shadowView.onerror = function (e) { var _a; (_a = _this.events) === null || _a === void 0 ? void 0 : _a.loadError.bind(_this)(); _this.trigger('loadError'); reject(e); }; _this.loadContent(); if (prepare === false || _this.config.animation === false) { return resolve('instant'); } setTimeout(function () { resolve('timeout'); }, _this.status.preload ? 800 : 200); }); }; ModuleView.prototype.preload = function () { var _this = this; return new Promise(function (resolve, reject) { var _a, _b; var uri = _this.uri; var head = document.head; var sandbox = new Sandbox(uri, ''); sandbox.onload = function (e) { var _a; _this.status.preload = true; sandbox.exit(); (_a = _this.events) === null || _a === void 0 ? void 0 : _a.preload.bind(_this)(); _this.trigger('preload'); r