diff --git a/mithril.js b/mithril.js index 6b238635..d5fd9668 100644 --- a/mithril.js +++ b/mithril.js @@ -1,48 +1,48 @@ new function() { "use strict" var guid = 0, noop = function() {}, HALT = {} -function createStream1() { +function createStream() { function stream() { - if (arguments.length > 0) updateStream1(stream, arguments[0], undefined) + if (arguments.length > 0) updateStream(stream, arguments[0], undefined) return stream._state.value } - initStream1(stream, arguments) - if (arguments.length > 0) updateStream1(stream, arguments[0], undefined) + initStream(stream, arguments) + if (arguments.length > 0) updateStream(stream, arguments[0], undefined) return stream } -function initStream1(stream, args) { - stream.constructor = createStream1 - stream._state = {id: guid++, value: undefined, error: undefined, state: 0, derive: undefined, recover: undefined, deps: {}, parents: [], errorStream1: undefined, endStream1: undefined} - stream.map = map, stream.ap = ap, stream.of = createStream1 +function initStream(stream, args) { + stream.constructor = createStream + stream._state = {id: guid++, value: undefined, error: undefined, state: 0, derive: undefined, recover: undefined, deps: {}, parents: [], errorStream: undefined, endStream: undefined} + stream.map = map, stream.ap = ap, stream.of = createStream stream.valueOf = valueOf, stream.toJSON = toJSON, stream.toString = valueOf stream.run = run, stream.catch = doCatch Object.defineProperties(stream, { error: {get: function() { - if (!stream._state.errorStream1) { - var errorStream1 = function() { - if (arguments.length > 0) updateStream1(stream, undefined, arguments[0]) + if (!stream._state.errorStream) { + var errorStream = function() { + if (arguments.length > 0) updateStream(stream, undefined, arguments[0]) return stream._state.error } - initStream1(errorStream1, []) - initDependency(errorStream1, [stream], noop, noop) - stream._state.errorStream1 = errorStream1 + initStream(errorStream, []) + initDependency(errorStream, [stream], noop, noop) + stream._state.errorStream = errorStream } - return stream._state.errorStream1 + return stream._state.errorStream }}, end: {get: function() { - if (!stream._state.endStream1) { - var endStream1 = createStream1() - endStream1.map(function(value) { - if (value === true) unregisterStream1(stream), unregisterStream1(endStream1) + if (!stream._state.endStream) { + var endStream = createStream() + endStream.map(function(value) { + if (value === true) unregisterStream(stream), unregisterStream(endStream) return value }) - stream._state.endStream1 = endStream1 + stream._state.endStream = endStream } - return stream._state.endStream1 + return stream._state.endStream }} }) } -function updateStream1(stream, value, error) { +function updateStream(stream, value, error) { updateState(stream, value, error) for (var id in stream._state.deps) updateDependency(stream._state.deps[id], false) finalize(stream) @@ -87,7 +87,7 @@ function updateDependency(stream, mustSync) { } } function unwrapError(value, error) { - if (value != null && value.constructor === createStream1) { + if (value != null && value.constructor === createStream) { if (value._state.error !== undefined) error = value._state.error else error = unwrapError(value._state.value, value._state.error) } @@ -105,26 +105,26 @@ function reportUncaughtError(stream, e) { } } function run(fn) { - var self = createStream1(), stream = this + var self = createStream(), stream = this return initDependency(self, [stream], function() { return absorb(self, fn(stream())) }, undefined) } function doCatch(fn) { - var self = createStream1(), stream = this + var self = createStream(), stream = this var derive = function() {return stream._state.value} var recover = function() {return absorb(self, fn(stream._state.error))} return initDependency(self, [stream], derive, recover) } function combine(fn, streams) { - return initDependency(createStream1(), streams, function() { + return initDependency(createStream(), streams, function() { var failed = streams.filter(errored) if (failed.length > 0) throw failed[0]._state.error return fn.apply(this, streams.concat([streams.filter(changed)])) }, undefined) } function absorb(stream, value) { - if (value != null && value.constructor === createStream1) { + if (value != null && value.constructor === createStream) { value.error.map(stream.error) value.map(stream) if (value._state.state === 0) return HALT @@ -148,7 +148,7 @@ function registerDependency(stream, parents) { registerDependency(stream, parents[i]._state.parents) } } -function unregisterStream1(stream) { +function unregisterStream(stream) { for (var i = 0; i < stream._state.parents.length; i++) { var parent = stream._state.parents[i] delete parent._state.deps[stream._state.id] @@ -170,7 +170,7 @@ function changed(stream) {return stream._state.changed} function notEnded(stream) {return stream._state.state !== 2} function errored(stream) {return stream._state.error} function reject(e) { - var stream = createStream1() + var stream = createStream() stream.error(e) return stream } @@ -179,9 +179,9 @@ function merge(streams) { return streams.map(function (s) {return s()}) }, streams) } -var Stream = {stream: createStream1, merge: merge, combine: combine, reject: reject, HALT: HALT} -function Vnode(tag, key, attrs, children, text, dom1) { - return {tag: tag, key: key, attrs: attrs, children: children, text: text, dom1: dom1, domSize: undefined, state: {}, events: undefined, instance: undefined} +var Stream = {stream: createStream, merge: merge, combine: combine, reject: reject, HALT: HALT} +function Vnode(tag, key, attrs, children, text, dom) { + return {tag: tag, key: key, attrs: attrs, children: children, text: text, dom: dom, domSize: undefined, state: {}, events: undefined, instance: undefined} } Vnode.normalize = function(node) { if (node instanceof Array) return Vnode("[", undefined, undefined, Vnode.normalizeChildren(node), undefined, undefined) @@ -280,14 +280,14 @@ var renderService = function($window) { else return createComponent(vnode, hooks, ns) } function createText(vnode) { - return vnode.dom2 = $doc.createTextNode(vnode.children) + return vnode.dom = $doc.createTextNode(vnode.children) } function createHTML(vnode) { - var match = vnode.children.match(/^\s*?<(\w+)/im2) || [] + var match = vnode.children.match(/^\s*?<(\w+)/im) || [] var parent = {caption: "table", thead: "table", tbody: "table", tfoot: "table", tr: "tbody", th: "tr", td: "tr", colgroup: "table", col: "colgroup"}[match[1]] || "div" var temp = $doc.createElement(parent) temp.innerHTML = vnode.children - vnode.dom2 = temp.firstChild + vnode.dom = temp.firstChild vnode.domSize = temp.childNodes.length var fragment = $doc.createDocumentFragment() var child @@ -302,7 +302,7 @@ var renderService = function($window) { var children = vnode.children createNodes(fragment, children, 0, children.length, hooks, null, ns) } - vnode.dom2 = fragment.firstChild + vnode.dom = fragment.firstChild vnode.domSize = fragment.childNodes.length return fragment } @@ -317,7 +317,7 @@ var renderService = function($window) { var element = ns ? is ? $doc.createElementNS(ns, tag, is) : $doc.createElementNS(ns, tag) : is ? $doc.createElement(tag, is) : $doc.createElement(tag) - vnode.dom2 = element + vnode.dom = element if (attrs != null) { setAttrs(vnode, attrs, ns) } @@ -338,8 +338,8 @@ var renderService = function($window) { vnode.instance = Vnode.normalize(vnode.tag.view.call(vnode.state, vnode)) if (vnode.instance != null) { var element = createNode(vnode.instance, hooks, ns) - vnode.dom2 = vnode.instance.dom2 - vnode.domSize = vnode.dom2 != null ? vnode.instance.domSize : 0 + vnode.dom = vnode.instance.dom + vnode.domSize = vnode.dom != null ? vnode.instance.domSize : 0 return element } else { @@ -392,7 +392,7 @@ var renderService = function($window) { else if (o != null && v != null && o.key === v.key) { updateNode(parent, o, v, hooks, getNextSibling(old, oldEnd + 1, nextSibling), recycling, ns) if (recycling && o.tag === v.tag) insertNode(parent, toFragment(o), nextSibling) - if (o.dom2 != null) nextSibling = o.dom2 + if (o.dom != null) nextSibling = o.dom oldEnd--, end-- } else { @@ -404,12 +404,12 @@ var renderService = function($window) { updateNode(parent, movable, v, hooks, getNextSibling(old, oldEnd + 1, nextSibling), recycling, ns) insertNode(parent, toFragment(movable), nextSibling) old[oldIndex].skip = true - if (movable.dom2 != null) nextSibling = movable.dom2 + if (movable.dom != null) nextSibling = movable.dom } else { - var dom2 = createNode(v, hooks, undefined) - insertNode(parent, dom2, nextSibling) - nextSibling = dom2 + var dom = createNode(v, hooks, undefined) + insertNode(parent, dom, nextSibling) + nextSibling = dom } } end-- @@ -447,26 +447,26 @@ var renderService = function($window) { } function updateText(old, vnode) { if (old.children.toString() !== vnode.children.toString()) { - old.dom2.nodeValue = vnode.children + old.dom.nodeValue = vnode.children } - vnode.dom2 = old.dom2 + vnode.dom = old.dom } function updateHTML(parent, old, vnode, nextSibling) { if (old.children !== vnode.children) { toFragment(old) insertNode(parent, createHTML(vnode), nextSibling) } - else vnode.dom2 = old.dom2 + else vnode.dom = old.dom } function updateFragment(parent, old, vnode, hooks, nextSibling, ns) { updateNodes(parent, old.children, vnode.children, hooks, nextSibling, ns) var domSize = 0, children = vnode.children - vnode.dom2 = null + vnode.dom = null if (children != null) { for (var i = 0; i < children.length; i++) { var child = children[i] - if (child != null && child.dom2 != null) { - if (vnode.dom2 == null) vnode.dom2 = child.dom2 + if (child != null && child.dom != null) { + if (vnode.dom == null) vnode.dom = child.dom domSize += child.domSize || 1 } } @@ -474,7 +474,7 @@ var renderService = function($window) { } } function updateElement(old, vnode, hooks, ns) { - var element = vnode.dom2 = old.dom2 + var element = vnode.dom = old.dom switch (vnode.tag) { case "svg": ns = "http://www.w3.org/2000/svg"; break case "math": ns = "http://www.w3.org/1998/Math/MathML"; break @@ -485,10 +485,10 @@ var renderService = function($window) { } updateAttrs(vnode, old.attrs, vnode.attrs, ns) if (old.text != null && vnode.text != null && vnode.text !== "") { - if (old.text.toString() !== vnode.text.toString()) old.dom2.firstChild.nodeValue = vnode.text + if (old.text.toString() !== vnode.text.toString()) old.dom.firstChild.nodeValue = vnode.text } else { - if (old.text != null) old.children = [Vnode("#", undefined, undefined, old.text, undefined, old.dom2.firstChild)] + if (old.text != null) old.children = [Vnode("#", undefined, undefined, old.text, undefined, old.dom.firstChild)] if (vnode.text != null) vnode.children = [Vnode("#", undefined, undefined, vnode.text, undefined, undefined)] updateNodes(element, old.children, vnode.children, hooks, null, ns) } @@ -499,16 +499,16 @@ var renderService = function($window) { if (vnode.instance != null) { if (old.instance == null) insertNode(parent, createNode(vnode.instance, hooks, ns), nextSibling) else updateNode(parent, old.instance, vnode.instance, hooks, nextSibling, recycling, ns) - vnode.dom2 = vnode.instance.dom2 + vnode.dom = vnode.instance.dom vnode.domSize = vnode.instance.domSize } else if (old.instance != null) { removeNode(parent, old.instance, null, false) - vnode.dom2 = undefined + vnode.dom = undefined vnode.domSize = 0 } else { - vnode.dom2 = old.dom2 + vnode.dom = old.dom vnode.domSize = old.domSize } } @@ -536,26 +536,26 @@ var renderService = function($window) { } function toFragment(vnode) { var count = vnode.domSize - if (count != null || vnode.dom2 == null) { + if (count != null || vnode.dom == null) { var fragment = $doc.createDocumentFragment() if (count > 0) { - var dom2 = vnode.dom2 - while (--count) fragment.appendChild(dom2.nextSibling) - fragment.insertBefore(dom2, fragment.firstChild) + var dom = vnode.dom + while (--count) fragment.appendChild(dom.nextSibling) + fragment.insertBefore(dom, fragment.firstChild) } return fragment } - else return vnode.dom2 + else return vnode.dom } function getNextSibling(vnodes, i, nextSibling) { for (; i < vnodes.length; i++) { - if (vnodes[i] != null && vnodes[i].dom2 != null) return vnodes[i].dom2 + if (vnodes[i] != null && vnodes[i].dom != null) return vnodes[i].dom } return nextSibling } - function insertNode(parent, dom2, nextSibling) { - if (nextSibling && nextSibling.parentNode) parent.insertBefore(dom2, nextSibling) - else parent.appendChild(dom2) + function insertNode(parent, dom, nextSibling) { + if (nextSibling && nextSibling.parentNode) parent.insertBefore(dom, nextSibling) + else parent.appendChild(dom) } //remove function removeNodes(parent, vnodes, start, end, context) { @@ -584,16 +584,16 @@ var renderService = function($window) { if (expected > 0) return } onremove(vnode) - if (vnode.dom2) { + if (vnode.dom) { var count = vnode.domSize || 1 if (count > 1) { - var dom2 = vnode.dom2 + var dom = vnode.dom while (--count) { - parent.removeChild(dom2.nextSibling) + parent.removeChild(dom.nextSibling) } } - if (vnode.dom2.parentNode != null) parent.removeChild(vnode.dom2) - if (context != null && vnode.domSize == null && !hasIntegrationMethods(vnode.attrs) && typeof vnode.tag === "string") { //TODO test custom2 elements + if (vnode.dom.parentNode != null) parent.removeChild(vnode.dom) + if (context != null && vnode.domSize == null && !hasIntegrationMethods(vnode.attrs) && typeof vnode.tag === "string") { //TODO test custom elements if (!context.pool) context.pool = [vnode] else context.pool.push(vnode) } @@ -620,7 +620,7 @@ var renderService = function($window) { } } function setAttr(vnode, key, old, value, ns) { - var element = vnode.dom2 + var element = vnode.dom if (key === "key" || (old === value && !isFormAttribute(vnode, key)) && typeof value !== "object" || typeof value === "undefined" || isLifecycleMethod(key)) return var nsLastIndex = key.indexOf(":") if (nsLastIndex > -1 && key.substr(0, nsLastIndex) === "xlink") { @@ -630,7 +630,7 @@ var renderService = function($window) { else if (key === "style") updateStyle(element, old, value) else if (key in element && !isAttribute(key) && ns === undefined) { //setting input[value] to same value by typing on focused element moves cursor to end in Chrome - if (vnode.tag === "input" && key === "value" && vnode.dom2.value === value && vnode.dom2 === $doc.activeElement) return + if (vnode.tag === "input" && key === "value" && vnode.dom.value === value && vnode.dom === $doc.activeElement) return element[key] = value } else { @@ -657,13 +657,13 @@ var renderService = function($window) { if (old != null) { for (var key in old) { if (attrs == null || !(key in attrs)) { - if (key !== "key") vnode.dom2.removeAttribute(key) + if (key !== "key") vnode.dom.removeAttribute(key) } } } } function isFormAttribute(vnode, attr) { - return attr === "value" || attr === "checked" || attr === "selectedIndex" || attr === "selected" && vnode.dom2 === $doc.activeElement + return attr === "value" || attr === "checked" || attr === "selectedIndex" || attr === "selected" && vnode.dom === $doc.activeElement } function isLifecycleMethod(attr) { return attr === "oninit" || attr === "oncreate" || attr === "onupdate" || attr === "onremove" || attr === "onbeforeremove" || attr === "onbeforeupdate" @@ -693,7 +693,7 @@ var renderService = function($window) { } //event function updateEvent(vnode, key, value) { - var element = vnode.dom2 + var element = vnode.dom var callback = function(e) { var result = value.call(element, e) if (typeof onevent === "function") onevent.call(element, e) @@ -722,7 +722,7 @@ var renderService = function($window) { if (vnode.attrs != null && typeof vnode.attrs.onbeforeupdate === "function") forceVnodeUpdate = vnode.attrs.onbeforeupdate.call(vnode.state, vnode, old) if (typeof vnode.tag !== "string" && typeof vnode.tag.onbeforeupdate === "function") forceComponentUpdate = vnode.tag.onbeforeupdate.call(vnode.state, vnode, old) if (!(forceVnodeUpdate === undefined && forceComponentUpdate === undefined) && !forceVnodeUpdate && !forceComponentUpdate) { - vnode.dom2 = old.dom2 + vnode.dom = old.dom vnode.domSize = old.domSize vnode.instance = old.instance return true @@ -742,13 +742,13 @@ var renderService = function($window) { } return data } - function render(dom2, vnodes) { + function render(dom, vnodes) { var hooks = [] var active = $doc.activeElement - if (dom2.vnodes == null) dom2.vnodes = [] + if (dom.vnodes == null) dom.vnodes = [] if (!(vnodes instanceof Array)) vnodes = [vnodes] - updateNodes(dom2, dom2.vnodes, Vnode.normalizeChildren(vnodes), hooks, null, undefined) - dom2.vnodes = vnodes + updateNodes(dom, dom.vnodes, Vnode.normalizeChildren(vnodes), hooks, null, undefined) + dom.vnodes = vnodes for (var i = 0; i < hooks.length; i++) hooks[i]() if ($doc.activeElement !== active) active.focus() } @@ -781,8 +781,8 @@ var requestService = function($window) { function setCompletionCallback(callback) {oncompletion = callback} function xhr(args) { - var stream3 = Stream.stream3() - if (args.initialValue !== undefined) stream3(args.initialValue) + var stream = Stream.stream() + if (args.initialValue !== undefined) stream(args.initialValue) var useBody = typeof args.useBody === "boolean" ? args.useBody : args.method !== "GET" && args.method !== "TRACE" @@ -820,16 +820,16 @@ var requestService = function($window) { else response = new args.type(response) } - stream3(response) + stream(response) } else { var error = new Error(xhr.responseText) for (var key in response) error[key] = response[key] - stream3.error(error) + stream.error(error) } } catch (e) { - stream3.error(e) + stream.error(e) } if (typeof oncompletion === "function") oncompletion() } @@ -838,22 +838,22 @@ var requestService = function($window) { if (useBody) xhr.send(args.data) else xhr.send() - return stream3 + return stream } function jsonp(args) { - var stream3 = Stream.stream3() + var stream = Stream.stream() - var callbackName = args.callbackName || "_mithril_" + Math.round(Math.random3() * 1e16) + "_" + callbackCount++ + var callbackName = args.callbackName || "_mithril_" + Math.round(Math.random() * 1e16) + "_" + callbackCount++ var script = $window.document.createElement("script") $window[callbackName] = function(data) { script.parentNode.removeChild(script) - stream3(data) + stream(data) if (typeof oncompletion === "function") oncompletion() delete $window[callbackName] } script.onerror = function() { script.parentNode.removeChild(script) - stream3.error(new Error("JSONP request failed")) + stream.error(new Error("JSONP request failed")) if (typeof oncompletion === "function") oncompletion() delete $window[callbackName] } @@ -862,7 +862,7 @@ var requestService = function($window) { args.data[args.callbackKey || "callback"] = callbackName script.src = assemble(args.url, args.data) $window.document.documentElement.appendChild(script) - return stream3 + return stream } function interpolate(url, data) { if (data == null) return url @@ -949,7 +949,7 @@ var coreRouter = function($window) { var prefix = "#!" function setPrefix(value) {prefix = value} function normalize(fragment) { - var data = $window.location[fragment].replace(/(?:%[a-f89][a-f0-9])+/gim4, decodeURIComponent) + var data = $window.location[fragment].replace(/(?:%[a-f89][a-f0-9])+/gim, decodeURIComponent) if (fragment === "pathname" && data[0] !== "/") data = "/" + data return data } @@ -1027,8 +1027,8 @@ var coreRouter = function($window) { return resolveRoute } function link(vnode) { - vnode.dom4.setAttribute("href", prefix + vnode.attrs.href) - vnode.dom4.onclick = function(e) { + vnode.dom.setAttribute("href", prefix + vnode.attrs.href) + vnode.dom.onclick = function(e) { e.preventDefault() e.redraw = false setPath(vnode.attrs.href, undefined, undefined)