-
Notifications
You must be signed in to change notification settings - Fork 7
/
redom.es.js
749 lines (584 loc) · 16 KB
/
redom.es.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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
function createElement (query, ns) {
var ref = parse(query);
var tag = ref.tag;
var id = ref.id;
var className = ref.className;
var element = ns ? document.createElementNS(ns, tag) : document.createElement(tag);
if (id) {
element.id = id;
}
if (className) {
if (ns) {
element.setAttribute('class', className);
} else {
element.className = className;
}
}
return element;
}
function parse (query) {
var chunks = query.split(/([.#])/);
var className = '';
var id = '';
for (var i = 1; i < chunks.length; i += 2) {
switch (chunks[i]) {
case '.':
className += " " + (chunks[i + 1]);
break;
case '#':
id = chunks[i + 1];
}
}
return {
className: className.trim(),
tag: chunks[0] || 'div',
id: id
};
}
function unmount (parent, child) {
var parentEl = getEl(parent);
var childEl = getEl(child);
if (child === childEl && childEl.__redom_view) {
// try to look up the view if not provided
child = childEl.__redom_view;
}
if (childEl.parentNode) {
doUnmount(child, childEl, parentEl);
parentEl.removeChild(childEl);
}
return child;
}
function doUnmount (child, childEl, parentEl) {
var hooks = childEl.__redom_lifecycle;
if (hooksAreEmpty(hooks)) {
childEl.__redom_lifecycle = {};
return;
}
var traverse = parentEl;
if (childEl.__redom_mounted) {
trigger(childEl, 'onunmount');
}
while (traverse) {
var parentHooks = traverse.__redom_lifecycle || {};
for (var hook in hooks) {
if (parentHooks[hook]) {
parentHooks[hook] -= hooks[hook];
}
}
if (hooksAreEmpty(parentHooks)) {
traverse.__redom_lifecycle = null;
}
traverse = traverse.parentNode;
}
}
function hooksAreEmpty (hooks) {
if (hooks == null) {
return true;
}
for (var key in hooks) {
if (hooks[key]) {
return false;
}
}
return true;
}
/* global Node, ShadowRoot */
var hookNames = ['onmount', 'onremount', 'onunmount'];
var shadowRootAvailable = typeof window !== 'undefined' && 'ShadowRoot' in window;
function mount (parent, child, before, replace) {
var parentEl = getEl(parent);
var childEl = getEl(child);
if (child === childEl && childEl.__redom_view) {
// try to look up the view if not provided
child = childEl.__redom_view;
}
if (child !== childEl) {
childEl.__redom_view = child;
}
var wasMounted = childEl.__redom_mounted;
var oldParent = childEl.parentNode;
if (wasMounted && (oldParent !== parentEl)) {
doUnmount(child, childEl, oldParent);
}
if (before != null) {
if (replace) {
var beforeEl = getEl(before);
if (beforeEl.__redom_mounted) {
trigger(beforeEl, 'onunmount');
}
parentEl.replaceChild(childEl, beforeEl);
} else {
parentEl.insertBefore(childEl, getEl(before));
}
} else {
parentEl.appendChild(childEl);
}
doMount(child, childEl, parentEl, oldParent);
return child;
}
function trigger (el, eventName) {
if (eventName === 'onmount' || eventName === 'onremount') {
el.__redom_mounted = true;
} else if (eventName === 'onunmount') {
el.__redom_mounted = false;
}
var hooks = el.__redom_lifecycle;
if (!hooks) {
return;
}
var view = el.__redom_view;
var hookCount = 0;
view && view[eventName] && view[eventName]();
for (var hook in hooks) {
if (hook) {
hookCount++;
}
}
if (hookCount) {
var traverse = el.firstChild;
while (traverse) {
var next = traverse.nextSibling;
trigger(traverse, eventName);
traverse = next;
}
}
}
function doMount (child, childEl, parentEl, oldParent) {
var hooks = childEl.__redom_lifecycle || (childEl.__redom_lifecycle = {});
var remount = (parentEl === oldParent);
var hooksFound = false;
for (var i = 0, list = hookNames; i < list.length; i += 1) {
var hookName = list[i];
if (!remount) { // if already mounted, skip this phase
if (child !== childEl) { // only Views can have lifecycle events
if (hookName in child) {
hooks[hookName] = (hooks[hookName] || 0) + 1;
}
}
}
if (hooks[hookName]) {
hooksFound = true;
}
}
if (!hooksFound) {
childEl.__redom_lifecycle = {};
return;
}
var traverse = parentEl;
var triggered = false;
if (remount || (traverse && traverse.__redom_mounted)) {
trigger(childEl, remount ? 'onremount' : 'onmount');
triggered = true;
}
while (traverse) {
var parent = traverse.parentNode;
var parentHooks = traverse.__redom_lifecycle || (traverse.__redom_lifecycle = {});
for (var hook in hooks) {
parentHooks[hook] = (parentHooks[hook] || 0) + hooks[hook];
}
if (triggered) {
break;
} else {
if (traverse.nodeType === Node.DOCUMENT_NODE ||
(shadowRootAvailable && (traverse instanceof ShadowRoot)) ||
(parent && parent.__redom_mounted)
) {
trigger(traverse, remount ? 'onremount' : 'onmount');
triggered = true;
}
traverse = parent;
}
}
}
function setStyle (view, arg1, arg2) {
var el = getEl(view);
if (typeof arg1 === 'object') {
for (var key in arg1) {
setStyleValue(el, key, arg1[key]);
}
} else {
setStyleValue(el, arg1, arg2);
}
}
function setStyleValue (el, key, value) {
el.style[key] = value == null ? '' : value;
}
/* global SVGElement */
var xlinkns = 'http://www.w3.org/1999/xlink';
function setAttr (view, arg1, arg2) {
setAttrInternal(view, arg1, arg2);
}
function setAttrInternal (view, arg1, arg2, initial) {
var el = getEl(view);
var isObj = typeof arg1 === 'object';
if (isObj) {
for (var key in arg1) {
setAttrInternal(el, key, arg1[key], initial);
}
} else {
var isSVG = el instanceof SVGElement;
var isFunc = typeof arg2 === 'function';
if (arg1 === 'style' && typeof arg2 === 'object') {
setStyle(el, arg2);
} else if (isSVG && isFunc) {
el[arg1] = arg2;
} else if (arg1 === 'dataset') {
setData(el, arg2);
} else if (!isSVG && (arg1 in el || isFunc) && (arg1 !== 'list')) {
el[arg1] = arg2;
} else {
if (isSVG && (arg1 === 'xlink')) {
setXlink(el, arg2);
return;
}
if (initial && arg1 === 'class') {
arg2 = el.className + ' ' + arg2;
}
if (arg2 == null) {
el.removeAttribute(arg1);
} else {
el.setAttribute(arg1, arg2);
}
}
}
}
function setXlink (el, arg1, arg2) {
if (typeof arg1 === 'object') {
for (var key in arg1) {
setXlink(el, key, arg1[key]);
}
} else {
if (arg2 != null) {
el.setAttributeNS(xlinkns, arg1, arg2);
} else {
el.removeAttributeNS(xlinkns, arg1, arg2);
}
}
}
function setData (el, arg1, arg2) {
if (typeof arg1 === 'object') {
for (var key in arg1) {
setData(el, key, arg1[key]);
}
} else {
if (arg2 != null) {
el.dataset[arg1] = arg2;
} else {
delete el.dataset[arg1];
}
}
}
function text (str) {
return document.createTextNode((str != null) ? str : '');
}
function parseArgumentsInternal (element, args, initial) {
for (var i = 0, list = args; i < list.length; i += 1) {
var arg = list[i];
if (arg !== 0 && !arg) {
continue;
}
var type = typeof arg;
if (type === 'function') {
arg(element);
} else if (type === 'string' || type === 'number') {
element.appendChild(text(arg));
} else if (isNode(getEl(arg))) {
mount(element, arg);
} else if (arg.length) {
parseArgumentsInternal(element, arg, initial);
} else if (type === 'object') {
setAttrInternal(element, arg, null, initial);
}
}
}
function ensureEl (parent) {
return typeof parent === 'string' ? html(parent) : getEl(parent);
}
function getEl (parent) {
return (parent.nodeType && parent) || (!parent.el && parent) || getEl(parent.el);
}
function isNode (arg) {
return arg && arg.nodeType;
}
function html (query) {
var args = [], len = arguments.length - 1;
while ( len-- > 0 ) args[ len ] = arguments[ len + 1 ];
var element;
var type = typeof query;
if (type === 'string') {
element = createElement(query);
} else if (type === 'function') {
var Query = query;
element = new (Function.prototype.bind.apply( Query, [ null ].concat( args) ));
} else {
throw new Error('At least one argument required');
}
parseArgumentsInternal(getEl(element), args, true);
return element;
}
var el = html;
var h = html;
html.extend = function extendHtml () {
var args = [], len = arguments.length;
while ( len-- ) args[ len ] = arguments[ len ];
return html.bind.apply(html, [ this ].concat( args ));
};
function setChildren (parent) {
var children = [], len = arguments.length - 1;
while ( len-- > 0 ) children[ len ] = arguments[ len + 1 ];
var parentEl = getEl(parent);
var current = traverse(parent, children, parentEl.firstChild);
while (current) {
var next = current.nextSibling;
unmount(parent, current);
current = next;
}
}
function traverse (parent, children, _current) {
var current = _current;
var childEls = Array(children.length);
for (var i = 0; i < children.length; i++) {
childEls[i] = children[i] && getEl(children[i]);
}
for (var i$1 = 0; i$1 < children.length; i$1++) {
var child = children[i$1];
if (!child) {
continue;
}
var childEl = childEls[i$1];
if (childEl === current) {
current = current.nextSibling;
continue;
}
if (isNode(childEl)) {
var next = current && current.nextSibling;
var exists = child.__redom_index != null;
var replace = exists && next === childEls[i$1 + 1];
mount(parent, child, current, replace);
if (replace) {
current = next;
}
continue;
}
if (child.length != null) {
current = traverse(parent, child, current);
}
}
return current;
}
function listPool (View, key, initData) {
return new ListPool(View, key, initData);
}
var ListPool = function ListPool (View, key, initData) {
this.View = View;
this.initData = initData;
this.oldLookup = {};
this.lookup = {};
this.oldViews = [];
this.views = [];
if (key != null) {
this.key = typeof key === 'function' ? key : propKey(key);
}
};
ListPool.prototype.update = function update (data, context) {
var ref = this;
var View = ref.View;
var key = ref.key;
var initData = ref.initData;
var keySet = key != null;
var oldLookup = this.lookup;
var newLookup = {};
var newViews = Array(data.length);
var oldViews = this.views;
for (var i = 0; i < data.length; i++) {
var item = data[i];
var view = (void 0);
if (keySet) {
var id = key(item);
view = oldLookup[id] || new View(initData, item, i, data);
newLookup[id] = view;
view.__redom_id = id;
} else {
view = oldViews[i] || new View(initData, item, i, data);
}
view.update && view.update(item, i, data, context);
var el = getEl(view.el);
el.__redom_view = view;
newViews[i] = view;
}
this.oldViews = oldViews;
this.views = newViews;
this.oldLookup = oldLookup;
this.lookup = newLookup;
};
function propKey (key) {
return function (item) {
return item[key];
};
}
function list (parent, View, key, initData) {
return new List(parent, View, key, initData);
}
var List = function List (parent, View, key, initData) {
this.View = View;
this.initData = initData;
this.views = [];
this.pool = new ListPool(View, key, initData);
this.el = ensureEl(parent);
this.keySet = key != null;
};
List.prototype.update = function update (data, context) {
if ( data === void 0 ) data = [];
var ref = this;
var keySet = ref.keySet;
var oldViews = this.views;
this.pool.update(data, context);
var ref$1 = this.pool;
var views = ref$1.views;
var lookup = ref$1.lookup;
if (keySet) {
for (var i = 0; i < oldViews.length; i++) {
var oldView = oldViews[i];
var id = oldView.__redom_id;
if (lookup[id] == null) {
oldView.__redom_index = null;
unmount(this, oldView);
}
}
}
for (var i$1 = 0; i$1 < views.length; i$1++) {
var view = views[i$1];
view.__redom_index = i$1;
}
setChildren(this, views);
if (keySet) {
this.lookup = lookup;
}
this.views = views;
};
List.extend = function extendList (parent, View, key, initData) {
return List.bind(List, parent, View, key, initData);
};
list.extend = List.extend;
/* global Node */
function place (View, initData) {
return new Place(View, initData);
}
var Place = function Place (View, initData) {
this.el = text('');
this.visible = false;
this.view = null;
this._placeholder = this.el;
if (View instanceof Node) {
this._el = View;
} else if (View.el instanceof Node) {
this._el = View;
this.view = View;
} else {
this._View = View;
}
this._initData = initData;
};
Place.prototype.update = function update (visible, data) {
var placeholder = this._placeholder;
var parentNode = this.el.parentNode;
if (visible) {
if (!this.visible) {
if (this._el) {
mount(parentNode, this._el, placeholder);
unmount(parentNode, placeholder);
this.el = getEl(this._el);
this.visible = visible;
} else {
var View = this._View;
var view = new View(this._initData);
this.el = getEl(view);
this.view = view;
mount(parentNode, view, placeholder);
unmount(parentNode, placeholder);
}
}
this.view && this.view.update && this.view.update(data);
} else {
if (this.visible) {
if (this._el) {
mount(parentNode, placeholder, this._el);
unmount(parentNode, this._el);
this.el = placeholder;
this.visible = visible;
return;
}
mount(parentNode, placeholder, this.view);
unmount(parentNode, this.view);
this.el = placeholder;
this.view = null;
}
}
this.visible = visible;
};
/* global Node */
function router (parent, views, initData) {
return new Router(parent, views, initData);
}
var Router = function Router (parent, views, initData) {
this.el = ensureEl(parent);
this.views = views;
this.Views = views; // backwards compatibility
this.initData = initData;
};
Router.prototype.update = function update (route, data) {
if (route !== this.route) {
var views = this.views;
var View = views[route];
this.route = route;
if (View && (View instanceof Node || View.el instanceof Node)) {
this.view = View;
} else {
this.view = View && new View(this.initData, data);
}
setChildren(this.el, [this.view]);
}
this.view && this.view.update && this.view.update(data, route);
};
var ns = 'http://www.w3.org/2000/svg';
function svg (query) {
var args = [], len = arguments.length - 1;
while ( len-- > 0 ) args[ len ] = arguments[ len + 1 ];
var element;
var type = typeof query;
if (type === 'string') {
element = createElement(query, ns);
} else if (type === 'function') {
var Query = query;
element = new (Function.prototype.bind.apply( Query, [ null ].concat( args) ));
} else {
throw new Error('At least one argument required');
}
parseArgumentsInternal(getEl(element), args, true);
return element;
}
var s = svg;
svg.extend = function extendSvg () {
var args = [], len = arguments.length;
while ( len-- ) args[ len ] = arguments[ len ];
return svg.bind.apply(svg, [ this ].concat( args ));
};
svg.ns = ns;
function viewFactory (views, key) {
if (!views || typeof views !== 'object') {
throw new Error('views must be an object');
}
if (!key || typeof key !== 'string') {
throw new Error('key must be a string');
}
return function (initData, item, i, data) {
var viewKey = item[key];
var View = views[viewKey];
if (View) {
return new View(initData, item, i, data);
} else {
throw new Error(("view " + viewKey + " not found"));
}
};
}
export { List, ListPool, Place, Router, el, h, html, list, listPool, mount, place, router, s, setAttr, setChildren, setData, setStyle, setXlink, svg, text, unmount, viewFactory };