UNPKG

@web-atoms/core

Version:
308 lines (307 loc) • 11 kB
System.register(["tslib", "./ExpressionParser", "./types"], function (_export, _context) { "use strict"; var __awaiter, parsePath, parsePathLists, CancelToken, ignoreValue, Bind, isEvent, bindSymbol; function twoWaysConvert(name, b, control, e, creator) { control.bind(e, name, b.thisPathList || b.pathList, b.eventList || true, null, b.thisPathList ? creator : undefined); } _export("default", void 0); return { setters: [function (_tslib) { __awaiter = _tslib.__awaiter; }, function (_ExpressionParser) { parsePath = _ExpressionParser.parsePath; parsePathLists = _ExpressionParser.parsePathLists; }, function (_types) { CancelToken = _types.CancelToken; ignoreValue = _types.ignoreValue; }], execute: function () { isEvent = /^event/i; _export("bindSymbol", bindSymbol = Symbol("Bind")); _export("default", Bind = class Bind { static forControl() { return Bind; } static forData() { return Bind; } static forViewModel() { return Bind; } static forLocalViewModel() { return Bind; } static presenter(name) { return { [bindSymbol](cn, control, e, creator) { if (typeof name === "function") { name(control); return; } const n = name || cn; let c = control.element; while (c) { if (c.atomControl && c.atomControl[n] !== undefined) { break; } c = c._logicalParent || c.parentElement; } (c && c.atomControl || control)[n] = e; } }; } static event(sourcePath) { return { [bindSymbol](name, control, e) { control.runAfterInit(() => { if (isEvent.test(name)) { name = name.substring(5); if (name.startsWith("-")) { name = name.substring(1).replace(/-([a-z])/g, g => g[1].toUpperCase()); } else { name = name[0].toLowerCase() + name.substring(1); } } control.bindEvent(e, name, e1 => { return sourcePath(control, e1); }); }); } }; } static oneTime(sourcePath, now) { return { [bindSymbol](name, control, e) { control.runAfterInit(() => { control.setLocalValue(e, name, sourcePath(control, e)); }); if (typeof now !== "undefined") { control.setLocalValue(e, name, now); } } }; } static oneTimeAsync(sourcePath, now) { return { [bindSymbol](name, control, e) { control.runAfterInit(() => { control.app.runAsync(() => __awaiter(this, void 0, void 0, function* () { const value = yield sourcePath(control, e, new CancelToken()); control.setLocalValue(e, name, value); })); }); if (typeof now !== "undefined") { control.setLocalValue(e, name, now); } } }; } static source(source, path, now) { const lists = parsePath(path, false).map(x => ["this", ...x]); return { [bindSymbol](name, control, e, creator) { const self = { control, source }; control.bind(e, name, lists, false, () => { return path.call(self, self); }, self); if (typeof now !== "undefined") { control.setLocalValue(e, name, now); } } }; } static oneWayAsync(sourcePath, { watchDelayInMS = 250, default: defaultValue } = {}) { let pathList; let combined; let thisPathList; if (Array.isArray(sourcePath)) { pathList = sourcePath; } else { const lists = parsePathLists(sourcePath); if (lists.combined.length) { combined = lists.combined; } if (lists.pathList.length) { pathList = lists.pathList; } if (lists.thisPath.length) { thisPathList = lists.thisPath; } } if (!(combined || pathList || thisPathList)) { throw new Error(`Failed to setup binding for ${sourcePath}, parsing failed`); } return { [bindSymbol](name, control, e, creator) { let bindingSource; let finalPathList = pathList; if (combined) { bindingSource = { t: creator, x: control }; finalPathList = combined; } else if (thisPathList) { finalPathList = thisPathList; bindingSource = creator; } const asyncState = { token: 0, cancelToken: undefined }; control.bind(e, name, finalPathList, false, () => { var _a; const app = control.app; (_a = asyncState.cancelToken) === null || _a === void 0 ? void 0 : _a.cancel(); asyncState.cancelToken = undefined; asyncState.token = app.setTimeoutAsync(() => __awaiter(this, void 0, void 0, function* () { var _a, _b; if ((_a = asyncState.cancelToken) === null || _a === void 0 ? void 0 : _a.cancelled) { return; } asyncState.token = undefined; (_b = asyncState.cancelToken) === null || _b === void 0 ? void 0 : _b.cancel(); const ct = asyncState.cancelToken = new CancelToken(); const value = yield sourcePath.call(creator, control, e, ct); if (!ct.cancelled) { control.setLocalValue(e, name, value); } }), watchDelayInMS, asyncState.token); return ignoreValue; }, bindingSource); if (typeof defaultValue !== "undefined") { control.setLocalValue(e, name, defaultValue); } } }; } static oneWay(sourcePath, now) { let pathList; let combined; let thisPathList; if (Array.isArray(sourcePath)) { pathList = sourcePath; } else { const lists = parsePathLists(sourcePath); if (lists.combined.length) { combined = lists.combined; } if (lists.pathList.length) { pathList = lists.pathList; } if (lists.thisPath.length) { thisPathList = lists.thisPath; } } if (!(combined || pathList || thisPathList)) { throw new Error(`Failed to setup binding for ${sourcePath}, parsing failed`); } return { [bindSymbol](name, control, e, creator) { if (pathList) { control.bind(e, name, pathList, false, () => { return sourcePath.call(creator, control, e); }); if (typeof now !== "undefined") { control.setLocalValue(e, name, now); } return; } if (combined) { const a = { t: creator, x: control }; control.bind(e, name, combined, false, () => { return sourcePath.call(creator, control, e); }, a); if (typeof now !== "undefined") { control.setLocalValue(e, name, now); } return; } control.bind(e, name, thisPathList, false, () => { return sourcePath.call(creator, control, e); }, creator); if (typeof now !== "undefined") { control.setLocalValue(e, name, now); } } }; } static twoWays(sourcePath, events, converter) { let pathList; let thisPathList; if (Array.isArray(sourcePath)) { pathList = sourcePath; } else { const lists = parsePathLists(sourcePath); if (lists.combined.length) { throw new Error("Cannot have combined binding for two ways"); } if (lists.pathList.length) { pathList = lists.pathList; } if (lists.thisPath.length) { thisPathList = lists.thisPath; } } if (!(thisPathList || pathList)) { throw new Error(`Failed to setup twoWay binding on ${sourcePath}`); } return { [bindSymbol](name, control, e, creator) { control.bind(e, name, thisPathList || pathList, events || true, converter, thisPathList ? creator : undefined); } }; } static sourceTwoWays(source, path, events = ["input", "cut", "paste", "change"]) { const lists = parsePath(path, false).map(x => ["this", ...x]); return { [bindSymbol](name, control, e, creator) { const self = { control, source }; control.bind(e, name, lists, events, lists, self); } }; } static twoWaysImmediate(sourcePath, converter) { return this.twoWays(sourcePath, ["change", "input", "paste", "cut"], converter); } constructor(setupFunction, sourcePath, name, eventList) { this.setupFunction = setupFunction; this.name = name; this.eventList = eventList; this.sourcePath = sourcePath; this[bindSymbol] = true; if (!this.sourcePath) { return; } if (Array.isArray(this.sourcePath)) { this.pathList = this.sourcePath; } else { const lists = parsePathLists(this.sourcePath); if (lists.combined.length) { this.combined = lists.combined; } if (lists.pathList.length) { this.pathList = lists.pathList; } if (lists.thisPath.length) { this.thisPathList = lists.thisPath; } } } }); } }; }); //# sourceMappingURL=Bind.js.map