UNPKG

@web-atoms/core

Version:
289 lines (288 loc) • 10.2 kB
System.register(["tslib", "../App", "../core/Command", "../core/EventScope", "../core/StringHelper", "../core/types", "../services/http/JsonError", "../services/NavigationService", "../web/services/PopupService"], function (_export, _context) { "use strict"; var __awaiter, App, Command, EventScope, StringHelper, CancelToken, errorHandled, JsonError, NavigationService, NotifyType, PopupService, MarkBusySet, onEventHandler; function Action({ onEvent = void 0, onEventTarget = void 0, onEventSetBusy, blockMultipleExecution = true, dispatchEvent, authorize = void 0, defer = void 0, success = null, successTitle = "Done", successMode = "notify", confirm = null, confirmTitle = null, validate = false, validateTitle = null, close = false, notifyDelay = 2000 } = {}) { return (target, key, descriptor) => { const getEventNames = names => { if (names === null || names === void 0) { return; } if (Array.isArray(names)) { return names.map(getEventNames); } if (names instanceof Command) { onEventTarget !== null && onEventTarget !== void 0 ? onEventTarget : onEventTarget = window; return names.eventScope.eventType; } if (names instanceof EventScope) { onEventTarget !== null && onEventTarget !== void 0 ? onEventTarget : onEventTarget = window; return names.eventType; } return names; }; onEvent = getEventNames(onEvent); if ((onEvent === null || onEvent === void 0 ? void 0 : onEvent.length) > 0) { const oldCreate = target.beginEdit; if (oldCreate) { const onEventName = Array.isArray(onEvent) ? onEvent.map(StringHelper.fromHyphenToCamel) : StringHelper.fromHyphenToCamel(onEvent); const busyKey = Symbol.for("isBusy" + key.toString()); target.beginEdit = function () { const result = oldCreate.apply(this, arguments); const c = this; let element = this.element; if (element) { if (onEventTarget) { element = onEventTarget; } const handler = onEventHandler(c, blockMultipleExecution, key, busyKey, onEventSetBusy); if (typeof onEventName === "string") { c.bindEvent(element, onEventName, handler); } else { for (const eventName of onEventName) { c.bindEvent(element, eventName, handler); } } } return result; }; } } const { value } = descriptor; const deferSymbol = defer ? Symbol.for(`${key.toString()}Defer`) : void 0; return { get: function () { const vm = this; const oldMethod = value; const fx = function (...a) { return __awaiter(this, void 0, void 0, function* () { var _a, _b; const vm = this; if (defer) { const previous = vm[deferSymbol]; if (previous !== 0) { if (previous > 0) { clearTimeout(previous); } vm[deferSymbol] = setTimeout(() => { vm[deferSymbol] = 0; const r = vm[key](...a); delete vm[deferSymbol]; return r; }, defer); return; } } const app = vm.app; const ns = app.resolve(NavigationService); try { if (authorize && !App.authorize()) { return; } if (validate) { if (!vm.isValid) { const vMsg = typeof validate === "boolean" ? "Please enter correct information" : validate; yield ns.alert(vMsg, validateTitle || "Error"); return; } } if (confirm) { if (!(yield ns.confirm(confirm, confirmTitle || "Confirm"))) { return; } } let result = oldMethod.apply(vm, a); if (result === null || result === void 0 ? void 0 : result.then) { result = yield result; } if (success) { if (successMode === "notify") { yield ns.notify(success, successTitle, NotifyType.Information, notifyDelay); } else { yield ns.alert(success, successTitle); } } if (close) { (_a = vm.close) === null || _a === void 0 ? void 0 : _a.call(vm, result); } if (dispatchEvent) { const element = (_b = vm.element) !== null && _b !== void 0 ? _b : document.body; if (typeof dispatchEvent !== "string") { dispatchEvent = dispatchEvent.eventType; } element.dispatchEvent(new CustomEvent(dispatchEvent, { detail: result, bubbles: true })); } return result; } catch (e) { if (CancelToken.isCancelled(e)) { throw e; } e[errorHandled] = true; if (/^timeout$/i.test(e.toString().trim())) { console.warn(e); throw e; } if (e instanceof JsonError) { if (e.details) { yield PopupService.alert({ message: e.message, title: "Error", detail: e.details }); throw e; } } yield ns.alert(e, "Error"); throw e; } }); }; Object.defineProperty(vm, key, { value: fx, writable: true, enumerable: false }); return fx; }, configurable: true }; }; } _export({ MarkBusySet: void 0, default: Action }); return { setters: [function (_tslib) { __awaiter = _tslib.__awaiter; }, function (_App) { App = _App.App; }, function (_coreCommand) { Command = _coreCommand.default; }, function (_coreEventScope) { EventScope = _coreEventScope.default; }, function (_coreStringHelper) { StringHelper = _coreStringHelper.StringHelper; }, function (_coreTypes) { CancelToken = _coreTypes.CancelToken; errorHandled = _coreTypes.errorHandled; }, function (_servicesHttpJsonError) { JsonError = _servicesHttpJsonError.default; }, function (_servicesNavigationService) { NavigationService = _servicesNavigationService.NavigationService; NotifyType = _servicesNavigationService.NotifyType; }, function (_webServicesPopupService) { PopupService = _webServicesPopupService.default; }], execute: function () { _export("MarkBusySet", MarkBusySet = class MarkBusySet { static selector(selector) { return new MarkBusySet(function* (target, currentTarget) { let start = target; while (start) { if (start.matches(selector)) { yield start; break; } start = start.parentElement; } }); } constructor(set) { this.set = set; } *find(event) { yield* this.set(event.target, event.currentTarget); } }); MarkBusySet.none = new MarkBusySet(function* () {}); MarkBusySet.target = new MarkBusySet(function* (t, ct) { yield t; }); MarkBusySet.currentTarget = new MarkBusySet(function* (t, ct) { yield ct; }); MarkBusySet.tillCurrentTarget = new MarkBusySet(function* (target, currentTarget) { let start = target; do { yield start; start = start.parentElement; } while (start !== currentTarget); yield currentTarget; }); MarkBusySet.button = new MarkBusySet(function* (target, currentTarget) { let start = target; while (start) { if (start.tagName === "BUTTON") { yield start; break; } start = start.parentElement; } }); MarkBusySet.buttonOrAnchor = new MarkBusySet(function* (target, currentTarget) { let start = target; while (start) { if (start.tagName === "BUTTON" || start.tagName === "A") { yield start; break; } start = start.parentElement; } }); MarkBusySet.allAncestors = new MarkBusySet(function* (target, currentTarget) { do { yield target; target = target.parentElement; } while (target); }); onEventHandler = (owner, blockMultipleExecution, key, busyKey, onEventSetBusy) => ce => __awaiter(void 0, void 0, void 0, function* () { const element = ce.currentTarget; if (owner[busyKey]) { if (blockMultipleExecution) { return; } } owner[busyKey] = true; try { if (onEventSetBusy) { for (const iterator of onEventSetBusy.find(ce)) { iterator.setAttribute("data-busy", "true"); } } const detail = ce.detail; return yield owner[key](detail, ce); } finally { delete owner[busyKey]; if (onEventSetBusy) { for (const iterator of onEventSetBusy.find(ce)) { iterator.removeAttribute("data-busy"); } } } }); } }; }); //# sourceMappingURL=Action.js.map