-
Notifications
You must be signed in to change notification settings - Fork 9
/
ampersand-dom-bindings.js
274 lines (257 loc) · 9.94 KB
/
ampersand-dom-bindings.js
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
/*$AMPERSAND_VERSION*/
var Store = require('key-tree-store');
var dom = require('ampersand-dom');
var matchesSelector = require('matches-selector');
var partial = require('lodash/partial');
var slice = Array.prototype.slice;
function getMatches(el, selector, firstOnly) {
if (selector === '') return [el];
var matches = [];
if (!selector) return matches;
if (firstOnly) {
if (matchesSelector(el, selector)) return [el];
return el.querySelector(selector) ? [el.querySelector(selector)] : [];
} else {
if (matchesSelector(el, selector)) matches.push(el);
return matches.concat(slice.call(el.querySelectorAll(selector)));
}
}
function setAttributes(el, attrs) {
for (var name in attrs) {
dom.setAttribute(el, name, attrs[name]);
}
}
function removeAttributes(el, attrs) {
for (var name in attrs) {
dom.removeAttribute(el, name);
}
}
function makeArray(val) {
return Array.isArray(val) ? val : [val];
}
function switchHandler(binding, el, value) {
// the element selector to show
var showValue = binding.cases[value];
var firstMatchOnly = binding.firstMatchOnly;
// hide all the other elements with a different value
for (var item in binding.cases) {
var curValue = binding.cases[item];
if (value !== item && curValue !== showValue) {
getMatches(el, curValue, firstMatchOnly).forEach(function (match) {
dom.hide(match);
});
}
}
getMatches(el, showValue, firstMatchOnly).forEach(function (match) {
dom.show(match);
});
}
function getSelector(binding) {
if (typeof binding.selector === 'string') {
return binding.selector;
} else if (binding.hook) {
return '[data-hook~="' + binding.hook + '"]';
} else {
return '';
}
}
function getBindingFunc(binding, context) {
var type = binding.type || 'text';
var isCustomBinding = typeof type === 'function';
var selector = getSelector(binding);
var firstMatchOnly = binding.firstMatchOnly;
var yes = binding.yes;
var no = binding.no;
var hasYesNo = !!(yes || no);
// storage variable for previous if relevant
var previousValue;
if (isCustomBinding) {
return function (el, value) {
getMatches(el, selector, firstMatchOnly).forEach(function (match) {
type.call(context, match, value, previousValue);
});
previousValue = value;
};
} else if (type === 'text') {
return function (el, value) {
getMatches(el, selector, firstMatchOnly).forEach(function (match) {
dom.text(match, value);
});
};
} else if (type === 'class') {
return function (el, value) {
getMatches(el, selector, firstMatchOnly).forEach(function (match) {
dom.switchClass(match, previousValue, value);
});
previousValue = value;
};
} else if (type === 'attribute') {
if (!binding.name) throw Error('attribute bindings must have a "name"');
return function (el, value) {
var names = makeArray(binding.name);
getMatches(el, selector, firstMatchOnly).forEach(function (match) {
names.forEach(function (name) {
dom.setAttribute(match, name, value);
});
});
previousValue = value;
};
} else if (type === 'value') {
return function (el, value) {
getMatches(el, selector, firstMatchOnly).forEach(function (match) {
if (!value && value !== 0) value = '';
// only apply bindings if element is not currently focused
if (document.activeElement !== match) match.value = value;
});
previousValue = value;
};
} else if (type === 'booleanClass') {
// if there's a `no` case this is actually a switch
if (hasYesNo) {
yes = makeArray(yes || '');
no = makeArray(no || '');
return function (el, value) {
var prevClass = value ? no : yes;
var newClass = value ? yes : no;
getMatches(el, selector, firstMatchOnly).forEach(function (match) {
prevClass.forEach(function (pc) {
dom.removeClass(match, pc);
});
newClass.forEach(function (nc) {
dom.addClass(match, nc);
});
});
};
} else {
return function (el, value, keyName) {
var name = makeArray(binding.name || keyName);
var invert = (binding.invert || false);
value = (invert ? (value ? false : true) : value);
getMatches(el, selector, firstMatchOnly).forEach(function (match) {
name.forEach(function (className) {
dom[value ? 'addClass' : 'removeClass'](match, className);
});
});
};
}
} else if (type === 'booleanAttribute') {
// if there are `yes` and `no` selectors, this swaps between them
if (hasYesNo) {
yes = makeArray(yes || '');
no = makeArray(no || '');
return function (el, value) {
var prevAttribute = value ? no : yes;
var newAttribute = value ? yes : no;
getMatches(el, selector, firstMatchOnly).forEach(function (match) {
prevAttribute.forEach(function (pa) {
if (pa) {
dom.removeAttribute(match, pa);
}
});
newAttribute.forEach(function (na) {
if (na) {
dom.addAttribute(match, na);
}
});
});
};
} else {
return function (el, value, keyName) {
var name = makeArray(binding.name || keyName);
var invert = (binding.invert || false);
value = (invert ? (value ? false : true) : value);
getMatches(el, selector, firstMatchOnly).forEach(function (match) {
name.forEach(function (attr) {
dom[value ? 'addAttribute' : 'removeAttribute'](match, attr);
});
});
};
}
} else if (type === 'toggle') {
var mode = (binding.mode || 'display');
var invert = (binding.invert || false);
// this doesn't require a selector since we can pass yes/no selectors
if (hasYesNo) {
return function (el, value) {
getMatches(el, yes, firstMatchOnly).forEach(function (match) {
dom[value ? 'show' : 'hide'](match, mode);
});
getMatches(el, no, firstMatchOnly).forEach(function (match) {
dom[value ? 'hide' : 'show'](match, mode);
});
};
} else {
return function (el, value) {
value = (invert ? (value ? false : true) : value);
getMatches(el, selector, firstMatchOnly).forEach(function (match) {
dom[value ? 'show' : 'hide'](match, mode);
});
};
}
} else if (type === 'switch') {
if (!binding.cases) throw Error('switch bindings must have "cases"');
return partial(switchHandler, binding);
} else if (type === 'innerHTML') {
return function (el, value) {
getMatches(el, selector, firstMatchOnly).forEach(function (match) {
dom.html(match, value);
});
};
} else if (type === 'switchClass') {
if (!binding.cases) throw Error('switchClass bindings must have "cases"');
return function (el, value, keyName) {
var name = makeArray(binding.name || keyName);
for (var item in binding.cases) {
getMatches(el, binding.cases[item], firstMatchOnly).forEach(function (match) {
name.forEach(function (className) {
dom[value === item ? 'addClass' : 'removeClass'](match, className);
});
});
}
};
} else if (type === 'switchAttribute') {
if (!binding.cases) throw Error('switchAttribute bindings must have "cases"');
return function (el, value, keyName) {
getMatches(el, selector, firstMatchOnly).forEach(function (match) {
if (previousValue) {
removeAttributes(match, previousValue);
}
if (value in binding.cases) {
var attrs = binding.cases[value];
if (typeof attrs === 'string') {
attrs = {};
attrs[binding.name || keyName] = binding.cases[value];
}
setAttributes(match, attrs);
previousValue = attrs;
}
});
};
} else {
throw new Error('no such binding type: ' + type);
}
}
// returns a key-tree-store of functions
// that can be applied to any element/model.
// all resulting functions should be called
// like func(el, value, lastKeyName)
module.exports = function (bindings, context) {
var store = new Store();
var key, current;
for (key in bindings) {
current = bindings[key];
if (typeof current === 'string') {
store.add(key, getBindingFunc({
type: 'text',
selector: current
}));
} else if (current.forEach) {
current.forEach(function (binding) {
store.add(key, getBindingFunc(binding, context));
});
} else {
store.add(key, getBindingFunc(current, context));
}
}
return store;
};