-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
bindings.ts
132 lines (110 loc) · 3.61 KB
/
bindings.ts
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
import { get } from '@ember/-internals/metal';
import { assert } from '@ember/debug';
import { dasherize } from '@ember/string';
import { ElementOperations } from '@glimmer/interfaces';
import {
childRefFor,
childRefFromParts,
createComputeRef,
createPrimitiveRef,
Reference,
valueForRef,
} from '@glimmer/reference';
import { Component } from './curly-component-state-bucket';
function referenceForParts(rootRef: Reference<Component>, parts: string[]): Reference {
let isAttrs = parts[0] === 'attrs';
// TODO deprecate this
if (isAttrs) {
parts.shift();
if (parts.length === 1) {
return childRefFor(rootRef, parts[0]);
}
}
return childRefFromParts(rootRef, parts);
}
export function parseAttributeBinding(microsyntax: string): [string, string, boolean] {
let colonIndex = microsyntax.indexOf(':');
if (colonIndex === -1) {
assert(
'You cannot use class as an attributeBinding, use classNameBindings instead.',
microsyntax !== 'class'
);
return [microsyntax, microsyntax, true];
} else {
let prop = microsyntax.substring(0, colonIndex);
let attribute = microsyntax.substring(colonIndex + 1);
assert(
'You cannot use class as an attributeBinding, use classNameBindings instead.',
attribute !== 'class'
);
return [prop, attribute, false];
}
}
export function installAttributeBinding(
component: Component,
rootRef: Reference<Component>,
parsed: [string, string, boolean],
operations: ElementOperations
) {
let [prop, attribute, isSimple] = parsed;
if (attribute === 'id') {
let elementId = get(component, prop);
if (elementId === undefined || elementId === null) {
elementId = component.elementId;
}
elementId = createPrimitiveRef(elementId);
operations.setAttribute('id', elementId, true, null);
return;
}
let isPath = prop.indexOf('.') > -1;
let reference = isPath ? referenceForParts(rootRef, prop.split('.')) : childRefFor(rootRef, prop);
assert(
`Illegal attributeBinding: '${prop}' is not a valid attribute name.`,
!(isSimple && isPath)
);
operations.setAttribute(attribute, reference, false, null);
}
export function createClassNameBindingRef(
rootRef: Reference<Component>,
microsyntax: string,
operations: ElementOperations
) {
let [prop, truthy, falsy] = microsyntax.split(':');
let isStatic = prop === '';
if (isStatic) {
operations.setAttribute('class', createPrimitiveRef(truthy), true, null);
} else {
let isPath = prop.indexOf('.') > -1;
let parts = isPath ? prop.split('.') : [];
let value = isPath ? referenceForParts(rootRef, parts) : childRefFor(rootRef, prop);
let ref;
if (truthy === undefined) {
ref = createSimpleClassNameBindingRef(value, isPath ? parts[parts.length - 1] : prop);
} else {
ref = createColonClassNameBindingRef(value, truthy, falsy);
}
operations.setAttribute('class', ref, false, null);
}
}
export function createSimpleClassNameBindingRef(inner: Reference, path?: string) {
let dasherizedPath: string;
return createComputeRef(() => {
let value = valueForRef(inner);
if (value === true) {
assert(
'You must pass a path when binding a to a class name using classNameBindings',
path !== undefined
);
return dasherizedPath || (dasherizedPath = dasherize(path!));
} else if (value || value === 0) {
return String(value);
} else {
return null;
}
});
}
export function createColonClassNameBindingRef(inner: Reference, truthy: string, falsy: string) {
return createComputeRef(() => {
return valueForRef(inner) ? truthy : falsy;
});
}