-
Notifications
You must be signed in to change notification settings - Fork 396
/
Element.js
executable file
·599 lines (493 loc) · 16.4 KB
/
Element.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
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
import { ATTRIBUTE, BINDING_FLAG, DECORATOR, DELEGATE_FLAG, EVENT, TRANSITION } from 'config/types';
import { win } from 'config/environment';
import { html, svg } from 'config/namespaces';
import { toArray, addToArray, removeFromArray } from 'utils/array';
import { escapeHtml, voidElements } from 'utils/html';
import { createElement, detachNode, matches, safeAttributeString } from 'utils/dom';
import runloop from 'src/global/runloop';
import Context from 'shared/Context';
import { destroyed, shuffled } from 'shared/methodCallers';
import { ContainerItem } from './shared/Item';
import Fragment from '../Fragment';
import ConditionalAttribute from './element/ConditionalAttribute';
import createItem from './createItem';
import findElement from './shared/findElement';
import selectBinding from './element/binding/selectBinding';
import { assign, create, defineProperty, keys } from 'utils/object';
import { isString } from 'utils/is';
const endsWithSemi = /;\s*$/;
export default class Element extends ContainerItem {
constructor(options) {
super(options);
this.name = options.template.e.toLowerCase();
// find parent element
this.parent = findElement(this.up, false);
if (this.parent && this.parent.name === 'option') {
throw new Error(
`An <option> element cannot contain other elements (encountered <${this.name}>)`
);
}
this.decorators = [];
// create attributes
this.attributeByName = {};
let attrs;
let n, attr, val, cls, name, template, leftovers;
const m = this.template.m;
const len = (m && m.length) || 0;
for (let i = 0; i < len; i++) {
template = m[i];
if (template.g) {
(this.statics || (this.statics = {}))[template.n] = isString(template.f)
? template.f
: template.n;
} else {
switch (template.t) {
case ATTRIBUTE:
case BINDING_FLAG:
case DECORATOR:
case EVENT:
case TRANSITION:
attr = createItem({
owner: this,
up: this.up,
template
});
n = template.n;
attrs = attrs || (attrs = this.attributes = []);
if (n === 'value') val = attr;
else if (n === 'name') name = attr;
else if (n === 'class') cls = attr;
else attrs.push(attr);
break;
case DELEGATE_FLAG:
this.delegate = false;
break;
default:
(leftovers || (leftovers = [])).push(template);
break;
}
}
}
if (val) attrs.push(val);
if (name) attrs.push(name);
if (cls) attrs.unshift(cls);
if (leftovers) {
(attrs || (this.attributes = [])).push(
new ConditionalAttribute({
owner: this,
up: this.up,
template: leftovers
})
);
// empty leftovers array
leftovers = [];
}
// create children
if (options.template.f && !options.deferContent) {
this.fragment = new Fragment({
template: options.template.f,
owner: this,
cssIds: null
});
}
this.binding = null; // filled in later
}
bind() {
const attrs = this.attributes;
if (attrs) {
attrs.binding = true;
const len = attrs.length;
for (let i = 0; i < len; i++) attrs[i].bind();
attrs.binding = false;
}
if (this.fragment) this.fragment.bind();
// create two-way binding if necessary
if (!this.binding) this.recreateTwowayBinding();
else this.binding.bind();
}
createTwowayBinding() {
if ('twoway' in this ? this.twoway : this.ractive.twoway) {
const Binding = selectBinding(this);
if (Binding) {
const binding = new Binding(this);
if (binding && binding.model) return binding;
}
}
}
destroyed() {
if (this.attributes) this.attributes.forEach(destroyed);
if (this.fragment) this.fragment.destroyed();
}
detach() {
// if this element is no longer rendered, the transitions are complete and the attributes can be torn down
if (!this.rendered) this.destroyed();
return detachNode(this.node);
}
find(selector, options) {
if (this.node && matches(this.node, selector)) return this.node;
if (this.fragment) {
return this.fragment.find(selector, options);
}
}
findAll(selector, options) {
const { result } = options;
if (matches(this.node, selector)) {
result.push(this.node);
}
if (this.fragment) {
this.fragment.findAll(selector, options);
}
}
findNextNode() {
return null;
}
firstNode() {
return this.node;
}
getAttribute(name) {
if (this.statics && name in this.statics) return this.statics[name];
const attribute = this.attributeByName[name];
return attribute ? attribute.getValue() : undefined;
}
getContext(...assigns) {
if (this.fragment) return this.fragment.getContext(...assigns);
if (!this.ctx) this.ctx = new Context(this.up, this);
assigns.unshift(create(this.ctx));
return assign.apply(null, assigns);
}
off(event, callback, capture = false) {
const delegate = this.up.delegate;
const ref = this.listeners && this.listeners[event];
if (!ref) return;
removeFromArray(ref, callback);
if (delegate) {
const listeners =
(delegate.listeners || (delegate.listeners = [])) &&
(delegate.listeners[event] || (delegate.listeners[event] = []));
if (listeners.refs && !--listeners.refs) delegate.off(event, delegateHandler, true);
} else if (this.rendered) {
const n = this.node;
const add = n.addEventListener;
const rem = n.removeEventListener;
if (!ref.length) {
rem.call(n, event, handler, capture);
} else if (ref.length && !ref.refs && capture) {
rem.call(n, event, handler, true);
add.call(n, event, handler, false);
}
}
}
on(event, callback, capture = false) {
const delegate = this.up.delegate;
const ref = (this.listeners || (this.listeners = {}))[event] || (this.listeners[event] = []);
if (delegate) {
const listeners =
((delegate.listeners || (delegate.listeners = [])) && delegate.listeners[event]) ||
(delegate.listeners[event] = []);
if (!listeners.refs) {
listeners.refs = 0;
delegate.on(event, delegateHandler, true);
listeners.refs++;
} else {
listeners.refs++;
}
} else if (this.rendered) {
const n = this.node;
const add = n.addEventListener;
const rem = n.removeEventListener;
if (!ref.length) {
add.call(n, event, handler, capture);
} else if (ref.length && !ref.refs && capture) {
rem.call(n, event, handler, false);
add.call(n, event, handler, true);
}
}
addToArray(this.listeners[event], callback);
}
recreateTwowayBinding() {
if (this.binding) {
this.binding.unbind();
this.binding.unrender();
}
if ((this.binding = this.createTwowayBinding())) {
this.binding.bind();
if (this.rendered) this.binding.render();
}
}
rebound(update) {
super.rebound(update);
if (this.attributes) this.attributes.forEach(x => x.rebound(update));
if (this.binding) this.binding.rebound(update);
}
render(target, occupants) {
// TODO determine correct namespace
this.namespace = getNamespace(this);
let node;
let existing = false;
if (occupants) {
let n;
while ((n = occupants.shift())) {
if (
n.nodeName.toUpperCase() === this.template.e.toUpperCase() &&
n.namespaceURI === this.namespace
) {
this.node = node = n;
existing = true;
break;
} else {
detachNode(n);
}
}
}
if (!existing && this.node) {
node = this.node;
target.appendChild(node);
existing = true;
}
if (!node) {
const name = this.template.e;
node = createElement(
this.namespace === html ? name.toLowerCase() : name,
this.namespace,
this.getAttribute('is')
);
this.node = node;
}
// tie the node to this vdom element
defineProperty(node, '_ractive', {
value: {
proxy: this
},
configurable: true
});
if (this.statics) {
keys(this.statics).forEach(k => {
node.setAttribute(k, this.statics[k]);
});
}
if (existing && this.foundNode) this.foundNode(node);
// register intro before rendering content so children can find the intro
const intro = this.intro;
if (intro && intro.shouldFire('intro')) {
intro.isIntro = true;
intro.isOutro = false;
runloop.registerTransition(intro);
}
if (this.fragment) {
const children = existing ? toArray(node.childNodes) : undefined;
this.fragment.render(node, children);
// clean up leftover children
if (children) {
children.forEach(detachNode);
}
}
if (existing) {
// store initial values for two-way binding
if (this.binding && this.binding.wasUndefined) this.binding.setFromNode(node);
// remove unused attributes
let i = node.attributes.length;
while (i--) {
const name = node.attributes[i].name;
if (!(name in this.attributeByName) && (!this.statics || !(name in this.statics)))
node.removeAttribute(name);
}
}
// Is this a top-level node of a component? If so, we may need to add
// a data-ractive-css attribute, for CSS encapsulation
if (this.up.cssIds) {
node.setAttribute('data-ractive-css', this.up.cssIds.map(x => `{${x}}`).join(' '));
}
if (this.attributes) {
const len = this.attributes.length;
for (let i = 0; i < len; i++) this.attributes[i].render();
}
if (this.binding) this.binding.render();
if (!this.up.delegate && this.listeners) {
const ls = this.listeners;
for (const k in ls) {
if (ls[k] && ls[k].length) this.node.addEventListener(k, handler, !!ls[k].refs);
}
}
if (!existing) {
target.appendChild(node);
}
this.rendered = true;
}
shuffled() {
super.shuffled();
this.decorators.forEach(shuffled);
}
toString() {
const tagName = this.template.e;
let attrs = (this.attributes && this.attributes.map(stringifyAttribute).join('')) || '';
if (this.statics)
keys(this.statics).forEach(
k =>
k !== 'class' &&
k !== 'style' &&
(attrs = ` ${k}="${safeAttributeString(this.statics[k])}"` + attrs)
);
// Special case - selected options
if (this.name === 'option' && this.isSelected()) {
attrs += ' selected';
}
// Special case - two-way radio name bindings
if (this.name === 'input' && inputIsCheckedRadio(this)) {
attrs += ' checked';
}
// Special case style and class attributes and directives
let style = this.statics ? this.statics.style : undefined;
let cls = this.statics ? this.statics.class : undefined;
this.attributes &&
this.attributes.forEach(attr => {
if (attr.name === 'class') {
cls = (cls || '') + (cls ? ' ' : '') + safeAttributeString(attr.getString());
} else if (attr.name === 'style') {
style = (style || '') + (style ? ' ' : '') + safeAttributeString(attr.getString());
if (style && !endsWithSemi.test(style)) style += ';';
} else if (attr.style) {
style =
(style || '') +
(style ? ' ' : '') +
`${attr.style}: ${safeAttributeString(attr.getString())};`;
} else if (attr.inlineClass && attr.getValue()) {
cls = (cls || '') + (cls ? ' ' : '') + attr.inlineClass;
}
});
// put classes first, then inline style
if (style !== undefined) attrs = ' style' + (style ? `="${style}"` : '') + attrs;
if (cls !== undefined) attrs = ' class' + (cls ? `="${cls}"` : '') + attrs;
if (this.up.cssIds) {
attrs += ` data-ractive-css="${this.up.cssIds.map(x => `{${x}}`).join(' ')}"`;
}
let str = `<${tagName}${attrs}>`;
if (voidElements[this.name.toLowerCase()]) return str;
// Special case - textarea
if (this.name === 'textarea' && this.getAttribute('value') !== undefined) {
str += escapeHtml(this.getAttribute('value'));
} else if (this.getAttribute('contenteditable') !== undefined) {
// Special case - contenteditable
str += this.getAttribute('value') || '';
}
if (this.fragment) {
str += this.fragment.toString(!/^(?:script|style)$/i.test(this.template.e)); // escape text unless script/style
}
str += `</${tagName}>`;
return str;
}
unbind(view) {
const attrs = this.attributes;
if (attrs) {
attrs.unbinding = true;
const len = attrs.length;
for (let i = 0; i < len; i++) attrs[i].unbind(view);
attrs.unbinding = false;
}
if (this.binding) this.binding.unbind(view);
if (this.fragment) this.fragment.unbind(view);
}
unrender(shouldDestroy) {
if (!this.rendered) return;
this.rendered = false;
// unrendering before intro completed? complete it now
// TODO should be an API for aborting transitions
const transition = this.intro;
if (transition && transition.complete) transition.complete();
// Detach as soon as we can
if (this.name === 'option') {
// <option> elements detach immediately, so that
// their parent <select> element syncs correctly, and
// since option elements can't have transitions anyway
this.detach();
} else if (shouldDestroy) {
runloop.detachWhenReady(this);
}
// outro transition
const outro = this.outro;
if (outro && outro.shouldFire('outro')) {
outro.isIntro = false;
outro.isOutro = true;
runloop.registerTransition(outro);
}
if (this.fragment) this.fragment.unrender();
if (this.binding) this.binding.unrender();
}
update() {
if (this.dirty) {
this.dirty = false;
const attrs = this.attributes;
if (attrs) {
const len = attrs.length;
for (let i = 0; i < len; i++) attrs[i].update();
}
if (this.fragment) this.fragment.update();
}
}
}
function inputIsCheckedRadio(element) {
const nameAttr = element.attributeByName.name;
return (
element.getAttribute('type') === 'radio' &&
(nameAttr || {}).interpolator &&
element.getAttribute('value') === nameAttr.interpolator.model.get()
);
}
function stringifyAttribute(attribute) {
const str = attribute.toString();
return str ? ' ' + str : '';
}
function getNamespace(element) {
// Use specified namespace...
const xmlns = element.getAttribute('xmlns');
if (xmlns) return xmlns;
// ...or SVG namespace, if this is an <svg> element
if (element.name === 'svg') return svg;
const parent = element.parent;
if (parent) {
// ...or HTML, if the parent is a <foreignObject>
if (parent.name === 'foreignobject') return html;
// ...or inherit from the parent node
return parent.node.namespaceURI;
}
return element.ractive.el.namespaceURI;
}
function delegateHandler(ev) {
const name = ev.type;
const end = ev.currentTarget;
const endEl = end._ractive && end._ractive.proxy;
let node = ev.target;
let bubble = true;
let listeners;
// starting with the origin node, walk up the DOM looking for ractive nodes with a matching event listener
while (bubble && node && node !== end) {
const proxy = node._ractive && node._ractive.proxy;
if (proxy && proxy.up.delegate === endEl && shouldFire(ev, node, end)) {
listeners = proxy.listeners && proxy.listeners[name];
if (listeners) {
const len = listeners.length;
for (let i = 0; i < len; i++) bubble = listeners[i].call(node, ev) !== false && bubble;
}
}
node = node.parentNode || node.correspondingUseElement; // SVG with a <use> element in certain environments
}
return bubble;
}
const UIEvent = win !== null ? win.UIEvent : null;
function shouldFire(event, start, end) {
if (UIEvent && event instanceof UIEvent) {
let node = start;
while (node && node !== end) {
if (node.disabled) return false;
node = node.parentNode || node.correspondingUseElement;
}
}
return true;
}
function handler(ev) {
const el = this._ractive.proxy;
let listeners;
if (el.listeners && (listeners = el.listeners[ev.type])) {
const len = listeners.length;
for (let i = 0; i < len; i++) listeners[i].call(this, ev);
}
}