-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
bindings.ts
205 lines (165 loc) · 5.52 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
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
import { Opaque, Option } from '@glimmer/interfaces';
import {
CachedReference,
combine,
map,
Reference,
referenceFromParts,
Tag,
} from '@glimmer/reference';
import {
Ops,
} from '@glimmer/wire-format';
import { assert } from 'ember-debug';
import { get } from 'ember-metal';
import { String as StringUtils } from 'ember-runtime';
import { ROOT_REF } from '../component';
import { htmlSafe, isHTMLSafe, SafeString } from './string';
function referenceForKey(component, key) {
return component[ROOT_REF].get(key);
}
function referenceForParts(component, parts) {
let isAttrs = parts[0] === 'attrs';
// TODO deprecate this
if (isAttrs) {
parts.shift();
if (parts.length === 1) {
return referenceForKey(component, parts[0]);
}
}
return referenceFromParts(component[ROOT_REF], parts);
}
// TODO we should probably do this transform at build time
export function wrapComponentClassAttribute(hash) {
if (!hash) {
return hash;
}
let [ keys, values ] = hash;
let index = keys.indexOf('class');
if (index !== -1) {
let [ type ] = values[index];
if (type === Ops.Get) {
let getExp = values[index];
let path = getExp[2];
let propName = path[path.length - 1];
hash[1][index] = [Ops.Helper, ['-class'], [getExp, propName]];
}
}
return hash;
}
export const AttributeBinding = {
parse(microsyntax) {
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];
}
},
install(element, component, parsed, operations) {
let [prop, attribute, isSimple] = parsed;
if (attribute === 'id') {
let elementId = get(component, prop);
if (elementId === undefined || elementId === null) {
elementId = component.elementId;
}
operations.addStaticAttribute(element, 'id', elementId);
return;
}
let isPath = prop.indexOf('.') > -1;
let reference = isPath ? referenceForParts(component, prop.split('.')) : referenceForKey(component, prop);
assert(`Illegal attributeBinding: '${prop}' is not a valid attribute name.`, !(isSimple && isPath));
if (attribute === 'style') {
reference = new StyleBindingReference(reference, referenceForKey(component, 'isVisible'));
}
operations.addDynamicAttribute(element, attribute, reference);
},
};
const DISPLAY_NONE = 'display: none;';
const SAFE_DISPLAY_NONE = htmlSafe(DISPLAY_NONE);
class StyleBindingReference extends CachedReference<string | SafeString> {
public tag: Tag;
constructor(private inner: Reference<string>, private isVisible: Reference<Opaque>) {
super();
this.tag = combine([inner.tag, isVisible.tag]);
}
compute(): string | SafeString {
let value = this.inner.value();
let isVisible = this.isVisible.value();
if (isVisible !== false) {
return value;
} else if (!value) {
return SAFE_DISPLAY_NONE;
} else {
let style = value + ' ' + DISPLAY_NONE;
return isHTMLSafe(value) ? htmlSafe(style) : style;
}
}
}
export const IsVisibleBinding = {
install(element, component, operations) {
operations.addDynamicAttribute(element, 'style', map(referenceForKey(component, 'isVisible'), this.mapStyleValue));
},
mapStyleValue(isVisible) {
return isVisible === false ? SAFE_DISPLAY_NONE : null;
},
};
export const ClassNameBinding = {
install(element, component, microsyntax, operations) {
let [ prop, truthy, falsy ] = microsyntax.split(':');
let isStatic = prop === '';
if (isStatic) {
operations.addStaticAttribute(element, 'class', truthy);
} else {
let isPath = prop.indexOf('.') > -1;
let parts = isPath && prop.split('.');
let value = isPath ? referenceForParts(component, parts) : referenceForKey(component, prop);
let ref;
if (truthy === undefined) {
ref = new SimpleClassNameBindingReference(value, isPath ? parts[parts.length - 1] : prop);
} else {
ref = new ColonClassNameBindingReference(value, truthy, falsy);
}
operations.addDynamicAttribute(element, 'class', ref);
}
},
};
class SimpleClassNameBindingReference extends CachedReference<Option<string>> {
public tag: Tag;
private dasherizedPath: Option<string>;
constructor(private inner: Reference<Opaque | number>, private path: string) {
super();
this.tag = inner.tag;
this.inner = inner;
this.path = path;
this.dasherizedPath = null;
}
compute(): Option<string> {
let value = this.inner.value();
if (value === true) {
let { path, dasherizedPath } = this;
return dasherizedPath || (this.dasherizedPath = StringUtils.dasherize(path));
} else if (value || value === 0) {
return String(value);
} else {
return null;
}
}
}
class ColonClassNameBindingReference extends CachedReference<Option<string>> {
public tag: Tag;
constructor(private inner: Reference<Opaque>,
private truthy: Option<string> = null,
private falsy: Option<string> = null) {
super();
this.tag = inner.tag;
}
compute(): Option<string> {
let { inner, truthy, falsy } = this;
return inner.value() ? truthy : falsy;
}
}