Merge branch 'next' into svg

Conflicts:
	archive/v0.1.2/mithril-tests.js
	archive/v0.1.2/mithril.min.js
	archive/v0.1.2/mithril.min.map
	archive/v0.1.2/mithril.min.zip
	mithril.js
	tests/mithril-tests.js
This commit is contained in:
Leo Horie 2014-04-09 16:48:07 -04:00
commit 1aef5dd942
171 changed files with 14131 additions and 105 deletions

View file

@ -32,8 +32,12 @@ new function(window) {
}
return cell
}
function build(parent, data, cached, namespace) {
if (data === null || data === undefined) return
function build(parent, data, cached, shouldReattach, index, namespace) {
if (data === null || data === undefined) {
if (cached) clear(cached.nodes)
return
}
if (data.subtree === "retain") return
var cachedType = type.call(cached), dataType = type.call(data)
if (cachedType != dataType) {
@ -45,7 +49,7 @@ new function(window) {
if (dataType == "[object Array]") {
var nodes = [], intact = cached.length === data.length
for (var i = 0; i < data.length; i++) {
var item = build(parent, data[i], cached[i], namespace)
var item = build(parent, data[i], cached[i], shouldReattach, i, namespace)
if (item === undefined) continue
if (!item.nodes.intact) intact = false
cached[i] = item
@ -59,21 +63,27 @@ new function(window) {
}
}
else if (dataType == "[object Object]") {
if (typeof data.tag != "string") return
if (data.tag != cached.tag || Object.keys(data.attrs).join() != Object.keys(cached.attrs).join()) clear(cached.nodes)
if (typeof data.tag != "string") return
var node, isNew = cached.nodes.length === 0
if (data.tag === "svg") namespace = "http://www.w3.org/2000/svg"
if (isNew) {
node = namespace === undefined ? window.document.createElement(data.tag) : window.document.createElementNS(namespace, data.tag)
cached = {tag: data.tag, attrs: setAttributes(node, data.attrs, {}, namespace), children: build(node, data.children, cached.children, namespace), nodes: [node]}
parent.appendChild(node)
cached = {
tag: data.tag,
attrs: setAttributes(node, data.attrs, {}, namespace),
children: build(node, data.children, cached.children, true, undefined, namespace),
nodes: [node]
}
parent.insertBefore(node, parent.childNodes[index])
}
else {
node = cached.nodes[0]
setAttributes(node, data.attrs, cached.attrs, namespace)
cached.children = build(node, data.children, cached.children, namespace)
cached.children = build(node, data.children, cached.children, false, undefined, namespace)
cached.nodes.intact = true
if (shouldReattach === true) parent.insertBefore(node, parent.childNodes[index])
}
if (type.call(data.attrs["config"]) == "[object Function]") data.attrs["config"](node, !isNew)
}
@ -87,7 +97,7 @@ new function(window) {
}
else {
node = window.document.createTextNode(data)
parent.appendChild(node)
parent.insertBefore(node, parent.childNodes[index])
}
cached = "string number boolean".indexOf(typeof data) > -1 ? new data.constructor(data) : data
cached.nodes = [node]
@ -106,7 +116,7 @@ new function(window) {
}
else {
node = cached.nodes[0]
parent.appendChild(node)
parent.insertBefore(node, parent.childNodes[index])
node.nodeValue = data
}
cached = new data.constructor(data)
@ -120,13 +130,24 @@ new function(window) {
function setAttributes(node, dataAttrs, cachedAttrs, namespace) {
for (var attrName in dataAttrs) {
var dataAttr = dataAttrs[attrName]
if (!(attrName in cachedAttrs) || (cachedAttrs[attrName] !== dataAttr)) {
var cachedAttr = cachedAttrs[attrName]
if (!(attrName in cachedAttrs) || (cachedAttr !== dataAttr) || node === window.document.activeElement) {
cachedAttrs[attrName] = dataAttr
if (attrName == "config") continue
if (attrName.indexOf("on") == 0 && typeof dataAttr == "function") dataAttr = autoredraw(dataAttr, node)
if (attrName == "style") for (var rule in dataAttr) node.style[rule] = dataAttr[rule]
else if (attrName in node && namespace === undefined) node[attrName] = dataAttr
else if (namespace !== undefined && attrName === "href") node.setAttributeNS("http://www.w3.org/1999/xlink", "href", dataAttr)
if (attrName === "config") continue
else if (typeof dataAttr == "function" && attrName.indexOf("on") == 0) {
node[attrName] = autoredraw(dataAttr, node)
}
else if (attrName === "style") {
for (var rule in dataAttr) {
if (cachedAttr === undefined || cachedAttr[rule] !== dataAttr[rule]) node.style[rule] = dataAttr[rule]
}
}
else if (namespace !== undefined) {
if (attrName === "href") node.setAttributeNS("http://www.w3.org/1999/xlink", "href", dataAttr)
else if (attrName === "className") node.setAttribute("class", dataAttr)
else node.setAttribute(attrName, dataAttr)
}
else if (attrName in node) node[attrName] = dataAttr
else node.setAttribute(attrName, dataAttr)
}
}
@ -142,9 +163,9 @@ new function(window) {
return result
}
function autoredraw(callback, object) {
return function() {
return function(e) {
m.startComputation()
var output = callback.apply(object || window, arguments)
var output = callback.call(object, e)
m.endComputation()
return output
}
@ -170,7 +191,7 @@ new function(window) {
var index = nodeCache.indexOf(root)
var id = index < 0 ? nodeCache.push(root) - 1 : index
var node = root == window.document || root == window.document.documentElement ? documentNode : root
cellCache[id] = build(node, cell, cellCache[id])
cellCache[id] = build(node, cell, cellCache[id], false)
}
m.trust = function(value) {
@ -188,7 +209,7 @@ new function(window) {
m.endComputation()
}
m.redraw = function() {
m.render(currentRoot || window.document, currentModule.view(currentController))
m.render(currentRoot, currentModule.view(currentController))
lastRedraw = now
}
function redraw() {
@ -313,7 +334,8 @@ new function(window) {
list.push(function(value) {
try {
var result = callback(value)
next[method](result !== undefined ? result : value)
if (result && typeof result.then == "function") result.then(next[method], error)
else next[method](result !== undefined ? result : value)
}
catch (e) {
if (e instanceof Error && e.constructor !== Error) throw e
@ -377,7 +399,7 @@ new function(window) {
return xhrOptions
}
function parameterizeUrl(url, data) {
var tokens = url.match(/:\w+/g)
var tokens = url.match(/:[a-z]\w+/gi)
if (tokens && data) {
for (var i = 0; i < tokens.length; i++) {
var key = tokens[i].slice(1)
@ -419,6 +441,9 @@ new function(window) {
}
}
if (typeof module != "undefined" && module !== null) module.exports = m
if (typeof define == "function" && define.amd) define(function() {return m})
//testing API
m.deps = function(mock) {return window = mock}
}(this)