1 | (function (global, factory) {
|
2 | typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
3 | typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
4 | (factory((global.preact = global.preact || {})));
|
5 | }(this, function (exports) {
|
6 | function VNode(nodeName, attributes, children) {
|
7 |
|
8 | this.nodeName = nodeName;
|
9 |
|
10 |
|
11 | this.attributes = attributes;
|
12 |
|
13 |
|
14 | this.children = children;
|
15 |
|
16 |
|
17 | this.key = attributes && attributes.key;
|
18 | }
|
19 |
|
20 | var options = {
|
21 |
|
22 | |
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | |
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | };
|
43 |
|
44 | var stack = [];
|
45 |
|
46 | |
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | function h(nodeName, attributes) {
|
57 | var children = [],
|
58 | lastSimple,
|
59 | child,
|
60 | simple,
|
61 | i;
|
62 | for (i = arguments.length; i-- > 2;) {
|
63 | stack.push(arguments[i]);
|
64 | }
|
65 | if (attributes && attributes.children) {
|
66 | if (!stack.length) stack.push(attributes.children);
|
67 | delete attributes.children;
|
68 | }
|
69 | while (stack.length) {
|
70 | if ((child = stack.pop()) instanceof Array) {
|
71 | for (i = child.length; i--;) stack.push(child[i]);
|
72 | } else if (child != null && child !== false) {
|
73 | if (typeof child == 'number' || child === true) child = String(child);
|
74 | simple = typeof child == 'string';
|
75 | if (simple && lastSimple) {
|
76 | children[children.length - 1] += child;
|
77 | } else {
|
78 | children.push(child);
|
79 | lastSimple = simple;
|
80 | }
|
81 | }
|
82 | }
|
83 |
|
84 | var p = new VNode(nodeName, attributes || undefined, children);
|
85 |
|
86 |
|
87 | if (options.vnode) options.vnode(p);
|
88 |
|
89 | return p;
|
90 | }
|
91 |
|
92 | function extend(obj, props) {
|
93 | if (props) {
|
94 | for (var i in props) {
|
95 | obj[i] = props[i];
|
96 | }
|
97 | }
|
98 | return obj;
|
99 | }
|
100 |
|
101 | |
102 |
|
103 |
|
104 |
|
105 | function clone(obj) {
|
106 | return extend({}, obj);
|
107 | }
|
108 |
|
109 | |
110 |
|
111 |
|
112 |
|
113 | function delve(obj, key) {
|
114 | for (var p = key.split('.'), i = 0; i < p.length && obj; i++) {
|
115 | obj = obj[p[i]];
|
116 | }
|
117 | return obj;
|
118 | }
|
119 |
|
120 |
|
121 |
|
122 | function isFunction(obj) {
|
123 | return 'function' === typeof obj;
|
124 | }
|
125 |
|
126 |
|
127 |
|
128 | function isString(obj) {
|
129 | return 'string' === typeof obj;
|
130 | }
|
131 |
|
132 | |
133 |
|
134 |
|
135 |
|
136 | function hashToClassName(c) {
|
137 | var str = '';
|
138 | for (var prop in c) {
|
139 | if (c[prop]) {
|
140 | if (str) str += ' ';
|
141 | str += prop;
|
142 | }
|
143 | }
|
144 | return str;
|
145 | }
|
146 |
|
147 |
|
148 | var lcCache = {};
|
149 | var toLowerCase = function toLowerCase(s) {
|
150 | return lcCache[s] || (lcCache[s] = s.toLowerCase());
|
151 | };
|
152 |
|
153 | |
154 |
|
155 |
|
156 | var resolved = typeof Promise !== 'undefined' && Promise.resolve();
|
157 | var defer = resolved ? function (f) {
|
158 | resolved.then(f);
|
159 | } : setTimeout;
|
160 |
|
161 | function cloneElement(vnode, props) {
|
162 | return h(vnode.nodeName, extend(clone(vnode.attributes), props), arguments.length > 2 ? [].slice.call(arguments, 2) : vnode.children);
|
163 | }
|
164 |
|
165 | var EMPTY = {};
|
166 |
|
167 | var ATTR_KEY = typeof Symbol !== 'undefined' ? Symbol['for']('preactattr') : '__preactattr_';
|
168 |
|
169 |
|
170 | var NON_DIMENSION_PROPS = {
|
171 | boxFlex: 1, boxFlexGroup: 1, columnCount: 1, fillOpacity: 1, flex: 1, flexGrow: 1,
|
172 | flexPositive: 1, flexShrink: 1, flexNegative: 1, fontWeight: 1, lineClamp: 1, lineHeight: 1,
|
173 | opacity: 1, order: 1, orphans: 1, strokeOpacity: 1, widows: 1, zIndex: 1, zoom: 1
|
174 | };
|
175 |
|
176 |
|
177 | var NON_BUBBLING_EVENTS = { blur: 1, error: 1, focus: 1, load: 1, resize: 1, scroll: 1 };
|
178 |
|
179 | |
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 | function createLinkedState(component, key, eventPath) {
|
188 | var path = key.split('.');
|
189 | return function (e) {
|
190 | var t = e && e.target || this,
|
191 | state = {},
|
192 | obj = state,
|
193 | v = isString(eventPath) ? delve(e, eventPath) : t.nodeName ? t.type.match(/^che|rad/) ? t.checked : t.value : e,
|
194 | i = 0;
|
195 | for (; i < path.length - 1; i++) {
|
196 | obj = obj[path[i]] || (obj[path[i]] = !i && component.state[path[i]] || {});
|
197 | }
|
198 | obj[path[i]] = v;
|
199 | component.setState(state);
|
200 | };
|
201 | }
|
202 |
|
203 |
|
204 |
|
205 |
|
206 | var items = [];
|
207 |
|
208 | function enqueueRender(component) {
|
209 | if (!component._dirty && (component._dirty = true) && items.push(component) == 1) {
|
210 | (options.debounceRendering || defer)(rerender);
|
211 | }
|
212 | }
|
213 |
|
214 | function rerender() {
|
215 | var p,
|
216 | list = items;
|
217 | items = [];
|
218 | while (p = list.pop()) {
|
219 | if (p._dirty) renderComponent(p);
|
220 | }
|
221 | }
|
222 |
|
223 | |
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 | function isFunctionalComponent(vnode) {
|
231 | var nodeName = vnode && vnode.nodeName;
|
232 | return nodeName && isFunction(nodeName) && !(nodeName.prototype && nodeName.prototype.render);
|
233 | }
|
234 |
|
235 | |
236 |
|
237 |
|
238 |
|
239 |
|
240 | function buildFunctionalComponent(vnode, context) {
|
241 | return vnode.nodeName(getNodeProps(vnode), context || EMPTY);
|
242 | }
|
243 |
|
244 | |
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 | function isSameNodeType(node, vnode) {
|
251 | if (isString(vnode)) {
|
252 | return node instanceof Text;
|
253 | }
|
254 | if (isString(vnode.nodeName)) {
|
255 | return isNamedNode(node, vnode.nodeName);
|
256 | }
|
257 | if (isFunction(vnode.nodeName)) {
|
258 | return node._componentConstructor === vnode.nodeName || isFunctionalComponent(vnode);
|
259 | }
|
260 | }
|
261 |
|
262 | function isNamedNode(node, nodeName) {
|
263 | return node.normalizedNodeName === nodeName || toLowerCase(node.nodeName) === toLowerCase(nodeName);
|
264 | }
|
265 |
|
266 | |
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 | function getNodeProps(vnode) {
|
275 | var props = clone(vnode.attributes);
|
276 | props.children = vnode.children;
|
277 |
|
278 | var defaultProps = vnode.nodeName.defaultProps;
|
279 | if (defaultProps) {
|
280 | for (var i in defaultProps) {
|
281 | if (props[i] === undefined) {
|
282 | props[i] = defaultProps[i];
|
283 | }
|
284 | }
|
285 | }
|
286 |
|
287 | return props;
|
288 | }
|
289 |
|
290 |
|
291 |
|
292 | function removeNode(node) {
|
293 | var p = node.parentNode;
|
294 | if (p) p.removeChild(node);
|
295 | }
|
296 |
|
297 | |
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 |
|
304 |
|
305 |
|
306 | function setAccessor(node, name, old, value, isSvg) {
|
307 |
|
308 | if (name === 'className') name = 'class';
|
309 |
|
310 | if (name === 'class' && value && typeof value === 'object') {
|
311 | value = hashToClassName(value);
|
312 | }
|
313 |
|
314 | if (name === 'key') {
|
315 |
|
316 | } else if (name === 'class' && !isSvg) {
|
317 | node.className = value || '';
|
318 | } else if (name === 'style') {
|
319 | if (!value || isString(value) || isString(old)) {
|
320 | node.style.cssText = value || '';
|
321 | }
|
322 | if (value && typeof value === 'object') {
|
323 | if (!isString(old)) {
|
324 | for (var i in old) {
|
325 | if (!(i in value)) node.style[i] = '';
|
326 | }
|
327 | }
|
328 | for (var i in value) {
|
329 | node.style[i] = typeof value[i] === 'number' && !NON_DIMENSION_PROPS[i] ? value[i] + 'px' : value[i];
|
330 | }
|
331 | }
|
332 | } else if (name === 'dangerouslySetInnerHTML') {
|
333 | if (value) node.innerHTML = value.__html;
|
334 | } else if (name[0] == 'o' && name[1] == 'n') {
|
335 | var l = node._listeners || (node._listeners = {});
|
336 | name = toLowerCase(name.substring(2));
|
337 |
|
338 |
|
339 | if (value) {
|
340 | if (!l[name]) node.addEventListener(name, eventProxy, !!NON_BUBBLING_EVENTS[name]);
|
341 | } else if (l[name]) {
|
342 | node.removeEventListener(name, eventProxy, !!NON_BUBBLING_EVENTS[name]);
|
343 | }
|
344 | l[name] = value;
|
345 | } else if (name !== 'list' && name !== 'type' && !isSvg && name in node) {
|
346 | setProperty(node, name, value == null ? '' : value);
|
347 | if (value == null || value === false) node.removeAttribute(name);
|
348 | } else {
|
349 | var ns = isSvg && name.match(/^xlink\:?(.+)/);
|
350 | if (value == null || value === false) {
|
351 | if (ns) node.removeAttributeNS('http://www.w3.org/1999/xlink', toLowerCase(ns[1]));else node.removeAttribute(name);
|
352 | } else if (typeof value !== 'object' && !isFunction(value)) {
|
353 | if (ns) node.setAttributeNS('http://www.w3.org/1999/xlink', toLowerCase(ns[1]), value);else node.setAttribute(name, value);
|
354 | }
|
355 | }
|
356 | }
|
357 |
|
358 | |
359 |
|
360 |
|
361 | function setProperty(node, name, value) {
|
362 | try {
|
363 | node[name] = value;
|
364 | } catch (e) {}
|
365 | }
|
366 |
|
367 | |
368 |
|
369 |
|
370 | function eventProxy(e) {
|
371 | return this._listeners[e.type](options.event && options.event(e) || e);
|
372 | }
|
373 |
|
374 |
|
375 |
|
376 | var nodes = {};
|
377 |
|
378 | function collectNode(node) {
|
379 | removeNode(node);
|
380 |
|
381 | if (node instanceof Element) {
|
382 | node._component = node._componentConstructor = null;
|
383 |
|
384 | var _name = node.normalizedNodeName || toLowerCase(node.nodeName);
|
385 | (nodes[_name] || (nodes[_name] = [])).push(node);
|
386 | }
|
387 | }
|
388 |
|
389 | function createNode(nodeName, isSvg) {
|
390 | var name = toLowerCase(nodeName),
|
391 | node = nodes[name] && nodes[name].pop() || (isSvg ? document.createElementNS('http://www.w3.org/2000/svg', nodeName) : document.createElement(nodeName));
|
392 | node.normalizedNodeName = name;
|
393 | return node;
|
394 | }
|
395 |
|
396 |
|
397 | var mounts = [];
|
398 |
|
399 |
|
400 | var diffLevel = 0;
|
401 |
|
402 | var isSvgMode = false;
|
403 |
|
404 | function flushMounts() {
|
405 | var c;
|
406 | while (c = mounts.pop()) {
|
407 | if (options.afterMount) options.afterMount(c);
|
408 | if (c.componentDidMount) c.componentDidMount();
|
409 | }
|
410 | }
|
411 |
|
412 | |
413 |
|
414 |
|
415 |
|
416 |
|
417 |
|
418 |
|
419 | function diff(dom, vnode, context, mountAll, parent, componentRoot) {
|
420 | if (! diffLevel++) isSvgMode = parent instanceof SVGElement;
|
421 | var ret = idiff(dom, vnode, context, mountAll);
|
422 | if (parent && ret.parentNode !== parent) parent.appendChild(ret);
|
423 | if (! --diffLevel && !componentRoot) flushMounts();
|
424 | return ret;
|
425 | }
|
426 |
|
427 | function idiff(dom, vnode, context, mountAll) {
|
428 | var originalAttributes = vnode && vnode.attributes;
|
429 |
|
430 | while (isFunctionalComponent(vnode)) {
|
431 | vnode = buildFunctionalComponent(vnode, context);
|
432 | }
|
433 |
|
434 | if (vnode == null) vnode = '';
|
435 |
|
436 | if (isString(vnode)) {
|
437 | if (dom) {
|
438 | if (dom instanceof Text && dom.parentNode) {
|
439 | if (dom.nodeValue != vnode) {
|
440 | dom.nodeValue = vnode;
|
441 | }
|
442 | return dom;
|
443 | }
|
444 | recollectNodeTree(dom);
|
445 | }
|
446 | return document.createTextNode(vnode);
|
447 | }
|
448 |
|
449 | if (isFunction(vnode.nodeName)) {
|
450 | return buildComponentFromVNode(dom, vnode, context, mountAll);
|
451 | }
|
452 |
|
453 | var out = dom,
|
454 | nodeName = vnode.nodeName,
|
455 | prevSvgMode = isSvgMode,
|
456 | vchildren = vnode.children;
|
457 |
|
458 | if (!isString(nodeName)) {
|
459 | nodeName = String(nodeName);
|
460 | }
|
461 |
|
462 | isSvgMode = nodeName === 'svg' ? true : nodeName === 'foreignObject' ? false : isSvgMode;
|
463 |
|
464 | if (!dom) {
|
465 | out = createNode(nodeName, isSvgMode);
|
466 | } else if (!isNamedNode(dom, nodeName)) {
|
467 | out = createNode(nodeName, isSvgMode);
|
468 |
|
469 | while (dom.firstChild) out.appendChild(dom.firstChild);
|
470 |
|
471 | recollectNodeTree(dom);
|
472 | }
|
473 |
|
474 |
|
475 | if (vchildren && vchildren.length === 1 && typeof vchildren[0] === 'string' && out.childNodes.length === 1 && out.firstChild instanceof Text) {
|
476 | if (out.firstChild.nodeValue != vchildren[0]) {
|
477 | out.firstChild.nodeValue = vchildren[0];
|
478 | }
|
479 | } else if (vchildren && vchildren.length || out.firstChild) {
|
480 | innerDiffNode(out, vchildren, context, mountAll);
|
481 | }
|
482 |
|
483 | var props = out[ATTR_KEY];
|
484 | if (!props) {
|
485 | out[ATTR_KEY] = props = {};
|
486 | for (var a = out.attributes, i = a.length; i--;) {
|
487 | props[a[i].name] = a[i].value;
|
488 | }
|
489 | }
|
490 |
|
491 | diffAttributes(out, vnode.attributes, props);
|
492 |
|
493 | if (originalAttributes && typeof originalAttributes.ref === 'function') {
|
494 | (props.ref = originalAttributes.ref)(out);
|
495 | }
|
496 |
|
497 | isSvgMode = prevSvgMode;
|
498 |
|
499 | return out;
|
500 | }
|
501 |
|
502 |
|
503 | function innerDiffNode(dom, vchildren, context, mountAll) {
|
504 | var originalChildren = dom.childNodes,
|
505 | children = [],
|
506 | keyed = {},
|
507 | keyedLen = 0,
|
508 | min = 0,
|
509 | len = originalChildren.length,
|
510 | childrenLen = 0,
|
511 | vlen = vchildren && vchildren.length,
|
512 | j,
|
513 | c,
|
514 | vchild,
|
515 | child;
|
516 |
|
517 | if (len) {
|
518 | for (var i = 0; i < len; i++) {
|
519 | var _child = originalChildren[i],
|
520 | key = vlen ? (c = _child._component) ? c.__key : (c = _child[ATTR_KEY]) ? c.key : null : null;
|
521 | if (key || key === 0) {
|
522 | keyedLen++;
|
523 | keyed[key] = _child;
|
524 | } else {
|
525 | children[childrenLen++] = _child;
|
526 | }
|
527 | }
|
528 | }
|
529 |
|
530 | if (vlen) {
|
531 | for (var i = 0; i < vlen; i++) {
|
532 | vchild = vchildren[i];
|
533 | child = null;
|
534 |
|
535 |
|
536 |
|
537 |
|
538 |
|
539 |
|
540 | var key = vchild.key;
|
541 | if (key != null) {
|
542 | if (keyedLen && key in keyed) {
|
543 | child = keyed[key];
|
544 | keyed[key] = undefined;
|
545 | keyedLen--;
|
546 | }
|
547 | }
|
548 |
|
549 | else if (!child && min < childrenLen) {
|
550 | for (j = min; j < childrenLen; j++) {
|
551 | c = children[j];
|
552 | if (c && isSameNodeType(c, vchild)) {
|
553 | child = c;
|
554 | children[j] = undefined;
|
555 | if (j === childrenLen - 1) childrenLen--;
|
556 | if (j === min) min++;
|
557 | break;
|
558 | }
|
559 | }
|
560 | if (!child && min < childrenLen && isFunction(vchild.nodeName) && mountAll) {
|
561 | child = children[min];
|
562 | children[min++] = undefined;
|
563 | }
|
564 | }
|
565 |
|
566 |
|
567 | child = idiff(child, vchild, context, mountAll);
|
568 |
|
569 | if (child && child !== dom && child !== originalChildren[i]) {
|
570 | dom.insertBefore(child, originalChildren[i] || null);
|
571 | }
|
572 | }
|
573 | }
|
574 |
|
575 | if (keyedLen) {
|
576 | for (var i in keyed) {
|
577 | if (keyed[i]) recollectNodeTree(keyed[i]);
|
578 | }
|
579 | }
|
580 |
|
581 |
|
582 | if (min < childrenLen) {
|
583 | removeOrphanedChildren(children);
|
584 | }
|
585 | }
|
586 |
|
587 |
|
588 |
|
589 | function removeOrphanedChildren(children, unmountOnly) {
|
590 | for (var i = children.length; i--;) {
|
591 | if (children[i]) {
|
592 | recollectNodeTree(children[i], unmountOnly);
|
593 | }
|
594 | }
|
595 | }
|
596 |
|
597 |
|
598 |
|
599 | function recollectNodeTree(node, unmountOnly) {
|
600 |
|
601 |
|
602 |
|
603 |
|
604 | var component = node._component;
|
605 | if (component) {
|
606 | unmountComponent(component, !unmountOnly);
|
607 | } else {
|
608 | if (node[ATTR_KEY] && node[ATTR_KEY].ref) node[ATTR_KEY].ref(null);
|
609 |
|
610 | if (!unmountOnly) {
|
611 | collectNode(node);
|
612 | }
|
613 |
|
614 | if (node.childNodes && node.childNodes.length) {
|
615 | removeOrphanedChildren(node.childNodes, unmountOnly);
|
616 | }
|
617 | }
|
618 | }
|
619 |
|
620 |
|
621 | function diffAttributes(dom, attrs, old) {
|
622 | for (var _name in old) {
|
623 | if (!(attrs && _name in attrs) && old[_name] != null) {
|
624 | setAccessor(dom, _name, old[_name], old[_name] = undefined, isSvgMode);
|
625 | }
|
626 | }
|
627 |
|
628 |
|
629 | if (attrs) {
|
630 | for (var _name2 in attrs) {
|
631 | if (_name2 !== 'children' && _name2 !== 'innerHTML' && (!(_name2 in old) || attrs[_name2] !== (_name2 === 'value' || _name2 === 'checked' ? dom[_name2] : old[_name2]))) {
|
632 | setAccessor(dom, _name2, old[_name2], old[_name2] = attrs[_name2], isSvgMode);
|
633 | }
|
634 | }
|
635 | }
|
636 | }
|
637 |
|
638 | |
639 |
|
640 |
|
641 |
|
642 | var components = {};
|
643 |
|
644 | function collectComponent(component) {
|
645 | var name = component.constructor.name,
|
646 | list = components[name];
|
647 | if (list) list.push(component);else components[name] = [component];
|
648 | }
|
649 |
|
650 | function createComponent(Ctor, props, context) {
|
651 | var inst = new Ctor(props, context),
|
652 | list = components[Ctor.name];
|
653 | Component.call(inst, props, context);
|
654 | if (list) {
|
655 | for (var i = list.length; i--;) {
|
656 | if (list[i].constructor === Ctor) {
|
657 | inst.nextBase = list[i].nextBase;
|
658 | list.splice(i, 1);
|
659 | break;
|
660 | }
|
661 | }
|
662 | }
|
663 | return inst;
|
664 | }
|
665 |
|
666 | |
667 |
|
668 |
|
669 |
|
670 |
|
671 |
|
672 |
|
673 | function setComponentProps(component, props, opts, context, mountAll) {
|
674 | if (component._disable) return;
|
675 | component._disable = true;
|
676 |
|
677 | if (component.__ref = props.ref) delete props.ref;
|
678 | if (component.__key = props.key) delete props.key;
|
679 |
|
680 | if (!component.base || mountAll) {
|
681 | if (component.componentWillMount) component.componentWillMount();
|
682 | } else if (component.componentWillReceiveProps) {
|
683 | component.componentWillReceiveProps(props, context);
|
684 | }
|
685 |
|
686 | if (context && context !== component.context) {
|
687 | if (!component.prevContext) component.prevContext = component.context;
|
688 | component.context = context;
|
689 | }
|
690 |
|
691 | if (!component.prevProps) component.prevProps = component.props;
|
692 | component.props = props;
|
693 |
|
694 | component._disable = false;
|
695 |
|
696 | if (opts !== 0) {
|
697 | if (opts === 1 || options.syncComponentUpdates !== false || !component.base) {
|
698 | renderComponent(component, 1, mountAll);
|
699 | } else {
|
700 | enqueueRender(component);
|
701 | }
|
702 | }
|
703 |
|
704 | if (component.__ref) component.__ref(component);
|
705 | }
|
706 |
|
707 | |
708 |
|
709 |
|
710 |
|
711 |
|
712 |
|
713 |
|
714 | function renderComponent(component, opts, mountAll, isChild) {
|
715 | if (component._disable) return;
|
716 |
|
717 | var skip,
|
718 | rendered,
|
719 | props = component.props,
|
720 | state = component.state,
|
721 | context = component.context,
|
722 | previousProps = component.prevProps || props,
|
723 | previousState = component.prevState || state,
|
724 | previousContext = component.prevContext || context,
|
725 | isUpdate = component.base,
|
726 | nextBase = component.nextBase,
|
727 | initialBase = isUpdate || nextBase,
|
728 | initialChildComponent = component._component,
|
729 | inst,
|
730 | cbase;
|
731 |
|
732 |
|
733 | if (isUpdate) {
|
734 | component.props = previousProps;
|
735 | component.state = previousState;
|
736 | component.context = previousContext;
|
737 | if (opts !== 2 && component.shouldComponentUpdate && component.shouldComponentUpdate(props, state, context) === false) {
|
738 | skip = true;
|
739 | } else if (component.componentWillUpdate) {
|
740 | component.componentWillUpdate(props, state, context);
|
741 | }
|
742 | component.props = props;
|
743 | component.state = state;
|
744 | component.context = context;
|
745 | }
|
746 |
|
747 | component.prevProps = component.prevState = component.prevContext = component.nextBase = null;
|
748 | component._dirty = false;
|
749 |
|
750 | if (!skip) {
|
751 | if (component.render) rendered = component.render(props, state, context);
|
752 |
|
753 |
|
754 | if (component.getChildContext) {
|
755 | context = extend(clone(context), component.getChildContext());
|
756 | }
|
757 |
|
758 | while (isFunctionalComponent(rendered)) {
|
759 | rendered = buildFunctionalComponent(rendered, context);
|
760 | }
|
761 |
|
762 | var childComponent = rendered && rendered.nodeName,
|
763 | toUnmount,
|
764 | base;
|
765 |
|
766 | if (isFunction(childComponent)) {
|
767 |
|
768 |
|
769 | inst = initialChildComponent;
|
770 | var childProps = getNodeProps(rendered);
|
771 |
|
772 | if (inst && inst.constructor === childComponent) {
|
773 | setComponentProps(inst, childProps, 1, context);
|
774 | } else {
|
775 | toUnmount = inst;
|
776 |
|
777 | inst = createComponent(childComponent, childProps, context);
|
778 | inst.nextBase = inst.nextBase || nextBase;
|
779 | inst._parentComponent = component;
|
780 | component._component = inst;
|
781 | setComponentProps(inst, childProps, 0, context);
|
782 | renderComponent(inst, 1, mountAll, true);
|
783 | }
|
784 |
|
785 | base = inst.base;
|
786 | } else {
|
787 | cbase = initialBase;
|
788 |
|
789 |
|
790 | toUnmount = initialChildComponent;
|
791 | if (toUnmount) {
|
792 | cbase = component._component = null;
|
793 | }
|
794 |
|
795 | if (initialBase || opts === 1) {
|
796 | if (cbase) cbase._component = null;
|
797 | base = diff(cbase, rendered, context, mountAll || !isUpdate, initialBase && initialBase.parentNode, true);
|
798 | }
|
799 | }
|
800 |
|
801 | if (initialBase && base !== initialBase && inst !== initialChildComponent) {
|
802 | var baseParent = initialBase.parentNode;
|
803 | if (baseParent && base !== baseParent) {
|
804 | baseParent.replaceChild(base, initialBase);
|
805 |
|
806 | if (!toUnmount) {
|
807 | initialBase._component = null;
|
808 | recollectNodeTree(initialBase);
|
809 | }
|
810 | }
|
811 | }
|
812 |
|
813 | if (toUnmount) {
|
814 | unmountComponent(toUnmount, base !== initialBase);
|
815 | }
|
816 |
|
817 | component.base = base;
|
818 | if (base && !isChild) {
|
819 | var componentRef = component,
|
820 | t = component;
|
821 | while (t = t._parentComponent) {
|
822 | (componentRef = t).base = base;
|
823 | }
|
824 | base._component = componentRef;
|
825 | base._componentConstructor = componentRef.constructor;
|
826 | }
|
827 | }
|
828 |
|
829 | if (!isUpdate || mountAll) {
|
830 | mounts.unshift(component);
|
831 | } else if (!skip) {
|
832 | if (component.componentDidUpdate) {
|
833 | component.componentDidUpdate(previousProps, previousState, previousContext);
|
834 | }
|
835 | if (options.afterUpdate) options.afterUpdate(component);
|
836 | }
|
837 |
|
838 | var cb = component._renderCallbacks,
|
839 | fn;
|
840 | if (cb) while (fn = cb.pop()) fn.call(component);
|
841 |
|
842 | if (!diffLevel && !isChild) flushMounts();
|
843 | }
|
844 |
|
845 | |
846 |
|
847 |
|
848 |
|
849 |
|
850 |
|
851 |
|
852 | function buildComponentFromVNode(dom, vnode, context, mountAll) {
|
853 | var c = dom && dom._component,
|
854 | oldDom = dom,
|
855 | isDirectOwner = c && dom._componentConstructor === vnode.nodeName,
|
856 | isOwner = isDirectOwner,
|
857 | props = getNodeProps(vnode);
|
858 | while (c && !isOwner && (c = c._parentComponent)) {
|
859 | isOwner = c.constructor === vnode.nodeName;
|
860 | }
|
861 |
|
862 | if (c && isOwner && (!mountAll || c._component)) {
|
863 | setComponentProps(c, props, 3, context, mountAll);
|
864 | dom = c.base;
|
865 | } else {
|
866 | if (c && !isDirectOwner) {
|
867 | unmountComponent(c, true);
|
868 | dom = oldDom = null;
|
869 | }
|
870 |
|
871 | c = createComponent(vnode.nodeName, props, context);
|
872 | if (dom && !c.nextBase) {
|
873 | c.nextBase = dom;
|
874 |
|
875 | oldDom = null;
|
876 | }
|
877 | setComponentProps(c, props, 1, context, mountAll);
|
878 | dom = c.base;
|
879 |
|
880 | if (oldDom && dom !== oldDom) {
|
881 | oldDom._component = null;
|
882 | recollectNodeTree(oldDom);
|
883 | }
|
884 | }
|
885 |
|
886 | return dom;
|
887 | }
|
888 |
|
889 | |
890 |
|
891 |
|
892 |
|
893 |
|
894 |
|
895 | function unmountComponent(component, remove) {
|
896 | if (options.beforeUnmount) options.beforeUnmount(component);
|
897 |
|
898 |
|
899 | var base = component.base;
|
900 |
|
901 | component._disable = true;
|
902 |
|
903 | if (component.componentWillUnmount) component.componentWillUnmount();
|
904 |
|
905 | component.base = null;
|
906 |
|
907 |
|
908 | var inner = component._component;
|
909 | if (inner) {
|
910 | unmountComponent(inner, remove);
|
911 | } else if (base) {
|
912 | if (base[ATTR_KEY] && base[ATTR_KEY].ref) base[ATTR_KEY].ref(null);
|
913 |
|
914 | component.nextBase = base;
|
915 |
|
916 | if (remove) {
|
917 | removeNode(base);
|
918 | collectComponent(component);
|
919 | }
|
920 | removeOrphanedChildren(base.childNodes, !remove);
|
921 | }
|
922 |
|
923 | if (component.__ref) component.__ref(null);
|
924 | if (component.componentDidUnmount) component.componentDidUnmount();
|
925 | }
|
926 |
|
927 | |
928 |
|
929 |
|
930 |
|
931 |
|
932 |
|
933 |
|
934 |
|
935 |
|
936 |
|
937 |
|
938 | function Component(props, context) {
|
939 |
|
940 | this._dirty = true;
|
941 |
|
942 |
|
943 |
|
944 |
|
945 |
|
946 | this.context = context;
|
947 |
|
948 | this.props = props;
|
949 |
|
950 | if (!this.state) this.state = {};
|
951 | }
|
952 |
|
953 | extend(Component.prototype, {
|
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 | linkState: function linkState(key, eventPath) {
|
987 | var c = this._linkedStates || (this._linkedStates = {});
|
988 | return c[key + eventPath] || (c[key + eventPath] = createLinkedState(this, key, eventPath));
|
989 | },
|
990 |
|
991 | |
992 |
|
993 |
|
994 | setState: function setState(state, callback) {
|
995 | var s = this.state;
|
996 | if (!this.prevState) this.prevState = clone(s);
|
997 | extend(s, isFunction(state) ? state(s, this.props) : state);
|
998 | if (callback) (this._renderCallbacks = this._renderCallbacks || []).push(callback);
|
999 | enqueueRender(this);
|
1000 | },
|
1001 |
|
1002 | |
1003 |
|
1004 |
|
1005 | forceUpdate: function forceUpdate() {
|
1006 | renderComponent(this, 2);
|
1007 | },
|
1008 |
|
1009 | |
1010 |
|
1011 |
|
1012 |
|
1013 |
|
1014 |
|
1015 |
|
1016 | render: function render() {}
|
1017 |
|
1018 | });
|
1019 |
|
1020 | |
1021 |
|
1022 |
|
1023 |
|
1024 |
|
1025 |
|
1026 |
|
1027 |
|
1028 |
|
1029 |
|
1030 |
|
1031 |
|
1032 |
|
1033 |
|
1034 |
|
1035 |
|
1036 | function render(vnode, parent, merge) {
|
1037 | return diff(merge, vnode, {}, false, parent);
|
1038 | }
|
1039 |
|
1040 | exports.h = h;
|
1041 | exports.cloneElement = cloneElement;
|
1042 | exports.Component = Component;
|
1043 | exports.render = render;
|
1044 | exports.rerender = rerender;
|
1045 | exports.options = options;
|
1046 |
|
1047 | Object.defineProperty(exports, '__esModule', { value: true });
|
1048 | }));
|
1049 |
|