@thomas-siegfried/tapout 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +1205 -1205
- package/package.json +55 -55
- package/src/applyBindings.ts +372 -372
- package/src/arrayToDomMapping.ts +300 -300
- package/src/attributeInterpolationMarkup.ts +91 -91
- package/src/bindingContext.ts +207 -207
- package/src/bindingEvent.ts +198 -198
- package/src/bindingProvider.ts +260 -260
- package/src/bindings.ts +963 -963
- package/src/compareArrays.ts +122 -122
- package/src/componentBinding.ts +318 -318
- package/src/componentDecorator.ts +62 -62
- package/src/components.ts +367 -367
- package/src/computed.ts +439 -439
- package/src/configure.ts +52 -52
- package/src/controlFlowBindings.ts +206 -206
- package/src/core.ts +60 -60
- package/src/decorators.ts +407 -407
- package/src/dependencyDetection.ts +76 -76
- package/src/disposable.ts +36 -36
- package/src/domData.ts +42 -42
- package/src/domNodeDisposal.ts +94 -94
- package/src/effects.ts +29 -29
- package/src/event.ts +173 -173
- package/src/expressionRewriting.ts +219 -219
- package/src/extenders.ts +102 -102
- package/src/filters.ts +91 -91
- package/src/index.ts +150 -150
- package/src/interpolationMarkup.ts +130 -130
- package/src/memoization.ts +71 -71
- package/src/namespacedBindings.ts +132 -132
- package/src/observable.ts +48 -48
- package/src/observableArray.ts +397 -397
- package/src/options.ts +25 -25
- package/src/selectExtensions.ts +68 -68
- package/src/slotBinding.ts +84 -84
- package/src/subscribable.ts +266 -266
- package/src/tasks.ts +79 -79
- package/src/templateEngine.ts +108 -108
- package/src/templateRendering.ts +399 -399
- package/src/templateRewriting.ts +94 -94
- package/src/templateSources.ts +134 -134
- package/src/utils.ts +123 -123
- package/src/utilsDom.ts +87 -87
- package/src/virtualElements.ts +153 -153
- package/src/wireParams.ts +49 -49
|
@@ -1,91 +1,91 @@
|
|
|
1
|
-
import { addNodePreprocessor, getBindingHandler } from './bindingProvider.js';
|
|
2
|
-
import type { NodePreprocessFn } from './bindingProvider.js';
|
|
3
|
-
import { options } from './options.js';
|
|
4
|
-
import { parseInterpolationMarkup } from './interpolationMarkup.js';
|
|
5
|
-
|
|
6
|
-
const DATA_BIND_ATTR = 'data-bind';
|
|
7
|
-
|
|
8
|
-
// Determines the binding expression for an attribute.
|
|
9
|
-
// If a binding handler exists for the attribute name, use it directly.
|
|
10
|
-
// Otherwise, fall back to `attr.name` (namespaced binding syntax).
|
|
11
|
-
export function attributeBinding(
|
|
12
|
-
name: string,
|
|
13
|
-
value: string,
|
|
14
|
-
_node: Element,
|
|
15
|
-
): string {
|
|
16
|
-
if (getBindingHandler(name)) {
|
|
17
|
-
return name + ':' + value;
|
|
18
|
-
} else {
|
|
19
|
-
return 'attr.' + name + ':' + value;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
// Customizable hook — override to change how attribute names map to bindings.
|
|
24
|
-
let _attributeBindingFn = attributeBinding;
|
|
25
|
-
|
|
26
|
-
export function setAttributeBinding(
|
|
27
|
-
fn: (name: string, value: string, node: Element) => string,
|
|
28
|
-
): void {
|
|
29
|
-
_attributeBindingFn = fn;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export const attributeInterpolationMarkupPreprocessor: NodePreprocessFn = function (node: Node): void {
|
|
33
|
-
if (node.nodeType !== 1 || !(node as Element).attributes?.length) return;
|
|
34
|
-
|
|
35
|
-
const element = node as Element;
|
|
36
|
-
let dataBindAttribute = element.getAttribute(DATA_BIND_ATTR);
|
|
37
|
-
|
|
38
|
-
const attrs: Attr[] = [];
|
|
39
|
-
for (let i = 0; i < element.attributes.length; i++) {
|
|
40
|
-
attrs.push(element.attributes[i]);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
for (const attr of attrs) {
|
|
44
|
-
if (
|
|
45
|
-
attr.specified !== false &&
|
|
46
|
-
attr.name !== DATA_BIND_ATTR &&
|
|
47
|
-
attr.value.indexOf('{{') !== -1
|
|
48
|
-
) {
|
|
49
|
-
const parts: string[] = [];
|
|
50
|
-
let attrValue = '';
|
|
51
|
-
|
|
52
|
-
function addText(text: string): void {
|
|
53
|
-
if (text) {
|
|
54
|
-
parts.push('"' + text.replace(/"/g, '\\"') + '"');
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function addExpr(expressionText: string): void {
|
|
59
|
-
if (expressionText) {
|
|
60
|
-
attrValue = expressionText;
|
|
61
|
-
parts.push('$unwrap(' + expressionText + ')');
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
parseInterpolationMarkup(attr.value, addText, addExpr);
|
|
66
|
-
|
|
67
|
-
if (parts.length > 1) {
|
|
68
|
-
attrValue = '""+' + parts.join('+');
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
if (attrValue) {
|
|
72
|
-
const attrName = attr.name.toLowerCase();
|
|
73
|
-
const binding = _attributeBindingFn(attrName, attrValue, element);
|
|
74
|
-
|
|
75
|
-
if (!dataBindAttribute) {
|
|
76
|
-
dataBindAttribute = binding;
|
|
77
|
-
} else {
|
|
78
|
-
dataBindAttribute += ',' + binding;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
element.setAttribute(DATA_BIND_ATTR, dataBindAttribute);
|
|
82
|
-
element.removeAttribute(attr.name);
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
export function enableAttributeInterpolationMarkup(): void {
|
|
89
|
-
options.attributeInterpolation = true;
|
|
90
|
-
addNodePreprocessor(attributeInterpolationMarkupPreprocessor);
|
|
91
|
-
}
|
|
1
|
+
import { addNodePreprocessor, getBindingHandler } from './bindingProvider.js';
|
|
2
|
+
import type { NodePreprocessFn } from './bindingProvider.js';
|
|
3
|
+
import { options } from './options.js';
|
|
4
|
+
import { parseInterpolationMarkup } from './interpolationMarkup.js';
|
|
5
|
+
|
|
6
|
+
const DATA_BIND_ATTR = 'data-bind';
|
|
7
|
+
|
|
8
|
+
// Determines the binding expression for an attribute.
|
|
9
|
+
// If a binding handler exists for the attribute name, use it directly.
|
|
10
|
+
// Otherwise, fall back to `attr.name` (namespaced binding syntax).
|
|
11
|
+
export function attributeBinding(
|
|
12
|
+
name: string,
|
|
13
|
+
value: string,
|
|
14
|
+
_node: Element,
|
|
15
|
+
): string {
|
|
16
|
+
if (getBindingHandler(name)) {
|
|
17
|
+
return name + ':' + value;
|
|
18
|
+
} else {
|
|
19
|
+
return 'attr.' + name + ':' + value;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// Customizable hook — override to change how attribute names map to bindings.
|
|
24
|
+
let _attributeBindingFn = attributeBinding;
|
|
25
|
+
|
|
26
|
+
export function setAttributeBinding(
|
|
27
|
+
fn: (name: string, value: string, node: Element) => string,
|
|
28
|
+
): void {
|
|
29
|
+
_attributeBindingFn = fn;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export const attributeInterpolationMarkupPreprocessor: NodePreprocessFn = function (node: Node): void {
|
|
33
|
+
if (node.nodeType !== 1 || !(node as Element).attributes?.length) return;
|
|
34
|
+
|
|
35
|
+
const element = node as Element;
|
|
36
|
+
let dataBindAttribute = element.getAttribute(DATA_BIND_ATTR);
|
|
37
|
+
|
|
38
|
+
const attrs: Attr[] = [];
|
|
39
|
+
for (let i = 0; i < element.attributes.length; i++) {
|
|
40
|
+
attrs.push(element.attributes[i]);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
for (const attr of attrs) {
|
|
44
|
+
if (
|
|
45
|
+
attr.specified !== false &&
|
|
46
|
+
attr.name !== DATA_BIND_ATTR &&
|
|
47
|
+
attr.value.indexOf('{{') !== -1
|
|
48
|
+
) {
|
|
49
|
+
const parts: string[] = [];
|
|
50
|
+
let attrValue = '';
|
|
51
|
+
|
|
52
|
+
function addText(text: string): void {
|
|
53
|
+
if (text) {
|
|
54
|
+
parts.push('"' + text.replace(/"/g, '\\"') + '"');
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function addExpr(expressionText: string): void {
|
|
59
|
+
if (expressionText) {
|
|
60
|
+
attrValue = expressionText;
|
|
61
|
+
parts.push('$unwrap(' + expressionText + ')');
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
parseInterpolationMarkup(attr.value, addText, addExpr);
|
|
66
|
+
|
|
67
|
+
if (parts.length > 1) {
|
|
68
|
+
attrValue = '""+' + parts.join('+');
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (attrValue) {
|
|
72
|
+
const attrName = attr.name.toLowerCase();
|
|
73
|
+
const binding = _attributeBindingFn(attrName, attrValue, element);
|
|
74
|
+
|
|
75
|
+
if (!dataBindAttribute) {
|
|
76
|
+
dataBindAttribute = binding;
|
|
77
|
+
} else {
|
|
78
|
+
dataBindAttribute += ',' + binding;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
element.setAttribute(DATA_BIND_ATTR, dataBindAttribute);
|
|
82
|
+
element.removeAttribute(attr.name);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
export function enableAttributeInterpolationMarkup(): void {
|
|
89
|
+
options.attributeInterpolation = true;
|
|
90
|
+
addNodePreprocessor(attributeInterpolationMarkupPreprocessor);
|
|
91
|
+
}
|
package/src/bindingContext.ts
CHANGED
|
@@ -1,207 +1,207 @@
|
|
|
1
|
-
import { PureComputed } from './computed.js';
|
|
2
|
-
import type { Computed } from './computed.js';
|
|
3
|
-
import { isReadableSubscribable } from './subscribable.js';
|
|
4
|
-
import { domDataGet } from './domData.js';
|
|
5
|
-
import { getDependencies } from './dependencyDetection.js';
|
|
6
|
-
|
|
7
|
-
function unwrapObservable(value: unknown): unknown {
|
|
8
|
-
return isReadableSubscribable(value) ? value.get() : value;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
const SUBSCRIBABLE = Symbol('subscribable');
|
|
12
|
-
const ANCESTOR_BINDING_INFO = Symbol('ancestorBindingInfo');
|
|
13
|
-
const DATA_DEPENDENCY = Symbol('dataDependency');
|
|
14
|
-
const INHERIT = Symbol('inheritParentVm');
|
|
15
|
-
|
|
16
|
-
export { SUBSCRIBABLE, ANCESTOR_BINDING_INFO, DATA_DEPENDENCY };
|
|
17
|
-
|
|
18
|
-
export interface BindingContextOptions {
|
|
19
|
-
exportDependencies?: boolean;
|
|
20
|
-
dataDependency?: Computed<unknown>;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export interface CreateChildContextOptions {
|
|
24
|
-
as?: string;
|
|
25
|
-
extend?: (context: BindingContext) => void;
|
|
26
|
-
noChildContext?: boolean;
|
|
27
|
-
exportDependencies?: boolean;
|
|
28
|
-
dataDependency?: Computed<unknown>;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export type ExtendCallback = (
|
|
32
|
-
self: BindingContext,
|
|
33
|
-
parentContext: BindingContext | undefined,
|
|
34
|
-
dataItem: unknown,
|
|
35
|
-
) => void;
|
|
36
|
-
|
|
37
|
-
export class BindingContext {
|
|
38
|
-
$data: unknown;
|
|
39
|
-
$rawData: unknown;
|
|
40
|
-
$root: unknown;
|
|
41
|
-
$parent?: unknown;
|
|
42
|
-
$parentContext?: BindingContext;
|
|
43
|
-
$parents: unknown[] = [];
|
|
44
|
-
|
|
45
|
-
[SUBSCRIBABLE]?: PureComputed<unknown>;
|
|
46
|
-
[ANCESTOR_BINDING_INFO]?: unknown;
|
|
47
|
-
[DATA_DEPENDENCY]?: Computed<unknown>;
|
|
48
|
-
|
|
49
|
-
// Allow dynamic alias properties set by createChildContext/extend
|
|
50
|
-
[key: string | symbol]: unknown;
|
|
51
|
-
|
|
52
|
-
constructor(
|
|
53
|
-
dataItemOrAccessor: unknown,
|
|
54
|
-
parentContext?: BindingContext,
|
|
55
|
-
dataItemAlias?: string | null,
|
|
56
|
-
extendCallback?: ExtendCallback,
|
|
57
|
-
options?: BindingContextOptions,
|
|
58
|
-
) {
|
|
59
|
-
const self = this;
|
|
60
|
-
const shouldInheritData = dataItemOrAccessor === INHERIT;
|
|
61
|
-
const realDataItemOrAccessor = shouldInheritData ? undefined : dataItemOrAccessor;
|
|
62
|
-
const isFunc = typeof realDataItemOrAccessor === 'function' && !isReadableSubscribable(realDataItemOrAccessor);
|
|
63
|
-
const dataDependency = options?.dataDependency;
|
|
64
|
-
|
|
65
|
-
function updateContext() {
|
|
66
|
-
const dataItemOrObservable = isFunc
|
|
67
|
-
? (realDataItemOrAccessor as () => unknown)()
|
|
68
|
-
: realDataItemOrAccessor;
|
|
69
|
-
const dataItem = unwrapObservable(dataItemOrObservable);
|
|
70
|
-
|
|
71
|
-
if (parentContext) {
|
|
72
|
-
Object.assign(self, parentContext);
|
|
73
|
-
|
|
74
|
-
if (ANCESTOR_BINDING_INFO in parentContext) {
|
|
75
|
-
self[ANCESTOR_BINDING_INFO] = parentContext[ANCESTOR_BINDING_INFO];
|
|
76
|
-
}
|
|
77
|
-
} else {
|
|
78
|
-
self.$parents = [];
|
|
79
|
-
self.$root = dataItem;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
self[SUBSCRIBABLE] = subscribable;
|
|
83
|
-
|
|
84
|
-
if (shouldInheritData) {
|
|
85
|
-
// $data is already set from parent via Object.assign
|
|
86
|
-
} else {
|
|
87
|
-
self.$rawData = dataItemOrObservable;
|
|
88
|
-
self.$data = dataItem;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
if (dataItemAlias) {
|
|
92
|
-
self[dataItemAlias] = dataItem;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
if (extendCallback) {
|
|
96
|
-
extendCallback(self, parentContext, dataItem);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
// Ensure child depends on parent context's data dependencies.
|
|
100
|
-
// Reading the parent subscribable registers it as a dependency of
|
|
101
|
-
// the current PureComputed. Dependency deduplication makes this safe
|
|
102
|
-
// even if updateContext already transitively depends on it.
|
|
103
|
-
if (parentContext?.[SUBSCRIBABLE]) {
|
|
104
|
-
parentContext[SUBSCRIBABLE]!.get();
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
if (dataDependency) {
|
|
108
|
-
self[DATA_DEPENDENCY] = dataDependency;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
return self.$data;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
let subscribable: PureComputed<unknown> | undefined;
|
|
115
|
-
|
|
116
|
-
if (options?.exportDependencies) {
|
|
117
|
-
updateContext();
|
|
118
|
-
} else {
|
|
119
|
-
subscribable = new PureComputed(updateContext);
|
|
120
|
-
subscribable.peek();
|
|
121
|
-
|
|
122
|
-
if (subscribable.isActive()) {
|
|
123
|
-
subscribable.equalityComparer = undefined;
|
|
124
|
-
} else {
|
|
125
|
-
self[SUBSCRIBABLE] = undefined;
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
createChildContext(
|
|
131
|
-
dataItemOrAccessor: unknown,
|
|
132
|
-
aliasOrOptions?: string | CreateChildContextOptions,
|
|
133
|
-
extendCallback?: (context: BindingContext) => void,
|
|
134
|
-
options?: BindingContextOptions,
|
|
135
|
-
): BindingContext {
|
|
136
|
-
let dataItemAlias: string | undefined;
|
|
137
|
-
|
|
138
|
-
if (!options && aliasOrOptions && typeof aliasOrOptions === 'object') {
|
|
139
|
-
options = aliasOrOptions;
|
|
140
|
-
dataItemAlias = aliasOrOptions.as;
|
|
141
|
-
extendCallback = aliasOrOptions.extend;
|
|
142
|
-
} else if (typeof aliasOrOptions === 'string') {
|
|
143
|
-
dataItemAlias = aliasOrOptions;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
const childOptions = options as CreateChildContextOptions | undefined;
|
|
147
|
-
|
|
148
|
-
if (dataItemAlias && childOptions?.noChildContext) {
|
|
149
|
-
const isFunc = typeof dataItemOrAccessor === 'function' && !isReadableSubscribable(dataItemOrAccessor);
|
|
150
|
-
return new BindingContext(INHERIT, this, null, (self) => {
|
|
151
|
-
if (extendCallback) {
|
|
152
|
-
extendCallback(self);
|
|
153
|
-
}
|
|
154
|
-
self[dataItemAlias!] = isFunc ? (dataItemOrAccessor as () => unknown)() : dataItemOrAccessor;
|
|
155
|
-
}, options);
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
return new BindingContext(dataItemOrAccessor, this, dataItemAlias, (self, parentCtx) => {
|
|
159
|
-
self.$parentContext = parentCtx;
|
|
160
|
-
self.$parent = parentCtx?.$data;
|
|
161
|
-
self.$parents = (parentCtx?.$parents || []).slice(0);
|
|
162
|
-
self.$parents.unshift(self.$parent);
|
|
163
|
-
if (extendCallback) {
|
|
164
|
-
extendCallback(self);
|
|
165
|
-
}
|
|
166
|
-
}, options);
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
extend(
|
|
170
|
-
properties: Record<string, unknown> | ((self: BindingContext) => Record<string, unknown>) | null,
|
|
171
|
-
options?: BindingContextOptions,
|
|
172
|
-
): BindingContext {
|
|
173
|
-
return new BindingContext(INHERIT, this, null, (self) => {
|
|
174
|
-
if (properties) {
|
|
175
|
-
Object.assign(
|
|
176
|
-
self,
|
|
177
|
-
typeof properties === 'function' ? properties(self) : properties,
|
|
178
|
-
);
|
|
179
|
-
}
|
|
180
|
-
}, options);
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
// DOM data key for storing binding info on nodes (shared with applyBindings)
|
|
185
|
-
export const BINDING_INFO_KEY = '__tapout_bindingInfo';
|
|
186
|
-
|
|
187
|
-
interface BindingInfo {
|
|
188
|
-
context?: BindingContext;
|
|
189
|
-
alreadyBound?: boolean;
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
export function storedBindingContextForNode(node: Node): BindingContext | undefined {
|
|
193
|
-
const info = domDataGet(node, BINDING_INFO_KEY) as BindingInfo | undefined;
|
|
194
|
-
return info?.context;
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
export function contextFor(node: Node): BindingContext | undefined {
|
|
198
|
-
if (node && (node.nodeType === 1 || node.nodeType === 8)) {
|
|
199
|
-
return storedBindingContextForNode(node);
|
|
200
|
-
}
|
|
201
|
-
return undefined;
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
export function dataFor(node: Node): unknown {
|
|
205
|
-
const context = contextFor(node);
|
|
206
|
-
return context ? context.$data : undefined;
|
|
207
|
-
}
|
|
1
|
+
import { PureComputed } from './computed.js';
|
|
2
|
+
import type { Computed } from './computed.js';
|
|
3
|
+
import { isReadableSubscribable } from './subscribable.js';
|
|
4
|
+
import { domDataGet } from './domData.js';
|
|
5
|
+
import { getDependencies } from './dependencyDetection.js';
|
|
6
|
+
|
|
7
|
+
function unwrapObservable(value: unknown): unknown {
|
|
8
|
+
return isReadableSubscribable(value) ? value.get() : value;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const SUBSCRIBABLE = Symbol('subscribable');
|
|
12
|
+
const ANCESTOR_BINDING_INFO = Symbol('ancestorBindingInfo');
|
|
13
|
+
const DATA_DEPENDENCY = Symbol('dataDependency');
|
|
14
|
+
const INHERIT = Symbol('inheritParentVm');
|
|
15
|
+
|
|
16
|
+
export { SUBSCRIBABLE, ANCESTOR_BINDING_INFO, DATA_DEPENDENCY };
|
|
17
|
+
|
|
18
|
+
export interface BindingContextOptions {
|
|
19
|
+
exportDependencies?: boolean;
|
|
20
|
+
dataDependency?: Computed<unknown>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface CreateChildContextOptions {
|
|
24
|
+
as?: string;
|
|
25
|
+
extend?: (context: BindingContext) => void;
|
|
26
|
+
noChildContext?: boolean;
|
|
27
|
+
exportDependencies?: boolean;
|
|
28
|
+
dataDependency?: Computed<unknown>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export type ExtendCallback = (
|
|
32
|
+
self: BindingContext,
|
|
33
|
+
parentContext: BindingContext | undefined,
|
|
34
|
+
dataItem: unknown,
|
|
35
|
+
) => void;
|
|
36
|
+
|
|
37
|
+
export class BindingContext {
|
|
38
|
+
$data: unknown;
|
|
39
|
+
$rawData: unknown;
|
|
40
|
+
$root: unknown;
|
|
41
|
+
$parent?: unknown;
|
|
42
|
+
$parentContext?: BindingContext;
|
|
43
|
+
$parents: unknown[] = [];
|
|
44
|
+
|
|
45
|
+
[SUBSCRIBABLE]?: PureComputed<unknown>;
|
|
46
|
+
[ANCESTOR_BINDING_INFO]?: unknown;
|
|
47
|
+
[DATA_DEPENDENCY]?: Computed<unknown>;
|
|
48
|
+
|
|
49
|
+
// Allow dynamic alias properties set by createChildContext/extend
|
|
50
|
+
[key: string | symbol]: unknown;
|
|
51
|
+
|
|
52
|
+
constructor(
|
|
53
|
+
dataItemOrAccessor: unknown,
|
|
54
|
+
parentContext?: BindingContext,
|
|
55
|
+
dataItemAlias?: string | null,
|
|
56
|
+
extendCallback?: ExtendCallback,
|
|
57
|
+
options?: BindingContextOptions,
|
|
58
|
+
) {
|
|
59
|
+
const self = this;
|
|
60
|
+
const shouldInheritData = dataItemOrAccessor === INHERIT;
|
|
61
|
+
const realDataItemOrAccessor = shouldInheritData ? undefined : dataItemOrAccessor;
|
|
62
|
+
const isFunc = typeof realDataItemOrAccessor === 'function' && !isReadableSubscribable(realDataItemOrAccessor);
|
|
63
|
+
const dataDependency = options?.dataDependency;
|
|
64
|
+
|
|
65
|
+
function updateContext() {
|
|
66
|
+
const dataItemOrObservable = isFunc
|
|
67
|
+
? (realDataItemOrAccessor as () => unknown)()
|
|
68
|
+
: realDataItemOrAccessor;
|
|
69
|
+
const dataItem = unwrapObservable(dataItemOrObservable);
|
|
70
|
+
|
|
71
|
+
if (parentContext) {
|
|
72
|
+
Object.assign(self, parentContext);
|
|
73
|
+
|
|
74
|
+
if (ANCESTOR_BINDING_INFO in parentContext) {
|
|
75
|
+
self[ANCESTOR_BINDING_INFO] = parentContext[ANCESTOR_BINDING_INFO];
|
|
76
|
+
}
|
|
77
|
+
} else {
|
|
78
|
+
self.$parents = [];
|
|
79
|
+
self.$root = dataItem;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
self[SUBSCRIBABLE] = subscribable;
|
|
83
|
+
|
|
84
|
+
if (shouldInheritData) {
|
|
85
|
+
// $data is already set from parent via Object.assign
|
|
86
|
+
} else {
|
|
87
|
+
self.$rawData = dataItemOrObservable;
|
|
88
|
+
self.$data = dataItem;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (dataItemAlias) {
|
|
92
|
+
self[dataItemAlias] = dataItem;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (extendCallback) {
|
|
96
|
+
extendCallback(self, parentContext, dataItem);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Ensure child depends on parent context's data dependencies.
|
|
100
|
+
// Reading the parent subscribable registers it as a dependency of
|
|
101
|
+
// the current PureComputed. Dependency deduplication makes this safe
|
|
102
|
+
// even if updateContext already transitively depends on it.
|
|
103
|
+
if (parentContext?.[SUBSCRIBABLE]) {
|
|
104
|
+
parentContext[SUBSCRIBABLE]!.get();
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (dataDependency) {
|
|
108
|
+
self[DATA_DEPENDENCY] = dataDependency;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return self.$data;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
let subscribable: PureComputed<unknown> | undefined;
|
|
115
|
+
|
|
116
|
+
if (options?.exportDependencies) {
|
|
117
|
+
updateContext();
|
|
118
|
+
} else {
|
|
119
|
+
subscribable = new PureComputed(updateContext);
|
|
120
|
+
subscribable.peek();
|
|
121
|
+
|
|
122
|
+
if (subscribable.isActive()) {
|
|
123
|
+
subscribable.equalityComparer = undefined;
|
|
124
|
+
} else {
|
|
125
|
+
self[SUBSCRIBABLE] = undefined;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
createChildContext(
|
|
131
|
+
dataItemOrAccessor: unknown,
|
|
132
|
+
aliasOrOptions?: string | CreateChildContextOptions,
|
|
133
|
+
extendCallback?: (context: BindingContext) => void,
|
|
134
|
+
options?: BindingContextOptions,
|
|
135
|
+
): BindingContext {
|
|
136
|
+
let dataItemAlias: string | undefined;
|
|
137
|
+
|
|
138
|
+
if (!options && aliasOrOptions && typeof aliasOrOptions === 'object') {
|
|
139
|
+
options = aliasOrOptions;
|
|
140
|
+
dataItemAlias = aliasOrOptions.as;
|
|
141
|
+
extendCallback = aliasOrOptions.extend;
|
|
142
|
+
} else if (typeof aliasOrOptions === 'string') {
|
|
143
|
+
dataItemAlias = aliasOrOptions;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const childOptions = options as CreateChildContextOptions | undefined;
|
|
147
|
+
|
|
148
|
+
if (dataItemAlias && childOptions?.noChildContext) {
|
|
149
|
+
const isFunc = typeof dataItemOrAccessor === 'function' && !isReadableSubscribable(dataItemOrAccessor);
|
|
150
|
+
return new BindingContext(INHERIT, this, null, (self) => {
|
|
151
|
+
if (extendCallback) {
|
|
152
|
+
extendCallback(self);
|
|
153
|
+
}
|
|
154
|
+
self[dataItemAlias!] = isFunc ? (dataItemOrAccessor as () => unknown)() : dataItemOrAccessor;
|
|
155
|
+
}, options);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return new BindingContext(dataItemOrAccessor, this, dataItemAlias, (self, parentCtx) => {
|
|
159
|
+
self.$parentContext = parentCtx;
|
|
160
|
+
self.$parent = parentCtx?.$data;
|
|
161
|
+
self.$parents = (parentCtx?.$parents || []).slice(0);
|
|
162
|
+
self.$parents.unshift(self.$parent);
|
|
163
|
+
if (extendCallback) {
|
|
164
|
+
extendCallback(self);
|
|
165
|
+
}
|
|
166
|
+
}, options);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
extend(
|
|
170
|
+
properties: Record<string, unknown> | ((self: BindingContext) => Record<string, unknown>) | null,
|
|
171
|
+
options?: BindingContextOptions,
|
|
172
|
+
): BindingContext {
|
|
173
|
+
return new BindingContext(INHERIT, this, null, (self) => {
|
|
174
|
+
if (properties) {
|
|
175
|
+
Object.assign(
|
|
176
|
+
self,
|
|
177
|
+
typeof properties === 'function' ? properties(self) : properties,
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
}, options);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// DOM data key for storing binding info on nodes (shared with applyBindings)
|
|
185
|
+
export const BINDING_INFO_KEY = '__tapout_bindingInfo';
|
|
186
|
+
|
|
187
|
+
interface BindingInfo {
|
|
188
|
+
context?: BindingContext;
|
|
189
|
+
alreadyBound?: boolean;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export function storedBindingContextForNode(node: Node): BindingContext | undefined {
|
|
193
|
+
const info = domDataGet(node, BINDING_INFO_KEY) as BindingInfo | undefined;
|
|
194
|
+
return info?.context;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export function contextFor(node: Node): BindingContext | undefined {
|
|
198
|
+
if (node && (node.nodeType === 1 || node.nodeType === 8)) {
|
|
199
|
+
return storedBindingContextForNode(node);
|
|
200
|
+
}
|
|
201
|
+
return undefined;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export function dataFor(node: Node): unknown {
|
|
205
|
+
const context = contextFor(node);
|
|
206
|
+
return context ? context.$data : undefined;
|
|
207
|
+
}
|