forked from domvm/domvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdomvm.dev.js
2111 lines (1686 loc) · 45.9 KB
/
domvm.dev.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
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright (c) 2017, Leon Sorokin
* All rights reserved. (MIT Licensed)
*
* domvm.full.js - DOM ViewModel
* A thin, fast, dependency-free vdom view layer
* @preserve https://github.com/leeoniya/domvm (2.x-dev, dev)
*/
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global.domvm = factory());
}(this, (function () { 'use strict';
// NOTE: if adding a new *VNode* type, make it < COMMENT and renumber rest.
// There are some places that test <= COMMENT to assert if node is a VNode
// VNode types
var ELEMENT = 1;
var TEXT = 2;
var COMMENT = 3;
// placeholder types
var VVIEW = 4;
var VMODEL = 5;
var ENV_DOM = typeof window !== "undefined";
var TRUE = true;
var win = ENV_DOM ? window : {};
var rAF = win.requestAnimationFrame;
var emptyObj = {};
function noop() {}
var isArr = Array.isArray;
function isSet(val) {
return val != null;
}
function isPlainObj(val) {
return val != null && val.constructor === Object; // && typeof val === "object"
}
function insertArr(targ, arr, pos, rem) {
targ.splice.apply(targ, [pos, rem].concat(arr));
}
function isVal(val) {
var t = typeof val;
return t === "string" || t === "number";
}
function isFunc(val) {
return typeof val === "function";
}
function isProm(val) {
return typeof val === "object" && isFunc(val.then);
}
function assignObj(targ) {
var args = arguments;
for (var i = 1; i < args.length; i++)
{ for (var k in args[i])
{ targ[k] = args[i][k]; } }
return targ;
}
// export const defProp = Object.defineProperty;
function deepSet(targ, path, val) {
var seg;
while (seg = path.shift()) {
if (path.length === 0)
{ targ[seg] = val; }
else
{ targ[seg] = targ = targ[seg] || {}; }
}
}
/*
export function deepUnset(targ, path) {
var seg;
while (seg = path.shift()) {
if (path.length === 0)
targ[seg] = val;
else
targ[seg] = targ = targ[seg] || {};
}
}
*/
function sliceArgs(args, offs) {
var arr = [];
for (var i = offs; i < args.length; i++)
{ arr.push(args[i]); }
return arr;
}
function cmpObj(a, b) {
for (var i in a)
{ if (a[i] !== b[i])
{ return false; } }
return true;
}
function cmpArr(a, b) {
var alen = a.length;
if (b.length !== alen)
{ return false; }
for (var i = 0; i < alen; i++)
{ if (a[i] !== b[i])
{ return false; } }
return true;
}
// https://github.com/darsain/raft
// rAF throttler, aggregates multiple repeated redraw calls within single animframe
function raft(fn) {
if (!rAF)
{ return fn; }
var id, ctx, args;
function call() {
id = 0;
fn.apply(ctx, args);
}
return function() {
ctx = this;
args = arguments;
if (!id) { id = rAF(call); }
};
}
function curry(fn, args, ctx) {
return function() {
return fn.apply(ctx, args);
};
}
function prop(val, cb, ctx, args) {
return function(newVal, execCb) {
if (newVal !== undefined && newVal !== val) {
val = newVal;
execCb !== false && isFunc(cb) && cb.apply(ctx, args);
}
return val;
};
}
// adapted from https://github.com/Olical/binary-search
function binaryKeySearch(list, item) {
var min = 0;
var max = list.length - 1;
var guess;
var bitwise = (max <= 2147483647) ? true : false;
if (bitwise) {
while (min <= max) {
guess = (min + max) >> 1;
if (list[guess].key === item) { return guess; }
else {
if (list[guess].key < item) { min = guess + 1; }
else { max = guess - 1; }
}
}
} else {
while (min <= max) {
guess = Math.floor((min + max) / 2);
if (list[guess].key === item) { return guess; }
else {
if (list[guess].key < item) { min = guess + 1; }
else { max = guess - 1; }
}
}
}
return -1;
}
function isEvProp(name) {
return name[0] === "o" && name[1] === "n";
}
function isSplProp(name) {
return name[0] === "_";
}
function isStyleProp(name) {
return name === "style";
}
function repaint(node) {
node && node.el && node.el.offsetHeight;
}
// tests interactive props where real val should be compared
function isDynProp(tag, attr) {
// switch (tag) {
// case "input":
// case "textarea":
// case "select":
// case "option":
switch (attr) {
case "value":
case "checked":
case "selected":
// case "selectedIndex":
return true;
}
// }
return false;
}
function getVm(n) {
n = n || emptyObj;
while (n.vm == null && n.parent)
{ n = n.parent; }
return n.vm;
}
var unitlessProps = {
animationIterationCount: TRUE,
boxFlex: TRUE,
boxFlexGroup: TRUE,
columnCount: TRUE,
counterIncrement: TRUE,
// fillOpacity: TRUE,
flex: TRUE,
flexGrow: TRUE,
flexOrder: TRUE,
flexPositive: TRUE,
flexShrink: TRUE,
float: TRUE,
fontWeight: TRUE,
gridColumn: TRUE,
lineHeight: TRUE,
lineClamp: TRUE,
opacity: TRUE,
order: TRUE,
orphans: TRUE,
// stopOpacity: TRUE,
// strokeDashoffset: TRUE,
// strokeOpacity: TRUE,
// strokeWidth: TRUE,
tabSize: TRUE,
transform: TRUE,
transformOrigin: TRUE,
widows: TRUE,
zIndex: TRUE,
zoom: TRUE,
};
function autoPx(name, val) {
// typeof val === 'number' is faster but fails for numeric strings
return !isNaN(val) && !unitlessProps[name] ? (val + "px") : val;
}
var tagCache = {};
var RE_ATTRS = /\[(\w+)(?:=(\w+))?\]/g;
// function VTag() {}
function cssTag(raw) {
var cached = tagCache[raw];
if (cached == null) {
var tag, id, cls, attr;
tagCache[raw] = cached = {
tag: (tag = raw.match( /^[-\w]+/)) ? tag[0] : "div",
id: (id = raw.match( /#([-\w]+)/)) ? id[1] : null,
class: (cls = raw.match(/\.([-\w.]+)/)) ? cls[1].replace(/\./g, " ") : null,
attrs: null,
};
while (attr = RE_ATTRS.exec(raw)) {
if (cached.attrs == null)
{ cached.attrs = {}; }
cached.attrs[attr[1]] = attr[2] || "";
}
}
return cached;
}
var isStream = function() { return false };
var streamVal = null;
var subStream = null;
var unsubStream = null;
/* example flyd adapter:
{
is: s => flyd.isStream(s),
val: s => s(),
sub: (s,fn) => flyd.on(fn, s),
unsub: s => s.end(),
}
*/
function streamCfg(cfg) {
isStream = cfg.is;
streamVal = cfg.val;
subStream = cfg.sub;
unsubStream = cfg.unsub;
}
// creates a one-shot self-ending stream that redraws target vm
// TODO: if it's already registered by any parent vm, then ignore to avoid simultaneous parent & child refresh
function hookStream(s, vm) {
var redrawStream = subStream(s, function (val) {
// this "if" ignores the initial firing during subscription (there's no redrawable vm yet)
if (redrawStream) {
// if vm fully is formed (or mounted vm.node.el?)
if (vm.node != null)
{ vm.redraw(); }
unsubStream(redrawStream);
}
});
return streamVal(s);
}
// assumes if styles exist both are objects or both are strings
function patchStyle(n, o) {
var ns = (n.attrs || emptyObj).style;
var os = o ? (o.attrs || emptyObj).style : null;
// replace or remove in full
if (ns == null || isVal(ns))
{ n.el.style.cssText = ns; }
else {
for (var nn in ns) {
var nv = ns[nn];
if (isStream(nv))
{ nv = hookStream(nv, getVm(n)); }
if (os == null || nv != null && nv !== os[nn])
{ n.el.style[nn] = autoPx(nn, nv); }
}
// clean old
if (os) {
for (var on in os) {
if (ns[on] == null)
{ n.el.style[on] = ""; }
}
}
}
}
var didQueue = [];
function fireHook(did, fn, o, n, immediate) {
if (did) { // did*
// console.log(name + " should queue till repaint", o, n);
immediate ? repaint(o.parent) && fn(o, n) : didQueue.push([fn, o, n]);
}
else { // will*
// console.log(name + " may delay by promise", o, n);
return fn(o, n); // or pass done() resolver
}
}
function fireHooks(name, o, n, immediate) {
var hook = o.hooks[name];
if (hook) {
var did = name[0] === "d" && name[1] === "i" && name[2] === "d";
if (isArr(hook)) {
// TODO: promise.all() this?
return hook.map(function(hook2) {
return fireHook(did, hook2, o, n);
});
}
else
{ return fireHook(did, hook, o, n, immediate); }
}
}
function VNode() {}
var VNodeProto = VNode.prototype = {
constructor: VNode,
type: null,
vm: null,
// all this stuff can just live in attrs (as defined) just have getters here for it
key: null,
ref: null,
data: null,
hooks: null,
raw: false,
ns: null,
el: null,
tag: null,
attrs: null,
body: null,
flags: 0,
_class: null,
idx: null,
parent: null,
/*
// break out into optional fluent module
key: function(val) { this.key = val; return this; },
ref: function(val) { this.ref = val; return this; }, // deep refs
data: function(val) { this.data = val; return this; },
hooks: function(val) { this.hooks = val; return this; }, // h("div").hooks()
html: function(val) { this.html = true; return this.body(val); },
body: function(val) { this.body = val; return this; },
*/
};
var DEVMODE = {
enabled: true,
verbose: true,
AUTOKEYED_VIEW: function(vm, model) {
var msg = "A view has been auto-keyed by its model's identity. If this model is replaced between redraws,"
+ " this view will unmount, its internal state and DOM will be destroyed and recreated."
+ " Consider providing a fixed key to this view to ensure its persistence & DOM recycling. See https://github.com/leeoniya/domvm#dom-recycling.";
return [msg, vm, model];
},
UNKEYED_INPUT: function(vnode) {
return ["Unkeyed <input> detected. Consider adding a name, id, _key, or _ref attr to avoid accidental DOM recycling between different <input> types.", vnode];
},
UNMOUNTED_REDRAW: function(vm) {
return ["Invoking redraw() of an unmounted (sub)view may result in errors.", vm];
},
INLINE_HANDLER: function(vnode, oval, nval) {
return ["Anonymous event handlers get re-bound on each redraw, consider defining them outside of templates for better reuse.", vnode, oval, nval];
},
MISMATCHED_HANDLER: function(vnode, oval, nval) {
return ["Patching of different event handler styles is not fully supported for performance reasons. Ensure that handlers are defined using the same style.", vnode, oval, nval];
},
SVG_WRONG_FACTORY: function(vnode) {
return ["<svg> defined using domvm.defineElement. Use domvm.defineSvgElement for <svg> & child nodes.", vnode];
},
FOREIGN_ELEMENT: function(el) {
return ["domvm stumbled upon an element in its DOM that it didn't create, which may be problematic. You can inject external elements into the vtree using domvm.injectElement.", el];
},
};
function devNotify(key, args) {
if (DEVMODE.enabled) {
var msgArgs = DEVMODE[key].apply(null, args);
if (msgArgs) {
msgArgs[0] = key + ": " + (DEVMODE.verbose ? msgArgs[0] : "");
console.warn.apply(null, msgArgs);
}
}
}
// (de)optimization flags
// prevents inserting/removing/reordering of children
var FIXED_BODY = 1;
// forces slow bottom-up removeChild to fire deep willRemove/willUnmount hooks,
var DEEP_REMOVE = 2;
// enables fast keyed lookup of children via binary search, expects homogeneous keyed body
var KEYED_LIST = 4;
function initElementNode(tag, attrs, body, flags) {
var node = new VNode;
node.type = ELEMENT;
if (isSet(flags))
{ node.flags = flags; }
node.attrs = attrs;
var parsed = cssTag(tag);
node.tag = parsed.tag;
// meh, weak assertion, will fail for id=0, etc.
if (parsed.id || parsed.class || parsed.attrs) {
var p = node.attrs || {};
if (parsed.id && !isSet(p.id))
{ p.id = parsed.id; }
if (parsed.class) {
node._class = parsed.class; // static class
p.class = parsed.class + (isSet(p.class) ? (" " + p.class) : "");
}
if (parsed.attrs) {
for (var key in parsed.attrs)
{ if (!isSet(p[key]))
{ p[key] = parsed.attrs[key]; } }
}
// if (node.attrs !== p)
node.attrs = p;
}
var mergedAttrs = node.attrs;
if (isSet(mergedAttrs)) {
if (isSet(mergedAttrs._key))
{ node.key = mergedAttrs._key; }
if (isSet(mergedAttrs._ref))
{ node.ref = mergedAttrs._ref; }
if (isSet(mergedAttrs._hooks))
{ node.hooks = mergedAttrs._hooks; }
if (isSet(mergedAttrs._raw))
{ node.raw = mergedAttrs._raw; }
if (isSet(mergedAttrs._data))
{ node.data = mergedAttrs._data; }
if (isSet(mergedAttrs._flags))
{ node.flags = mergedAttrs._flags; }
if (!isSet(node.key)) {
if (isSet(node.ref))
{ node.key = node.ref; }
else if (isSet(mergedAttrs.id))
{ node.key = mergedAttrs.id; }
else if (isSet(mergedAttrs.name))
{ node.key = mergedAttrs.name; }
}
}
if (body != null)
{ node.body = body; }
{
if (node.tag === "svg") {
setTimeout(function() {
node.ns == null && devNotify("SVG_WRONG_FACTORY", [node]);
}, 16);
}
else if (node.tag === "input" && node.key == null)
{ devNotify("UNKEYED_INPUT", [node]); }
}
return node;
}
var doc = ENV_DOM ? document : null;
function closestVNode(el) {
while (el._node == null)
{ el = el.parentNode; }
return el._node;
}
function createElement(tag, ns) {
if (ns != null)
{ return doc.createElementNS(ns, tag); }
return doc.createElement(tag);
}
function createTextNode(body) {
return doc.createTextNode(body);
}
function createComment(body) {
return doc.createComment(body);
}
// ? removes if !recycled
function nextSib(sib) {
return sib.nextSibling;
}
// ? removes if !recycled
function prevSib(sib) {
return sib.previousSibling;
}
// TODO: this should collect all deep proms from all hooks and return Promise.all()
function deepNotifyRemove(node) {
var hooks = node.hooks, vm = node.vm;
vm && vm.hooks && fireHooks("willUnmount", vm);
var res = hooks && fireHooks("willRemove", node);
if ((node.flags & DEEP_REMOVE) === DEEP_REMOVE && isArr(node.body)) {
for (var i = 0; i < node.body.length; i++)
{ deepNotifyRemove(node.body[i]); }
}
return res;
}
function _removeChild(parEl, el, immediate) {
var node = el._node, hooks = node.hooks, vm = node.vm;
if ((node.flags & DEEP_REMOVE) === DEEP_REMOVE && isArr(node.body)) {
// var parEl = node.el;
for (var i = 0; i < node.body.length; i++)
{ _removeChild(el, node.body[i].el); }
}
parEl.removeChild(el);
hooks && fireHooks("didRemove", node, null, immediate);
vm && vm.hooks && fireHooks("didUnmount", vm, null, immediate);
}
// todo: should delay parent unmount() by returning res prom?
function removeChild(parEl, el) {
var node = el._node, hooks = node.hooks;
var res = deepNotifyRemove(node);
if (res != null && isProm(res))
{ res.then(curry(_removeChild, [parEl, el, true])); }
else
{ _removeChild(parEl, el); }
}
function clearChildren(parent) {
var parEl = parent.el;
if ((parent.flags & DEEP_REMOVE) === 0)
{ parEl.textContent = null; }
else {
while (parEl.firstChild)
{ removeChild(parEl, parEl.firstChild); }
}
}
// todo: hooks
function insertBefore(parEl, el, refEl) {
var node = el._node, hooks = node.hooks, inDom = el.parentNode != null;
// el === refEl is asserted as a no-op insert called to fire hooks
var vm = (el === refEl || !inDom) && node.vm;
vm && vm.hooks && fireHooks("willMount", vm);
hooks && fireHooks(inDom ? "willReinsert" : "willInsert", node);
parEl.insertBefore(el, refEl);
hooks && fireHooks(inDom ? "didReinsert" : "didInsert", node);
vm && vm.hooks && fireHooks("didMount", vm);
}
function insertAfter(parEl, el, refEl) {
insertBefore(parEl, el, refEl ? nextSib(refEl) : null);
}
var globalCfg = {
onevent: noop,
};
function config(newCfg) {
assignObj(globalCfg, newCfg);
}
function bindEv(el, type, fn) {
// DEBUG && console.log("addEventListener");
el[type] = fn;
}
function handle(e, fn, args) {
var node = closestVNode(e.target);
var vm = getVm(node);
var out = fn.apply(null, args.concat(e, node, vm));
globalCfg.onevent.apply(null, [e, node, vm].concat(args));
if (out === false) {
e.preventDefault();
e.stopPropagation();
}
}
function wrapHandler(fn, args) {
// console.log("wrapHandler");
return function wrap(e) {
handle(e, fn, args);
};
}
// delagated handlers {".moo": [fn, a, b]}, {".moo": fn}
function wrapHandlers(hash) {
// console.log("wrapHandlers");
return function wrap(e) {
for (var sel in hash) {
if (e.target.matches(sel)) {
var hnd = hash[sel];
var isarr = isArr(hnd);
var fn = isarr ? hnd[0] : hnd;
var args = isarr ? hnd.slice(1) : [];
handle(e, fn, args);
}
}
}
}
// could merge with on*
function patchEvent(node, name, nval, oval) {
if (nval === oval)
{ return; }
{
if (isFunc(nval) && isFunc(oval) && oval.name == nval.name)
{ devNotify("INLINE_HANDLER", [node, oval, nval]); }
}
var el = node.el;
// param'd eg onclick: [myFn, 1, 2, 3...]
if (isArr(nval)) {
{
if (oval != null && !isArr(oval))
{ devNotify("MISMATCHED_HANDLER", [node, oval, nval]); }
}
var diff = oval == null || !cmpArr(nval, oval);
diff && bindEv(el, name, wrapHandler(nval[0], nval.slice(1)));
}
// basic onclick: myFn (or extracted)
else if (isFunc(nval) && nval !== oval) {
{
if (oval != null && !isFunc(oval))
{ devNotify("MISMATCHED_HANDLER", [node, oval, nval]); }
}
bindEv(el, name, wrapHandler(nval, []));
}
// delegated onclick: {".sel": myFn} & onclick: {".sel": [myFn, 1, 2, 3]}
else // isPlainObj, TODO:, diff with old/clean
{ bindEv(el, name, wrapHandlers(nval)); }
}
function remAttr(node, name, asProp) {
if (asProp)
{ node.el[name] = ""; }
else
{ node.el.removeAttribute(name); }
}
// setAttr
// diff, ".", "on*", bool vals, skip _*, value/checked/selected selectedIndex
function setAttr(node, name, val, asProp) {
var el = node.el;
if (val == null)
{ remAttr(node, name); } //, asProp? // will also removeAttr of style: null
else if (node.ns != null)
{ el.setAttribute(name, val); }
else if (name === "class")
{ el.className = val; }
else if (name === "id" || typeof val === "boolean" || asProp)
{ el[name] = val; }
else if (name[0] === ".")
{ el[name.substr(1)] = val; }
else
{ el.setAttribute(name, val); }
}
function patchAttrs(vnode, donor) {
var nattrs = vnode.attrs || emptyObj;
var oattrs = donor.attrs || emptyObj;
for (var key in nattrs) {
var nval = nattrs[key];
var isDyn = isDynProp(vnode.tag, key);
var oval = isDyn ? vnode.el[key] : oattrs[key];
if (isStream(nval))
{ nattrs[key] = nval = hookStream(nval, getVm(vnode)); }
if (nval === oval) {}
else if (isStyleProp(key))
{ patchStyle(vnode, donor); }
else if (isSplProp(key)) {}
else if (isEvProp(key))
{ patchEvent(vnode, key, nval, oval); }
else
{ setAttr(vnode, key, nval, isDyn); }
}
// TODO: handle key[0] === "."
// should bench style.cssText = "" vs removeAttribute("style")
for (var key in oattrs) {
!(key in nattrs) &&
!isSplProp(key) &&
remAttr(vnode, key, isDynProp(vnode.tag, key) || isEvProp(key));
}
}
function createView(view, model, key, opts) {
if (view.type === VVIEW) {
model = view.model;
key = view.key;
opts = view.opts;
view = view.view;
}
else if (view.prototype._isClass)
{ return new view(model, key, opts); }
return new ViewModel(view, model, key, opts);
}
//import { XML_NS, XLINK_NS } from './defineSvgElement';
// TODO: DRY this out. reusing normal patchAttrs here negatively affects V8's JIT
function patchAttrs2(vnode) {
var nattrs = vnode.attrs;
for (var key in nattrs) {
var nval = nattrs[key];
var isDyn = isDynProp(vnode.tag, key);
if (isStream(nval))
{ nattrs[key] = nval = hookStream(nval, getVm(vnode)); }
if (isStyleProp(key))
{ patchStyle(vnode); }
else if (isSplProp(key)) {}
else if (isEvProp(key))
{ patchEvent(vnode, key, nval); }
else if (nval != null)
{ setAttr(vnode, key, nval, isDyn); }
}
}
function hydrateBody(vnode) {
for (var i = 0; i < vnode.body.length; i++) {
var vnode2 = vnode.body[i];
var type2 = vnode2.type;
// ELEMENT,TEXT,COMMENT
if (type2 <= COMMENT)
{ insertBefore(vnode.el, hydrate(vnode2)); } // vnode.el.appendChild(hydrate(vnode2))
else if (type2 === VVIEW) {
var vm = createView(vnode2.view, vnode2.model, vnode2.key, vnode2.opts)._redraw(vnode, i, false); // todo: handle new model updates
type2 = vm.node.type;
insertBefore(vnode.el, hydrate(vm.node));
}
else if (type2 === VMODEL) {
var vm = vnode2.vm;
vm._redraw(vnode, i); // , false
type2 = vm.node.type;
insertBefore(vnode.el, vm.node.el); // , hydrate(vm.node)
}
}
}
// TODO: DRY this out. reusing normal patch here negatively affects V8's JIT
function hydrate(vnode, withEl) {
if (vnode.el == null) {
if (vnode.type === ELEMENT) {
vnode.el = withEl || createElement(vnode.tag, vnode.ns);
// if (vnode.tag === "svg")
// vnode.el.setAttributeNS(XML_NS, 'xmlns:xlink', XLINK_NS);
if (vnode.attrs != null)
{ patchAttrs2(vnode); }
if (isArr(vnode.body))
{ hydrateBody(vnode); }
else if (vnode.body != null && vnode.body !== "") {
if (vnode.raw)
{ vnode.el.innerHTML = vnode.body; }
else
{ vnode.el.textContent = vnode.body; }
}
}
else if (vnode.type === TEXT)
{ vnode.el = withEl || createTextNode(vnode.body); }
else if (vnode.type === COMMENT)
{ vnode.el = withEl || createComment(vnode.body); }
}
vnode.el._node = vnode;
return vnode.el;
}
function nextNode(node, body) {
return body[node.idx + 1];
}
function prevNode(node, body) {
return body[node.idx - 1];
}
function parentNode(node) {
return node.parent;
}
function tmpEdges(fn, parEl, lftSib, rgtSib) {
// get outer immute edges
var lftLft = prevSib(lftSib);
var rgtRgt = nextSib(rgtSib);
fn(lftLft, rgtRgt);
return {
lftSib: lftLft ? nextSib(lftLft) : parEl.firstChild,
rgtSib: rgtRgt ? prevSib(rgtRgt) : parEl.lastChild,
};
}
function headTailTry(parEl, lftSib, lftNode, rgtSib, rgtNode) {
var areAdjacent = rgtNode.idx === lftNode.idx + 1;
var headToTail = areAdjacent ? false : lftSib._node === rgtNode;
var tailToHead = areAdjacent ? true : rgtSib._node === lftNode;
if (headToTail || tailToHead) {
return tmpEdges(function(lftLft, rgtRgt) {
if (tailToHead)
{ insertBefore(parEl, rgtSib, lftSib); }
if (headToTail)
{ insertBefore(parEl, lftSib, rgtRgt); }
}, parEl, lftSib, rgtSib);
}
return null;
}
// init vm,
// selection sort of DOM (cause move cost >> cmp cost)
// todo: skip removed
function sortDOM(parEl, lftSib, rgtSib, cmpFn) {
// DEBUG && console.log("selection sort!");
return tmpEdges(function(lftLft, rgtRgt) {
var min;
for (var i = lftSib; i !== rgtRgt; i = nextSib(i)) {
lftSib = min = i;
for (var j = nextSib(i); j !== rgtRgt; j = nextSib(j)) {
if (cmpFn(min, j) > 0)
{ min = j; }
}
if (min === i)
{ continue; }
insertBefore(parEl, min, lftSib);
i = min;
}
}, parEl, lftSib, rgtSib);
}
function cmpElNodeIdx(a, b) {
return a._node.idx - b._node.idx;
}
function syncChildren(node, donor) {
var parEl = node.el,
body = node.body,
obody = donor.body,
lftNode = body[0],