@web-atoms/core
Version:
278 lines (277 loc) • 10.3 kB
JavaScript
System.register(["tslib", "./EventScope", "./Route", "./types"], function (_export, _context) {
"use strict";
var __awaiter, EventScope, Route, CancelToken, PageCommands, Command, Commands, routeSymbol, displayRouteSymbol, id, defaultOrSelf;
_export({
PageCommands: void 0,
Commands: void 0
});
return {
setters: [function (_tslib) {
__awaiter = _tslib.__awaiter;
}, function (_EventScope) {
EventScope = _EventScope.default;
}, function (_Route) {
Route = _Route.default;
}, function (_types) {
CancelToken = _types.CancelToken;
}],
execute: function () {
_export("routeSymbol", routeSymbol = Symbol("routeSymbol"));
_export("displayRouteSymbol", displayRouteSymbol = Symbol("displayRouteSymbol"));
document.body.addEventListener("click", ce => {
let target = ce.target;
let clickCommand;
let commandParameter;
while (target) {
clickCommand = target.getAttribute("data-click-command");
const cp = target.commandParameter;
if (cp) {
commandParameter = cp;
}
if (clickCommand) {
break;
}
target = target.parentElement;
}
if (!clickCommand) {
return;
}
const cmd = Command.registry.get(clickCommand);
if (cmd) {
cmd.eventScope.dispatch(target, commandParameter);
}
});
id = 1;
_export("PageCommands", PageCommands = class PageCommands {});
defaultOrSelf = x => {
var _a;
return (_a = x === null || x === void 0 ? void 0 : x.default) !== null && _a !== void 0 ? _a : x;
};
Command = class Command {
static invokeRoute(route = location.hash.startsWith("#!") ? location.hash.substring(2) : location.href, forceDisplay = false) {
let sp;
if (/^http(s)?\:\/\//i.test(route)) {
const url = new URL(route);
sp = url.searchParams;
route = url.hash.startsWith("#!") ? url.hash.substring(2) : url.pathname;
} else {
if (route.startsWith("#!")) {
route = route.substring(2);
}
}
const index = route.indexOf("?");
if (index !== -1) {
sp = new URLSearchParams(route.substring(index + 1));
route = route.substring(0, index);
} else {
sp !== null && sp !== void 0 ? sp : sp = new URLSearchParams("");
}
for (const iterator of this.routes) {
const params = iterator.route.matches(route, sp);
if (params) {
params[routeSymbol] = route;
params[displayRouteSymbol] = forceDisplay ? sp.size > 0 ? `${route}?${sp.toString()}` : route : "";
iterator.dispatch(params, true);
return iterator;
}
}
}
static create({
name = `command${id++}`,
eventScope = EventScope.create(),
route,
routeQueries,
routeOrder = 0,
routeDefaults,
pageLoader,
pageListener,
openPage,
pushPage,
registerOnClick,
pushPageForResult,
pushPageForResultOrCancel,
listener
}) {
let cmd = new Command(name, eventScope, registerOnClick);
if (route) {
if (this.routePrefix) {
if (!route.startsWith(this.routePrefix)) {
route = this.routePrefix + route;
}
}
cmd = cmd.withRoute(route, routeQueries, routeOrder, routeDefaults);
}
cmd.listener = listener;
if (openPage) {
let pageType;
cmd.listener = ce => __awaiter(this, void 0, void 0, function* () {
var _a;
const p = (_a = ce.detail) !== null && _a !== void 0 ? _a : {};
return p.returnResult ? PageCommands.pushPageForResult(pageType !== null && pageType !== void 0 ? pageType : pageType = defaultOrSelf(yield openPage()), p) : PageCommands.openPage(pageType !== null && pageType !== void 0 ? pageType : pageType = defaultOrSelf(yield openPage()), p);
});
}
if (pushPage) {
let pageType;
cmd.listener = ce => __awaiter(this, void 0, void 0, function* () {
var _a;
const p = (_a = ce.detail) !== null && _a !== void 0 ? _a : {};
return p.returnResult ? PageCommands.pushPageForResult(pageType !== null && pageType !== void 0 ? pageType : pageType = defaultOrSelf(yield pushPage()), p) : PageCommands.pushPage(pageType !== null && pageType !== void 0 ? pageType : pageType = defaultOrSelf(yield pushPage()), p);
});
}
if (pushPageForResult) {
let pageType;
cmd.listener = ce => __awaiter(this, void 0, void 0, function* () {
var _a;
return PageCommands.pushPageForResult(pageType !== null && pageType !== void 0 ? pageType : pageType = defaultOrSelf(yield pushPageForResult()), (_a = ce.detail) !== null && _a !== void 0 ? _a : {});
});
}
if (pushPageForResultOrCancel) {
let pageType;
cmd.listener = ce => __awaiter(this, void 0, void 0, function* () {
var _a;
try {
return yield PageCommands.pushPageForResult(pageType !== null && pageType !== void 0 ? pageType : pageType = defaultOrSelf(yield pushPageForResultOrCancel()), (_a = ce.detail) !== null && _a !== void 0 ? _a : {});
} catch (e) {
if (CancelToken.isCancelled(e)) {
return;
}
console.error(e);
}
});
}
if (pageLoader) {
let pageType;
cmd.listener = ce => __awaiter(this, void 0, void 0, function* () {
try {
pageType !== null && pageType !== void 0 ? pageType : pageType = defaultOrSelf(yield pageLoader());
return pageListener(pageType)(ce);
} catch (e) {
if (CancelToken.isCancelled(e)) {
return;
}
console.error(e);
}
});
}
return cmd;
}
get eventName() {
return this.eventScope.eventType;
}
get route() {
return this.routeObj;
}
constructor(name = `command${id++}`, eventScope = EventScope.create(), registerOnClick = p => ({
"data-click-command": this.name,
"commandParameter": p
})) {
this.name = name;
this.eventScope = eventScope;
this.registerOnClick = registerOnClick;
Command.registry.set(this.name, this);
}
displayRoute(p, absoluteUrl = false) {
let route = Route.encodeUrl(this.routeObj.substitute(p));
if (absoluteUrl) {
if (route.startsWith("#!")) {
route = location.href.split("#")[0] + route;
} else if (route.startsWith("/")) {
route = location.protocol + "//" + location.host + route;
}
}
return route;
}
withRoute(route, queries, order = 0, defaults) {
this.routeObj = Route.create(route, queries, order);
Command.routes.push(this);
Command.routes.sort((a, b) => a.route.order - b.route.order);
this.defaults = defaults;
return this;
}
listen(r, handler = this.listener) {
if (!handler) {
throw new Error("Handler must be specified...");
}
const d = this.eventScope.listen(e => {
const ce = e;
try {
ce.executed = true;
ce.promise = handler(e);
} catch (error) {
ce.promise = Promise.reject(error);
}
r.app.runAsync(() => ce.promise);
});
return r.registerDisposable(d);
}
dispatch(detail, cancelable) {
if (this.route) {
detail = this.updateRoute(detail);
}
this.eventScope.dispatch(document.body, detail, {
cancelable,
bubbles: true
});
}
updateRoute(detail) {
const d = detail !== null && detail !== void 0 ? detail : detail = {};
let r = d[routeSymbol];
if (r === null || r === void 0) {
r = this.route.substitute(d);
d[routeSymbol] = r;
d[displayRouteSymbol] = r;
}
const {
defaults
} = this;
if (defaults) {
for (const key in defaults) {
if (Object.prototype.hasOwnProperty.call(defaults, key)) {
const element = defaults[key];
if (d[key] === void 0) {
d[key] = element;
}
}
}
}
return detail;
}
dispatchAsync(detail, cancelable) {
return __awaiter(this, void 0, void 0, function* () {
if (this.route) {
detail = this.updateRoute(detail);
}
const ce = new CustomEvent(this.eventScope.eventType, {
detail,
cancelable
});
ce.returnResult = true;
window.dispatchEvent(ce);
if (ce.executed) {
const promise = ce.promise;
if (promise) {
return yield promise;
}
}
});
}
};
Command.routePrefix = "";
Command.registry = new Map();
Command.routes = [];
_export("default", Command);
_export("Commands", Commands = class Commands {
static install(control) {
this.app = control.app;
for (const key in this) {
const element = this[key];
if (element instanceof Command) {
element.listen(control);
}
}
}
});
}
};
});
//# sourceMappingURL=Command.js.map