const style

This commit is contained in:
Leo Horie 2014-11-14 23:37:36 -05:00
parent 1321aec9fb
commit 5afefb30b8

View file

@ -1,5 +1,5 @@
Mithril = m = new function app(window, undefined) { Mithril = m = new function app(window, undefined) {
var sObj = "[object Object]", sArr = "[object Array]", sStr = "[object String]", sFn = "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)$/;
@ -33,7 +33,7 @@ Mithril = m = new function app(window, undefined) {
*/ */
function m() { function m() {
var args = [].slice.call(arguments); var args = [].slice.call(arguments);
var hasAttrs = args[1] != null && type.call(args[1]) == sObj && !("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 = {tag: "div", attrs: {}};
@ -51,7 +51,7 @@ Mithril = m = new function app(window, undefined) {
var children = hasAttrs ? args[2] : args[1]; var children = hasAttrs ? args[2] : args[1];
if (type.call(children) == sArr) { if (type.call(children) == ARRAY) {
cell.children = children cell.children = children
} }
else { else {
@ -97,7 +97,7 @@ Mithril = m = new function app(window, undefined) {
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 == sArr ? 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)
@ -107,10 +107,10 @@ Mithril = m = new function app(window, undefined) {
cached.nodes = [] cached.nodes = []
} }
if (dataType == sArr) { if (dataType == ARRAY) {
//recursively flatten array //recursively flatten array
for (var i = 0; i < data.length; i++) { for (var i = 0; i < data.length; i++) {
if (type.call(data[i]) == sArr) { if (type.call(data[i]) == ARRAY) {
data = data.concat.apply([], data); data = data.concat.apply([], data);
i-- //check current index again and flatten until there are no more nested arrays at that index i-- //check current index again and flatten until there are no more nested arrays at that index
} }
@ -193,7 +193,7 @@ Mithril = m = new function app(window, undefined) {
//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) == sArr ? item.length : 1; else subArrayCount += type.call(item) == ARRAY ? item.length : 1;
cached[cacheCount++] = item cached[cacheCount++] = item
} }
if (!intact) { if (!intact) {
@ -212,7 +212,7 @@ Mithril = m = new function app(window, undefined) {
cached.nodes = nodes cached.nodes = nodes
} }
} }
else if (data != null && dataType == sObj) { else if (data != null && dataType == OBJECT) {
if (!data.attrs) data.attrs = {}; if (!data.attrs) data.attrs = {};
if (!cached.attrs) cached.attrs = {}; if (!cached.attrs) cached.attrs = {};
@ -221,9 +221,9 @@ Mithril = m = new function app(window, undefined) {
//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) clear(cached.nodes);
if (cached.configContext && typeof cached.configContext.onunload == sFn) cached.configContext.onunload() if (cached.configContext && typeof cached.configContext.onunload == FUNCTION) cached.configContext.onunload()
} }
if (type.call(data.tag) != sStr) return; if (type.call(data.tag) != STRING) return;
var node, isNew = cached.nodes.length === 0; var node, isNew = cached.nodes.length === 0;
if (data.attrs.xmlns) namespace = data.attrs.xmlns; if (data.attrs.xmlns) namespace = data.attrs.xmlns;
@ -254,7 +254,7 @@ Mithril = m = new function app(window, undefined) {
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"] == sFn) { if (typeof data.attrs["config"] == FUNCTION) {
var context = cached.configContext = cached.configContext || {}; var context = cached.configContext = cached.configContext || {};
// bind // bind
@ -266,7 +266,7 @@ Mithril = m = new function app(window, undefined) {
configs.push(callback(data, [node, !isNew, context, cached])) configs.push(callback(data, [node, !isNew, context, cached]))
} }
} }
else if (typeof dataType != sFn) { 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) {
@ -321,11 +321,11 @@ Mithril = m = new function app(window, undefined) {
//we don't ignore `key` because it must be unique and having it on the DOM helps debugging //we don't ignore `key` because it must be unique and having it on the DOM helps debugging
if (attrName === "config") continue; if (attrName === "config") continue;
//hook event handlers to the auto-redrawing system //hook event handlers to the auto-redrawing system
else if (typeof dataAttr == sFn && attrName.indexOf("on") == 0) { else if (typeof dataAttr == FUNCTION && attrName.indexOf("on") == 0) {
node[attrName] = autoredraw(dataAttr, node) node[attrName] = autoredraw(dataAttr, node)
} }
//handle `style: {...}` //handle `style: {...}`
else if (attrName === "style" && dataAttr != null && type.call(dataAttr) == sObj) { 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]
} }
@ -369,9 +369,9 @@ Mithril = m = new function app(window, undefined) {
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 == sFn) 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) == sArr) { if (type.call(cached.children) == ARRAY) {
for (var i = 0; i < cached.children.length; i++) unload(cached.children[i]) for (var i = 0; i < cached.children.length; i++) unload(cached.children[i])
} }
else if (cached.children.tag) unload(cached.children) else if (cached.children.tag) unload(cached.children)
@ -463,7 +463,7 @@ Mithril = m = new function app(window, undefined) {
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 //note: using non-strict equality check here because we're checking if store is null OR undefined
if (((store != null && type.call(store) == sObj) || typeof store == sFn) && typeof store.then == sFn) { if (((store != null && type.call(store) == OBJECT) || typeof store == FUNCTION) && typeof store.then == FUNCTION) {
return propify(store) return propify(store)
} }
@ -476,7 +476,7 @@ Mithril = m = new function app(window, undefined) {
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 == sFn) { if (controllers[index] && typeof controllers[index].onunload == FUNCTION) {
var event = { var event = {
preventDefault: function() {isPrevented = true} preventDefault: function() {isPrevented = true}
}; };
@ -553,7 +553,7 @@ Mithril = m = new function app(window, undefined) {
m.route = function() { m.route = function() {
//m.route() //m.route()
if (arguments.length === 0) return currentRoute; if (arguments.length === 0) return currentRoute;
else if (arguments.length === 3 && type.call(arguments[1]) == sStr) { 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);
@ -580,9 +580,9 @@ Mithril = m = new function app(window, undefined) {
element.addEventListener("click", routeUnobtrusive) element.addEventListener("click", routeUnobtrusive)
} }
//m.route(route) //m.route(route)
else if (type.call(arguments[0]) == sStr) { else if (type.call(arguments[0]) == STRING) {
currentRoute = arguments[0]; currentRoute = arguments[0];
var querystring = arguments[1] != null && type.call(arguments[1]) == sObj ? buildQueryString(arguments[1]) : null; var querystring = arguments[1] != null && type.call(arguments[1]) == OBJECT ? buildQueryString(arguments[1]) : null;
if (querystring) currentRoute += (currentRoute.indexOf("?") === -1 ? "?" : "&") + querystring; if (querystring) currentRoute += (currentRoute.indexOf("?") === -1 ? "?" : "&") + querystring;
var shouldReplaceHistoryEntry = (arguments.length == 3 ? arguments[2] : arguments[1]) === true; var shouldReplaceHistoryEntry = (arguments.length == 3 ? arguments[2] : arguments[1]) === true;
@ -645,7 +645,7 @@ Mithril = m = new function app(window, undefined) {
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) == sObj ? 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("&")
} }
@ -731,7 +731,7 @@ Mithril = m = new function app(window, undefined) {
} }
function thennable(then, successCallback, failureCallback, notThennableCallback) { function thennable(then, successCallback, failureCallback, notThennableCallback) {
if (((promiseValue != null && type.call(promiseValue) == sObj) || typeof promiseValue == sFn) && typeof then == sFn) { if (((promiseValue != null && type.call(promiseValue) == OBJECT) || typeof promiseValue == FUNCTION) && typeof then == FUNCTION) {
try { try {
// count protects against abuse calls from spec checker // count protects against abuse calls from spec checker
var count = 0; var count = 0;
@ -775,7 +775,7 @@ Mithril = m = new function app(window, undefined) {
fire() fire()
}, function() { }, function() {
try { try {
if (state == RESOLVING && typeof successCallback == sFn) { 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") {
@ -892,13 +892,13 @@ Mithril = m = new function app(window, undefined) {
if (options.deserialize == JSON.parse) { if (options.deserialize == JSON.parse) {
xhr.setRequestHeader("Accept", "application/json, text/*"); xhr.setRequestHeader("Accept", "application/json, text/*");
} }
if (typeof options.config == sFn) { 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
if (data && (type.call(data) != sStr && data.constructor != window.FormData)) { if (data && (type.call(data) != STRING && data.constructor != window.FormData)) {
throw "Request data should be either be a string or FormData. Check the `serialize` option in `m.request`"; throw "Request data should be either be a string or FormData. Check the `serialize` option in `m.request`";
} }
xhr.send(data); xhr.send(data);
@ -943,7 +943,7 @@ Mithril = m = new function app(window, undefined) {
var unwrap = (e.type == "load" ? xhrOptions.unwrapSuccess : xhrOptions.unwrapError) || identity; var unwrap = (e.type == "load" ? xhrOptions.unwrapSuccess : xhrOptions.unwrapError) || identity;
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) == sArr && 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)