UNPKG

@web-atoms/core

Version:
341 lines (340 loc) • 13.6 kB
System.register(["tslib", "../../App", "../../Atom", "../../core/AtomLoader", "../../core/AtomUri", "../../core/FormattedError", "../../core/FormattedString", "../../core/types", "../../di/Inject", "../../di/RegisterSingleton", "../../di/ServiceCollection", "../../services/JsonService", "../../services/NavigationService", "../../view-model/AtomWindowViewModel", "../../web/core/AtomUI", "../controls/AtomAlertWindow", "../controls/AtomNotification", "../controls/AtomWindow", "../styles/AtomPopupStyle", "../styles/AtomStyleSheet", "../styles/StyleBuilder", "./NotificationPopup", "./PopupService"], function (_export, _context) { "use strict"; var __awaiter, __decorate, __metadata, __param, App, Atom, AtomLoader, AtomUri, FormattedError, FormattedString, CancelToken, Inject, RegisterSingleton, Scope, ServiceCollection, JsonService, NavigationService, AtomWindowViewModel, AtomUI, AtomAlertWindow, AtomNotification, AtomWindow, AtomPopupStyle, AtomStyleSheet, cssNumberToString, NotificationPopup, PopupService, WindowService; return { setters: [function (_tslib) { __awaiter = _tslib.__awaiter; __decorate = _tslib.__decorate; __metadata = _tslib.__metadata; __param = _tslib.__param; }, function (_App) { App = _App.App; }, function (_Atom) { Atom = _Atom.Atom; }, function (_coreAtomLoader) { AtomLoader = _coreAtomLoader.AtomLoader; }, function (_coreAtomUri) { AtomUri = _coreAtomUri.AtomUri; }, function (_coreFormattedError) { FormattedError = _coreFormattedError.default; }, function (_coreFormattedString) { FormattedString = _coreFormattedString.default; }, function (_coreTypes) { CancelToken = _coreTypes.CancelToken; }, function (_diInject) { Inject = _diInject.Inject; }, function (_diRegisterSingleton) { RegisterSingleton = _diRegisterSingleton.RegisterSingleton; }, function (_diServiceCollection) { Scope = _diServiceCollection.Scope; ServiceCollection = _diServiceCollection.ServiceCollection; }, function (_servicesJsonService) { JsonService = _servicesJsonService.JsonService; }, function (_servicesNavigationService) { NavigationService = _servicesNavigationService.NavigationService; }, function (_viewModelAtomWindowViewModel) { AtomWindowViewModel = _viewModelAtomWindowViewModel.AtomWindowViewModel; }, function (_webCoreAtomUI) { AtomUI = _webCoreAtomUI.AtomUI; }, function (_controlsAtomAlertWindow) { AtomAlertWindow = _controlsAtomAlertWindow.default; }, function (_controlsAtomNotification) { AtomNotification = _controlsAtomNotification.default; }, function (_controlsAtomWindow) { AtomWindow = _controlsAtomWindow.AtomWindow; }, function (_stylesAtomPopupStyle) { AtomPopupStyle = _stylesAtomPopupStyle.AtomPopupStyle; }, function (_stylesAtomStyleSheet) { AtomStyleSheet = _stylesAtomStyleSheet.AtomStyleSheet; }, function (_stylesStyleBuilder) { cssNumberToString = _stylesStyleBuilder.cssNumberToString; }, function (_NotificationPopup) { NotificationPopup = _NotificationPopup.default; }, function (_PopupService) { PopupService = _PopupService.default; }], execute: function () { _export("WindowService", WindowService = class WindowService extends NavigationService { get title() { return window.document.title; } set title(v) { window.document.title = v; } get location() { return new AtomUri(location.href); } set location(v) { location.href = v.toString(); } constructor(app, jsonService) { super(app); this.jsonService = jsonService; this.popups = []; this.hostForElementFunc = []; this.lastPopupID = 0; this.screen = app.screen; let st = "desktop"; if (/mobile|android|ios/i.test(window.navigator.userAgent)) { st = "mobile"; if (/tablet/i.test(window.navigator.userAgent)) { st = "tablet"; } } this.screen.screenType = st; if (window) { const update = e => { this.refreshScreen(); }; if (st !== "mobile") { window.addEventListener("resize", update); window.addEventListener("scroll", update); document.body.addEventListener("scroll", update); document.body.addEventListener("resize", update); } setTimeout(() => { update(null); }, 1000); } } registerHostForWindow(f) { this.hostForElementFunc.push(f); return { dispose: () => { this.hostForElementFunc.remove(f); } }; } navigate(url) { location.href = url; } back() { window.history.back(); } register(id, type) { ServiceCollection.instance.register(type, null, Scope.Transient, id); } confirm(message, title) { return PopupService.confirm({ message: message, title }); } alert(message, title) { if (!(message instanceof FormattedString || typeof message === "string")) { if (message instanceof FormattedError) { message = message.formattedMessage; } else { message = message.message ? message.message : message.toString(); } } return PopupService.alert({ message, title }); } refresh() { location.reload(); } getHostForElement() { const ce = PopupService.lastTarget; if (!ce) { return null; } for (const iterator of this.hostForElementFunc) { const e = iterator(ce); if (e) { return e; } } return null; } refreshScreen() { const height = this.screen.height = window.innerHeight || document.body.clientHeight; const width = this.screen.width = window.innerWidth || document.body.clientWidth; this.screen.scrollLeft = window.scrollX || document.body.scrollLeft || 0; this.screen.scrollTop = window.scrollY || document.body.scrollTop || 0; this.screen.orientation = width > height ? "landscape" : "portrait"; } notify(message, title, type, delay) { return __awaiter(this, void 0, void 0, function* () { try { const notification = NotificationPopup({ message, type }); const cancelToken = new CancelToken(delay !== null && delay !== void 0 ? delay : 5000); yield notification.showWindow(notification, { title, cancelToken }); } catch (ex) { console.error(ex); } }); } openWindow(url, options) { return __awaiter(this, void 0, void 0, function* () { yield Atom.delay(1); const lastTarget = PopupService.lastTarget; const { view: popup, returnPromise, disposables } = yield AtomLoader.loadView(url, this.app, true, () => this.app.resolve(AtomWindowViewModel, true)); if (options && options.onInit) { options.onInit(popup); } const cancelToken = options.cancelToken; if (cancelToken) { if (cancelToken.cancelled) { this.app.callLater(() => { this.remove(popup, true); }); } cancelToken.registerForCancel(() => { this.remove(popup, true); }); } const e = popup.element; let isPopup = true; if (popup instanceof AtomWindow) { isPopup = false; e.style.opacity = "0"; } popup.registerDisposable({ dispose: () => { PopupService.lastTarget = lastTarget; } }); if (isPopup) { const root = popup.element; const last = PopupService.lastTarget; const closePopup = et => { let target = et.target; while (target) { if (target === root || target === last) { return; } target = target.parentElement; } this.remove(popup, true); }; document.body.addEventListener("click", closePopup); popup.registerDisposable({ dispose: () => { document.body.removeEventListener("click", closePopup); } }); } e._logicalParent = lastTarget; e.sourceUrl = url; const pvm = popup.viewModel; if (pvm) { let ce = PopupService.lastTarget; if (ce) { while (!ce.atomControl) { ce = ce.parentElement; if (!ce) { break; } } if (ce && ce.atomControl && ce.atomControl.viewModel) { pvm.parent = ce.atomControl.viewModel; } } } const theme = this.app.get(AtomStyleSheet).getNamedStyle(AtomPopupStyle); e.style.zIndex = 10000 + this.lastPopupID + ""; const isNotification = popup instanceof AtomNotification; if (isPopup) { const sr = AtomUI.screenOffset(PopupService.lastTarget); const x = sr.x; const y = sr.y; const h = sr.height; e.style.position = "absolute"; e.style.left = x + "px"; e.style.top = y + h + "px"; e.classList.add(theme.name); this.popups.push(popup); disposables.add(() => { this.popups.remove(popup); }); document.body.appendChild(e); if (isNotification) { e.style.opacity = "0"; this.centerElement(popup); } } else { const eHost = (options === null || options === void 0 ? void 0 : options.modal) ? null : this.getHostForElement(); if (eHost) { eHost.appendChild(e); } else { const host = document.createElement("div"); document.body.appendChild(host); host.style.position = "absolute"; host.appendChild(e); disposables.add({ dispose() { host.remove(); } }); this.refreshScreen(); popup.bind(host, "styleLeft", [["this", "scrollLeft"]], false, cssNumberToString, this.screen); popup.bind(host, "styleTop", [["this", "scrollTop"]], false, cssNumberToString, this.screen); popup.bind(host, "styleWidth", [["this", "width"]], false, cssNumberToString, this.screen); popup.bind(host, "styleHeight", [["this", "height"]], false, cssNumberToString, this.screen); } } popup.bindEvent(document.body, "keyup", keyboardEvent => { if (keyboardEvent.key === "Escape") { this.app.runAsync(() => this.remove(popup)); } }); disposables.add({ dispose: () => { e.innerHTML = ""; e.remove(); } }); return yield returnPromise; }); } centerElement(c) { const e = c.element; const parent = e.parentElement; if (parent === window || parent === document.body) { setTimeout(() => { const ew = (document.body.offsetWidth - e.offsetWidth) / 2; const eh = window.scrollY + (window.innerHeight - e.offsetHeight) / 2; e.style.left = `${ew}px`; e.style.top = `${eh}px`; e.style.removeProperty("opacity"); }, 200); return; } if (parent.offsetWidth <= 0 || parent.offsetHeight <= 0) { setTimeout(() => { this.centerElement(c); }, 100); return; } if (e.offsetWidth <= 0 || e.offsetHeight <= 0) { setTimeout(() => { this.centerElement(c); }, 100); return; } const x = (parent.offsetWidth - e.offsetWidth) / 2; const y = (parent.offsetHeight - e.offsetHeight) / 2; e.style.left = `${x}px`; e.style.top = `${y}px`; e.style.removeProperty("opacity"); } }); WindowService.alertWindow = AtomAlertWindow; _export("WindowService", WindowService = __decorate([RegisterSingleton, __param(0, Inject), __param(1, Inject), __metadata("design:paramtypes", [App, JsonService])], WindowService)); } }; }); //# sourceMappingURL=WindowService.js.map