UNPKG

@web-atoms/core

Version:
169 lines (168 loc) • 6.14 kB
System.register(["../App", "../core/AtomOnce", "../core/AtomUri", "../di/ServiceCollection", "../services/BusyIndicatorService", "../services/NavigationService", "./services/WebBusyIndicatorService", "./services/WindowService", "./styles/AtomStyleSheet", "./styles/AtomTheme"], function (_export, _context) { "use strict"; var App, AtomOnce, AtomUri, ServiceCollection, BusyIndicatorService, NavigationService, WebBusyIndicatorService, WindowService, AtomStyleSheet, AtomTheme, WebApp; _export("default", void 0); return { setters: [function (_App) { App = _App.App; }, function (_coreAtomOnce) { AtomOnce = _coreAtomOnce.AtomOnce; }, function (_coreAtomUri) { AtomUri = _coreAtomUri.AtomUri; }, function (_diServiceCollection) { ServiceCollection = _diServiceCollection.ServiceCollection; }, function (_servicesBusyIndicatorService) { BusyIndicatorService = _servicesBusyIndicatorService.BusyIndicatorService; }, function (_servicesNavigationService) { NavigationService = _servicesNavigationService.NavigationService; }, function (_servicesWebBusyIndicatorService) { WebBusyIndicatorService = _servicesWebBusyIndicatorService.WebBusyIndicatorService; }, function (_servicesWindowService) { WindowService = _servicesWindowService.WindowService; }, function (_stylesAtomStyleSheet) { AtomStyleSheet = _stylesAtomStyleSheet.AtomStyleSheet; }, function (_stylesAtomTheme) { AtomTheme = _stylesAtomTheme.AtomTheme; }], execute: function () { _export("default", WebApp = class WebApp extends App { get parentElement() { return document.body; } get root() { return this.mRoot; } set root(v) { const old = this.mRoot; if (old) { old.dispose(); } this.mRoot = v; if (!v) { return; } const pe = this.parentElement; const de = Array.from(pe.children); for (const iterator of de) { iterator.remove(); } pe.appendChild(v.element); } get theme() { return this.get(AtomStyleSheet); } set theme(v) { this.put(AtomTheme, v); this.put(AtomStyleSheet, v); } get contextId() { var _a; (_a = this.mContextId) !== null && _a !== void 0 ? _a : this.mContextId = parseInt((this.url.hash.contextId || "0").toString(), 10); if (!this.mContextId) { for (let index = 0; index < 100; index++) { const cid = `contextId${index}`; const cidData = sessionStorage.getItem(`contextId${index}`); if (!cidData) { this.mContextId = index; sessionStorage.setItem(cid, cid); this.url.hash.contextId = index; this.syncUrl(); break; } } } return `contextId_${this.mContextId}`; } constructor() { super(); this.hashUpdater = new AtomOnce(); this.url = new AtomUri(location.href); this.put(NavigationService, this.resolve(WindowService)); this.put(WebApp, this); this.put(BusyIndicatorService, this.resolve(WebBusyIndicatorService)); ServiceCollection.instance.registerSingleton(AtomStyleSheet, sp => sp.resolve(AtomTheme)); window.addEventListener("hashchange", () => { this.hashUpdater.run(() => { this.url = new AtomUri(location.href); }); }); this.initPromise = new Promise((resolve, reject) => { this.onReady(resolve); }); } updateDefaultStyle(textContent) { if (this.styleElement) { if (this.styleElement.textContent === textContent) { return; } } const ss = document.createElement("style"); ss.textContent = textContent; if (this.styleElement) { this.styleElement.remove(); } document.head.appendChild(ss); this.styleElement = ss; } syncUrl() { this.hashUpdater.run(() => { const currentUrl = new AtomUri(location.href); const sourceHash = this.url.hash; const keyValues = []; let modified = false; for (const key in sourceHash) { if (/^\_\$\_/.test(key)) { continue; } if (sourceHash.hasOwnProperty(key)) { const element = sourceHash[key]; const cv = currentUrl.hash[key]; if (element !== undefined) { keyValues.push({ key, value: element }); } if (cv === element) { continue; } modified = true; } } if (!modified) { return; } const hash = keyValues.map(s => `${s.key}=${encodeURIComponent(s.value)}`).join("&"); location.hash = hash; }); } invokeReady() { if (document.readyState === "complete") { super.invokeReady(); return; } document.addEventListener("readystatechange", e => { super.invokeReady(); }); } }); (function () { if (typeof window.CustomEvent === "function") { return false; } function CustomEvent(event, params) { params = params || { bubbles: false, cancelable: false, detail: null }; const evt = document.createEvent("CustomEvent"); evt.initCustomEvent(event, params.bubbles, params.cancelable, params.detail); return evt; } window.CustomEvent = CustomEvent; })(); } }; }); //# sourceMappingURL=WebApp.js.map