jsfmt'd code

This commit is contained in:
Yusuf Simonson 2014-12-18 17:11:33 -05:00
parent e9c892cb05
commit 4def679b2b
2 changed files with 2270 additions and 1862 deletions

View file

@ -1,14 +1,18 @@
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 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)$/;
// caching commonly used variables
var $document, $location, $requestAnimationFrame, $cancelAnimationFrame;
// self invoking function needed because of the way mocks work
function initialize(window){
function initialize(window) {
$document = window.document;
$location = window.location;
$cancelAnimationFrame = window.cancelAnimationFrame || window.clearTimeout;
@ -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 attrs = hasAttrs ? args[1] : {};
var classAttrName = "class" in attrs ? "class" : "className";
var cell = {tag: "div", attrs: {}};
var match, classes = [];
if (type.call(args[0]) != STRING) throw new Error("selector in m(selector, attrs, children) should be a string")
var cell = {
tag: "div",
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])) {
if (match[1] === "" && match[2]) cell.tag = match[2];
else if (match[1] === "#") cell.attrs.id = match[2];
else if (match[1] === ".") classes.push(match[2]);
else if (match[3][0] === "[") {
if (match[1] === "" && match[2]) {
cell.tag = match[2];
} else if (match[1] === "#") {
cell.attrs.id = match[2];
} else if (match[1] === ".") {
classes.push(match[2]);
} else if (match[3][0] === "[") {
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];
if (type.call(children) === ARRAY) {
cell.children = children
}
else {
} else {
cell.children = hasAttrs ? args.slice(2) : args.slice(1)
}
for (var attrName in attrs) {
if (attrName === classAttrName) cell.attrs[attrName] = (cell.attrs[attrName] || "") + " " + attrs[attrName];
else cell.attrs[attrName] = attrs[attrName]
if (attrName === classAttrName) {
cell.attrs[attrName] = (cell.attrs[attrName] || "") + " " + attrs[attrName];
} else {
cell.attrs[attrName] = attrs[attrName]
}
}
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
//- 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
if (data == null || data.toString() == null) data = "";
if (data == null || data.toString() == null) {
data = "";
}
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) {
if (parentCache && parentCache.nodes) {
var offset = index - parentIndex;
var end = offset + (dataType === ARRAY ? data : cached.nodes).length;
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;
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 = []
}
@ -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
//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
//4) for each key, handle its corresponding action as marked in previous steps
//5) copy unkeyed items into their respective gaps
var DELETION = 1, INSERTION = 2 , MOVE = 3;
var existing = {}, unkeyed = [], shouldMaintainIdentities = false;
var DELETION = 1,
INSERTION = 2 ,
MOVE = 3;
var existing = {},
unkeyed = [],
shouldMaintainIdentities = false;
for (var i = 0, len = cached.length; i < len; i++) {
if (cached[i] && cached[i].attrs && cached[i].attrs.key != null) {
shouldMaintainIdentities = true;
existing[cached[i].attrs.key] = {action: DELETION, index: i}
existing[cached[i].attrs.key] = {
action: DELETION,
index: i
}
}
}
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++) {
if (data[i] && data[i].attrs) {
if (data[i].attrs.key != null) {
var key = data[i].attrs.key;
if (!existing[key]) existing[key] = {action: INSERTION, index: i};
else existing[key] = {
if (!existing[key]) {
existing[key] = {
action: INSERTION,
index: i
};
} else {
existing[key] = {
action: MOVE,
index: i,
from: existing[key].index,
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();
for (var i = 0, change; change = changes[i]; i++) {
@ -163,7 +212,12 @@ var m = (function app(window, undefined) {
var dummy = $document.createElement("div");
dummy.key = data[change.index].attrs.key;
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) {
@ -180,7 +234,9 @@ var m = (function app(window, undefined) {
}
cached = newCached;
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
@ -188,14 +244,17 @@ var m = (function app(window, undefined) {
//diff each item in the array
var item = build(parentElement, parentTag, cached, index, data[i], cached[cacheCount], shouldReattach, index + subArrayCount || subArrayCount, editable, namespace, configs);
if (item === undefined) continue;
if (!item.nodes.intact) intact = false;
if (!item.nodes.intact) {
intact = false;
}
if (item.$trusted) {
//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 second clause (after the pipe) matches text nodes
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
}
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
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
//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++) {
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
}
} 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 hasKeys = dataAttrKeys.length > ("key" in data.attrs ? 1 : 0)
//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 (cached.nodes.length) clear(cached.nodes);
if (cached.configContext && typeof cached.configContext.onunload === FUNCTION) cached.configContext.onunload()
if (cached.nodes.length) {
clear(cached.nodes);
}
if (cached.configContext && typeof cached.configContext.onunload === FUNCTION) {
cached.configContext.onunload()
}
}
if (type.call(data.tag) != STRING) return;
var node, isNew = cached.nodes.length === 0;
if (data.attrs.xmlns) 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";
var node,
isNew = cached.nodes.length === 0;
if (data.attrs.xmlns) {
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 (data.attrs.is) 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);
if (data.attrs.is) {
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 = {
tag: data.tag,
//set attributes first, then create children
@ -243,17 +323,26 @@ var m = (function app(window, undefined) {
data.children,
nodes: [node]
};
if (cached.children && !cached.children.nodes) cached.children.nodes = [];
//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)
if (cached.children && !cached.children.nodes) {
cached.children.nodes = [];
}
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];
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.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
if (typeof data.attrs["config"] === FUNCTION) {
@ -267,34 +356,34 @@ var m = (function app(window, undefined) {
};
configs.push(callback(data, [node, !isNew, context, cached]))
}
}
else if (typeof dataType != FUNCTION) {
} else if (typeof dataType != FUNCTION) {
//handle text nodes
var nodes;
if (cached.nodes.length === 0) {
if (data.$trusted) {
nodes = injectHTML(parentElement, index, data)
}
else {
} else {
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.nodes = nodes
}
else if (cached.valueOf() !== data.valueOf() || shouldReattach === true) {
} else if (cached.valueOf() !== data.valueOf() || shouldReattach === true) {
nodes = cached.nodes;
if (!editable || editable !== $document.activeElement) {
if (data.$trusted) {
clear(nodes, cached);
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
//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;
else if (editable) editable.innerHTML = data;
else {
if (parentTag === "textarea") {
parentElement.value = data;
} else if (editable) {
editable.innerHTML = data;
} else {
if (nodes[0].nodeType === 1 || nodes.length > 1) { //was a trusted string
clear(cached.nodes, cached);
nodes = [$document.createTextNode(data)]
@ -306,8 +395,9 @@ var m = (function app(window, undefined) {
}
cached = new data.constructor(data);
cached.nodes = nodes
} else {
cached.nodes.intact = true
}
else cached.nodes.intact = true
}
return cached
@ -329,30 +419,41 @@ var m = (function app(window, undefined) {
//handle `style: {...}`
else if (attrName === "style" && dataAttr != null && type.call(dataAttr) === OBJECT) {
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) {
if (!(rule in dataAttr)) node.style[rule] = ""
if (!(rule in dataAttr)) {
node.style[rule] = ""
}
}
}
//handle SVG
else if (namespace != null) {
if (attrName === "href") node.setAttributeNS("http://www.w3.org/1999/xlink", "href", dataAttr);
else if (attrName === "className") node.setAttribute("class", dataAttr);
else node.setAttribute(attrName, dataAttr)
if (attrName === "href") {
node.setAttributeNS("http://www.w3.org/1999/xlink", "href", dataAttr);
} else if (attrName === "className") {
node.setAttribute("class", dataAttr);
} else {
node.setAttribute(attrName, dataAttr)
}
}
//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
//- 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")) {
//#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
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)
@ -365,21 +466,31 @@ var m = (function app(window, undefined) {
function clear(nodes, cached) {
for (var i = nodes.length - 1; i > -1; i--) {
if (nodes[i] && nodes[i].parentNode) {
try {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)
try {
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);
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) {
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 (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) {
@ -391,10 +502,12 @@ var m = (function app(window, undefined) {
parentElement.insertBefore(placeholder, nextSibling || null);
placeholder.insertAdjacentHTML("beforebegin", data);
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 = [];
while (parentElement.childNodes[index] !== nextSibling) {
nodes.push(parentElement.childNodes[index]);
@ -407,8 +520,9 @@ var m = (function app(window, undefined) {
e = e || event;
m.redraw.strategy("diff");
m.startComputation();
try {return callback.call(object, e)}
finally {
try {
return callback.call(object, e)
} finally {
endFirstComputation()
}
}
@ -417,11 +531,14 @@ var m = (function app(window, undefined) {
var html;
var documentNode = {
appendChild: function(node) {
if (html === undefined) html = $document.createElement("html");
if (html === undefined) {
html = $document.createElement("html");
}
if ($document.documentElement && $document.documentElement !== node) {
$document.replaceChild(node, $document.documentElement)
} else {
$document.appendChild(node);
}
else $document.appendChild(node);
this.childNodes = $document.childNodes
},
insertBefore: function(node) {
@ -429,18 +546,32 @@ var m = (function app(window, undefined) {
},
childNodes: []
};
var nodeCache = [], cellCache = {};
var nodeCache = [],
cellCache = {};
m.render = function(root, cell, forceRecreation) {
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 isDocumentRoot = root === $document;
var node = isDocumentRoot || root === $document.documentElement ? documentNode : root;
if (isDocumentRoot && cell.tag != "html") cell = {tag: "html", attrs: {}, children: cell};
if (cellCache[id] === undefined) clear(node.childNodes);
if (forceRecreation === true) reset(root);
if (isDocumentRoot && cell.tag != "html") {
cell = {
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);
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) {
var index = nodeCache.indexOf(element);
@ -455,7 +586,9 @@ var m = (function app(window, undefined) {
function gettersetter(store) {
var prop = function() {
if (arguments.length) store = arguments[0];
if (arguments.length) {
store = arguments[0];
}
return store
};
@ -466,7 +599,7 @@ var m = (function app(window, undefined) {
return prop
}
m.prop = function (store) {
m.prop = function(store) {
//note: using non-strict equality check here because we're checking if store is null OR undefined
if (((store != null && type.call(store) === OBJECT) || typeof store === FUNCTION) && typeof store.then === FUNCTION) {
return propify(store)
@ -475,15 +608,25 @@ var m = (function app(window, undefined) {
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
m.module = function(root, module) {
var index = roots.indexOf(root);
if (index < 0) index = roots.length;
if (index < 0) {
index = roots.length;
}
var isPrevented = false;
if (controllers[index] && typeof controllers[index].onunload === FUNCTION) {
var event = {
preventDefault: function() {isPrevented = true}
preventDefault: function() {
isPrevented = true
}
};
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 rAF: always reschedule redraw
if (new Date - lastRedrawCallTime > FRAME_BUDGET || $requestAnimationFrame === window.requestAnimationFrame) {
if (lastRedrawId > 0) $cancelAnimationFrame(lastRedrawId);
if (lastRedrawId > 0) {
$cancelAnimationFrame(lastRedrawId);
}
lastRedrawId = $requestAnimationFrame(redraw, FRAME_BUDGET)
}
}
else {
} else {
redraw();
lastRedrawId = $requestAnimationFrame(function() {lastRedrawId = null}, FRAME_BUDGET)
lastRedrawId = $requestAnimationFrame(function() {
lastRedrawId = null
}, FRAME_BUDGET)
}
};
m.redraw.strategy = m.prop();
@ -538,17 +684,22 @@ var m = (function app(window, undefined) {
}
var pendingRequests = 0;
m.startComputation = function() {pendingRequests++};
m.startComputation = function() {
pendingRequests++
};
m.endComputation = function() {
pendingRequests = Math.max(pendingRequests - 1, 0);
if (pendingRequests === 0) m.redraw()
if (pendingRequests === 0) {
m.redraw()
}
};
var endFirstComputation = function() {
if (m.redraw.strategy() == "none") {
pendingRequests--
m.redraw.strategy("diff")
} else {
m.endComputation();
}
else m.endComputation();
}
m.withAttr = function(prop, withAttrCallback) {
@ -560,14 +711,20 @@ var m = (function app(window, undefined) {
};
//routing
var modes = {pathname: "", hash: "#", search: "?"};
var modes = {
pathname: "",
hash: "#",
search: "?"
};
var redirect = function() {}, routeParams, currentRoute;
m.route = function() {
//m.route()
if (arguments.length === 0) return currentRoute;
//m.route(el, defaultRoute, routes)
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) {
var path = currentRoute = normalizeRoute(source);
if (!routeByValue(root, router, path)) {
@ -601,7 +758,9 @@ var m = (function app(window, undefined) {
for (var i in args) params[i] = args[i]
var querystring = buildQueryString(params)
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;
@ -611,16 +770,20 @@ var m = (function app(window, undefined) {
setScroll()
};
redirect(modes[m.route.mode] + currentRoute)
} else {
$location[m.route.mode] = currentRoute
}
else $location[m.route.mode] = currentRoute
}
};
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]
};
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) {
routeParams = {};
@ -642,7 +805,9 @@ var m = (function app(window, undefined) {
path.replace(matcher, function() {
var keys = route.match(/:[^\/]+/g) || [];
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])
});
return true
@ -652,26 +817,34 @@ var m = (function app(window, undefined) {
function routeUnobtrusive(e) {
e = e || event;
if (e.ctrlKey || e.metaKey || e.which === 2) return;
if (e.preventDefault) e.preventDefault();
else e.returnValue = false;
if (e.preventDefault) {
e.preventDefault();
} else {
e.returnValue = false;
}
var currentTarget = e.currentTarget || this;
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)
}
function setScroll() {
if (m.route.mode != "hash" && $location.hash) $location.hash = $location.hash;
else window.scrollTo(0, 0)
if (m.route.mode != "hash" && $location.hash) {
$location.hash = $location.hash;
} else {
window.scrollTo(0, 0)
}
}
function buildQueryString(object, prefix) {
var str = [];
for(var prop in object) {
var key = prefix ? prefix + "[" + prop + "]" : prop, value = object[prop];
for (var prop in object) {
var key = prefix ? prefix + "[" + prop + "]" : prop,
value = object[prop];
str.push(value != null && type.call(value) === OBJECT ? buildQueryString(value, key) : encodeURIComponent(key) + "=" + encodeURIComponent(value))
}
return str.join("&")
}
function parseQueryString(str) {
var pairs = str.split("&"), params = {};
var pairs = str.split("&"),
params = {};
for (var i = 0, len = pairs.length; i < len; i++) {
var pair = pairs[i].split("=");
params[decodeSpace(pair[0])] = pair[1] ? decodeSpace(pair[1]) : ""
@ -687,7 +860,7 @@ var m = (function app(window, undefined) {
cellCache[cacheKey] = undefined
}
m.deferred = function () {
m.deferred = function() {
var deferred = new Deferred();
deferred.promise = propify(deferred.promise);
return deferred
@ -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
//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) {
var RESOLVING = 1, REJECTING = 2, RESOLVED = 3, REJECTED = 4;
var self = this, state = 0, promiseValue = 0, next = [];
var RESOLVING = 1,
REJECTING = 2,
RESOLVED = 3,
REJECTED = 4;
var self = this,
state = 0,
promiseValue = 0,
next = [];
self["promise"] = {};
@ -734,11 +913,9 @@ var m = (function app(window, undefined) {
var deferred = new Deferred(successCallback, failureCallback);
if (state === RESOLVED) {
deferred.resolve(promiseValue)
}
else if (state === REJECTED) {
} else if (state === REJECTED) {
deferred.reject(promiseValue)
}
else {
} else {
next.push(deferred)
}
return deferred.promise
@ -760,13 +937,12 @@ var m = (function app(window, undefined) {
if (count++) return;
promiseValue = value;
successCallback()
}, function (value) {
}, function(value) {
if (count++) return;
promiseValue = value;
failureCallback()
})
}
catch (e) {
} catch ( e ) {
m.deferred.onerror(e);
promiseValue = e;
failureCallback()
@ -781,8 +957,7 @@ var m = (function app(window, undefined) {
var then;
try {
then = promiseValue && promiseValue.then
}
catch (e) {
} catch ( e ) {
m.deferred.onerror(e);
promiseValue = e;
state = REJECTING;
@ -798,13 +973,11 @@ var m = (function app(window, undefined) {
try {
if (state === RESOLVING && typeof successCallback === FUNCTION) {
promiseValue = successCallback(promiseValue)
}
else if (state === REJECTING && typeof failureCallback === "function") {
} else if (state === REJECTING && typeof failureCallback === "function") {
promiseValue = failureCallback(promiseValue);
state = RESOLVING
}
}
catch (e) {
} catch ( e ) {
m.deferred.onerror(e);
promiseValue = e;
return finish()
@ -813,11 +986,10 @@ var m = (function app(window, undefined) {
if (promiseValue === self) {
promiseValue = TypeError();
finish()
}
else {
thennable(then, function () {
} else {
thennable(then, function() {
finish(RESOLVED)
}, finish, function () {
}, finish, function() {
finish(state === RESOLVING && RESOLVED)
})
}
@ -825,7 +997,8 @@ var m = (function app(window, undefined) {
}
}
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) {
@ -833,7 +1006,9 @@ var m = (function app(window, undefined) {
function synchronizer(pos, resolved) {
return function(value) {
results[pos] = value;
if (!resolved) method = "reject";
if (!resolved) {
method = "reject";
}
if (--outstanding === 0) {
deferred.promise(results);
deferred[method](results)
@ -849,12 +1024,15 @@ var m = (function app(window, undefined) {
for (var i = 0; i < args.length; i++) {
args[i].then(synchronizer(i, true), synchronizer(i, false))
}
} else {
deferred.resolve([]);
}
else deferred.resolve([]);
return deferred.promise
};
function identity(value) {return value}
function identity(value) {
return value
}
function ajax(options) {
if (options.dataType && options.dataType.toLowerCase() === "jsonp") {
@ -879,7 +1057,9 @@ var m = (function app(window, undefined) {
type: "error",
target: {
status: 500,
responseText: JSON.stringify({error: "Error making jsonp request"})
responseText: JSON.stringify({
error: "Error making jsonp request"
})
}
});
window[callbackKey] = undefined;
@ -897,14 +1077,22 @@ var m = (function app(window, undefined) {
+ "=" + callbackKey
+ "&" + buildQueryString(options.data || {});
$document.body.appendChild(script)
}
else {
} else {
var xhr = new window.XMLHttpRequest;
xhr.open(options.method, options.url, true, options.user, options.password);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
if (xhr.status >= 200 && xhr.status < 300) options.onload({type: "load", target: xhr});
else options.onerror({type: "error", target: xhr})
if (xhr.status >= 200 && xhr.status < 300) {
options.onload({
type: "load",
target: xhr
});
} else {
options.onerror({
type: "error",
target: xhr
})
}
}
};
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) {
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
@ -931,8 +1121,9 @@ var m = (function app(window, undefined) {
var prefix = xhrOptions.url.indexOf("?") < 0 ? "?" : "&";
var querystring = buildQueryString(data);
xhrOptions.url = xhrOptions.url + (querystring ? prefix + querystring : "")
} else {
xhrOptions.data = serialize(data);
}
else xhrOptions.data = serialize(data);
return xhrOptions
}
function parameterizeUrl(url, data) {
@ -948,7 +1139,9 @@ var m = (function app(window, undefined) {
}
m.request = function(xhrOptions) {
if (xhrOptions.background !== true) m.startComputation();
if (xhrOptions.background !== true) {
m.startComputation();
}
var deferred = m.deferred();
var isJSONP = xhrOptions.dataType && xhrOptions.dataType.toLowerCase() === "jsonp";
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)));
if (e.type === "load") {
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)
}
catch (e) {
} catch ( e ) {
m.deferred.onerror(e);
deferred.reject(e)
}
if (xhrOptions.background !== true) m.endComputation()
if (xhrOptions.background !== true) {
m.endComputation()
}
};
ajax(xhrOptions);
deferred.promise(xhrOptions.initialValue);
@ -993,8 +1190,15 @@ var m = (function app(window, undefined) {
return m
})(typeof window != "undefined" ? window : {});
if (typeof module != "undefined" && module !== null && module.exports) module.exports = m;
else if (typeof define === "function" && define.amd) define(function() {return m});
if (typeof module != "undefined" && module !== null && module.exports) {
module.exports = m;
} else if (typeof define === "function" && define.amd) {
define(function() {
return m
});
}
//make "use strict" and nodejs happy
var window = this

View file

@ -1,14 +1,18 @@
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 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)$/;
// caching commonly used variables
var $document, $location, $requestAnimationFrame, $cancelAnimationFrame;
// self invoking function needed because of the way mocks work
function initialize(window){
function initialize(window) {
$document = window.document;
$location = window.location;
$cancelAnimationFrame = window.cancelAnimationFrame || window.clearTimeout;
@ -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 attrs = hasAttrs ? args[1] : {};
var classAttrName = "class" in attrs ? "class" : "className";
var cell = {tag: "div", attrs: {}};
var match, classes = [];
if (type.call(args[0]) != STRING) throw new Error("selector in m(selector, attrs, children) should be a string")
var cell = {
tag: "div",
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])) {
if (match[1] === "" && match[2]) cell.tag = match[2];
else if (match[1] === "#") cell.attrs.id = match[2];
else if (match[1] === ".") classes.push(match[2]);
else if (match[3][0] === "[") {
if (match[1] === "" && match[2]) {
cell.tag = match[2];
} else if (match[1] === "#") {
cell.attrs.id = match[2];
} else if (match[1] === ".") {
classes.push(match[2]);
} else if (match[3][0] === "[") {
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];
if (type.call(children) === ARRAY) {
cell.children = children
}
else {
} else {
cell.children = hasAttrs ? args.slice(2) : args.slice(1)
}
for (var attrName in attrs) {
if (attrName === classAttrName) cell.attrs[attrName] = (cell.attrs[attrName] || "") + " " + attrs[attrName];
else cell.attrs[attrName] = attrs[attrName]
if (attrName === classAttrName) {
cell.attrs[attrName] = (cell.attrs[attrName] || "") + " " + attrs[attrName];
} else {
cell.attrs[attrName] = attrs[attrName]
}
}
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
//- 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
if (data == null || data.toString() == null) data = "";
if (data == null || data.toString() == null) {
data = "";
}
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) {
if (parentCache && parentCache.nodes) {
var offset = index - parentIndex;
var end = offset + (dataType === ARRAY ? data : cached.nodes).length;
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;
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 = []
}
@ -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
//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
//4) for each key, handle its corresponding action as marked in previous steps
//5) copy unkeyed items into their respective gaps
var DELETION = 1, INSERTION = 2 , MOVE = 3;
var existing = {}, unkeyed = [], shouldMaintainIdentities = false;
var DELETION = 1,
INSERTION = 2 ,
MOVE = 3;
var existing = {},
unkeyed = [],
shouldMaintainIdentities = false;
for (var i = 0, len = cached.length; i < len; i++) {
if (cached[i] && cached[i].attrs && cached[i].attrs.key != null) {
shouldMaintainIdentities = true;
existing[cached[i].attrs.key] = {action: DELETION, index: i}
existing[cached[i].attrs.key] = {
action: DELETION,
index: i
}
}
}
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++) {
if (data[i] && data[i].attrs) {
if (data[i].attrs.key != null) {
var key = data[i].attrs.key;
if (!existing[key]) existing[key] = {action: INSERTION, index: i};
else existing[key] = {
if (!existing[key]) {
existing[key] = {
action: INSERTION,
index: i
};
} else {
existing[key] = {
action: MOVE,
index: i,
from: existing[key].index,
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();
for (var i = 0, change; change = changes[i]; i++) {
@ -163,7 +212,12 @@ var m = (function app(window, undefined) {
var dummy = $document.createElement("div");
dummy.key = data[change.index].attrs.key;
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) {
@ -180,7 +234,9 @@ var m = (function app(window, undefined) {
}
cached = newCached;
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
@ -188,14 +244,17 @@ var m = (function app(window, undefined) {
//diff each item in the array
var item = build(parentElement, parentTag, cached, index, data[i], cached[cacheCount], shouldReattach, index + subArrayCount || subArrayCount, editable, namespace, configs);
if (item === undefined) continue;
if (!item.nodes.intact) intact = false;
if (!item.nodes.intact) {
intact = false;
}
if (item.$trusted) {
//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 second clause (after the pipe) matches text nodes
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
}
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
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
//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++) {
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
}
} 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 hasKeys = dataAttrKeys.length > ("key" in data.attrs ? 1 : 0)
//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 (cached.nodes.length) clear(cached.nodes);
if (cached.configContext && typeof cached.configContext.onunload === FUNCTION) cached.configContext.onunload()
if (cached.nodes.length) {
clear(cached.nodes);
}
if (cached.configContext && typeof cached.configContext.onunload === FUNCTION) {
cached.configContext.onunload()
}
}
if (type.call(data.tag) != STRING) return;
var node, isNew = cached.nodes.length === 0;
if (data.attrs.xmlns) 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";
var node,
isNew = cached.nodes.length === 0;
if (data.attrs.xmlns) {
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 (data.attrs.is) 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);
if (data.attrs.is) {
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 = {
tag: data.tag,
//set attributes first, then create children
@ -243,17 +323,26 @@ var m = (function app(window, undefined) {
data.children,
nodes: [node]
};
if (cached.children && !cached.children.nodes) cached.children.nodes = [];
//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)
if (cached.children && !cached.children.nodes) {
cached.children.nodes = [];
}
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];
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.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
if (typeof data.attrs["config"] === FUNCTION) {
@ -267,34 +356,34 @@ var m = (function app(window, undefined) {
};
configs.push(callback(data, [node, !isNew, context, cached]))
}
}
else if (typeof dataType != FUNCTION) {
} else if (typeof dataType != FUNCTION) {
//handle text nodes
var nodes;
if (cached.nodes.length === 0) {
if (data.$trusted) {
nodes = injectHTML(parentElement, index, data)
}
else {
} else {
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.nodes = nodes
}
else if (cached.valueOf() !== data.valueOf() || shouldReattach === true) {
} else if (cached.valueOf() !== data.valueOf() || shouldReattach === true) {
nodes = cached.nodes;
if (!editable || editable !== $document.activeElement) {
if (data.$trusted) {
clear(nodes, cached);
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
//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;
else if (editable) editable.innerHTML = data;
else {
if (parentTag === "textarea") {
parentElement.value = data;
} else if (editable) {
editable.innerHTML = data;
} else {
if (nodes[0].nodeType === 1 || nodes.length > 1) { //was a trusted string
clear(cached.nodes, cached);
nodes = [$document.createTextNode(data)]
@ -306,8 +395,9 @@ var m = (function app(window, undefined) {
}
cached = new data.constructor(data);
cached.nodes = nodes
} else {
cached.nodes.intact = true
}
else cached.nodes.intact = true
}
return cached
@ -329,30 +419,41 @@ var m = (function app(window, undefined) {
//handle `style: {...}`
else if (attrName === "style" && dataAttr != null && type.call(dataAttr) === OBJECT) {
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) {
if (!(rule in dataAttr)) node.style[rule] = ""
if (!(rule in dataAttr)) {
node.style[rule] = ""
}
}
}
//handle SVG
else if (namespace != null) {
if (attrName === "href") node.setAttributeNS("http://www.w3.org/1999/xlink", "href", dataAttr);
else if (attrName === "className") node.setAttribute("class", dataAttr);
else node.setAttribute(attrName, dataAttr)
if (attrName === "href") {
node.setAttributeNS("http://www.w3.org/1999/xlink", "href", dataAttr);
} else if (attrName === "className") {
node.setAttribute("class", dataAttr);
} else {
node.setAttribute(attrName, dataAttr)
}
}
//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
//- 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")) {
//#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
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)
@ -365,21 +466,31 @@ var m = (function app(window, undefined) {
function clear(nodes, cached) {
for (var i = nodes.length - 1; i > -1; i--) {
if (nodes[i] && nodes[i].parentNode) {
try {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)
try {
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);
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) {
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 (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) {
@ -391,10 +502,12 @@ var m = (function app(window, undefined) {
parentElement.insertBefore(placeholder, nextSibling || null);
placeholder.insertAdjacentHTML("beforebegin", data);
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 = [];
while (parentElement.childNodes[index] !== nextSibling) {
nodes.push(parentElement.childNodes[index]);
@ -407,8 +520,9 @@ var m = (function app(window, undefined) {
e = e || event;
m.redraw.strategy("diff");
m.startComputation();
try {return callback.call(object, e)}
finally {
try {
return callback.call(object, e)
} finally {
endFirstComputation()
}
}
@ -417,11 +531,14 @@ var m = (function app(window, undefined) {
var html;
var documentNode = {
appendChild: function(node) {
if (html === undefined) html = $document.createElement("html");
if (html === undefined) {
html = $document.createElement("html");
}
if ($document.documentElement && $document.documentElement !== node) {
$document.replaceChild(node, $document.documentElement)
} else {
$document.appendChild(node);
}
else $document.appendChild(node);
this.childNodes = $document.childNodes
},
insertBefore: function(node) {
@ -429,18 +546,32 @@ var m = (function app(window, undefined) {
},
childNodes: []
};
var nodeCache = [], cellCache = {};
var nodeCache = [],
cellCache = {};
m.render = function(root, cell, forceRecreation) {
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 isDocumentRoot = root === $document;
var node = isDocumentRoot || root === $document.documentElement ? documentNode : root;
if (isDocumentRoot && cell.tag != "html") cell = {tag: "html", attrs: {}, children: cell};
if (cellCache[id] === undefined) clear(node.childNodes);
if (forceRecreation === true) reset(root);
if (isDocumentRoot && cell.tag != "html") {
cell = {
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);
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) {
var index = nodeCache.indexOf(element);
@ -455,7 +586,9 @@ var m = (function app(window, undefined) {
function gettersetter(store) {
var prop = function() {
if (arguments.length) store = arguments[0];
if (arguments.length) {
store = arguments[0];
}
return store
};
@ -466,7 +599,7 @@ var m = (function app(window, undefined) {
return prop
}
m.prop = function (store) {
m.prop = function(store) {
//note: using non-strict equality check here because we're checking if store is null OR undefined
if (((store != null && type.call(store) === OBJECT) || typeof store === FUNCTION) && typeof store.then === FUNCTION) {
return propify(store)
@ -475,15 +608,25 @@ var m = (function app(window, undefined) {
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
m.module = function(root, module) {
var index = roots.indexOf(root);
if (index < 0) index = roots.length;
if (index < 0) {
index = roots.length;
}
var isPrevented = false;
if (controllers[index] && typeof controllers[index].onunload === FUNCTION) {
var event = {
preventDefault: function() {isPrevented = true}
preventDefault: function() {
isPrevented = true
}
};
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 rAF: always reschedule redraw
if (new Date - lastRedrawCallTime > FRAME_BUDGET || $requestAnimationFrame === window.requestAnimationFrame) {
if (lastRedrawId > 0) $cancelAnimationFrame(lastRedrawId);
if (lastRedrawId > 0) {
$cancelAnimationFrame(lastRedrawId);
}
lastRedrawId = $requestAnimationFrame(redraw, FRAME_BUDGET)
}
}
else {
} else {
redraw();
lastRedrawId = $requestAnimationFrame(function() {lastRedrawId = null}, FRAME_BUDGET)
lastRedrawId = $requestAnimationFrame(function() {
lastRedrawId = null
}, FRAME_BUDGET)
}
};
m.redraw.strategy = m.prop();
@ -538,17 +684,22 @@ var m = (function app(window, undefined) {
}
var pendingRequests = 0;
m.startComputation = function() {pendingRequests++};
m.startComputation = function() {
pendingRequests++
};
m.endComputation = function() {
pendingRequests = Math.max(pendingRequests - 1, 0);
if (pendingRequests === 0) m.redraw()
if (pendingRequests === 0) {
m.redraw()
}
};
var endFirstComputation = function() {
if (m.redraw.strategy() == "none") {
pendingRequests--
m.redraw.strategy("diff")
} else {
m.endComputation();
}
else m.endComputation();
}
m.withAttr = function(prop, withAttrCallback) {
@ -560,14 +711,20 @@ var m = (function app(window, undefined) {
};
//routing
var modes = {pathname: "", hash: "#", search: "?"};
var modes = {
pathname: "",
hash: "#",
search: "?"
};
var redirect = function() {}, routeParams, currentRoute;
m.route = function() {
//m.route()
if (arguments.length === 0) return currentRoute;
//m.route(el, defaultRoute, routes)
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) {
var path = currentRoute = normalizeRoute(source);
if (!routeByValue(root, router, path)) {
@ -601,7 +758,9 @@ var m = (function app(window, undefined) {
for (var i in args) params[i] = args[i]
var querystring = buildQueryString(params)
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;
@ -611,16 +770,20 @@ var m = (function app(window, undefined) {
setScroll()
};
redirect(modes[m.route.mode] + currentRoute)
} else {
$location[m.route.mode] = currentRoute
}
else $location[m.route.mode] = currentRoute
}
};
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]
};
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) {
routeParams = {};
@ -642,7 +805,9 @@ var m = (function app(window, undefined) {
path.replace(matcher, function() {
var keys = route.match(/:[^\/]+/g) || [];
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])
});
return true
@ -652,26 +817,34 @@ var m = (function app(window, undefined) {
function routeUnobtrusive(e) {
e = e || event;
if (e.ctrlKey || e.metaKey || e.which === 2) return;
if (e.preventDefault) e.preventDefault();
else e.returnValue = false;
if (e.preventDefault) {
e.preventDefault();
} else {
e.returnValue = false;
}
var currentTarget = e.currentTarget || this;
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)
}
function setScroll() {
if (m.route.mode != "hash" && $location.hash) $location.hash = $location.hash;
else window.scrollTo(0, 0)
if (m.route.mode != "hash" && $location.hash) {
$location.hash = $location.hash;
} else {
window.scrollTo(0, 0)
}
}
function buildQueryString(object, prefix) {
var str = [];
for(var prop in object) {
var key = prefix ? prefix + "[" + prop + "]" : prop, value = object[prop];
for (var prop in object) {
var key = prefix ? prefix + "[" + prop + "]" : prop,
value = object[prop];
str.push(value != null && type.call(value) === OBJECT ? buildQueryString(value, key) : encodeURIComponent(key) + "=" + encodeURIComponent(value))
}
return str.join("&")
}
function parseQueryString(str) {
var pairs = str.split("&"), params = {};
var pairs = str.split("&"),
params = {};
for (var i = 0, len = pairs.length; i < len; i++) {
var pair = pairs[i].split("=");
params[decodeSpace(pair[0])] = pair[1] ? decodeSpace(pair[1]) : ""
@ -687,7 +860,7 @@ var m = (function app(window, undefined) {
cellCache[cacheKey] = undefined
}
m.deferred = function () {
m.deferred = function() {
var deferred = new Deferred();
deferred.promise = propify(deferred.promise);
return deferred
@ -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
//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) {
var RESOLVING = 1, REJECTING = 2, RESOLVED = 3, REJECTED = 4;
var self = this, state = 0, promiseValue = 0, next = [];
var RESOLVING = 1,
REJECTING = 2,
RESOLVED = 3,
REJECTED = 4;
var self = this,
state = 0,
promiseValue = 0,
next = [];
self["promise"] = {};
@ -734,11 +913,9 @@ var m = (function app(window, undefined) {
var deferred = new Deferred(successCallback, failureCallback);
if (state === RESOLVED) {
deferred.resolve(promiseValue)
}
else if (state === REJECTED) {
} else if (state === REJECTED) {
deferred.reject(promiseValue)
}
else {
} else {
next.push(deferred)
}
return deferred.promise
@ -760,13 +937,12 @@ var m = (function app(window, undefined) {
if (count++) return;
promiseValue = value;
successCallback()
}, function (value) {
}, function(value) {
if (count++) return;
promiseValue = value;
failureCallback()
})
}
catch (e) {
} catch ( e ) {
m.deferred.onerror(e);
promiseValue = e;
failureCallback()
@ -781,8 +957,7 @@ var m = (function app(window, undefined) {
var then;
try {
then = promiseValue && promiseValue.then
}
catch (e) {
} catch ( e ) {
m.deferred.onerror(e);
promiseValue = e;
state = REJECTING;
@ -798,13 +973,11 @@ var m = (function app(window, undefined) {
try {
if (state === RESOLVING && typeof successCallback === FUNCTION) {
promiseValue = successCallback(promiseValue)
}
else if (state === REJECTING && typeof failureCallback === "function") {
} else if (state === REJECTING && typeof failureCallback === "function") {
promiseValue = failureCallback(promiseValue);
state = RESOLVING
}
}
catch (e) {
} catch ( e ) {
m.deferred.onerror(e);
promiseValue = e;
return finish()
@ -813,11 +986,10 @@ var m = (function app(window, undefined) {
if (promiseValue === self) {
promiseValue = TypeError();
finish()
}
else {
thennable(then, function () {
} else {
thennable(then, function() {
finish(RESOLVED)
}, finish, function () {
}, finish, function() {
finish(state === RESOLVING && RESOLVED)
})
}
@ -825,7 +997,8 @@ var m = (function app(window, undefined) {
}
}
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) {
@ -833,7 +1006,9 @@ var m = (function app(window, undefined) {
function synchronizer(pos, resolved) {
return function(value) {
results[pos] = value;
if (!resolved) method = "reject";
if (!resolved) {
method = "reject";
}
if (--outstanding === 0) {
deferred.promise(results);
deferred[method](results)
@ -849,12 +1024,15 @@ var m = (function app(window, undefined) {
for (var i = 0; i < args.length; i++) {
args[i].then(synchronizer(i, true), synchronizer(i, false))
}
} else {
deferred.resolve([]);
}
else deferred.resolve([]);
return deferred.promise
};
function identity(value) {return value}
function identity(value) {
return value
}
function ajax(options) {
if (options.dataType && options.dataType.toLowerCase() === "jsonp") {
@ -879,7 +1057,9 @@ var m = (function app(window, undefined) {
type: "error",
target: {
status: 500,
responseText: JSON.stringify({error: "Error making jsonp request"})
responseText: JSON.stringify({
error: "Error making jsonp request"
})
}
});
window[callbackKey] = undefined;
@ -897,14 +1077,22 @@ var m = (function app(window, undefined) {
+ "=" + callbackKey
+ "&" + buildQueryString(options.data || {});
$document.body.appendChild(script)
}
else {
} else {
var xhr = new window.XMLHttpRequest;
xhr.open(options.method, options.url, true, options.user, options.password);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
if (xhr.status >= 200 && xhr.status < 300) options.onload({type: "load", target: xhr});
else options.onerror({type: "error", target: xhr})
if (xhr.status >= 200 && xhr.status < 300) {
options.onload({
type: "load",
target: xhr
});
} else {
options.onerror({
type: "error",
target: xhr
})
}
}
};
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) {
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
@ -931,8 +1121,9 @@ var m = (function app(window, undefined) {
var prefix = xhrOptions.url.indexOf("?") < 0 ? "?" : "&";
var querystring = buildQueryString(data);
xhrOptions.url = xhrOptions.url + (querystring ? prefix + querystring : "")
} else {
xhrOptions.data = serialize(data);
}
else xhrOptions.data = serialize(data);
return xhrOptions
}
function parameterizeUrl(url, data) {
@ -948,7 +1139,9 @@ var m = (function app(window, undefined) {
}
m.request = function(xhrOptions) {
if (xhrOptions.background !== true) m.startComputation();
if (xhrOptions.background !== true) {
m.startComputation();
}
var deferred = m.deferred();
var isJSONP = xhrOptions.dataType && xhrOptions.dataType.toLowerCase() === "jsonp";
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)));
if (e.type === "load") {
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)
}
catch (e) {
} catch ( e ) {
m.deferred.onerror(e);
deferred.reject(e)
}
if (xhrOptions.background !== true) m.endComputation()
if (xhrOptions.background !== true) {
m.endComputation()
}
};
ajax(xhrOptions);
deferred.promise(xhrOptions.initialValue);
@ -993,5 +1190,12 @@ var m = (function app(window, undefined) {
return m
})(typeof window != "undefined" ? window : {});
if (typeof module != "undefined" && module !== null && module.exports) module.exports = m;
else if (typeof define === "function" && define.amd) define(function() {return m});
if (typeof module != "undefined" && module !== null && module.exports) {
module.exports = m;
} else if (typeof define === "function" && define.amd) {
define(function() {
return m
});
}