@web-atoms/core
Version:
139 lines (138 loc) • 5.13 kB
JavaScript
System.register(["tslib", "../core/AtomComponent", "../core/AtomUri", "../core/FormattedString", "../core/types", "./ReferenceService"], function (_export, _context) {
"use strict";
var __awaiter, AtomComponent, AtomUri, FormattedString, ArrayHelper, ReferenceService, ObjectReference, NavigationService, NotifyType, nameSymbol;
function hasPageUrl(target) {
const url = target[nameSymbol];
if (!url) {
return false;
}
const baseClass = Object.getPrototypeOf(target);
if (!baseClass) {
return false;
}
return baseClass[nameSymbol] !== url;
}
_export({
NavigationService: void 0,
NotifyType: void 0
});
return {
setters: [function (_tslib) {
__awaiter = _tslib.__awaiter;
}, function (_coreAtomComponent) {
AtomComponent = _coreAtomComponent.AtomComponent;
}, function (_coreAtomUri) {
AtomUri = _coreAtomUri.AtomUri;
}, function (_coreFormattedString) {
FormattedString = _coreFormattedString.default;
}, function (_coreTypes) {
ArrayHelper = _coreTypes.ArrayHelper;
}, function (_ReferenceService) {
ReferenceService = _ReferenceService.default;
ObjectReference = _ReferenceService.ObjectReference;
}],
execute: function () {
(function (NotifyType) {
NotifyType["Information"] = "info";
NotifyType["Warning"] = "warn";
NotifyType["Error"] = "error";
})(NotifyType || _export("NotifyType", NotifyType = {}));
nameSymbol = UMD.nameSymbol;
_export("NavigationService", NavigationService = class NavigationService {
constructor(app) {
this.app = app;
this.callbacks = [];
}
pushPage(pageName, viewModelParameters, options) {
this.app.runAsync(() => this.openPage(pageName, viewModelParameters, options));
}
openPage(pageName, viewModelParameters, options) {
options = options || {};
if (typeof pageName !== "string") {
if (hasPageUrl(pageName)) {
pageName = pageName[nameSymbol];
} else {
const rs = this.app.resolve(ReferenceService);
const host = pageName instanceof AtomComponent ? "reference" : "class";
const r = rs.put(pageName);
pageName = `ref://${host}/${r.key}`;
}
}
if (this.beforeCallbacks) {
for (const iterator of this.beforeCallbacks) {
const r = iterator(pageName, viewModelParameters, options);
if (r) {
return r;
}
}
}
const url = new AtomUri(pageName);
if (viewModelParameters) {
for (const key in viewModelParameters) {
if (viewModelParameters.hasOwnProperty(key)) {
const element = viewModelParameters[key];
if (element === undefined) {
continue;
}
if (element === null) {
url.query["json:" + key] = "null";
continue;
}
if (key.startsWith("ref:") || element instanceof FormattedString) {
const r = element instanceof ObjectReference ? element : this.app.resolve(ReferenceService).put(element);
url.query[key.startsWith("ref:") ? key : `ref:${key}`] = r.key;
continue;
}
if (typeof element !== "string" && (typeof element === "object" || Array.isArray(element))) {
url.query["json:" + key] = JSON.stringify(element);
} else {
url.query[key] = element;
}
}
}
}
for (const iterator of this.callbacks) {
const r = iterator(url, options);
if (r) {
return r;
}
}
return this.openWindow(url, options);
}
remove(view, force) {
return __awaiter(this, void 0, void 0, function* () {
if (force) {
this.app.broadcast(`atom-window-cancel:${view.id}`, "cancelled");
return true;
}
const vm = view.viewModel;
if (vm && vm.cancel) {
const a = yield vm.cancel();
return a;
}
this.app.broadcast(`atom-window-cancel:${view.id}`, "cancelled");
return true;
});
}
registerNavigationHook(callback) {
this.callbacks.push(callback);
return {
dispose: () => {
ArrayHelper.remove(this.callbacks, a => a === callback);
}
};
}
registerPreNavigationHook(callback) {
var _a;
((_a = this.beforeCallbacks) !== null && _a !== void 0 ? _a : this.beforeCallbacks = []).push(callback);
return {
dispose: () => {
this.beforeCallbacks.remove(callback);
}
};
}
});
}
};
});
//# sourceMappingURL=NavigationService.js.map