jsfmt'd code
This commit is contained in:
parent
e9c892cb05
commit
4def679b2b
2 changed files with 2270 additions and 1862 deletions
|
|
@ -1,7 +1,11 @@
|
||||||
var m = (function app(window, undefined) {
|
var m = (function app(window, undefined) {
|
||||||
var OBJECT = "[object Object]", ARRAY = "[object Array]", STRING = "[object String]", FUNCTION = "function";
|
var OBJECT = "[object Object]",
|
||||||
|
ARRAY = "[object Array]",
|
||||||
|
STRING = "[object String]",
|
||||||
|
FUNCTION = "function";
|
||||||
var type = {}.toString;
|
var type = {}.toString;
|
||||||
var parser = /(?:(^|#|\.)([^#\.\[\]]+))|(\[.+?\])/g, attrParser = /\[(.+?)(?:=("|'|)(.*?)\2)?\]/;
|
var parser = /(?:(^|#|\.)([^#\.\[\]]+))|(\[.+?\])/g,
|
||||||
|
attrParser = /\[(.+?)(?:=("|'|)(.*?)\2)?\]/;
|
||||||
var voidElements = /^(AREA|BASE|BR|COL|COMMAND|EMBED|HR|IMG|INPUT|KEYGEN|LINK|META|PARAM|SOURCE|TRACK|WBR)$/;
|
var voidElements = /^(AREA|BASE|BR|COL|COMMAND|EMBED|HR|IMG|INPUT|KEYGEN|LINK|META|PARAM|SOURCE|TRACK|WBR)$/;
|
||||||
|
|
||||||
// caching commonly used variables
|
// caching commonly used variables
|
||||||
|
|
@ -36,32 +40,44 @@ var m = (function app(window, undefined) {
|
||||||
var hasAttrs = args[1] != null && type.call(args[1]) === OBJECT && !("tag" in args[1]) && !("subtree" in args[1]);
|
var hasAttrs = args[1] != null && type.call(args[1]) === OBJECT && !("tag" in args[1]) && !("subtree" in args[1]);
|
||||||
var attrs = hasAttrs ? args[1] : {};
|
var attrs = hasAttrs ? args[1] : {};
|
||||||
var classAttrName = "class" in attrs ? "class" : "className";
|
var classAttrName = "class" in attrs ? "class" : "className";
|
||||||
var cell = {tag: "div", attrs: {}};
|
var cell = {
|
||||||
var match, classes = [];
|
tag: "div",
|
||||||
if (type.call(args[0]) != STRING) throw new Error("selector in m(selector, attrs, children) should be a string")
|
attrs: {}
|
||||||
|
};
|
||||||
|
var match,
|
||||||
|
classes = [];
|
||||||
|
if (type.call(args[0]) != STRING)
|
||||||
|
throw new Error("selector in m(selector, attrs, children) should be a string")
|
||||||
while (match = parser.exec(args[0])) {
|
while (match = parser.exec(args[0])) {
|
||||||
if (match[1] === "" && match[2]) cell.tag = match[2];
|
if (match[1] === "" && match[2]) {
|
||||||
else if (match[1] === "#") cell.attrs.id = match[2];
|
cell.tag = match[2];
|
||||||
else if (match[1] === ".") classes.push(match[2]);
|
} else if (match[1] === "#") {
|
||||||
else if (match[3][0] === "[") {
|
cell.attrs.id = match[2];
|
||||||
|
} else if (match[1] === ".") {
|
||||||
|
classes.push(match[2]);
|
||||||
|
} else if (match[3][0] === "[") {
|
||||||
var pair = attrParser.exec(match[3]);
|
var pair = attrParser.exec(match[3]);
|
||||||
cell.attrs[pair[1]] = pair[3] || (pair[2] ? "" : true)
|
cell.attrs[pair[1]] = pair[3] || (pair[2] ? "" : true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (classes.length > 0) cell.attrs[classAttrName] = classes.join(" ");
|
if (classes.length > 0) {
|
||||||
|
cell.attrs[classAttrName] = classes.join(" ");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
var children = hasAttrs ? args[2] : args[1];
|
var children = hasAttrs ? args[2] : args[1];
|
||||||
if (type.call(children) === ARRAY) {
|
if (type.call(children) === ARRAY) {
|
||||||
cell.children = children
|
cell.children = children
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
cell.children = hasAttrs ? args.slice(2) : args.slice(1)
|
cell.children = hasAttrs ? args.slice(2) : args.slice(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
for (var attrName in attrs) {
|
for (var attrName in attrs) {
|
||||||
if (attrName === classAttrName) cell.attrs[attrName] = (cell.attrs[attrName] || "") + " " + attrs[attrName];
|
if (attrName === classAttrName) {
|
||||||
else cell.attrs[attrName] = attrs[attrName]
|
cell.attrs[attrName] = (cell.attrs[attrName] || "") + " " + attrs[attrName];
|
||||||
|
} else {
|
||||||
|
cell.attrs[attrName] = attrs[attrName]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return cell
|
return cell
|
||||||
}
|
}
|
||||||
|
|
@ -91,20 +107,26 @@ var m = (function app(window, undefined) {
|
||||||
//there's logic that relies on the assumption that null and undefined data are equivalent to empty strings
|
//there's logic that relies on the assumption that null and undefined data are equivalent to empty strings
|
||||||
//- this prevents lifecycle surprises from procedural helpers that mix implicit and explicit return statements (e.g. function foo() {if (cond) return m("div")}
|
//- this prevents lifecycle surprises from procedural helpers that mix implicit and explicit return statements (e.g. function foo() {if (cond) return m("div")}
|
||||||
//- it simplifies diffing code
|
//- it simplifies diffing code
|
||||||
if (data == null || data.toString() == null) data = "";
|
if (data == null || data.toString() == null) {
|
||||||
|
data = "";
|
||||||
|
}
|
||||||
if (data.subtree === "retain") return cached;
|
if (data.subtree === "retain") return cached;
|
||||||
var cachedType = type.call(cached), dataType = type.call(data);
|
var cachedType = type.call(cached),
|
||||||
|
dataType = type.call(data);
|
||||||
if (cached == null || cachedType !== dataType) {
|
if (cached == null || cachedType !== dataType) {
|
||||||
if (cached != null) {
|
if (cached != null) {
|
||||||
if (parentCache && parentCache.nodes) {
|
if (parentCache && parentCache.nodes) {
|
||||||
var offset = index - parentIndex;
|
var offset = index - parentIndex;
|
||||||
var end = offset + (dataType === ARRAY ? data : cached.nodes).length;
|
var end = offset + (dataType === ARRAY ? data : cached.nodes).length;
|
||||||
clear(parentCache.nodes.slice(offset, end), parentCache.slice(offset, end))
|
clear(parentCache.nodes.slice(offset, end), parentCache.slice(offset, end))
|
||||||
|
} else if (cached.nodes) {
|
||||||
|
clear(cached.nodes, cached)
|
||||||
}
|
}
|
||||||
else if (cached.nodes) clear(cached.nodes, cached)
|
|
||||||
}
|
}
|
||||||
cached = new data.constructor;
|
cached = new data.constructor;
|
||||||
if (cached.tag) cached = {}; //if constructor creates a virtual dom element, use a blank object as the base cached node instead of copying the virtual el (#277)
|
if (cached.tag) {
|
||||||
|
cached = {};
|
||||||
|
} //if constructor creates a virtual dom element, use a blank object as the base cached node instead of copying the virtual el (#277)
|
||||||
cached.nodes = []
|
cached.nodes = []
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -117,7 +139,9 @@ var m = (function app(window, undefined) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var nodes = [], intact = cached.length === data.length, subArrayCount = 0;
|
var nodes = [],
|
||||||
|
intact = cached.length === data.length,
|
||||||
|
subArrayCount = 0;
|
||||||
|
|
||||||
//keys algorithm: sort elements without recreating them if keys are present
|
//keys algorithm: sort elements without recreating them if keys are present
|
||||||
//1) create a map of all existing keys, and mark all for deletion
|
//1) create a map of all existing keys, and mark all for deletion
|
||||||
|
|
@ -125,33 +149,58 @@ var m = (function app(window, undefined) {
|
||||||
//3) if key exists in new list, change action from deletion to a move
|
//3) if key exists in new list, change action from deletion to a move
|
||||||
//4) for each key, handle its corresponding action as marked in previous steps
|
//4) for each key, handle its corresponding action as marked in previous steps
|
||||||
//5) copy unkeyed items into their respective gaps
|
//5) copy unkeyed items into their respective gaps
|
||||||
var DELETION = 1, INSERTION = 2 , MOVE = 3;
|
var DELETION = 1,
|
||||||
var existing = {}, unkeyed = [], shouldMaintainIdentities = false;
|
INSERTION = 2 ,
|
||||||
|
MOVE = 3;
|
||||||
|
var existing = {},
|
||||||
|
unkeyed = [],
|
||||||
|
shouldMaintainIdentities = false;
|
||||||
for (var i = 0, len = cached.length; i < len; i++) {
|
for (var i = 0, len = cached.length; i < len; i++) {
|
||||||
if (cached[i] && cached[i].attrs && cached[i].attrs.key != null) {
|
if (cached[i] && cached[i].attrs && cached[i].attrs.key != null) {
|
||||||
shouldMaintainIdentities = true;
|
shouldMaintainIdentities = true;
|
||||||
existing[cached[i].attrs.key] = {action: DELETION, index: i}
|
existing[cached[i].attrs.key] = {
|
||||||
|
action: DELETION,
|
||||||
|
index: i
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (shouldMaintainIdentities) {
|
if (shouldMaintainIdentities) {
|
||||||
if (data.indexOf(null) > -1) data = data.filter(function(x) {return x != null})
|
if (data.indexOf(null) > -1) {
|
||||||
|
data = data.filter(function(x) {
|
||||||
|
return x != null
|
||||||
|
})
|
||||||
|
}
|
||||||
for (var i = 0, len = data.length; i < len; i++) {
|
for (var i = 0, len = data.length; i < len; i++) {
|
||||||
if (data[i] && data[i].attrs) {
|
if (data[i] && data[i].attrs) {
|
||||||
if (data[i].attrs.key != null) {
|
if (data[i].attrs.key != null) {
|
||||||
var key = data[i].attrs.key;
|
var key = data[i].attrs.key;
|
||||||
if (!existing[key]) existing[key] = {action: INSERTION, index: i};
|
if (!existing[key]) {
|
||||||
else existing[key] = {
|
existing[key] = {
|
||||||
|
action: INSERTION,
|
||||||
|
index: i
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
existing[key] = {
|
||||||
action: MOVE,
|
action: MOVE,
|
||||||
index: i,
|
index: i,
|
||||||
from: existing[key].index,
|
from: existing[key].index,
|
||||||
element: parentElement.childNodes[existing[key].index] || $document.createElement("div")
|
element: parentElement.childNodes[existing[key].index] || $document.createElement("div")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else unkeyed.push({index: i, element: parentElement.childNodes[i] || $document.createElement("div")})
|
} else {
|
||||||
|
unkeyed.push({
|
||||||
|
index: i,
|
||||||
|
element: parentElement.childNodes[i] || $document.createElement("div")
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var actions = Object.keys(existing).map(function(key) {return existing[key]});
|
}
|
||||||
var changes = actions.sort(function(a, b) {return a.action - b.action || a.index - b.index});
|
var actions = Object.keys(existing).map(function(key) {
|
||||||
|
return existing[key]
|
||||||
|
});
|
||||||
|
var changes = actions.sort(function(a, b) {
|
||||||
|
return a.action - b.action || a.index - b.index
|
||||||
|
});
|
||||||
var newCached = cached.slice();
|
var newCached = cached.slice();
|
||||||
|
|
||||||
for (var i = 0, change; change = changes[i]; i++) {
|
for (var i = 0, change; change = changes[i]; i++) {
|
||||||
|
|
@ -163,7 +212,12 @@ var m = (function app(window, undefined) {
|
||||||
var dummy = $document.createElement("div");
|
var dummy = $document.createElement("div");
|
||||||
dummy.key = data[change.index].attrs.key;
|
dummy.key = data[change.index].attrs.key;
|
||||||
parentElement.insertBefore(dummy, parentElement.childNodes[change.index] || null);
|
parentElement.insertBefore(dummy, parentElement.childNodes[change.index] || null);
|
||||||
newCached.splice(change.index, 0, {attrs: {key: data[change.index].attrs.key}, nodes: [dummy]})
|
newCached.splice(change.index, 0, {
|
||||||
|
attrs: {
|
||||||
|
key: data[change.index].attrs.key
|
||||||
|
},
|
||||||
|
nodes: [dummy]
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
if (change.action === MOVE) {
|
if (change.action === MOVE) {
|
||||||
|
|
@ -180,7 +234,9 @@ var m = (function app(window, undefined) {
|
||||||
}
|
}
|
||||||
cached = newCached;
|
cached = newCached;
|
||||||
cached.nodes = [];
|
cached.nodes = [];
|
||||||
for (var i = 0, child; child = parentElement.childNodes[i]; i++) cached.nodes.push(child)
|
for (var i = 0, child; child = parentElement.childNodes[i]; i++) {
|
||||||
|
cached.nodes.push(child)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
//end key algorithm
|
//end key algorithm
|
||||||
|
|
||||||
|
|
@ -188,14 +244,17 @@ var m = (function app(window, undefined) {
|
||||||
//diff each item in the array
|
//diff each item in the array
|
||||||
var item = build(parentElement, parentTag, cached, index, data[i], cached[cacheCount], shouldReattach, index + subArrayCount || subArrayCount, editable, namespace, configs);
|
var item = build(parentElement, parentTag, cached, index, data[i], cached[cacheCount], shouldReattach, index + subArrayCount || subArrayCount, editable, namespace, configs);
|
||||||
if (item === undefined) continue;
|
if (item === undefined) continue;
|
||||||
if (!item.nodes.intact) intact = false;
|
if (!item.nodes.intact) {
|
||||||
|
intact = false;
|
||||||
|
}
|
||||||
if (item.$trusted) {
|
if (item.$trusted) {
|
||||||
//fix offset of next element if item was a trusted string w/ more than one html element
|
//fix offset of next element if item was a trusted string w/ more than one html element
|
||||||
//the first clause in the regexp matches elements
|
//the first clause in the regexp matches elements
|
||||||
//the second clause (after the pipe) matches text nodes
|
//the second clause (after the pipe) matches text nodes
|
||||||
subArrayCount += (item.match(/<[^\/]|\>\s*[^<]/g) || []).length
|
subArrayCount += (item.match(/<[^\/]|\>\s*[^<]/g) || []).length
|
||||||
|
} else {
|
||||||
|
subArrayCount += type.call(item) === ARRAY ? item.length : 1;
|
||||||
}
|
}
|
||||||
else subArrayCount += type.call(item) === ARRAY ? item.length : 1;
|
|
||||||
cached[cacheCount++] = item
|
cached[cacheCount++] = item
|
||||||
}
|
}
|
||||||
if (!intact) {
|
if (!intact) {
|
||||||
|
|
@ -203,37 +262,58 @@ var m = (function app(window, undefined) {
|
||||||
|
|
||||||
//update the list of DOM nodes by collecting the nodes from each item
|
//update the list of DOM nodes by collecting the nodes from each item
|
||||||
for (var i = 0, len = data.length; i < len; i++) {
|
for (var i = 0, len = data.length; i < len; i++) {
|
||||||
if (cached[i] != null) nodes.push.apply(nodes, cached[i].nodes)
|
if (cached[i] != null) {
|
||||||
|
nodes.push.apply(nodes, cached[i].nodes)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
//remove items from the end of the array if the new array is shorter than the old one
|
//remove items from the end of the array if the new array is shorter than the old one
|
||||||
//if errors ever happen here, the issue is most likely a bug in the construction of the `cached` data structure somewhere earlier in the program
|
//if errors ever happen here, the issue is most likely a bug in the construction of the `cached` data structure somewhere earlier in the program
|
||||||
for (var i = 0, node; node = cached.nodes[i]; i++) {
|
for (var i = 0, node; node = cached.nodes[i]; i++) {
|
||||||
if (node.parentNode != null && nodes.indexOf(node) < 0) clear([node], [cached[i]])
|
if (node.parentNode != null && nodes.indexOf(node) < 0) {
|
||||||
|
clear([node], [cached[i]])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (data.length < cached.length) {
|
||||||
|
cached.length = data.length;
|
||||||
}
|
}
|
||||||
if (data.length < cached.length) cached.length = data.length;
|
|
||||||
cached.nodes = nodes
|
cached.nodes = nodes
|
||||||
}
|
}
|
||||||
|
} else if (data != null && dataType === OBJECT) {
|
||||||
|
if (!data.attrs) {
|
||||||
|
data.attrs = {};
|
||||||
|
}
|
||||||
|
if (!cached.attrs) {
|
||||||
|
cached.attrs = {};
|
||||||
}
|
}
|
||||||
else if (data != null && dataType === OBJECT) {
|
|
||||||
if (!data.attrs) data.attrs = {};
|
|
||||||
if (!cached.attrs) cached.attrs = {};
|
|
||||||
|
|
||||||
var dataAttrKeys = Object.keys(data.attrs);
|
var dataAttrKeys = Object.keys(data.attrs);
|
||||||
var hasKeys = dataAttrKeys.length > ("key" in data.attrs ? 1 : 0)
|
var hasKeys = dataAttrKeys.length > ("key" in data.attrs ? 1 : 0)
|
||||||
//if an element is different enough from the one in cache, recreate it
|
//if an element is different enough from the one in cache, recreate it
|
||||||
if (data.tag != cached.tag || dataAttrKeys.join() != Object.keys(cached.attrs).join() || data.attrs.id != cached.attrs.id) {
|
if (data.tag != cached.tag || dataAttrKeys.join() != Object.keys(cached.attrs).join() || data.attrs.id != cached.attrs.id) {
|
||||||
if (cached.nodes.length) clear(cached.nodes);
|
if (cached.nodes.length) {
|
||||||
if (cached.configContext && typeof cached.configContext.onunload === FUNCTION) cached.configContext.onunload()
|
clear(cached.nodes);
|
||||||
|
}
|
||||||
|
if (cached.configContext && typeof cached.configContext.onunload === FUNCTION) {
|
||||||
|
cached.configContext.onunload()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (type.call(data.tag) != STRING) return;
|
if (type.call(data.tag) != STRING) return;
|
||||||
|
|
||||||
var node, isNew = cached.nodes.length === 0;
|
var node,
|
||||||
if (data.attrs.xmlns) namespace = data.attrs.xmlns;
|
isNew = cached.nodes.length === 0;
|
||||||
else if (data.tag === "svg") namespace = "http://www.w3.org/2000/svg";
|
if (data.attrs.xmlns) {
|
||||||
else if (data.tag === "math") namespace = "http://www.w3.org/1998/Math/MathML";
|
namespace = data.attrs.xmlns;
|
||||||
|
} else if (data.tag === "svg") {
|
||||||
|
namespace = "http://www.w3.org/2000/svg";
|
||||||
|
} else if (data.tag === "math") {
|
||||||
|
namespace = "http://www.w3.org/1998/Math/MathML";
|
||||||
|
}
|
||||||
if (isNew) {
|
if (isNew) {
|
||||||
if (data.attrs.is) node = namespace === undefined ? $document.createElement(data.tag, data.attrs.is) : $document.createElementNS(namespace, data.tag, data.attrs.is);
|
if (data.attrs.is) {
|
||||||
else node = namespace === undefined ? $document.createElement(data.tag) : $document.createElementNS(namespace, data.tag);
|
node = namespace === undefined ? $document.createElement(data.tag, data.attrs.is) : $document.createElementNS(namespace, data.tag, data.attrs.is);
|
||||||
|
} else {
|
||||||
|
node = namespace === undefined ? $document.createElement(data.tag) : $document.createElementNS(namespace, data.tag);
|
||||||
|
}
|
||||||
cached = {
|
cached = {
|
||||||
tag: data.tag,
|
tag: data.tag,
|
||||||
//set attributes first, then create children
|
//set attributes first, then create children
|
||||||
|
|
@ -243,17 +323,26 @@ var m = (function app(window, undefined) {
|
||||||
data.children,
|
data.children,
|
||||||
nodes: [node]
|
nodes: [node]
|
||||||
};
|
};
|
||||||
if (cached.children && !cached.children.nodes) cached.children.nodes = [];
|
if (cached.children && !cached.children.nodes) {
|
||||||
//edge case: setting value on <select> doesn't work before children exist, so set it again after children have been created
|
cached.children.nodes = [];
|
||||||
if (data.tag === "select" && data.attrs.value) setAttributes(node, data.tag, {value: data.attrs.value}, {}, namespace);
|
|
||||||
parentElement.insertBefore(node, parentElement.childNodes[index] || null)
|
|
||||||
}
|
}
|
||||||
else {
|
//edge case: setting value on <select> doesn't work before children exist, so set it again after children have been created
|
||||||
|
if (data.tag === "select" && data.attrs.value) {
|
||||||
|
setAttributes(node, data.tag, {
|
||||||
|
value: data.attrs.value
|
||||||
|
}, {}, namespace);
|
||||||
|
}
|
||||||
|
parentElement.insertBefore(node, parentElement.childNodes[index] || null)
|
||||||
|
} else {
|
||||||
node = cached.nodes[0];
|
node = cached.nodes[0];
|
||||||
if (dataAttrKeys.length) setAttributes(node, data.tag, data.attrs, cached.attrs, namespace);
|
if (dataAttrKeys.length) {
|
||||||
|
setAttributes(node, data.tag, data.attrs, cached.attrs, namespace);
|
||||||
|
}
|
||||||
cached.children = build(node, data.tag, undefined, undefined, data.children, cached.children, false, 0, data.attrs.contenteditable ? node : editable, namespace, configs);
|
cached.children = build(node, data.tag, undefined, undefined, data.children, cached.children, false, 0, data.attrs.contenteditable ? node : editable, namespace, configs);
|
||||||
cached.nodes.intact = true;
|
cached.nodes.intact = true;
|
||||||
if (shouldReattach === true && node != null) parentElement.insertBefore(node, parentElement.childNodes[index] || null)
|
if (shouldReattach === true && node != null) {
|
||||||
|
parentElement.insertBefore(node, parentElement.childNodes[index] || null)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
//schedule configs to be called. They are called after `build` finishes running
|
//schedule configs to be called. They are called after `build` finishes running
|
||||||
if (typeof data.attrs["config"] === FUNCTION) {
|
if (typeof data.attrs["config"] === FUNCTION) {
|
||||||
|
|
@ -267,34 +356,34 @@ var m = (function app(window, undefined) {
|
||||||
};
|
};
|
||||||
configs.push(callback(data, [node, !isNew, context, cached]))
|
configs.push(callback(data, [node, !isNew, context, cached]))
|
||||||
}
|
}
|
||||||
}
|
} else if (typeof dataType != FUNCTION) {
|
||||||
else if (typeof dataType != FUNCTION) {
|
|
||||||
//handle text nodes
|
//handle text nodes
|
||||||
var nodes;
|
var nodes;
|
||||||
if (cached.nodes.length === 0) {
|
if (cached.nodes.length === 0) {
|
||||||
if (data.$trusted) {
|
if (data.$trusted) {
|
||||||
nodes = injectHTML(parentElement, index, data)
|
nodes = injectHTML(parentElement, index, data)
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
nodes = [$document.createTextNode(data)];
|
nodes = [$document.createTextNode(data)];
|
||||||
if (!parentElement.nodeName.match(voidElements)) parentElement.insertBefore(nodes[0], parentElement.childNodes[index] || null)
|
if (!parentElement.nodeName.match(voidElements)) {
|
||||||
|
parentElement.insertBefore(nodes[0], parentElement.childNodes[index] || null)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
cached = "string number boolean".indexOf(typeof data) > -1 ? new data.constructor(data) : data;
|
cached = "string number boolean".indexOf(typeof data) > -1 ? new data.constructor(data) : data;
|
||||||
cached.nodes = nodes
|
cached.nodes = nodes
|
||||||
}
|
} else if (cached.valueOf() !== data.valueOf() || shouldReattach === true) {
|
||||||
else if (cached.valueOf() !== data.valueOf() || shouldReattach === true) {
|
|
||||||
nodes = cached.nodes;
|
nodes = cached.nodes;
|
||||||
if (!editable || editable !== $document.activeElement) {
|
if (!editable || editable !== $document.activeElement) {
|
||||||
if (data.$trusted) {
|
if (data.$trusted) {
|
||||||
clear(nodes, cached);
|
clear(nodes, cached);
|
||||||
nodes = injectHTML(parentElement, index, data)
|
nodes = injectHTML(parentElement, index, data)
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
//corner case: replacing the nodeValue of a text node that is a child of a textarea/contenteditable doesn't work
|
//corner case: replacing the nodeValue of a text node that is a child of a textarea/contenteditable doesn't work
|
||||||
//we need to update the value property of the parent textarea or the innerHTML of the contenteditable element instead
|
//we need to update the value property of the parent textarea or the innerHTML of the contenteditable element instead
|
||||||
if (parentTag === "textarea") parentElement.value = data;
|
if (parentTag === "textarea") {
|
||||||
else if (editable) editable.innerHTML = data;
|
parentElement.value = data;
|
||||||
else {
|
} else if (editable) {
|
||||||
|
editable.innerHTML = data;
|
||||||
|
} else {
|
||||||
if (nodes[0].nodeType === 1 || nodes.length > 1) { //was a trusted string
|
if (nodes[0].nodeType === 1 || nodes.length > 1) { //was a trusted string
|
||||||
clear(cached.nodes, cached);
|
clear(cached.nodes, cached);
|
||||||
nodes = [$document.createTextNode(data)]
|
nodes = [$document.createTextNode(data)]
|
||||||
|
|
@ -306,8 +395,9 @@ var m = (function app(window, undefined) {
|
||||||
}
|
}
|
||||||
cached = new data.constructor(data);
|
cached = new data.constructor(data);
|
||||||
cached.nodes = nodes
|
cached.nodes = nodes
|
||||||
|
} else {
|
||||||
|
cached.nodes.intact = true
|
||||||
}
|
}
|
||||||
else cached.nodes.intact = true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return cached
|
return cached
|
||||||
|
|
@ -329,30 +419,41 @@ var m = (function app(window, undefined) {
|
||||||
//handle `style: {...}`
|
//handle `style: {...}`
|
||||||
else if (attrName === "style" && dataAttr != null && type.call(dataAttr) === OBJECT) {
|
else if (attrName === "style" && dataAttr != null && type.call(dataAttr) === OBJECT) {
|
||||||
for (var rule in dataAttr) {
|
for (var rule in dataAttr) {
|
||||||
if (cachedAttr == null || cachedAttr[rule] !== dataAttr[rule]) node.style[rule] = dataAttr[rule]
|
if (cachedAttr == null || cachedAttr[rule] !== dataAttr[rule]) {
|
||||||
|
node.style[rule] = dataAttr[rule]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
for (var rule in cachedAttr) {
|
for (var rule in cachedAttr) {
|
||||||
if (!(rule in dataAttr)) node.style[rule] = ""
|
if (!(rule in dataAttr)) {
|
||||||
|
node.style[rule] = ""
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//handle SVG
|
//handle SVG
|
||||||
else if (namespace != null) {
|
else if (namespace != null) {
|
||||||
if (attrName === "href") node.setAttributeNS("http://www.w3.org/1999/xlink", "href", dataAttr);
|
if (attrName === "href") {
|
||||||
else if (attrName === "className") node.setAttribute("class", dataAttr);
|
node.setAttributeNS("http://www.w3.org/1999/xlink", "href", dataAttr);
|
||||||
else node.setAttribute(attrName, dataAttr)
|
} else if (attrName === "className") {
|
||||||
|
node.setAttribute("class", dataAttr);
|
||||||
|
} else {
|
||||||
|
node.setAttribute(attrName, dataAttr)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
//handle cases that are properties (but ignore cases where we should use setAttribute instead)
|
//handle cases that are properties (but ignore cases where we should use setAttribute instead)
|
||||||
//- list and form are typically used as strings, but are DOM element references in js
|
//- list and form are typically used as strings, but are DOM element references in js
|
||||||
//- when using CSS selectors (e.g. `m("[style='']")`), style is used as a string, but it's an object in js
|
//- when using CSS selectors (e.g. `m("[style='']")`), style is used as a string, but it's an object in js
|
||||||
else if (attrName in node && !(attrName === "list" || attrName === "style" || attrName === "form" || attrName === "type")) {
|
else if (attrName in node && !(attrName === "list" || attrName === "style" || attrName === "form" || attrName === "type")) {
|
||||||
//#348 don't set the value if not needed otherwise cursor placement breaks in Chrome
|
//#348 don't set the value if not needed otherwise cursor placement breaks in Chrome
|
||||||
if (attrName != "input" || node[attrName] !== dataAttr) node[attrName] = dataAttr
|
if (attrName != "input" || node[attrName] !== dataAttr) {
|
||||||
|
node[attrName] = dataAttr
|
||||||
}
|
}
|
||||||
else node.setAttribute(attrName, dataAttr)
|
} else {
|
||||||
|
node.setAttribute(attrName, dataAttr)
|
||||||
}
|
}
|
||||||
catch (e) {
|
} catch ( e ) {
|
||||||
//swallow IE's invalid argument errors to mimic HTML's fallback-to-doing-nothing-on-invalid-attributes behavior
|
//swallow IE's invalid argument errors to mimic HTML's fallback-to-doing-nothing-on-invalid-attributes behavior
|
||||||
if (e.message.indexOf("Invalid argument") < 0) throw e
|
if (e.message.indexOf("Invalid argument") < 0)
|
||||||
|
throw e
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//#348 dataAttr may not be a string, so use loose comparison (double equal) instead of strict (triple equal)
|
//#348 dataAttr may not be a string, so use loose comparison (double equal) instead of strict (triple equal)
|
||||||
|
|
@ -365,21 +466,31 @@ var m = (function app(window, undefined) {
|
||||||
function clear(nodes, cached) {
|
function clear(nodes, cached) {
|
||||||
for (var i = nodes.length - 1; i > -1; i--) {
|
for (var i = nodes.length - 1; i > -1; i--) {
|
||||||
if (nodes[i] && nodes[i].parentNode) {
|
if (nodes[i] && nodes[i].parentNode) {
|
||||||
try {nodes[i].parentNode.removeChild(nodes[i])}
|
try {
|
||||||
catch (e) {} //ignore if this fails due to order of events (see http://stackoverflow.com/questions/21926083/failed-to-execute-removechild-on-node)
|
nodes[i].parentNode.removeChild(nodes[i])
|
||||||
|
} catch ( e ) {} //ignore if this fails due to order of events (see http://stackoverflow.com/questions/21926083/failed-to-execute-removechild-on-node)
|
||||||
cached = [].concat(cached);
|
cached = [].concat(cached);
|
||||||
if (cached[i]) unload(cached[i])
|
if (cached[i]) {
|
||||||
|
unload(cached[i])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (nodes.length != 0) nodes.length = 0
|
}
|
||||||
|
if (nodes.length != 0) {
|
||||||
|
nodes.length = 0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
function unload(cached) {
|
function unload(cached) {
|
||||||
if (cached.configContext && typeof cached.configContext.onunload === FUNCTION) cached.configContext.onunload();
|
if (cached.configContext && typeof cached.configContext.onunload === FUNCTION) {
|
||||||
|
cached.configContext.onunload();
|
||||||
|
}
|
||||||
if (cached.children) {
|
if (cached.children) {
|
||||||
if (type.call(cached.children) === ARRAY) {
|
if (type.call(cached.children) === ARRAY) {
|
||||||
for (var i = 0, child; child = cached.children[i]; i++) unload(child)
|
for (var i = 0, child; child = cached.children[i]; i++) {
|
||||||
|
unload(child)
|
||||||
|
}
|
||||||
|
} else if (cached.children.tag) {
|
||||||
|
unload(cached.children)
|
||||||
}
|
}
|
||||||
else if (cached.children.tag) unload(cached.children)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
function injectHTML(parentElement, index, data) {
|
function injectHTML(parentElement, index, data) {
|
||||||
|
|
@ -391,10 +502,12 @@ var m = (function app(window, undefined) {
|
||||||
parentElement.insertBefore(placeholder, nextSibling || null);
|
parentElement.insertBefore(placeholder, nextSibling || null);
|
||||||
placeholder.insertAdjacentHTML("beforebegin", data);
|
placeholder.insertAdjacentHTML("beforebegin", data);
|
||||||
parentElement.removeChild(placeholder)
|
parentElement.removeChild(placeholder)
|
||||||
|
} else {
|
||||||
|
nextSibling.insertAdjacentHTML("beforebegin", data)
|
||||||
}
|
}
|
||||||
else nextSibling.insertAdjacentHTML("beforebegin", data)
|
} else {
|
||||||
|
parentElement.insertAdjacentHTML("beforeend", data);
|
||||||
}
|
}
|
||||||
else parentElement.insertAdjacentHTML("beforeend", data);
|
|
||||||
var nodes = [];
|
var nodes = [];
|
||||||
while (parentElement.childNodes[index] !== nextSibling) {
|
while (parentElement.childNodes[index] !== nextSibling) {
|
||||||
nodes.push(parentElement.childNodes[index]);
|
nodes.push(parentElement.childNodes[index]);
|
||||||
|
|
@ -407,8 +520,9 @@ var m = (function app(window, undefined) {
|
||||||
e = e || event;
|
e = e || event;
|
||||||
m.redraw.strategy("diff");
|
m.redraw.strategy("diff");
|
||||||
m.startComputation();
|
m.startComputation();
|
||||||
try {return callback.call(object, e)}
|
try {
|
||||||
finally {
|
return callback.call(object, e)
|
||||||
|
} finally {
|
||||||
endFirstComputation()
|
endFirstComputation()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -417,11 +531,14 @@ var m = (function app(window, undefined) {
|
||||||
var html;
|
var html;
|
||||||
var documentNode = {
|
var documentNode = {
|
||||||
appendChild: function(node) {
|
appendChild: function(node) {
|
||||||
if (html === undefined) html = $document.createElement("html");
|
if (html === undefined) {
|
||||||
|
html = $document.createElement("html");
|
||||||
|
}
|
||||||
if ($document.documentElement && $document.documentElement !== node) {
|
if ($document.documentElement && $document.documentElement !== node) {
|
||||||
$document.replaceChild(node, $document.documentElement)
|
$document.replaceChild(node, $document.documentElement)
|
||||||
|
} else {
|
||||||
|
$document.appendChild(node);
|
||||||
}
|
}
|
||||||
else $document.appendChild(node);
|
|
||||||
this.childNodes = $document.childNodes
|
this.childNodes = $document.childNodes
|
||||||
},
|
},
|
||||||
insertBefore: function(node) {
|
insertBefore: function(node) {
|
||||||
|
|
@ -429,18 +546,32 @@ var m = (function app(window, undefined) {
|
||||||
},
|
},
|
||||||
childNodes: []
|
childNodes: []
|
||||||
};
|
};
|
||||||
var nodeCache = [], cellCache = {};
|
var nodeCache = [],
|
||||||
|
cellCache = {};
|
||||||
m.render = function(root, cell, forceRecreation) {
|
m.render = function(root, cell, forceRecreation) {
|
||||||
var configs = [];
|
var configs = [];
|
||||||
if (!root) throw new Error("Please ensure the DOM element exists before rendering a template into it.");
|
if (!root)
|
||||||
|
throw new Error("Please ensure the DOM element exists before rendering a template into it.");
|
||||||
var id = getCellCacheKey(root);
|
var id = getCellCacheKey(root);
|
||||||
var isDocumentRoot = root === $document;
|
var isDocumentRoot = root === $document;
|
||||||
var node = isDocumentRoot || root === $document.documentElement ? documentNode : root;
|
var node = isDocumentRoot || root === $document.documentElement ? documentNode : root;
|
||||||
if (isDocumentRoot && cell.tag != "html") cell = {tag: "html", attrs: {}, children: cell};
|
if (isDocumentRoot && cell.tag != "html") {
|
||||||
if (cellCache[id] === undefined) clear(node.childNodes);
|
cell = {
|
||||||
if (forceRecreation === true) reset(root);
|
tag: "html",
|
||||||
|
attrs: {},
|
||||||
|
children: cell
|
||||||
|
};
|
||||||
|
}
|
||||||
|
if (cellCache[id] === undefined) {
|
||||||
|
clear(node.childNodes);
|
||||||
|
}
|
||||||
|
if (forceRecreation === true) {
|
||||||
|
reset(root);
|
||||||
|
}
|
||||||
cellCache[id] = build(node, null, undefined, undefined, cell, cellCache[id], false, 0, null, undefined, configs);
|
cellCache[id] = build(node, null, undefined, undefined, cell, cellCache[id], false, 0, null, undefined, configs);
|
||||||
for (var i = 0, len = configs.length; i < len; i++) configs[i]()
|
for (var i = 0, len = configs.length; i < len; i++) {
|
||||||
|
configs[i]()
|
||||||
|
}
|
||||||
};
|
};
|
||||||
function getCellCacheKey(element) {
|
function getCellCacheKey(element) {
|
||||||
var index = nodeCache.indexOf(element);
|
var index = nodeCache.indexOf(element);
|
||||||
|
|
@ -455,7 +586,9 @@ var m = (function app(window, undefined) {
|
||||||
|
|
||||||
function gettersetter(store) {
|
function gettersetter(store) {
|
||||||
var prop = function() {
|
var prop = function() {
|
||||||
if (arguments.length) store = arguments[0];
|
if (arguments.length) {
|
||||||
|
store = arguments[0];
|
||||||
|
}
|
||||||
return store
|
return store
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -475,15 +608,25 @@ var m = (function app(window, undefined) {
|
||||||
return gettersetter(store)
|
return gettersetter(store)
|
||||||
};
|
};
|
||||||
|
|
||||||
var roots = [], modules = [], controllers = [], lastRedrawId = null, lastRedrawCallTime = 0, computePostRedrawHook = null, prevented = false, topModule;
|
var roots = [],
|
||||||
|
modules = [],
|
||||||
|
controllers = [],
|
||||||
|
lastRedrawId = null,
|
||||||
|
lastRedrawCallTime = 0,
|
||||||
|
computePostRedrawHook = null,
|
||||||
|
prevented = false, topModule;
|
||||||
var FRAME_BUDGET = 16; //60 frames per second = 1 call per 16 ms
|
var FRAME_BUDGET = 16; //60 frames per second = 1 call per 16 ms
|
||||||
m.module = function(root, module) {
|
m.module = function(root, module) {
|
||||||
var index = roots.indexOf(root);
|
var index = roots.indexOf(root);
|
||||||
if (index < 0) index = roots.length;
|
if (index < 0) {
|
||||||
|
index = roots.length;
|
||||||
|
}
|
||||||
var isPrevented = false;
|
var isPrevented = false;
|
||||||
if (controllers[index] && typeof controllers[index].onunload === FUNCTION) {
|
if (controllers[index] && typeof controllers[index].onunload === FUNCTION) {
|
||||||
var event = {
|
var event = {
|
||||||
preventDefault: function() {isPrevented = true}
|
preventDefault: function() {
|
||||||
|
isPrevented = true
|
||||||
|
}
|
||||||
};
|
};
|
||||||
controllers[index].onunload(event)
|
controllers[index].onunload(event)
|
||||||
}
|
}
|
||||||
|
|
@ -510,13 +653,16 @@ var m = (function app(window, undefined) {
|
||||||
//when setTimeout: only reschedule redraw if time between now and previous redraw is bigger than a frame, otherwise keep currently scheduled timeout
|
//when setTimeout: only reschedule redraw if time between now and previous redraw is bigger than a frame, otherwise keep currently scheduled timeout
|
||||||
//when rAF: always reschedule redraw
|
//when rAF: always reschedule redraw
|
||||||
if (new Date - lastRedrawCallTime > FRAME_BUDGET || $requestAnimationFrame === window.requestAnimationFrame) {
|
if (new Date - lastRedrawCallTime > FRAME_BUDGET || $requestAnimationFrame === window.requestAnimationFrame) {
|
||||||
if (lastRedrawId > 0) $cancelAnimationFrame(lastRedrawId);
|
if (lastRedrawId > 0) {
|
||||||
|
$cancelAnimationFrame(lastRedrawId);
|
||||||
|
}
|
||||||
lastRedrawId = $requestAnimationFrame(redraw, FRAME_BUDGET)
|
lastRedrawId = $requestAnimationFrame(redraw, FRAME_BUDGET)
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
redraw();
|
redraw();
|
||||||
lastRedrawId = $requestAnimationFrame(function() {lastRedrawId = null}, FRAME_BUDGET)
|
lastRedrawId = $requestAnimationFrame(function() {
|
||||||
|
lastRedrawId = null
|
||||||
|
}, FRAME_BUDGET)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
m.redraw.strategy = m.prop();
|
m.redraw.strategy = m.prop();
|
||||||
|
|
@ -538,17 +684,22 @@ var m = (function app(window, undefined) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var pendingRequests = 0;
|
var pendingRequests = 0;
|
||||||
m.startComputation = function() {pendingRequests++};
|
m.startComputation = function() {
|
||||||
|
pendingRequests++
|
||||||
|
};
|
||||||
m.endComputation = function() {
|
m.endComputation = function() {
|
||||||
pendingRequests = Math.max(pendingRequests - 1, 0);
|
pendingRequests = Math.max(pendingRequests - 1, 0);
|
||||||
if (pendingRequests === 0) m.redraw()
|
if (pendingRequests === 0) {
|
||||||
|
m.redraw()
|
||||||
|
}
|
||||||
};
|
};
|
||||||
var endFirstComputation = function() {
|
var endFirstComputation = function() {
|
||||||
if (m.redraw.strategy() == "none") {
|
if (m.redraw.strategy() == "none") {
|
||||||
pendingRequests--
|
pendingRequests--
|
||||||
m.redraw.strategy("diff")
|
m.redraw.strategy("diff")
|
||||||
|
} else {
|
||||||
|
m.endComputation();
|
||||||
}
|
}
|
||||||
else m.endComputation();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
m.withAttr = function(prop, withAttrCallback) {
|
m.withAttr = function(prop, withAttrCallback) {
|
||||||
|
|
@ -560,14 +711,20 @@ var m = (function app(window, undefined) {
|
||||||
};
|
};
|
||||||
|
|
||||||
//routing
|
//routing
|
||||||
var modes = {pathname: "", hash: "#", search: "?"};
|
var modes = {
|
||||||
|
pathname: "",
|
||||||
|
hash: "#",
|
||||||
|
search: "?"
|
||||||
|
};
|
||||||
var redirect = function() {}, routeParams, currentRoute;
|
var redirect = function() {}, routeParams, currentRoute;
|
||||||
m.route = function() {
|
m.route = function() {
|
||||||
//m.route()
|
//m.route()
|
||||||
if (arguments.length === 0) return currentRoute;
|
if (arguments.length === 0) return currentRoute;
|
||||||
//m.route(el, defaultRoute, routes)
|
//m.route(el, defaultRoute, routes)
|
||||||
else if (arguments.length === 3 && type.call(arguments[1]) === STRING) {
|
else if (arguments.length === 3 && type.call(arguments[1]) === STRING) {
|
||||||
var root = arguments[0], defaultRoute = arguments[1], router = arguments[2];
|
var root = arguments[0],
|
||||||
|
defaultRoute = arguments[1],
|
||||||
|
router = arguments[2];
|
||||||
redirect = function(source) {
|
redirect = function(source) {
|
||||||
var path = currentRoute = normalizeRoute(source);
|
var path = currentRoute = normalizeRoute(source);
|
||||||
if (!routeByValue(root, router, path)) {
|
if (!routeByValue(root, router, path)) {
|
||||||
|
|
@ -601,7 +758,9 @@ var m = (function app(window, undefined) {
|
||||||
for (var i in args) params[i] = args[i]
|
for (var i in args) params[i] = args[i]
|
||||||
var querystring = buildQueryString(params)
|
var querystring = buildQueryString(params)
|
||||||
var currentPath = queryIndex > -1 ? currentRoute.slice(0, queryIndex) : currentRoute
|
var currentPath = queryIndex > -1 ? currentRoute.slice(0, queryIndex) : currentRoute
|
||||||
if (querystring) currentRoute = currentPath + (currentPath.indexOf("?") === -1 ? "?" : "&") + querystring;
|
if (querystring) {
|
||||||
|
currentRoute = currentPath + (currentPath.indexOf("?") === -1 ? "?" : "&") + querystring;
|
||||||
|
}
|
||||||
|
|
||||||
var shouldReplaceHistoryEntry = (arguments.length === 3 ? arguments[2] : arguments[1]) === true;
|
var shouldReplaceHistoryEntry = (arguments.length === 3 ? arguments[2] : arguments[1]) === true;
|
||||||
|
|
||||||
|
|
@ -611,16 +770,20 @@ var m = (function app(window, undefined) {
|
||||||
setScroll()
|
setScroll()
|
||||||
};
|
};
|
||||||
redirect(modes[m.route.mode] + currentRoute)
|
redirect(modes[m.route.mode] + currentRoute)
|
||||||
|
} else {
|
||||||
|
$location[m.route.mode] = currentRoute
|
||||||
}
|
}
|
||||||
else $location[m.route.mode] = currentRoute
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
m.route.param = function(key) {
|
m.route.param = function(key) {
|
||||||
if (!routeParams) throw new Error("You must call m.route(element, defaultRoute, routes) before calling m.route.param()")
|
if (!routeParams)
|
||||||
|
throw new Error("You must call m.route(element, defaultRoute, routes) before calling m.route.param()")
|
||||||
return routeParams[key]
|
return routeParams[key]
|
||||||
};
|
};
|
||||||
m.route.mode = "search";
|
m.route.mode = "search";
|
||||||
function normalizeRoute(route) {return route.slice(modes[m.route.mode].length)}
|
function normalizeRoute(route) {
|
||||||
|
return route.slice(modes[m.route.mode].length)
|
||||||
|
}
|
||||||
function routeByValue(root, router, path) {
|
function routeByValue(root, router, path) {
|
||||||
routeParams = {};
|
routeParams = {};
|
||||||
|
|
||||||
|
|
@ -642,7 +805,9 @@ var m = (function app(window, undefined) {
|
||||||
path.replace(matcher, function() {
|
path.replace(matcher, function() {
|
||||||
var keys = route.match(/:[^\/]+/g) || [];
|
var keys = route.match(/:[^\/]+/g) || [];
|
||||||
var values = [].slice.call(arguments, 1, -2);
|
var values = [].slice.call(arguments, 1, -2);
|
||||||
for (var i = 0, len = keys.length; i < len; i++) routeParams[keys[i].replace(/:|\./g, "")] = decodeURIComponent(values[i])
|
for (var i = 0, len = keys.length; i < len; i++) {
|
||||||
|
routeParams[keys[i].replace(/:|\./g, "")] = decodeURIComponent(values[i])
|
||||||
|
}
|
||||||
m.module(root, router[route])
|
m.module(root, router[route])
|
||||||
});
|
});
|
||||||
return true
|
return true
|
||||||
|
|
@ -652,26 +817,34 @@ var m = (function app(window, undefined) {
|
||||||
function routeUnobtrusive(e) {
|
function routeUnobtrusive(e) {
|
||||||
e = e || event;
|
e = e || event;
|
||||||
if (e.ctrlKey || e.metaKey || e.which === 2) return;
|
if (e.ctrlKey || e.metaKey || e.which === 2) return;
|
||||||
if (e.preventDefault) e.preventDefault();
|
if (e.preventDefault) {
|
||||||
else e.returnValue = false;
|
e.preventDefault();
|
||||||
|
} else {
|
||||||
|
e.returnValue = false;
|
||||||
|
}
|
||||||
var currentTarget = e.currentTarget || this;
|
var currentTarget = e.currentTarget || this;
|
||||||
var args = m.route.mode === "pathname" && currentTarget.search ? parseQueryString(currentTarget.search.slice(1)) : {};
|
var args = m.route.mode === "pathname" && currentTarget.search ? parseQueryString(currentTarget.search.slice(1)) : {};
|
||||||
m.route(currentTarget[m.route.mode].slice(modes[m.route.mode].length), args)
|
m.route(currentTarget[m.route.mode].slice(modes[m.route.mode].length), args)
|
||||||
}
|
}
|
||||||
function setScroll() {
|
function setScroll() {
|
||||||
if (m.route.mode != "hash" && $location.hash) $location.hash = $location.hash;
|
if (m.route.mode != "hash" && $location.hash) {
|
||||||
else window.scrollTo(0, 0)
|
$location.hash = $location.hash;
|
||||||
|
} else {
|
||||||
|
window.scrollTo(0, 0)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
function buildQueryString(object, prefix) {
|
function buildQueryString(object, prefix) {
|
||||||
var str = [];
|
var str = [];
|
||||||
for (var prop in object) {
|
for (var prop in object) {
|
||||||
var key = prefix ? prefix + "[" + prop + "]" : prop, value = object[prop];
|
var key = prefix ? prefix + "[" + prop + "]" : prop,
|
||||||
|
value = object[prop];
|
||||||
str.push(value != null && type.call(value) === OBJECT ? buildQueryString(value, key) : encodeURIComponent(key) + "=" + encodeURIComponent(value))
|
str.push(value != null && type.call(value) === OBJECT ? buildQueryString(value, key) : encodeURIComponent(key) + "=" + encodeURIComponent(value))
|
||||||
}
|
}
|
||||||
return str.join("&")
|
return str.join("&")
|
||||||
}
|
}
|
||||||
function parseQueryString(str) {
|
function parseQueryString(str) {
|
||||||
var pairs = str.split("&"), params = {};
|
var pairs = str.split("&"),
|
||||||
|
params = {};
|
||||||
for (var i = 0, len = pairs.length; i < len; i++) {
|
for (var i = 0, len = pairs.length; i < len; i++) {
|
||||||
var pair = pairs[i].split("=");
|
var pair = pairs[i].split("=");
|
||||||
params[decodeSpace(pair[0])] = pair[1] ? decodeSpace(pair[1]) : ""
|
params[decodeSpace(pair[0])] = pair[1] ? decodeSpace(pair[1]) : ""
|
||||||
|
|
@ -705,8 +878,14 @@ var m = (function app(window, undefined) {
|
||||||
//1) `then` callbacks are called synchronously (because setTimeout is too slow, and the setImmediate polyfill is too big
|
//1) `then` callbacks are called synchronously (because setTimeout is too slow, and the setImmediate polyfill is too big
|
||||||
//2) throwing subclasses of Error cause the error to be bubbled up instead of triggering rejection (because the spec does not account for the important use case of default browser error handling, i.e. message w/ line number)
|
//2) throwing subclasses of Error cause the error to be bubbled up instead of triggering rejection (because the spec does not account for the important use case of default browser error handling, i.e. message w/ line number)
|
||||||
function Deferred(successCallback, failureCallback) {
|
function Deferred(successCallback, failureCallback) {
|
||||||
var RESOLVING = 1, REJECTING = 2, RESOLVED = 3, REJECTED = 4;
|
var RESOLVING = 1,
|
||||||
var self = this, state = 0, promiseValue = 0, next = [];
|
REJECTING = 2,
|
||||||
|
RESOLVED = 3,
|
||||||
|
REJECTED = 4;
|
||||||
|
var self = this,
|
||||||
|
state = 0,
|
||||||
|
promiseValue = 0,
|
||||||
|
next = [];
|
||||||
|
|
||||||
self["promise"] = {};
|
self["promise"] = {};
|
||||||
|
|
||||||
|
|
@ -734,11 +913,9 @@ var m = (function app(window, undefined) {
|
||||||
var deferred = new Deferred(successCallback, failureCallback);
|
var deferred = new Deferred(successCallback, failureCallback);
|
||||||
if (state === RESOLVED) {
|
if (state === RESOLVED) {
|
||||||
deferred.resolve(promiseValue)
|
deferred.resolve(promiseValue)
|
||||||
}
|
} else if (state === REJECTED) {
|
||||||
else if (state === REJECTED) {
|
|
||||||
deferred.reject(promiseValue)
|
deferred.reject(promiseValue)
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
next.push(deferred)
|
next.push(deferred)
|
||||||
}
|
}
|
||||||
return deferred.promise
|
return deferred.promise
|
||||||
|
|
@ -765,8 +942,7 @@ var m = (function app(window, undefined) {
|
||||||
promiseValue = value;
|
promiseValue = value;
|
||||||
failureCallback()
|
failureCallback()
|
||||||
})
|
})
|
||||||
}
|
} catch ( e ) {
|
||||||
catch (e) {
|
|
||||||
m.deferred.onerror(e);
|
m.deferred.onerror(e);
|
||||||
promiseValue = e;
|
promiseValue = e;
|
||||||
failureCallback()
|
failureCallback()
|
||||||
|
|
@ -781,8 +957,7 @@ var m = (function app(window, undefined) {
|
||||||
var then;
|
var then;
|
||||||
try {
|
try {
|
||||||
then = promiseValue && promiseValue.then
|
then = promiseValue && promiseValue.then
|
||||||
}
|
} catch ( e ) {
|
||||||
catch (e) {
|
|
||||||
m.deferred.onerror(e);
|
m.deferred.onerror(e);
|
||||||
promiseValue = e;
|
promiseValue = e;
|
||||||
state = REJECTING;
|
state = REJECTING;
|
||||||
|
|
@ -798,13 +973,11 @@ var m = (function app(window, undefined) {
|
||||||
try {
|
try {
|
||||||
if (state === RESOLVING && typeof successCallback === FUNCTION) {
|
if (state === RESOLVING && typeof successCallback === FUNCTION) {
|
||||||
promiseValue = successCallback(promiseValue)
|
promiseValue = successCallback(promiseValue)
|
||||||
}
|
} else if (state === REJECTING && typeof failureCallback === "function") {
|
||||||
else if (state === REJECTING && typeof failureCallback === "function") {
|
|
||||||
promiseValue = failureCallback(promiseValue);
|
promiseValue = failureCallback(promiseValue);
|
||||||
state = RESOLVING
|
state = RESOLVING
|
||||||
}
|
}
|
||||||
}
|
} catch ( e ) {
|
||||||
catch (e) {
|
|
||||||
m.deferred.onerror(e);
|
m.deferred.onerror(e);
|
||||||
promiseValue = e;
|
promiseValue = e;
|
||||||
return finish()
|
return finish()
|
||||||
|
|
@ -813,8 +986,7 @@ var m = (function app(window, undefined) {
|
||||||
if (promiseValue === self) {
|
if (promiseValue === self) {
|
||||||
promiseValue = TypeError();
|
promiseValue = TypeError();
|
||||||
finish()
|
finish()
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
thennable(then, function() {
|
thennable(then, function() {
|
||||||
finish(RESOLVED)
|
finish(RESOLVED)
|
||||||
}, finish, function() {
|
}, finish, function() {
|
||||||
|
|
@ -825,7 +997,8 @@ var m = (function app(window, undefined) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
m.deferred.onerror = function(e) {
|
m.deferred.onerror = function(e) {
|
||||||
if (type.call(e) === "[object Error]" && !e.constructor.toString().match(/ Error/)) throw e
|
if (type.call(e) === "[object Error]" && !e.constructor.toString().match(/ Error/))
|
||||||
|
throw e
|
||||||
};
|
};
|
||||||
|
|
||||||
m.sync = function(args) {
|
m.sync = function(args) {
|
||||||
|
|
@ -833,7 +1006,9 @@ var m = (function app(window, undefined) {
|
||||||
function synchronizer(pos, resolved) {
|
function synchronizer(pos, resolved) {
|
||||||
return function(value) {
|
return function(value) {
|
||||||
results[pos] = value;
|
results[pos] = value;
|
||||||
if (!resolved) method = "reject";
|
if (!resolved) {
|
||||||
|
method = "reject";
|
||||||
|
}
|
||||||
if (--outstanding === 0) {
|
if (--outstanding === 0) {
|
||||||
deferred.promise(results);
|
deferred.promise(results);
|
||||||
deferred[method](results)
|
deferred[method](results)
|
||||||
|
|
@ -849,12 +1024,15 @@ var m = (function app(window, undefined) {
|
||||||
for (var i = 0; i < args.length; i++) {
|
for (var i = 0; i < args.length; i++) {
|
||||||
args[i].then(synchronizer(i, true), synchronizer(i, false))
|
args[i].then(synchronizer(i, true), synchronizer(i, false))
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
deferred.resolve([]);
|
||||||
}
|
}
|
||||||
else deferred.resolve([]);
|
|
||||||
|
|
||||||
return deferred.promise
|
return deferred.promise
|
||||||
};
|
};
|
||||||
function identity(value) {return value}
|
function identity(value) {
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
function ajax(options) {
|
function ajax(options) {
|
||||||
if (options.dataType && options.dataType.toLowerCase() === "jsonp") {
|
if (options.dataType && options.dataType.toLowerCase() === "jsonp") {
|
||||||
|
|
@ -879,7 +1057,9 @@ var m = (function app(window, undefined) {
|
||||||
type: "error",
|
type: "error",
|
||||||
target: {
|
target: {
|
||||||
status: 500,
|
status: 500,
|
||||||
responseText: JSON.stringify({error: "Error making jsonp request"})
|
responseText: JSON.stringify({
|
||||||
|
error: "Error making jsonp request"
|
||||||
|
})
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
window[callbackKey] = undefined;
|
window[callbackKey] = undefined;
|
||||||
|
|
@ -897,14 +1077,22 @@ var m = (function app(window, undefined) {
|
||||||
+ "=" + callbackKey
|
+ "=" + callbackKey
|
||||||
+ "&" + buildQueryString(options.data || {});
|
+ "&" + buildQueryString(options.data || {});
|
||||||
$document.body.appendChild(script)
|
$document.body.appendChild(script)
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
var xhr = new window.XMLHttpRequest;
|
var xhr = new window.XMLHttpRequest;
|
||||||
xhr.open(options.method, options.url, true, options.user, options.password);
|
xhr.open(options.method, options.url, true, options.user, options.password);
|
||||||
xhr.onreadystatechange = function() {
|
xhr.onreadystatechange = function() {
|
||||||
if (xhr.readyState === 4) {
|
if (xhr.readyState === 4) {
|
||||||
if (xhr.status >= 200 && xhr.status < 300) options.onload({type: "load", target: xhr});
|
if (xhr.status >= 200 && xhr.status < 300) {
|
||||||
else options.onerror({type: "error", target: xhr})
|
options.onload({
|
||||||
|
type: "load",
|
||||||
|
target: xhr
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
options.onerror({
|
||||||
|
type: "error",
|
||||||
|
target: xhr
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
if (options.serialize === JSON.stringify && options.data && options.method !== "GET") {
|
if (options.serialize === JSON.stringify && options.data && options.method !== "GET") {
|
||||||
|
|
@ -915,7 +1103,9 @@ var m = (function app(window, undefined) {
|
||||||
}
|
}
|
||||||
if (typeof options.config === FUNCTION) {
|
if (typeof options.config === FUNCTION) {
|
||||||
var maybeXhr = options.config(xhr, options);
|
var maybeXhr = options.config(xhr, options);
|
||||||
if (maybeXhr != null) xhr = maybeXhr
|
if (maybeXhr != null) {
|
||||||
|
xhr = maybeXhr
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var data = options.method === "GET" || !options.data ? "" : options.data
|
var data = options.method === "GET" || !options.data ? "" : options.data
|
||||||
|
|
@ -931,8 +1121,9 @@ var m = (function app(window, undefined) {
|
||||||
var prefix = xhrOptions.url.indexOf("?") < 0 ? "?" : "&";
|
var prefix = xhrOptions.url.indexOf("?") < 0 ? "?" : "&";
|
||||||
var querystring = buildQueryString(data);
|
var querystring = buildQueryString(data);
|
||||||
xhrOptions.url = xhrOptions.url + (querystring ? prefix + querystring : "")
|
xhrOptions.url = xhrOptions.url + (querystring ? prefix + querystring : "")
|
||||||
|
} else {
|
||||||
|
xhrOptions.data = serialize(data);
|
||||||
}
|
}
|
||||||
else xhrOptions.data = serialize(data);
|
|
||||||
return xhrOptions
|
return xhrOptions
|
||||||
}
|
}
|
||||||
function parameterizeUrl(url, data) {
|
function parameterizeUrl(url, data) {
|
||||||
|
|
@ -948,7 +1139,9 @@ var m = (function app(window, undefined) {
|
||||||
}
|
}
|
||||||
|
|
||||||
m.request = function(xhrOptions) {
|
m.request = function(xhrOptions) {
|
||||||
if (xhrOptions.background !== true) m.startComputation();
|
if (xhrOptions.background !== true) {
|
||||||
|
m.startComputation();
|
||||||
|
}
|
||||||
var deferred = m.deferred();
|
var deferred = m.deferred();
|
||||||
var isJSONP = xhrOptions.dataType && xhrOptions.dataType.toLowerCase() === "jsonp";
|
var isJSONP = xhrOptions.dataType && xhrOptions.dataType.toLowerCase() === "jsonp";
|
||||||
var serialize = xhrOptions.serialize = isJSONP ? identity : xhrOptions.serialize || JSON.stringify;
|
var serialize = xhrOptions.serialize = isJSONP ? identity : xhrOptions.serialize || JSON.stringify;
|
||||||
|
|
@ -965,17 +1158,21 @@ var m = (function app(window, undefined) {
|
||||||
var response = unwrap(deserialize(extract(e.target, xhrOptions)));
|
var response = unwrap(deserialize(extract(e.target, xhrOptions)));
|
||||||
if (e.type === "load") {
|
if (e.type === "load") {
|
||||||
if (type.call(response) === ARRAY && xhrOptions.type) {
|
if (type.call(response) === ARRAY && xhrOptions.type) {
|
||||||
for (var i = 0; i < response.length; i++) response[i] = new xhrOptions.type(response[i])
|
for (var i = 0; i < response.length; i++) {
|
||||||
|
response[i] = new xhrOptions.type(response[i])
|
||||||
|
}
|
||||||
|
} else if (xhrOptions.type) {
|
||||||
|
response = new xhrOptions.type(response)
|
||||||
}
|
}
|
||||||
else if (xhrOptions.type) response = new xhrOptions.type(response)
|
|
||||||
}
|
}
|
||||||
deferred[e.type === "load" ? "resolve" : "reject"](response)
|
deferred[e.type === "load" ? "resolve" : "reject"](response)
|
||||||
}
|
} catch ( e ) {
|
||||||
catch (e) {
|
|
||||||
m.deferred.onerror(e);
|
m.deferred.onerror(e);
|
||||||
deferred.reject(e)
|
deferred.reject(e)
|
||||||
}
|
}
|
||||||
if (xhrOptions.background !== true) m.endComputation()
|
if (xhrOptions.background !== true) {
|
||||||
|
m.endComputation()
|
||||||
|
}
|
||||||
};
|
};
|
||||||
ajax(xhrOptions);
|
ajax(xhrOptions);
|
||||||
deferred.promise(xhrOptions.initialValue);
|
deferred.promise(xhrOptions.initialValue);
|
||||||
|
|
@ -993,8 +1190,15 @@ var m = (function app(window, undefined) {
|
||||||
return m
|
return m
|
||||||
})(typeof window != "undefined" ? window : {});
|
})(typeof window != "undefined" ? window : {});
|
||||||
|
|
||||||
if (typeof module != "undefined" && module !== null && module.exports) module.exports = m;
|
if (typeof module != "undefined" && module !== null && module.exports) {
|
||||||
else if (typeof define === "function" && define.amd) define(function() {return m});
|
module.exports = m;
|
||||||
|
} else if (typeof define === "function" && define.amd) {
|
||||||
|
define(function() {
|
||||||
|
return m
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//make "use strict" and nodejs happy
|
//make "use strict" and nodejs happy
|
||||||
var window = this
|
var window = this
|
||||||
|
|
|
||||||
510
mithril.js
510
mithril.js
|
|
@ -1,7 +1,11 @@
|
||||||
var m = (function app(window, undefined) {
|
var m = (function app(window, undefined) {
|
||||||
var OBJECT = "[object Object]", ARRAY = "[object Array]", STRING = "[object String]", FUNCTION = "function";
|
var OBJECT = "[object Object]",
|
||||||
|
ARRAY = "[object Array]",
|
||||||
|
STRING = "[object String]",
|
||||||
|
FUNCTION = "function";
|
||||||
var type = {}.toString;
|
var type = {}.toString;
|
||||||
var parser = /(?:(^|#|\.)([^#\.\[\]]+))|(\[.+?\])/g, attrParser = /\[(.+?)(?:=("|'|)(.*?)\2)?\]/;
|
var parser = /(?:(^|#|\.)([^#\.\[\]]+))|(\[.+?\])/g,
|
||||||
|
attrParser = /\[(.+?)(?:=("|'|)(.*?)\2)?\]/;
|
||||||
var voidElements = /^(AREA|BASE|BR|COL|COMMAND|EMBED|HR|IMG|INPUT|KEYGEN|LINK|META|PARAM|SOURCE|TRACK|WBR)$/;
|
var voidElements = /^(AREA|BASE|BR|COL|COMMAND|EMBED|HR|IMG|INPUT|KEYGEN|LINK|META|PARAM|SOURCE|TRACK|WBR)$/;
|
||||||
|
|
||||||
// caching commonly used variables
|
// caching commonly used variables
|
||||||
|
|
@ -36,32 +40,44 @@ var m = (function app(window, undefined) {
|
||||||
var hasAttrs = args[1] != null && type.call(args[1]) === OBJECT && !("tag" in args[1]) && !("subtree" in args[1]);
|
var hasAttrs = args[1] != null && type.call(args[1]) === OBJECT && !("tag" in args[1]) && !("subtree" in args[1]);
|
||||||
var attrs = hasAttrs ? args[1] : {};
|
var attrs = hasAttrs ? args[1] : {};
|
||||||
var classAttrName = "class" in attrs ? "class" : "className";
|
var classAttrName = "class" in attrs ? "class" : "className";
|
||||||
var cell = {tag: "div", attrs: {}};
|
var cell = {
|
||||||
var match, classes = [];
|
tag: "div",
|
||||||
if (type.call(args[0]) != STRING) throw new Error("selector in m(selector, attrs, children) should be a string")
|
attrs: {}
|
||||||
|
};
|
||||||
|
var match,
|
||||||
|
classes = [];
|
||||||
|
if (type.call(args[0]) != STRING)
|
||||||
|
throw new Error("selector in m(selector, attrs, children) should be a string")
|
||||||
while (match = parser.exec(args[0])) {
|
while (match = parser.exec(args[0])) {
|
||||||
if (match[1] === "" && match[2]) cell.tag = match[2];
|
if (match[1] === "" && match[2]) {
|
||||||
else if (match[1] === "#") cell.attrs.id = match[2];
|
cell.tag = match[2];
|
||||||
else if (match[1] === ".") classes.push(match[2]);
|
} else if (match[1] === "#") {
|
||||||
else if (match[3][0] === "[") {
|
cell.attrs.id = match[2];
|
||||||
|
} else if (match[1] === ".") {
|
||||||
|
classes.push(match[2]);
|
||||||
|
} else if (match[3][0] === "[") {
|
||||||
var pair = attrParser.exec(match[3]);
|
var pair = attrParser.exec(match[3]);
|
||||||
cell.attrs[pair[1]] = pair[3] || (pair[2] ? "" : true)
|
cell.attrs[pair[1]] = pair[3] || (pair[2] ? "" : true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (classes.length > 0) cell.attrs[classAttrName] = classes.join(" ");
|
if (classes.length > 0) {
|
||||||
|
cell.attrs[classAttrName] = classes.join(" ");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
var children = hasAttrs ? args[2] : args[1];
|
var children = hasAttrs ? args[2] : args[1];
|
||||||
if (type.call(children) === ARRAY) {
|
if (type.call(children) === ARRAY) {
|
||||||
cell.children = children
|
cell.children = children
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
cell.children = hasAttrs ? args.slice(2) : args.slice(1)
|
cell.children = hasAttrs ? args.slice(2) : args.slice(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
for (var attrName in attrs) {
|
for (var attrName in attrs) {
|
||||||
if (attrName === classAttrName) cell.attrs[attrName] = (cell.attrs[attrName] || "") + " " + attrs[attrName];
|
if (attrName === classAttrName) {
|
||||||
else cell.attrs[attrName] = attrs[attrName]
|
cell.attrs[attrName] = (cell.attrs[attrName] || "") + " " + attrs[attrName];
|
||||||
|
} else {
|
||||||
|
cell.attrs[attrName] = attrs[attrName]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return cell
|
return cell
|
||||||
}
|
}
|
||||||
|
|
@ -91,20 +107,26 @@ var m = (function app(window, undefined) {
|
||||||
//there's logic that relies on the assumption that null and undefined data are equivalent to empty strings
|
//there's logic that relies on the assumption that null and undefined data are equivalent to empty strings
|
||||||
//- this prevents lifecycle surprises from procedural helpers that mix implicit and explicit return statements (e.g. function foo() {if (cond) return m("div")}
|
//- this prevents lifecycle surprises from procedural helpers that mix implicit and explicit return statements (e.g. function foo() {if (cond) return m("div")}
|
||||||
//- it simplifies diffing code
|
//- it simplifies diffing code
|
||||||
if (data == null || data.toString() == null) data = "";
|
if (data == null || data.toString() == null) {
|
||||||
|
data = "";
|
||||||
|
}
|
||||||
if (data.subtree === "retain") return cached;
|
if (data.subtree === "retain") return cached;
|
||||||
var cachedType = type.call(cached), dataType = type.call(data);
|
var cachedType = type.call(cached),
|
||||||
|
dataType = type.call(data);
|
||||||
if (cached == null || cachedType !== dataType) {
|
if (cached == null || cachedType !== dataType) {
|
||||||
if (cached != null) {
|
if (cached != null) {
|
||||||
if (parentCache && parentCache.nodes) {
|
if (parentCache && parentCache.nodes) {
|
||||||
var offset = index - parentIndex;
|
var offset = index - parentIndex;
|
||||||
var end = offset + (dataType === ARRAY ? data : cached.nodes).length;
|
var end = offset + (dataType === ARRAY ? data : cached.nodes).length;
|
||||||
clear(parentCache.nodes.slice(offset, end), parentCache.slice(offset, end))
|
clear(parentCache.nodes.slice(offset, end), parentCache.slice(offset, end))
|
||||||
|
} else if (cached.nodes) {
|
||||||
|
clear(cached.nodes, cached)
|
||||||
}
|
}
|
||||||
else if (cached.nodes) clear(cached.nodes, cached)
|
|
||||||
}
|
}
|
||||||
cached = new data.constructor;
|
cached = new data.constructor;
|
||||||
if (cached.tag) cached = {}; //if constructor creates a virtual dom element, use a blank object as the base cached node instead of copying the virtual el (#277)
|
if (cached.tag) {
|
||||||
|
cached = {};
|
||||||
|
} //if constructor creates a virtual dom element, use a blank object as the base cached node instead of copying the virtual el (#277)
|
||||||
cached.nodes = []
|
cached.nodes = []
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -117,7 +139,9 @@ var m = (function app(window, undefined) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var nodes = [], intact = cached.length === data.length, subArrayCount = 0;
|
var nodes = [],
|
||||||
|
intact = cached.length === data.length,
|
||||||
|
subArrayCount = 0;
|
||||||
|
|
||||||
//keys algorithm: sort elements without recreating them if keys are present
|
//keys algorithm: sort elements without recreating them if keys are present
|
||||||
//1) create a map of all existing keys, and mark all for deletion
|
//1) create a map of all existing keys, and mark all for deletion
|
||||||
|
|
@ -125,33 +149,58 @@ var m = (function app(window, undefined) {
|
||||||
//3) if key exists in new list, change action from deletion to a move
|
//3) if key exists in new list, change action from deletion to a move
|
||||||
//4) for each key, handle its corresponding action as marked in previous steps
|
//4) for each key, handle its corresponding action as marked in previous steps
|
||||||
//5) copy unkeyed items into their respective gaps
|
//5) copy unkeyed items into their respective gaps
|
||||||
var DELETION = 1, INSERTION = 2 , MOVE = 3;
|
var DELETION = 1,
|
||||||
var existing = {}, unkeyed = [], shouldMaintainIdentities = false;
|
INSERTION = 2 ,
|
||||||
|
MOVE = 3;
|
||||||
|
var existing = {},
|
||||||
|
unkeyed = [],
|
||||||
|
shouldMaintainIdentities = false;
|
||||||
for (var i = 0, len = cached.length; i < len; i++) {
|
for (var i = 0, len = cached.length; i < len; i++) {
|
||||||
if (cached[i] && cached[i].attrs && cached[i].attrs.key != null) {
|
if (cached[i] && cached[i].attrs && cached[i].attrs.key != null) {
|
||||||
shouldMaintainIdentities = true;
|
shouldMaintainIdentities = true;
|
||||||
existing[cached[i].attrs.key] = {action: DELETION, index: i}
|
existing[cached[i].attrs.key] = {
|
||||||
|
action: DELETION,
|
||||||
|
index: i
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (shouldMaintainIdentities) {
|
if (shouldMaintainIdentities) {
|
||||||
if (data.indexOf(null) > -1) data = data.filter(function(x) {return x != null})
|
if (data.indexOf(null) > -1) {
|
||||||
|
data = data.filter(function(x) {
|
||||||
|
return x != null
|
||||||
|
})
|
||||||
|
}
|
||||||
for (var i = 0, len = data.length; i < len; i++) {
|
for (var i = 0, len = data.length; i < len; i++) {
|
||||||
if (data[i] && data[i].attrs) {
|
if (data[i] && data[i].attrs) {
|
||||||
if (data[i].attrs.key != null) {
|
if (data[i].attrs.key != null) {
|
||||||
var key = data[i].attrs.key;
|
var key = data[i].attrs.key;
|
||||||
if (!existing[key]) existing[key] = {action: INSERTION, index: i};
|
if (!existing[key]) {
|
||||||
else existing[key] = {
|
existing[key] = {
|
||||||
|
action: INSERTION,
|
||||||
|
index: i
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
existing[key] = {
|
||||||
action: MOVE,
|
action: MOVE,
|
||||||
index: i,
|
index: i,
|
||||||
from: existing[key].index,
|
from: existing[key].index,
|
||||||
element: parentElement.childNodes[existing[key].index] || $document.createElement("div")
|
element: parentElement.childNodes[existing[key].index] || $document.createElement("div")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else unkeyed.push({index: i, element: parentElement.childNodes[i] || $document.createElement("div")})
|
} else {
|
||||||
|
unkeyed.push({
|
||||||
|
index: i,
|
||||||
|
element: parentElement.childNodes[i] || $document.createElement("div")
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var actions = Object.keys(existing).map(function(key) {return existing[key]});
|
}
|
||||||
var changes = actions.sort(function(a, b) {return a.action - b.action || a.index - b.index});
|
var actions = Object.keys(existing).map(function(key) {
|
||||||
|
return existing[key]
|
||||||
|
});
|
||||||
|
var changes = actions.sort(function(a, b) {
|
||||||
|
return a.action - b.action || a.index - b.index
|
||||||
|
});
|
||||||
var newCached = cached.slice();
|
var newCached = cached.slice();
|
||||||
|
|
||||||
for (var i = 0, change; change = changes[i]; i++) {
|
for (var i = 0, change; change = changes[i]; i++) {
|
||||||
|
|
@ -163,7 +212,12 @@ var m = (function app(window, undefined) {
|
||||||
var dummy = $document.createElement("div");
|
var dummy = $document.createElement("div");
|
||||||
dummy.key = data[change.index].attrs.key;
|
dummy.key = data[change.index].attrs.key;
|
||||||
parentElement.insertBefore(dummy, parentElement.childNodes[change.index] || null);
|
parentElement.insertBefore(dummy, parentElement.childNodes[change.index] || null);
|
||||||
newCached.splice(change.index, 0, {attrs: {key: data[change.index].attrs.key}, nodes: [dummy]})
|
newCached.splice(change.index, 0, {
|
||||||
|
attrs: {
|
||||||
|
key: data[change.index].attrs.key
|
||||||
|
},
|
||||||
|
nodes: [dummy]
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
if (change.action === MOVE) {
|
if (change.action === MOVE) {
|
||||||
|
|
@ -180,7 +234,9 @@ var m = (function app(window, undefined) {
|
||||||
}
|
}
|
||||||
cached = newCached;
|
cached = newCached;
|
||||||
cached.nodes = [];
|
cached.nodes = [];
|
||||||
for (var i = 0, child; child = parentElement.childNodes[i]; i++) cached.nodes.push(child)
|
for (var i = 0, child; child = parentElement.childNodes[i]; i++) {
|
||||||
|
cached.nodes.push(child)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
//end key algorithm
|
//end key algorithm
|
||||||
|
|
||||||
|
|
@ -188,14 +244,17 @@ var m = (function app(window, undefined) {
|
||||||
//diff each item in the array
|
//diff each item in the array
|
||||||
var item = build(parentElement, parentTag, cached, index, data[i], cached[cacheCount], shouldReattach, index + subArrayCount || subArrayCount, editable, namespace, configs);
|
var item = build(parentElement, parentTag, cached, index, data[i], cached[cacheCount], shouldReattach, index + subArrayCount || subArrayCount, editable, namespace, configs);
|
||||||
if (item === undefined) continue;
|
if (item === undefined) continue;
|
||||||
if (!item.nodes.intact) intact = false;
|
if (!item.nodes.intact) {
|
||||||
|
intact = false;
|
||||||
|
}
|
||||||
if (item.$trusted) {
|
if (item.$trusted) {
|
||||||
//fix offset of next element if item was a trusted string w/ more than one html element
|
//fix offset of next element if item was a trusted string w/ more than one html element
|
||||||
//the first clause in the regexp matches elements
|
//the first clause in the regexp matches elements
|
||||||
//the second clause (after the pipe) matches text nodes
|
//the second clause (after the pipe) matches text nodes
|
||||||
subArrayCount += (item.match(/<[^\/]|\>\s*[^<]/g) || []).length
|
subArrayCount += (item.match(/<[^\/]|\>\s*[^<]/g) || []).length
|
||||||
|
} else {
|
||||||
|
subArrayCount += type.call(item) === ARRAY ? item.length : 1;
|
||||||
}
|
}
|
||||||
else subArrayCount += type.call(item) === ARRAY ? item.length : 1;
|
|
||||||
cached[cacheCount++] = item
|
cached[cacheCount++] = item
|
||||||
}
|
}
|
||||||
if (!intact) {
|
if (!intact) {
|
||||||
|
|
@ -203,37 +262,58 @@ var m = (function app(window, undefined) {
|
||||||
|
|
||||||
//update the list of DOM nodes by collecting the nodes from each item
|
//update the list of DOM nodes by collecting the nodes from each item
|
||||||
for (var i = 0, len = data.length; i < len; i++) {
|
for (var i = 0, len = data.length; i < len; i++) {
|
||||||
if (cached[i] != null) nodes.push.apply(nodes, cached[i].nodes)
|
if (cached[i] != null) {
|
||||||
|
nodes.push.apply(nodes, cached[i].nodes)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
//remove items from the end of the array if the new array is shorter than the old one
|
//remove items from the end of the array if the new array is shorter than the old one
|
||||||
//if errors ever happen here, the issue is most likely a bug in the construction of the `cached` data structure somewhere earlier in the program
|
//if errors ever happen here, the issue is most likely a bug in the construction of the `cached` data structure somewhere earlier in the program
|
||||||
for (var i = 0, node; node = cached.nodes[i]; i++) {
|
for (var i = 0, node; node = cached.nodes[i]; i++) {
|
||||||
if (node.parentNode != null && nodes.indexOf(node) < 0) clear([node], [cached[i]])
|
if (node.parentNode != null && nodes.indexOf(node) < 0) {
|
||||||
|
clear([node], [cached[i]])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (data.length < cached.length) {
|
||||||
|
cached.length = data.length;
|
||||||
}
|
}
|
||||||
if (data.length < cached.length) cached.length = data.length;
|
|
||||||
cached.nodes = nodes
|
cached.nodes = nodes
|
||||||
}
|
}
|
||||||
|
} else if (data != null && dataType === OBJECT) {
|
||||||
|
if (!data.attrs) {
|
||||||
|
data.attrs = {};
|
||||||
|
}
|
||||||
|
if (!cached.attrs) {
|
||||||
|
cached.attrs = {};
|
||||||
}
|
}
|
||||||
else if (data != null && dataType === OBJECT) {
|
|
||||||
if (!data.attrs) data.attrs = {};
|
|
||||||
if (!cached.attrs) cached.attrs = {};
|
|
||||||
|
|
||||||
var dataAttrKeys = Object.keys(data.attrs);
|
var dataAttrKeys = Object.keys(data.attrs);
|
||||||
var hasKeys = dataAttrKeys.length > ("key" in data.attrs ? 1 : 0)
|
var hasKeys = dataAttrKeys.length > ("key" in data.attrs ? 1 : 0)
|
||||||
//if an element is different enough from the one in cache, recreate it
|
//if an element is different enough from the one in cache, recreate it
|
||||||
if (data.tag != cached.tag || dataAttrKeys.join() != Object.keys(cached.attrs).join() || data.attrs.id != cached.attrs.id) {
|
if (data.tag != cached.tag || dataAttrKeys.join() != Object.keys(cached.attrs).join() || data.attrs.id != cached.attrs.id) {
|
||||||
if (cached.nodes.length) clear(cached.nodes);
|
if (cached.nodes.length) {
|
||||||
if (cached.configContext && typeof cached.configContext.onunload === FUNCTION) cached.configContext.onunload()
|
clear(cached.nodes);
|
||||||
|
}
|
||||||
|
if (cached.configContext && typeof cached.configContext.onunload === FUNCTION) {
|
||||||
|
cached.configContext.onunload()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (type.call(data.tag) != STRING) return;
|
if (type.call(data.tag) != STRING) return;
|
||||||
|
|
||||||
var node, isNew = cached.nodes.length === 0;
|
var node,
|
||||||
if (data.attrs.xmlns) namespace = data.attrs.xmlns;
|
isNew = cached.nodes.length === 0;
|
||||||
else if (data.tag === "svg") namespace = "http://www.w3.org/2000/svg";
|
if (data.attrs.xmlns) {
|
||||||
else if (data.tag === "math") namespace = "http://www.w3.org/1998/Math/MathML";
|
namespace = data.attrs.xmlns;
|
||||||
|
} else if (data.tag === "svg") {
|
||||||
|
namespace = "http://www.w3.org/2000/svg";
|
||||||
|
} else if (data.tag === "math") {
|
||||||
|
namespace = "http://www.w3.org/1998/Math/MathML";
|
||||||
|
}
|
||||||
if (isNew) {
|
if (isNew) {
|
||||||
if (data.attrs.is) node = namespace === undefined ? $document.createElement(data.tag, data.attrs.is) : $document.createElementNS(namespace, data.tag, data.attrs.is);
|
if (data.attrs.is) {
|
||||||
else node = namespace === undefined ? $document.createElement(data.tag) : $document.createElementNS(namespace, data.tag);
|
node = namespace === undefined ? $document.createElement(data.tag, data.attrs.is) : $document.createElementNS(namespace, data.tag, data.attrs.is);
|
||||||
|
} else {
|
||||||
|
node = namespace === undefined ? $document.createElement(data.tag) : $document.createElementNS(namespace, data.tag);
|
||||||
|
}
|
||||||
cached = {
|
cached = {
|
||||||
tag: data.tag,
|
tag: data.tag,
|
||||||
//set attributes first, then create children
|
//set attributes first, then create children
|
||||||
|
|
@ -243,17 +323,26 @@ var m = (function app(window, undefined) {
|
||||||
data.children,
|
data.children,
|
||||||
nodes: [node]
|
nodes: [node]
|
||||||
};
|
};
|
||||||
if (cached.children && !cached.children.nodes) cached.children.nodes = [];
|
if (cached.children && !cached.children.nodes) {
|
||||||
//edge case: setting value on <select> doesn't work before children exist, so set it again after children have been created
|
cached.children.nodes = [];
|
||||||
if (data.tag === "select" && data.attrs.value) setAttributes(node, data.tag, {value: data.attrs.value}, {}, namespace);
|
|
||||||
parentElement.insertBefore(node, parentElement.childNodes[index] || null)
|
|
||||||
}
|
}
|
||||||
else {
|
//edge case: setting value on <select> doesn't work before children exist, so set it again after children have been created
|
||||||
|
if (data.tag === "select" && data.attrs.value) {
|
||||||
|
setAttributes(node, data.tag, {
|
||||||
|
value: data.attrs.value
|
||||||
|
}, {}, namespace);
|
||||||
|
}
|
||||||
|
parentElement.insertBefore(node, parentElement.childNodes[index] || null)
|
||||||
|
} else {
|
||||||
node = cached.nodes[0];
|
node = cached.nodes[0];
|
||||||
if (dataAttrKeys.length) setAttributes(node, data.tag, data.attrs, cached.attrs, namespace);
|
if (dataAttrKeys.length) {
|
||||||
|
setAttributes(node, data.tag, data.attrs, cached.attrs, namespace);
|
||||||
|
}
|
||||||
cached.children = build(node, data.tag, undefined, undefined, data.children, cached.children, false, 0, data.attrs.contenteditable ? node : editable, namespace, configs);
|
cached.children = build(node, data.tag, undefined, undefined, data.children, cached.children, false, 0, data.attrs.contenteditable ? node : editable, namespace, configs);
|
||||||
cached.nodes.intact = true;
|
cached.nodes.intact = true;
|
||||||
if (shouldReattach === true && node != null) parentElement.insertBefore(node, parentElement.childNodes[index] || null)
|
if (shouldReattach === true && node != null) {
|
||||||
|
parentElement.insertBefore(node, parentElement.childNodes[index] || null)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
//schedule configs to be called. They are called after `build` finishes running
|
//schedule configs to be called. They are called after `build` finishes running
|
||||||
if (typeof data.attrs["config"] === FUNCTION) {
|
if (typeof data.attrs["config"] === FUNCTION) {
|
||||||
|
|
@ -267,34 +356,34 @@ var m = (function app(window, undefined) {
|
||||||
};
|
};
|
||||||
configs.push(callback(data, [node, !isNew, context, cached]))
|
configs.push(callback(data, [node, !isNew, context, cached]))
|
||||||
}
|
}
|
||||||
}
|
} else if (typeof dataType != FUNCTION) {
|
||||||
else if (typeof dataType != FUNCTION) {
|
|
||||||
//handle text nodes
|
//handle text nodes
|
||||||
var nodes;
|
var nodes;
|
||||||
if (cached.nodes.length === 0) {
|
if (cached.nodes.length === 0) {
|
||||||
if (data.$trusted) {
|
if (data.$trusted) {
|
||||||
nodes = injectHTML(parentElement, index, data)
|
nodes = injectHTML(parentElement, index, data)
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
nodes = [$document.createTextNode(data)];
|
nodes = [$document.createTextNode(data)];
|
||||||
if (!parentElement.nodeName.match(voidElements)) parentElement.insertBefore(nodes[0], parentElement.childNodes[index] || null)
|
if (!parentElement.nodeName.match(voidElements)) {
|
||||||
|
parentElement.insertBefore(nodes[0], parentElement.childNodes[index] || null)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
cached = "string number boolean".indexOf(typeof data) > -1 ? new data.constructor(data) : data;
|
cached = "string number boolean".indexOf(typeof data) > -1 ? new data.constructor(data) : data;
|
||||||
cached.nodes = nodes
|
cached.nodes = nodes
|
||||||
}
|
} else if (cached.valueOf() !== data.valueOf() || shouldReattach === true) {
|
||||||
else if (cached.valueOf() !== data.valueOf() || shouldReattach === true) {
|
|
||||||
nodes = cached.nodes;
|
nodes = cached.nodes;
|
||||||
if (!editable || editable !== $document.activeElement) {
|
if (!editable || editable !== $document.activeElement) {
|
||||||
if (data.$trusted) {
|
if (data.$trusted) {
|
||||||
clear(nodes, cached);
|
clear(nodes, cached);
|
||||||
nodes = injectHTML(parentElement, index, data)
|
nodes = injectHTML(parentElement, index, data)
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
//corner case: replacing the nodeValue of a text node that is a child of a textarea/contenteditable doesn't work
|
//corner case: replacing the nodeValue of a text node that is a child of a textarea/contenteditable doesn't work
|
||||||
//we need to update the value property of the parent textarea or the innerHTML of the contenteditable element instead
|
//we need to update the value property of the parent textarea or the innerHTML of the contenteditable element instead
|
||||||
if (parentTag === "textarea") parentElement.value = data;
|
if (parentTag === "textarea") {
|
||||||
else if (editable) editable.innerHTML = data;
|
parentElement.value = data;
|
||||||
else {
|
} else if (editable) {
|
||||||
|
editable.innerHTML = data;
|
||||||
|
} else {
|
||||||
if (nodes[0].nodeType === 1 || nodes.length > 1) { //was a trusted string
|
if (nodes[0].nodeType === 1 || nodes.length > 1) { //was a trusted string
|
||||||
clear(cached.nodes, cached);
|
clear(cached.nodes, cached);
|
||||||
nodes = [$document.createTextNode(data)]
|
nodes = [$document.createTextNode(data)]
|
||||||
|
|
@ -306,8 +395,9 @@ var m = (function app(window, undefined) {
|
||||||
}
|
}
|
||||||
cached = new data.constructor(data);
|
cached = new data.constructor(data);
|
||||||
cached.nodes = nodes
|
cached.nodes = nodes
|
||||||
|
} else {
|
||||||
|
cached.nodes.intact = true
|
||||||
}
|
}
|
||||||
else cached.nodes.intact = true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return cached
|
return cached
|
||||||
|
|
@ -329,30 +419,41 @@ var m = (function app(window, undefined) {
|
||||||
//handle `style: {...}`
|
//handle `style: {...}`
|
||||||
else if (attrName === "style" && dataAttr != null && type.call(dataAttr) === OBJECT) {
|
else if (attrName === "style" && dataAttr != null && type.call(dataAttr) === OBJECT) {
|
||||||
for (var rule in dataAttr) {
|
for (var rule in dataAttr) {
|
||||||
if (cachedAttr == null || cachedAttr[rule] !== dataAttr[rule]) node.style[rule] = dataAttr[rule]
|
if (cachedAttr == null || cachedAttr[rule] !== dataAttr[rule]) {
|
||||||
|
node.style[rule] = dataAttr[rule]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
for (var rule in cachedAttr) {
|
for (var rule in cachedAttr) {
|
||||||
if (!(rule in dataAttr)) node.style[rule] = ""
|
if (!(rule in dataAttr)) {
|
||||||
|
node.style[rule] = ""
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//handle SVG
|
//handle SVG
|
||||||
else if (namespace != null) {
|
else if (namespace != null) {
|
||||||
if (attrName === "href") node.setAttributeNS("http://www.w3.org/1999/xlink", "href", dataAttr);
|
if (attrName === "href") {
|
||||||
else if (attrName === "className") node.setAttribute("class", dataAttr);
|
node.setAttributeNS("http://www.w3.org/1999/xlink", "href", dataAttr);
|
||||||
else node.setAttribute(attrName, dataAttr)
|
} else if (attrName === "className") {
|
||||||
|
node.setAttribute("class", dataAttr);
|
||||||
|
} else {
|
||||||
|
node.setAttribute(attrName, dataAttr)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
//handle cases that are properties (but ignore cases where we should use setAttribute instead)
|
//handle cases that are properties (but ignore cases where we should use setAttribute instead)
|
||||||
//- list and form are typically used as strings, but are DOM element references in js
|
//- list and form are typically used as strings, but are DOM element references in js
|
||||||
//- when using CSS selectors (e.g. `m("[style='']")`), style is used as a string, but it's an object in js
|
//- when using CSS selectors (e.g. `m("[style='']")`), style is used as a string, but it's an object in js
|
||||||
else if (attrName in node && !(attrName === "list" || attrName === "style" || attrName === "form" || attrName === "type")) {
|
else if (attrName in node && !(attrName === "list" || attrName === "style" || attrName === "form" || attrName === "type")) {
|
||||||
//#348 don't set the value if not needed otherwise cursor placement breaks in Chrome
|
//#348 don't set the value if not needed otherwise cursor placement breaks in Chrome
|
||||||
if (attrName != "input" || node[attrName] !== dataAttr) node[attrName] = dataAttr
|
if (attrName != "input" || node[attrName] !== dataAttr) {
|
||||||
|
node[attrName] = dataAttr
|
||||||
}
|
}
|
||||||
else node.setAttribute(attrName, dataAttr)
|
} else {
|
||||||
|
node.setAttribute(attrName, dataAttr)
|
||||||
}
|
}
|
||||||
catch (e) {
|
} catch ( e ) {
|
||||||
//swallow IE's invalid argument errors to mimic HTML's fallback-to-doing-nothing-on-invalid-attributes behavior
|
//swallow IE's invalid argument errors to mimic HTML's fallback-to-doing-nothing-on-invalid-attributes behavior
|
||||||
if (e.message.indexOf("Invalid argument") < 0) throw e
|
if (e.message.indexOf("Invalid argument") < 0)
|
||||||
|
throw e
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//#348 dataAttr may not be a string, so use loose comparison (double equal) instead of strict (triple equal)
|
//#348 dataAttr may not be a string, so use loose comparison (double equal) instead of strict (triple equal)
|
||||||
|
|
@ -365,21 +466,31 @@ var m = (function app(window, undefined) {
|
||||||
function clear(nodes, cached) {
|
function clear(nodes, cached) {
|
||||||
for (var i = nodes.length - 1; i > -1; i--) {
|
for (var i = nodes.length - 1; i > -1; i--) {
|
||||||
if (nodes[i] && nodes[i].parentNode) {
|
if (nodes[i] && nodes[i].parentNode) {
|
||||||
try {nodes[i].parentNode.removeChild(nodes[i])}
|
try {
|
||||||
catch (e) {} //ignore if this fails due to order of events (see http://stackoverflow.com/questions/21926083/failed-to-execute-removechild-on-node)
|
nodes[i].parentNode.removeChild(nodes[i])
|
||||||
|
} catch ( e ) {} //ignore if this fails due to order of events (see http://stackoverflow.com/questions/21926083/failed-to-execute-removechild-on-node)
|
||||||
cached = [].concat(cached);
|
cached = [].concat(cached);
|
||||||
if (cached[i]) unload(cached[i])
|
if (cached[i]) {
|
||||||
|
unload(cached[i])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (nodes.length != 0) nodes.length = 0
|
}
|
||||||
|
if (nodes.length != 0) {
|
||||||
|
nodes.length = 0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
function unload(cached) {
|
function unload(cached) {
|
||||||
if (cached.configContext && typeof cached.configContext.onunload === FUNCTION) cached.configContext.onunload();
|
if (cached.configContext && typeof cached.configContext.onunload === FUNCTION) {
|
||||||
|
cached.configContext.onunload();
|
||||||
|
}
|
||||||
if (cached.children) {
|
if (cached.children) {
|
||||||
if (type.call(cached.children) === ARRAY) {
|
if (type.call(cached.children) === ARRAY) {
|
||||||
for (var i = 0, child; child = cached.children[i]; i++) unload(child)
|
for (var i = 0, child; child = cached.children[i]; i++) {
|
||||||
|
unload(child)
|
||||||
|
}
|
||||||
|
} else if (cached.children.tag) {
|
||||||
|
unload(cached.children)
|
||||||
}
|
}
|
||||||
else if (cached.children.tag) unload(cached.children)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
function injectHTML(parentElement, index, data) {
|
function injectHTML(parentElement, index, data) {
|
||||||
|
|
@ -391,10 +502,12 @@ var m = (function app(window, undefined) {
|
||||||
parentElement.insertBefore(placeholder, nextSibling || null);
|
parentElement.insertBefore(placeholder, nextSibling || null);
|
||||||
placeholder.insertAdjacentHTML("beforebegin", data);
|
placeholder.insertAdjacentHTML("beforebegin", data);
|
||||||
parentElement.removeChild(placeholder)
|
parentElement.removeChild(placeholder)
|
||||||
|
} else {
|
||||||
|
nextSibling.insertAdjacentHTML("beforebegin", data)
|
||||||
}
|
}
|
||||||
else nextSibling.insertAdjacentHTML("beforebegin", data)
|
} else {
|
||||||
|
parentElement.insertAdjacentHTML("beforeend", data);
|
||||||
}
|
}
|
||||||
else parentElement.insertAdjacentHTML("beforeend", data);
|
|
||||||
var nodes = [];
|
var nodes = [];
|
||||||
while (parentElement.childNodes[index] !== nextSibling) {
|
while (parentElement.childNodes[index] !== nextSibling) {
|
||||||
nodes.push(parentElement.childNodes[index]);
|
nodes.push(parentElement.childNodes[index]);
|
||||||
|
|
@ -407,8 +520,9 @@ var m = (function app(window, undefined) {
|
||||||
e = e || event;
|
e = e || event;
|
||||||
m.redraw.strategy("diff");
|
m.redraw.strategy("diff");
|
||||||
m.startComputation();
|
m.startComputation();
|
||||||
try {return callback.call(object, e)}
|
try {
|
||||||
finally {
|
return callback.call(object, e)
|
||||||
|
} finally {
|
||||||
endFirstComputation()
|
endFirstComputation()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -417,11 +531,14 @@ var m = (function app(window, undefined) {
|
||||||
var html;
|
var html;
|
||||||
var documentNode = {
|
var documentNode = {
|
||||||
appendChild: function(node) {
|
appendChild: function(node) {
|
||||||
if (html === undefined) html = $document.createElement("html");
|
if (html === undefined) {
|
||||||
|
html = $document.createElement("html");
|
||||||
|
}
|
||||||
if ($document.documentElement && $document.documentElement !== node) {
|
if ($document.documentElement && $document.documentElement !== node) {
|
||||||
$document.replaceChild(node, $document.documentElement)
|
$document.replaceChild(node, $document.documentElement)
|
||||||
|
} else {
|
||||||
|
$document.appendChild(node);
|
||||||
}
|
}
|
||||||
else $document.appendChild(node);
|
|
||||||
this.childNodes = $document.childNodes
|
this.childNodes = $document.childNodes
|
||||||
},
|
},
|
||||||
insertBefore: function(node) {
|
insertBefore: function(node) {
|
||||||
|
|
@ -429,18 +546,32 @@ var m = (function app(window, undefined) {
|
||||||
},
|
},
|
||||||
childNodes: []
|
childNodes: []
|
||||||
};
|
};
|
||||||
var nodeCache = [], cellCache = {};
|
var nodeCache = [],
|
||||||
|
cellCache = {};
|
||||||
m.render = function(root, cell, forceRecreation) {
|
m.render = function(root, cell, forceRecreation) {
|
||||||
var configs = [];
|
var configs = [];
|
||||||
if (!root) throw new Error("Please ensure the DOM element exists before rendering a template into it.");
|
if (!root)
|
||||||
|
throw new Error("Please ensure the DOM element exists before rendering a template into it.");
|
||||||
var id = getCellCacheKey(root);
|
var id = getCellCacheKey(root);
|
||||||
var isDocumentRoot = root === $document;
|
var isDocumentRoot = root === $document;
|
||||||
var node = isDocumentRoot || root === $document.documentElement ? documentNode : root;
|
var node = isDocumentRoot || root === $document.documentElement ? documentNode : root;
|
||||||
if (isDocumentRoot && cell.tag != "html") cell = {tag: "html", attrs: {}, children: cell};
|
if (isDocumentRoot && cell.tag != "html") {
|
||||||
if (cellCache[id] === undefined) clear(node.childNodes);
|
cell = {
|
||||||
if (forceRecreation === true) reset(root);
|
tag: "html",
|
||||||
|
attrs: {},
|
||||||
|
children: cell
|
||||||
|
};
|
||||||
|
}
|
||||||
|
if (cellCache[id] === undefined) {
|
||||||
|
clear(node.childNodes);
|
||||||
|
}
|
||||||
|
if (forceRecreation === true) {
|
||||||
|
reset(root);
|
||||||
|
}
|
||||||
cellCache[id] = build(node, null, undefined, undefined, cell, cellCache[id], false, 0, null, undefined, configs);
|
cellCache[id] = build(node, null, undefined, undefined, cell, cellCache[id], false, 0, null, undefined, configs);
|
||||||
for (var i = 0, len = configs.length; i < len; i++) configs[i]()
|
for (var i = 0, len = configs.length; i < len; i++) {
|
||||||
|
configs[i]()
|
||||||
|
}
|
||||||
};
|
};
|
||||||
function getCellCacheKey(element) {
|
function getCellCacheKey(element) {
|
||||||
var index = nodeCache.indexOf(element);
|
var index = nodeCache.indexOf(element);
|
||||||
|
|
@ -455,7 +586,9 @@ var m = (function app(window, undefined) {
|
||||||
|
|
||||||
function gettersetter(store) {
|
function gettersetter(store) {
|
||||||
var prop = function() {
|
var prop = function() {
|
||||||
if (arguments.length) store = arguments[0];
|
if (arguments.length) {
|
||||||
|
store = arguments[0];
|
||||||
|
}
|
||||||
return store
|
return store
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -475,15 +608,25 @@ var m = (function app(window, undefined) {
|
||||||
return gettersetter(store)
|
return gettersetter(store)
|
||||||
};
|
};
|
||||||
|
|
||||||
var roots = [], modules = [], controllers = [], lastRedrawId = null, lastRedrawCallTime = 0, computePostRedrawHook = null, prevented = false, topModule;
|
var roots = [],
|
||||||
|
modules = [],
|
||||||
|
controllers = [],
|
||||||
|
lastRedrawId = null,
|
||||||
|
lastRedrawCallTime = 0,
|
||||||
|
computePostRedrawHook = null,
|
||||||
|
prevented = false, topModule;
|
||||||
var FRAME_BUDGET = 16; //60 frames per second = 1 call per 16 ms
|
var FRAME_BUDGET = 16; //60 frames per second = 1 call per 16 ms
|
||||||
m.module = function(root, module) {
|
m.module = function(root, module) {
|
||||||
var index = roots.indexOf(root);
|
var index = roots.indexOf(root);
|
||||||
if (index < 0) index = roots.length;
|
if (index < 0) {
|
||||||
|
index = roots.length;
|
||||||
|
}
|
||||||
var isPrevented = false;
|
var isPrevented = false;
|
||||||
if (controllers[index] && typeof controllers[index].onunload === FUNCTION) {
|
if (controllers[index] && typeof controllers[index].onunload === FUNCTION) {
|
||||||
var event = {
|
var event = {
|
||||||
preventDefault: function() {isPrevented = true}
|
preventDefault: function() {
|
||||||
|
isPrevented = true
|
||||||
|
}
|
||||||
};
|
};
|
||||||
controllers[index].onunload(event)
|
controllers[index].onunload(event)
|
||||||
}
|
}
|
||||||
|
|
@ -510,13 +653,16 @@ var m = (function app(window, undefined) {
|
||||||
//when setTimeout: only reschedule redraw if time between now and previous redraw is bigger than a frame, otherwise keep currently scheduled timeout
|
//when setTimeout: only reschedule redraw if time between now and previous redraw is bigger than a frame, otherwise keep currently scheduled timeout
|
||||||
//when rAF: always reschedule redraw
|
//when rAF: always reschedule redraw
|
||||||
if (new Date - lastRedrawCallTime > FRAME_BUDGET || $requestAnimationFrame === window.requestAnimationFrame) {
|
if (new Date - lastRedrawCallTime > FRAME_BUDGET || $requestAnimationFrame === window.requestAnimationFrame) {
|
||||||
if (lastRedrawId > 0) $cancelAnimationFrame(lastRedrawId);
|
if (lastRedrawId > 0) {
|
||||||
|
$cancelAnimationFrame(lastRedrawId);
|
||||||
|
}
|
||||||
lastRedrawId = $requestAnimationFrame(redraw, FRAME_BUDGET)
|
lastRedrawId = $requestAnimationFrame(redraw, FRAME_BUDGET)
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
redraw();
|
redraw();
|
||||||
lastRedrawId = $requestAnimationFrame(function() {lastRedrawId = null}, FRAME_BUDGET)
|
lastRedrawId = $requestAnimationFrame(function() {
|
||||||
|
lastRedrawId = null
|
||||||
|
}, FRAME_BUDGET)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
m.redraw.strategy = m.prop();
|
m.redraw.strategy = m.prop();
|
||||||
|
|
@ -538,17 +684,22 @@ var m = (function app(window, undefined) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var pendingRequests = 0;
|
var pendingRequests = 0;
|
||||||
m.startComputation = function() {pendingRequests++};
|
m.startComputation = function() {
|
||||||
|
pendingRequests++
|
||||||
|
};
|
||||||
m.endComputation = function() {
|
m.endComputation = function() {
|
||||||
pendingRequests = Math.max(pendingRequests - 1, 0);
|
pendingRequests = Math.max(pendingRequests - 1, 0);
|
||||||
if (pendingRequests === 0) m.redraw()
|
if (pendingRequests === 0) {
|
||||||
|
m.redraw()
|
||||||
|
}
|
||||||
};
|
};
|
||||||
var endFirstComputation = function() {
|
var endFirstComputation = function() {
|
||||||
if (m.redraw.strategy() == "none") {
|
if (m.redraw.strategy() == "none") {
|
||||||
pendingRequests--
|
pendingRequests--
|
||||||
m.redraw.strategy("diff")
|
m.redraw.strategy("diff")
|
||||||
|
} else {
|
||||||
|
m.endComputation();
|
||||||
}
|
}
|
||||||
else m.endComputation();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
m.withAttr = function(prop, withAttrCallback) {
|
m.withAttr = function(prop, withAttrCallback) {
|
||||||
|
|
@ -560,14 +711,20 @@ var m = (function app(window, undefined) {
|
||||||
};
|
};
|
||||||
|
|
||||||
//routing
|
//routing
|
||||||
var modes = {pathname: "", hash: "#", search: "?"};
|
var modes = {
|
||||||
|
pathname: "",
|
||||||
|
hash: "#",
|
||||||
|
search: "?"
|
||||||
|
};
|
||||||
var redirect = function() {}, routeParams, currentRoute;
|
var redirect = function() {}, routeParams, currentRoute;
|
||||||
m.route = function() {
|
m.route = function() {
|
||||||
//m.route()
|
//m.route()
|
||||||
if (arguments.length === 0) return currentRoute;
|
if (arguments.length === 0) return currentRoute;
|
||||||
//m.route(el, defaultRoute, routes)
|
//m.route(el, defaultRoute, routes)
|
||||||
else if (arguments.length === 3 && type.call(arguments[1]) === STRING) {
|
else if (arguments.length === 3 && type.call(arguments[1]) === STRING) {
|
||||||
var root = arguments[0], defaultRoute = arguments[1], router = arguments[2];
|
var root = arguments[0],
|
||||||
|
defaultRoute = arguments[1],
|
||||||
|
router = arguments[2];
|
||||||
redirect = function(source) {
|
redirect = function(source) {
|
||||||
var path = currentRoute = normalizeRoute(source);
|
var path = currentRoute = normalizeRoute(source);
|
||||||
if (!routeByValue(root, router, path)) {
|
if (!routeByValue(root, router, path)) {
|
||||||
|
|
@ -601,7 +758,9 @@ var m = (function app(window, undefined) {
|
||||||
for (var i in args) params[i] = args[i]
|
for (var i in args) params[i] = args[i]
|
||||||
var querystring = buildQueryString(params)
|
var querystring = buildQueryString(params)
|
||||||
var currentPath = queryIndex > -1 ? currentRoute.slice(0, queryIndex) : currentRoute
|
var currentPath = queryIndex > -1 ? currentRoute.slice(0, queryIndex) : currentRoute
|
||||||
if (querystring) currentRoute = currentPath + (currentPath.indexOf("?") === -1 ? "?" : "&") + querystring;
|
if (querystring) {
|
||||||
|
currentRoute = currentPath + (currentPath.indexOf("?") === -1 ? "?" : "&") + querystring;
|
||||||
|
}
|
||||||
|
|
||||||
var shouldReplaceHistoryEntry = (arguments.length === 3 ? arguments[2] : arguments[1]) === true;
|
var shouldReplaceHistoryEntry = (arguments.length === 3 ? arguments[2] : arguments[1]) === true;
|
||||||
|
|
||||||
|
|
@ -611,16 +770,20 @@ var m = (function app(window, undefined) {
|
||||||
setScroll()
|
setScroll()
|
||||||
};
|
};
|
||||||
redirect(modes[m.route.mode] + currentRoute)
|
redirect(modes[m.route.mode] + currentRoute)
|
||||||
|
} else {
|
||||||
|
$location[m.route.mode] = currentRoute
|
||||||
}
|
}
|
||||||
else $location[m.route.mode] = currentRoute
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
m.route.param = function(key) {
|
m.route.param = function(key) {
|
||||||
if (!routeParams) throw new Error("You must call m.route(element, defaultRoute, routes) before calling m.route.param()")
|
if (!routeParams)
|
||||||
|
throw new Error("You must call m.route(element, defaultRoute, routes) before calling m.route.param()")
|
||||||
return routeParams[key]
|
return routeParams[key]
|
||||||
};
|
};
|
||||||
m.route.mode = "search";
|
m.route.mode = "search";
|
||||||
function normalizeRoute(route) {return route.slice(modes[m.route.mode].length)}
|
function normalizeRoute(route) {
|
||||||
|
return route.slice(modes[m.route.mode].length)
|
||||||
|
}
|
||||||
function routeByValue(root, router, path) {
|
function routeByValue(root, router, path) {
|
||||||
routeParams = {};
|
routeParams = {};
|
||||||
|
|
||||||
|
|
@ -642,7 +805,9 @@ var m = (function app(window, undefined) {
|
||||||
path.replace(matcher, function() {
|
path.replace(matcher, function() {
|
||||||
var keys = route.match(/:[^\/]+/g) || [];
|
var keys = route.match(/:[^\/]+/g) || [];
|
||||||
var values = [].slice.call(arguments, 1, -2);
|
var values = [].slice.call(arguments, 1, -2);
|
||||||
for (var i = 0, len = keys.length; i < len; i++) routeParams[keys[i].replace(/:|\./g, "")] = decodeURIComponent(values[i])
|
for (var i = 0, len = keys.length; i < len; i++) {
|
||||||
|
routeParams[keys[i].replace(/:|\./g, "")] = decodeURIComponent(values[i])
|
||||||
|
}
|
||||||
m.module(root, router[route])
|
m.module(root, router[route])
|
||||||
});
|
});
|
||||||
return true
|
return true
|
||||||
|
|
@ -652,26 +817,34 @@ var m = (function app(window, undefined) {
|
||||||
function routeUnobtrusive(e) {
|
function routeUnobtrusive(e) {
|
||||||
e = e || event;
|
e = e || event;
|
||||||
if (e.ctrlKey || e.metaKey || e.which === 2) return;
|
if (e.ctrlKey || e.metaKey || e.which === 2) return;
|
||||||
if (e.preventDefault) e.preventDefault();
|
if (e.preventDefault) {
|
||||||
else e.returnValue = false;
|
e.preventDefault();
|
||||||
|
} else {
|
||||||
|
e.returnValue = false;
|
||||||
|
}
|
||||||
var currentTarget = e.currentTarget || this;
|
var currentTarget = e.currentTarget || this;
|
||||||
var args = m.route.mode === "pathname" && currentTarget.search ? parseQueryString(currentTarget.search.slice(1)) : {};
|
var args = m.route.mode === "pathname" && currentTarget.search ? parseQueryString(currentTarget.search.slice(1)) : {};
|
||||||
m.route(currentTarget[m.route.mode].slice(modes[m.route.mode].length), args)
|
m.route(currentTarget[m.route.mode].slice(modes[m.route.mode].length), args)
|
||||||
}
|
}
|
||||||
function setScroll() {
|
function setScroll() {
|
||||||
if (m.route.mode != "hash" && $location.hash) $location.hash = $location.hash;
|
if (m.route.mode != "hash" && $location.hash) {
|
||||||
else window.scrollTo(0, 0)
|
$location.hash = $location.hash;
|
||||||
|
} else {
|
||||||
|
window.scrollTo(0, 0)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
function buildQueryString(object, prefix) {
|
function buildQueryString(object, prefix) {
|
||||||
var str = [];
|
var str = [];
|
||||||
for (var prop in object) {
|
for (var prop in object) {
|
||||||
var key = prefix ? prefix + "[" + prop + "]" : prop, value = object[prop];
|
var key = prefix ? prefix + "[" + prop + "]" : prop,
|
||||||
|
value = object[prop];
|
||||||
str.push(value != null && type.call(value) === OBJECT ? buildQueryString(value, key) : encodeURIComponent(key) + "=" + encodeURIComponent(value))
|
str.push(value != null && type.call(value) === OBJECT ? buildQueryString(value, key) : encodeURIComponent(key) + "=" + encodeURIComponent(value))
|
||||||
}
|
}
|
||||||
return str.join("&")
|
return str.join("&")
|
||||||
}
|
}
|
||||||
function parseQueryString(str) {
|
function parseQueryString(str) {
|
||||||
var pairs = str.split("&"), params = {};
|
var pairs = str.split("&"),
|
||||||
|
params = {};
|
||||||
for (var i = 0, len = pairs.length; i < len; i++) {
|
for (var i = 0, len = pairs.length; i < len; i++) {
|
||||||
var pair = pairs[i].split("=");
|
var pair = pairs[i].split("=");
|
||||||
params[decodeSpace(pair[0])] = pair[1] ? decodeSpace(pair[1]) : ""
|
params[decodeSpace(pair[0])] = pair[1] ? decodeSpace(pair[1]) : ""
|
||||||
|
|
@ -705,8 +878,14 @@ var m = (function app(window, undefined) {
|
||||||
//1) `then` callbacks are called synchronously (because setTimeout is too slow, and the setImmediate polyfill is too big
|
//1) `then` callbacks are called synchronously (because setTimeout is too slow, and the setImmediate polyfill is too big
|
||||||
//2) throwing subclasses of Error cause the error to be bubbled up instead of triggering rejection (because the spec does not account for the important use case of default browser error handling, i.e. message w/ line number)
|
//2) throwing subclasses of Error cause the error to be bubbled up instead of triggering rejection (because the spec does not account for the important use case of default browser error handling, i.e. message w/ line number)
|
||||||
function Deferred(successCallback, failureCallback) {
|
function Deferred(successCallback, failureCallback) {
|
||||||
var RESOLVING = 1, REJECTING = 2, RESOLVED = 3, REJECTED = 4;
|
var RESOLVING = 1,
|
||||||
var self = this, state = 0, promiseValue = 0, next = [];
|
REJECTING = 2,
|
||||||
|
RESOLVED = 3,
|
||||||
|
REJECTED = 4;
|
||||||
|
var self = this,
|
||||||
|
state = 0,
|
||||||
|
promiseValue = 0,
|
||||||
|
next = [];
|
||||||
|
|
||||||
self["promise"] = {};
|
self["promise"] = {};
|
||||||
|
|
||||||
|
|
@ -734,11 +913,9 @@ var m = (function app(window, undefined) {
|
||||||
var deferred = new Deferred(successCallback, failureCallback);
|
var deferred = new Deferred(successCallback, failureCallback);
|
||||||
if (state === RESOLVED) {
|
if (state === RESOLVED) {
|
||||||
deferred.resolve(promiseValue)
|
deferred.resolve(promiseValue)
|
||||||
}
|
} else if (state === REJECTED) {
|
||||||
else if (state === REJECTED) {
|
|
||||||
deferred.reject(promiseValue)
|
deferred.reject(promiseValue)
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
next.push(deferred)
|
next.push(deferred)
|
||||||
}
|
}
|
||||||
return deferred.promise
|
return deferred.promise
|
||||||
|
|
@ -765,8 +942,7 @@ var m = (function app(window, undefined) {
|
||||||
promiseValue = value;
|
promiseValue = value;
|
||||||
failureCallback()
|
failureCallback()
|
||||||
})
|
})
|
||||||
}
|
} catch ( e ) {
|
||||||
catch (e) {
|
|
||||||
m.deferred.onerror(e);
|
m.deferred.onerror(e);
|
||||||
promiseValue = e;
|
promiseValue = e;
|
||||||
failureCallback()
|
failureCallback()
|
||||||
|
|
@ -781,8 +957,7 @@ var m = (function app(window, undefined) {
|
||||||
var then;
|
var then;
|
||||||
try {
|
try {
|
||||||
then = promiseValue && promiseValue.then
|
then = promiseValue && promiseValue.then
|
||||||
}
|
} catch ( e ) {
|
||||||
catch (e) {
|
|
||||||
m.deferred.onerror(e);
|
m.deferred.onerror(e);
|
||||||
promiseValue = e;
|
promiseValue = e;
|
||||||
state = REJECTING;
|
state = REJECTING;
|
||||||
|
|
@ -798,13 +973,11 @@ var m = (function app(window, undefined) {
|
||||||
try {
|
try {
|
||||||
if (state === RESOLVING && typeof successCallback === FUNCTION) {
|
if (state === RESOLVING && typeof successCallback === FUNCTION) {
|
||||||
promiseValue = successCallback(promiseValue)
|
promiseValue = successCallback(promiseValue)
|
||||||
}
|
} else if (state === REJECTING && typeof failureCallback === "function") {
|
||||||
else if (state === REJECTING && typeof failureCallback === "function") {
|
|
||||||
promiseValue = failureCallback(promiseValue);
|
promiseValue = failureCallback(promiseValue);
|
||||||
state = RESOLVING
|
state = RESOLVING
|
||||||
}
|
}
|
||||||
}
|
} catch ( e ) {
|
||||||
catch (e) {
|
|
||||||
m.deferred.onerror(e);
|
m.deferred.onerror(e);
|
||||||
promiseValue = e;
|
promiseValue = e;
|
||||||
return finish()
|
return finish()
|
||||||
|
|
@ -813,8 +986,7 @@ var m = (function app(window, undefined) {
|
||||||
if (promiseValue === self) {
|
if (promiseValue === self) {
|
||||||
promiseValue = TypeError();
|
promiseValue = TypeError();
|
||||||
finish()
|
finish()
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
thennable(then, function() {
|
thennable(then, function() {
|
||||||
finish(RESOLVED)
|
finish(RESOLVED)
|
||||||
}, finish, function() {
|
}, finish, function() {
|
||||||
|
|
@ -825,7 +997,8 @@ var m = (function app(window, undefined) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
m.deferred.onerror = function(e) {
|
m.deferred.onerror = function(e) {
|
||||||
if (type.call(e) === "[object Error]" && !e.constructor.toString().match(/ Error/)) throw e
|
if (type.call(e) === "[object Error]" && !e.constructor.toString().match(/ Error/))
|
||||||
|
throw e
|
||||||
};
|
};
|
||||||
|
|
||||||
m.sync = function(args) {
|
m.sync = function(args) {
|
||||||
|
|
@ -833,7 +1006,9 @@ var m = (function app(window, undefined) {
|
||||||
function synchronizer(pos, resolved) {
|
function synchronizer(pos, resolved) {
|
||||||
return function(value) {
|
return function(value) {
|
||||||
results[pos] = value;
|
results[pos] = value;
|
||||||
if (!resolved) method = "reject";
|
if (!resolved) {
|
||||||
|
method = "reject";
|
||||||
|
}
|
||||||
if (--outstanding === 0) {
|
if (--outstanding === 0) {
|
||||||
deferred.promise(results);
|
deferred.promise(results);
|
||||||
deferred[method](results)
|
deferred[method](results)
|
||||||
|
|
@ -849,12 +1024,15 @@ var m = (function app(window, undefined) {
|
||||||
for (var i = 0; i < args.length; i++) {
|
for (var i = 0; i < args.length; i++) {
|
||||||
args[i].then(synchronizer(i, true), synchronizer(i, false))
|
args[i].then(synchronizer(i, true), synchronizer(i, false))
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
deferred.resolve([]);
|
||||||
}
|
}
|
||||||
else deferred.resolve([]);
|
|
||||||
|
|
||||||
return deferred.promise
|
return deferred.promise
|
||||||
};
|
};
|
||||||
function identity(value) {return value}
|
function identity(value) {
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
function ajax(options) {
|
function ajax(options) {
|
||||||
if (options.dataType && options.dataType.toLowerCase() === "jsonp") {
|
if (options.dataType && options.dataType.toLowerCase() === "jsonp") {
|
||||||
|
|
@ -879,7 +1057,9 @@ var m = (function app(window, undefined) {
|
||||||
type: "error",
|
type: "error",
|
||||||
target: {
|
target: {
|
||||||
status: 500,
|
status: 500,
|
||||||
responseText: JSON.stringify({error: "Error making jsonp request"})
|
responseText: JSON.stringify({
|
||||||
|
error: "Error making jsonp request"
|
||||||
|
})
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
window[callbackKey] = undefined;
|
window[callbackKey] = undefined;
|
||||||
|
|
@ -897,14 +1077,22 @@ var m = (function app(window, undefined) {
|
||||||
+ "=" + callbackKey
|
+ "=" + callbackKey
|
||||||
+ "&" + buildQueryString(options.data || {});
|
+ "&" + buildQueryString(options.data || {});
|
||||||
$document.body.appendChild(script)
|
$document.body.appendChild(script)
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
var xhr = new window.XMLHttpRequest;
|
var xhr = new window.XMLHttpRequest;
|
||||||
xhr.open(options.method, options.url, true, options.user, options.password);
|
xhr.open(options.method, options.url, true, options.user, options.password);
|
||||||
xhr.onreadystatechange = function() {
|
xhr.onreadystatechange = function() {
|
||||||
if (xhr.readyState === 4) {
|
if (xhr.readyState === 4) {
|
||||||
if (xhr.status >= 200 && xhr.status < 300) options.onload({type: "load", target: xhr});
|
if (xhr.status >= 200 && xhr.status < 300) {
|
||||||
else options.onerror({type: "error", target: xhr})
|
options.onload({
|
||||||
|
type: "load",
|
||||||
|
target: xhr
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
options.onerror({
|
||||||
|
type: "error",
|
||||||
|
target: xhr
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
if (options.serialize === JSON.stringify && options.data && options.method !== "GET") {
|
if (options.serialize === JSON.stringify && options.data && options.method !== "GET") {
|
||||||
|
|
@ -915,7 +1103,9 @@ var m = (function app(window, undefined) {
|
||||||
}
|
}
|
||||||
if (typeof options.config === FUNCTION) {
|
if (typeof options.config === FUNCTION) {
|
||||||
var maybeXhr = options.config(xhr, options);
|
var maybeXhr = options.config(xhr, options);
|
||||||
if (maybeXhr != null) xhr = maybeXhr
|
if (maybeXhr != null) {
|
||||||
|
xhr = maybeXhr
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var data = options.method === "GET" || !options.data ? "" : options.data
|
var data = options.method === "GET" || !options.data ? "" : options.data
|
||||||
|
|
@ -931,8 +1121,9 @@ var m = (function app(window, undefined) {
|
||||||
var prefix = xhrOptions.url.indexOf("?") < 0 ? "?" : "&";
|
var prefix = xhrOptions.url.indexOf("?") < 0 ? "?" : "&";
|
||||||
var querystring = buildQueryString(data);
|
var querystring = buildQueryString(data);
|
||||||
xhrOptions.url = xhrOptions.url + (querystring ? prefix + querystring : "")
|
xhrOptions.url = xhrOptions.url + (querystring ? prefix + querystring : "")
|
||||||
|
} else {
|
||||||
|
xhrOptions.data = serialize(data);
|
||||||
}
|
}
|
||||||
else xhrOptions.data = serialize(data);
|
|
||||||
return xhrOptions
|
return xhrOptions
|
||||||
}
|
}
|
||||||
function parameterizeUrl(url, data) {
|
function parameterizeUrl(url, data) {
|
||||||
|
|
@ -948,7 +1139,9 @@ var m = (function app(window, undefined) {
|
||||||
}
|
}
|
||||||
|
|
||||||
m.request = function(xhrOptions) {
|
m.request = function(xhrOptions) {
|
||||||
if (xhrOptions.background !== true) m.startComputation();
|
if (xhrOptions.background !== true) {
|
||||||
|
m.startComputation();
|
||||||
|
}
|
||||||
var deferred = m.deferred();
|
var deferred = m.deferred();
|
||||||
var isJSONP = xhrOptions.dataType && xhrOptions.dataType.toLowerCase() === "jsonp";
|
var isJSONP = xhrOptions.dataType && xhrOptions.dataType.toLowerCase() === "jsonp";
|
||||||
var serialize = xhrOptions.serialize = isJSONP ? identity : xhrOptions.serialize || JSON.stringify;
|
var serialize = xhrOptions.serialize = isJSONP ? identity : xhrOptions.serialize || JSON.stringify;
|
||||||
|
|
@ -965,17 +1158,21 @@ var m = (function app(window, undefined) {
|
||||||
var response = unwrap(deserialize(extract(e.target, xhrOptions)));
|
var response = unwrap(deserialize(extract(e.target, xhrOptions)));
|
||||||
if (e.type === "load") {
|
if (e.type === "load") {
|
||||||
if (type.call(response) === ARRAY && xhrOptions.type) {
|
if (type.call(response) === ARRAY && xhrOptions.type) {
|
||||||
for (var i = 0; i < response.length; i++) response[i] = new xhrOptions.type(response[i])
|
for (var i = 0; i < response.length; i++) {
|
||||||
|
response[i] = new xhrOptions.type(response[i])
|
||||||
|
}
|
||||||
|
} else if (xhrOptions.type) {
|
||||||
|
response = new xhrOptions.type(response)
|
||||||
}
|
}
|
||||||
else if (xhrOptions.type) response = new xhrOptions.type(response)
|
|
||||||
}
|
}
|
||||||
deferred[e.type === "load" ? "resolve" : "reject"](response)
|
deferred[e.type === "load" ? "resolve" : "reject"](response)
|
||||||
}
|
} catch ( e ) {
|
||||||
catch (e) {
|
|
||||||
m.deferred.onerror(e);
|
m.deferred.onerror(e);
|
||||||
deferred.reject(e)
|
deferred.reject(e)
|
||||||
}
|
}
|
||||||
if (xhrOptions.background !== true) m.endComputation()
|
if (xhrOptions.background !== true) {
|
||||||
|
m.endComputation()
|
||||||
|
}
|
||||||
};
|
};
|
||||||
ajax(xhrOptions);
|
ajax(xhrOptions);
|
||||||
deferred.promise(xhrOptions.initialValue);
|
deferred.promise(xhrOptions.initialValue);
|
||||||
|
|
@ -993,5 +1190,12 @@ var m = (function app(window, undefined) {
|
||||||
return m
|
return m
|
||||||
})(typeof window != "undefined" ? window : {});
|
})(typeof window != "undefined" ? window : {});
|
||||||
|
|
||||||
if (typeof module != "undefined" && module !== null && module.exports) module.exports = m;
|
if (typeof module != "undefined" && module !== null && module.exports) {
|
||||||
else if (typeof define === "function" && define.amd) define(function() {return m});
|
module.exports = m;
|
||||||
|
} else if (typeof define === "function" && define.amd) {
|
||||||
|
define(function() {
|
||||||
|
return m
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue