forked from microsoft/fast
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbinding.ts
More file actions
370 lines (314 loc) · 10.9 KB
/
binding.ts
File metadata and controls
370 lines (314 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import { DOM } from "../dom.js";
import type { Behavior } from "../observation/behavior.js";
import {
Binding,
BindingObserver,
ExecutionContext,
Observable,
} from "../observation/observable.js";
import { TargetedHTMLDirective } from "./html-directive.js";
import type { SyntheticView } from "./view.js";
function normalBind(
this: BindingBehavior,
source: unknown,
context: ExecutionContext
): void {
this.source = source;
this.context = context;
if (this.bindingObserver === null) {
this.bindingObserver = Observable.binding(
this.binding,
this,
this.isBindingVolatile
);
}
this.updateTarget(this.bindingObserver!.observe(source, context));
}
function triggerBind(
this: BindingBehavior,
source: unknown,
context: ExecutionContext
): void {
this.source = source;
this.context = context;
this.target.addEventListener(this.targetName!, this);
}
function normalUnbind(this: BindingBehavior): void {
this.bindingObserver!.disconnect();
this.source = null;
this.context = null;
}
type ComposableView = SyntheticView & {
isComposed?: boolean;
needsBindOnly?: boolean;
};
function contentUnbind(this: BindingBehavior): void {
this.bindingObserver!.disconnect();
this.source = null;
this.context = null;
const view = this.target.$fastView as ComposableView;
if (view !== void 0 && view.isComposed) {
view.unbind();
view.needsBindOnly = true;
}
}
function triggerUnbind(this: BindingBehavior): void {
this.target.removeEventListener(this.targetName!, this);
this.source = null;
this.context = null;
}
function updateAttributeTarget(this: BindingBehavior, value: unknown): void {
DOM.setAttribute(this.target, this.targetName!, value);
}
function updateBooleanAttributeTarget(this: BindingBehavior, value: unknown): void {
DOM.setBooleanAttribute(this.target, this.targetName!, value as boolean);
}
function updateContentTarget(this: BindingBehavior, value: any): void {
// If there's no actual value, then this equates to the
// empty string for the purposes of content bindings.
if (value === null || value === undefined) {
value = "";
}
// If the value has a "create" method, then it's a template-like.
if (value.create) {
this.target.textContent = "";
let view = this.target.$fastView as ComposableView;
// If there's no previous view that we might be able to
// reuse then create a new view from the template.
if (view === void 0) {
view = value.create() as SyntheticView;
} else {
// If there is a previous view, but it wasn't created
// from the same template as the new value, then we
// need to remove the old view if it's still in the DOM
// and create a new view from the template.
if (this.target.$fastTemplate !== value) {
if (view.isComposed) {
view.remove();
view.unbind();
}
view = value.create() as SyntheticView;
}
}
// It's possible that the value is the same as the previous template
// and that there's actually no need to compose it.
if (!view.isComposed) {
view.isComposed = true;
view.bind(this.source, this.context!);
view.insertBefore(this.target);
this.target.$fastView = view;
this.target.$fastTemplate = value;
} else if (view.needsBindOnly) {
view.needsBindOnly = false;
view.bind(this.source, this.context!);
}
} else {
const view = this.target.$fastView as ComposableView;
// If there is a view and it's currently composed into
// the DOM, then we need to remove it.
if (view !== void 0 && view.isComposed) {
view.isComposed = false;
view.remove();
if (view.needsBindOnly) {
view.needsBindOnly = false;
} else {
view.unbind();
}
}
this.target.textContent = value;
}
}
function updatePropertyTarget(this: BindingBehavior, value: unknown): void {
this.target[this.targetName!] = value;
}
function updateClassTarget(this: BindingBehavior, value: string): void {
const classVersions = this.classVersions || Object.create(null);
const target = this.target;
let version = this.version || 0;
// Add the classes, tracking the version at which they were added.
if (value !== null && value !== undefined && value.length) {
const names = value.split(/\s+/);
for (let i = 0, ii = names.length; i < ii; ++i) {
const currentName = names[i];
if (currentName === "") {
continue;
}
classVersions[currentName] = version;
target.classList.add(currentName);
}
}
this.classVersions = classVersions;
this.version = version + 1;
// If this is the first call to add classes, there's no need to remove old ones.
if (version === 0) {
return;
}
// Remove classes from the previous version.
version -= 1;
for (const name in classVersions) {
if (classVersions[name] === version) {
target.classList.remove(name);
}
}
}
/**
* A directive that configures data binding to element content and attributes.
* @public
*/
export class HTMLBindingDirective extends TargetedHTMLDirective {
private cleanedTargetName?: string;
private originalTargetName?: string;
private bind: typeof normalBind = normalBind;
private unbind: typeof normalUnbind = normalUnbind;
private updateTarget: typeof updateAttributeTarget = updateAttributeTarget;
private isBindingVolatile: boolean;
/**
* Creates an instance of BindingDirective.
* @param binding - A binding that returns the data used to update the DOM.
*/
public constructor(public binding: Binding, isVolatile?: boolean) {
super();
this.isBindingVolatile = isVolatile ?? Observable.isVolatileBinding(this.binding);
}
/**
* Gets/sets the name of the attribute or property that this
* binding is targeting.
*/
public get targetName(): string | undefined {
return this.originalTargetName;
}
public set targetName(value: string | undefined) {
this.originalTargetName = value;
if (value === void 0) {
return;
}
switch (value[0]) {
case ":":
this.cleanedTargetName = value.substr(1);
this.updateTarget = updatePropertyTarget;
if (this.cleanedTargetName === "innerHTML") {
const binding = this.binding;
this.binding = (s, c) => DOM.createHTML(binding(s, c));
}
break;
case "?":
this.cleanedTargetName = value.substr(1);
this.updateTarget = updateBooleanAttributeTarget;
break;
case "@":
this.cleanedTargetName = value.substr(1);
this.bind = triggerBind;
this.unbind = triggerUnbind;
break;
default:
this.cleanedTargetName = value;
if (value === "class") {
this.updateTarget = updateClassTarget;
}
break;
}
}
/**
* Makes this binding target the content of an element rather than
* a particular attribute or property.
*/
public targetAtContent(): void {
this.updateTarget = updateContentTarget;
this.unbind = contentUnbind;
}
/**
* Creates the runtime BindingBehavior instance based on the configuration
* information stored in the BindingDirective.
* @param target - The target node that the binding behavior should attach to.
*/
createBehavior(target: Node): BindingBehavior {
/* eslint-disable-next-line @typescript-eslint/no-use-before-define */
return new BindingBehavior(
target,
this.binding,
this.isBindingVolatile,
this.bind,
this.unbind,
this.updateTarget,
this.cleanedTargetName
);
}
}
/**
* A behavior that updates content and attributes based on a configured
* BindingDirective.
* @public
*/
export class BindingBehavior implements Behavior {
/** @internal */
public source: unknown = null;
/** @internal */
public context: ExecutionContext | null = null;
/** @internal */
public bindingObserver: BindingObserver | null = null;
/** @internal */
public classVersions: Record<string, number>;
/** @internal */
public version: number;
/** @internal */
public target: any;
/** @internal */
public binding: Binding;
/** @internal */
public isBindingVolatile: boolean;
/** @internal */
public updateTarget: typeof updatePropertyTarget;
/** @internal */
public targetName?: string;
/**
* Bind this behavior to the source.
* @param source - The source to bind to.
* @param context - The execution context that the binding is operating within.
*/
public bind: typeof normalBind;
/**
* Unbinds this behavior from the source.
* @param source - The source to unbind from.
*/
public unbind: typeof normalUnbind;
/**
* Creates an instance of BindingBehavior.
* @param target - The target of the data updates.
* @param binding - The binding that returns the latest value for an update.
* @param isBindingVolatile - Indicates whether the binding has volatile dependencies.
* @param bind - The operation to perform during binding.
* @param unbind - The operation to perform during unbinding.
* @param updateTarget - The operation to perform when updating.
* @param targetName - The name of the target attribute or property to update.
*/
public constructor(
target: any,
binding: Binding,
isBindingVolatile: boolean,
bind: typeof normalBind,
unbind: typeof normalUnbind,
updateTarget: typeof updatePropertyTarget,
targetName?: string
) {
this.target = target;
this.binding = binding;
this.isBindingVolatile = isBindingVolatile;
this.bind = bind;
this.unbind = unbind;
this.updateTarget = updateTarget;
this.targetName = targetName;
}
/** @internal */
public handleChange(): void {
this.updateTarget(this.bindingObserver!.observe(this.source, this.context!));
}
/** @internal */
public handleEvent(event: Event): void {
ExecutionContext.setEvent(event);
const result = this.binding(this.source, this.context!);
ExecutionContext.setEvent(null);
if (result !== true) {
event.preventDefault();
}
}
}