diff options
Diffstat (limited to 'lib/vendor/preact.js')
-rw-r--r-- | lib/vendor/preact.js | 70 |
1 files changed, 40 insertions, 30 deletions
diff --git a/lib/vendor/preact.js b/lib/vendor/preact.js index e0239355e..cfab56635 100644 --- a/lib/vendor/preact.js +++ b/lib/vendor/preact.js @@ -8,7 +8,7 @@ this.key = attributes && attributes.key; } function h(nodeName, attributes) { - var children, lastSimple, child, simple, i; + var lastSimple, child, simple, i, children = []; for (i = arguments.length; i-- > 2; ) stack.push(arguments[i]); if (attributes && attributes.children) { if (!stack.length) stack.push(attributes.children); @@ -18,7 +18,7 @@ if ('number' == typeof child || child === !0) child = String(child); simple = 'string' == typeof child; if (simple && lastSimple) children[children.length - 1] += child; else { - if (children) children.push(child); else children = [ child ]; + children.push(child); lastSimple = simple; } } @@ -55,16 +55,12 @@ return h(vnode.nodeName, extend(clone(vnode.attributes), props), arguments.length > 2 ? [].slice.call(arguments, 2) : vnode.children); } function createLinkedState(component, key, eventPath) { - var path = key.split('.'), p0 = path[0]; + var path = key.split('.'); return function(e) { - var _component$setState; - var i, t = e && e.currentTarget || this, s = component.state, obj = s, v = isString(eventPath) ? delve(e, eventPath) : t.nodeName ? (t.nodeName + t.type).match(/^input(che|rad)/i) ? t.checked : t.value : e; - if (path.length > 1) { - for (i = 0; i < path.length - 1; i++) obj = obj[path[i]] || (obj[path[i]] = {}); - obj[path[i]] = v; - v = s[p0]; - } - component.setState((_component$setState = {}, _component$setState[p0] = v, _component$setState)); + var t = e && e.target || this, state = {}, obj = state, v = isString(eventPath) ? delve(e, eventPath) : t.nodeName ? t.type.match(/^che|rad/) ? t.checked : t.value : e, i = 0; + for (;i < path.length - 1; i++) obj = obj[path[i]] || (obj[path[i]] = !i && component.state[path[i]] || {}); + obj[path[i]] = v; + component.setState(state); }; } function enqueueRender(component) { @@ -91,20 +87,20 @@ return node.normalizedNodeName === nodeName || toLowerCase(node.nodeName) === toLowerCase(nodeName); } function getNodeProps(vnode) { - var defaultProps = vnode.nodeName.defaultProps, props = clone(vnode.attributes); + var props = clone(vnode.attributes); + props.children = vnode.children; + var defaultProps = vnode.nodeName.defaultProps; if (defaultProps) for (var i in defaultProps) if (void 0 === props[i]) props[i] = defaultProps[i]; - if (vnode.children) props.children = vnode.children; return props; } function removeNode(node) { var p = node.parentNode; if (p) p.removeChild(node); } - function setAccessor(node, name, value, old, isSvg) { - node[ATTR_KEY][name] = value; + function setAccessor(node, name, old, value, isSvg) { if ('className' === name) name = 'class'; if ('class' === name && value && 'object' == typeof value) value = hashToClassName(value); - if ('key' === name || 'children' === name || 'innerHTML' === name) ; else if ('class' === name && !isSvg) node.className = value || ''; else if ('style' === name) { + if ('key' === name) ; else if ('class' === name && !isSvg) node.className = value || ''; else if ('style' === name) { if (!value || isString(value) || isString(old)) node.style.cssText = value || ''; if (value && 'object' == typeof value) { if (!isString(old)) for (var i in old) if (!(i in value)) node.style[i] = ''; @@ -150,7 +146,10 @@ } function flushMounts() { var c; - while (c = mounts.pop()) if (c.componentDidMount) c.componentDidMount(); + while (c = mounts.pop()) { + if (options.afterMount) options.afterMount(c); + if (c.componentDidMount) c.componentDidMount(); + } } function diff(dom, vnode, context, mountAll, parent, componentRoot) { if (!diffLevel++) isSvgMode = parent instanceof SVGElement; @@ -166,7 +165,7 @@ if (isString(vnode)) { if (dom) { if (dom instanceof Text && dom.parentNode) { - dom.nodeValue = vnode; + if (dom.nodeValue != vnode) dom.nodeValue = vnode; return dom; } recollectNodeTree(dom); @@ -174,7 +173,7 @@ return document.createTextNode(vnode); } if (isFunction(vnode.nodeName)) return buildComponentFromVNode(dom, vnode, context, mountAll); - var out = dom, nodeName = vnode.nodeName, prevSvgMode = isSvgMode; + var out = dom, nodeName = vnode.nodeName, prevSvgMode = isSvgMode, vchildren = vnode.children; if (!isString(nodeName)) nodeName = String(nodeName); isSvgMode = 'svg' === nodeName ? !0 : 'foreignObject' === nodeName ? !1 : isSvgMode; if (!dom) out = createNode(nodeName, isSvgMode); else if (!isNamedNode(dom, nodeName)) { @@ -182,7 +181,9 @@ while (dom.firstChild) out.appendChild(dom.firstChild); recollectNodeTree(dom); } - if (vnode.children && 1 === vnode.children.length && 'string' == typeof vnode.children[0] && 1 === out.childNodes.length && out.firstChild instanceof Text) out.firstChild.nodeValue = vnode.children[0]; else if (vnode.children || out.firstChild) innerDiffNode(out, vnode.children, context, mountAll); + if (vchildren && 1 === vchildren.length && 'string' == typeof vchildren[0] && 1 === out.childNodes.length && out.firstChild instanceof Text) { + if (out.firstChild.nodeValue != vchildren[0]) out.firstChild.nodeValue = vchildren[0]; + } else if (vchildren && vchildren.length || out.firstChild) innerDiffNode(out, vchildren, context, mountAll); var props = out[ATTR_KEY]; if (!props) { out[ATTR_KEY] = props = {}; @@ -246,8 +247,8 @@ } } function diffAttributes(dom, attrs, old) { - for (var _name in old) if (!(attrs && _name in attrs) && null != old[_name]) setAccessor(dom, _name, null, old[_name], isSvgMode); - if (attrs) for (var _name2 in attrs) if (!(_name2 in old) || attrs[_name2] !== ('value' === _name2 || 'checked' === _name2 ? dom[_name2] : old[_name2])) setAccessor(dom, _name2, attrs[_name2], old[_name2], isSvgMode); + for (var _name in old) if (!(attrs && _name in attrs) && null != old[_name]) setAccessor(dom, _name, old[_name], old[_name] = void 0, isSvgMode); + if (attrs) for (var _name2 in attrs) if (!('children' === _name2 || 'innerHTML' === _name2 || _name2 in old && attrs[_name2] === ('value' === _name2 || 'checked' === _name2 ? dom[_name2] : old[_name2]))) setAccessor(dom, _name2, old[_name2], old[_name2] = attrs[_name2], isSvgMode); } function collectComponent(component) { var name = component.constructor.name, list = components[name]; @@ -325,22 +326,27 @@ } if (initialBase && base !== initialBase && inst !== initialChildComponent) { var baseParent = initialBase.parentNode; - if (baseParent && base !== baseParent) baseParent.replaceChild(base, initialBase); - if (!cbase && !toUnmount && component._parentComponent) { - initialBase._component = null; - recollectNodeTree(initialBase); + if (baseParent && base !== baseParent) { + baseParent.replaceChild(base, initialBase); + if (!toUnmount) { + initialBase._component = null; + recollectNodeTree(initialBase); + } } } if (toUnmount) unmountComponent(toUnmount, base !== initialBase); component.base = base; if (base && !isChild) { var componentRef = component, t = component; - while (t = t._parentComponent) componentRef = t; + while (t = t._parentComponent) (componentRef = t).base = base; base._component = componentRef; base._componentConstructor = componentRef.constructor; } } - if (!isUpdate || mountAll) mounts.unshift(component); else if (!skip && component.componentDidUpdate) component.componentDidUpdate(previousProps, previousState, previousContext); + if (!isUpdate || mountAll) mounts.unshift(component); else if (!skip) { + if (component.componentDidUpdate) component.componentDidUpdate(previousProps, previousState, previousContext); + if (options.afterUpdate) options.afterUpdate(component); + } var fn, cb = component._renderCallbacks; if (cb) while (fn = cb.pop()) fn.call(component); if (!diffLevel && !isChild) flushMounts(); @@ -358,7 +364,10 @@ dom = oldDom = null; } c = createComponent(vnode.nodeName, props, context); - if (dom && !c.nextBase) c.nextBase = dom; + if (dom && !c.nextBase) { + c.nextBase = dom; + oldDom = null; + } setComponentProps(c, props, 1, context, mountAll); dom = c.base; if (oldDom && dom !== oldDom) { @@ -369,6 +378,7 @@ return dom; } function unmountComponent(component, remove) { + if (options.beforeUnmount) options.beforeUnmount(component); var base = component.base; component._disable = !0; if (component.componentWillUnmount) component.componentWillUnmount(); @@ -467,4 +477,4 @@ exports.rerender = rerender; exports.options = options; }); -//# sourceMappingURL=preact.js.map
\ No newline at end of file +//# sourceMappingURL=preact.js.map |