From 067da7eedc125e349c2ceb2f4af6a668cee102ef Mon Sep 17 00:00:00 2001 From: Leo Horie Date: Sun, 14 Sep 2014 00:47:06 -0400 Subject: [PATCH] code style --- mithril.js | 118 ++++++++++++++++++++++++++--------------------------- 1 file changed, 59 insertions(+), 59 deletions(-) diff --git a/mithril.js b/mithril.js index c0983bb1..224d29f3 100644 --- a/mithril.js +++ b/mithril.js @@ -324,7 +324,9 @@ Mithril = m = new function app(window, undefined) { function unload(cached) { if (cached.configContext && typeof cached.configContext.onunload == "function") cached.configContext.onunload() if (cached.children) { - if (type.call(cached.children) == "[object Array]") for (var i = 0; i < cached.children.length; i++) unload(cached.children[i]) + if (type.call(cached.children) == "[object Array]") { + for (var i = 0; i < cached.children.length; i++) unload(cached.children[i]) + } else if (cached.children.tag) unload(cached.children) } } @@ -364,7 +366,7 @@ Mithril = m = new function app(window, undefined) { m.startComputation() try {return callback.call(object, e)} finally { - if (!lastRedrawId) lastRedrawId = -1; + if (!lastRedrawId) lastRedrawId = -1 m.endComputation() } } @@ -582,7 +584,8 @@ Mithril = m = new function app(window, undefined) { function routeUnobtrusive(e) { e = e || event if (e.ctrlKey || e.metaKey || e.which == 2) return - e.preventDefault() + if (e.preventDefault) e.preventDefault() + else e.returnValue = false m.route(e.currentTarget[m.route.mode].slice(modes[m.route.mode].length)) } function setScroll() { @@ -784,82 +787,79 @@ Mithril = m = new function app(window, undefined) { function identity(value) {return value} function serializeArray(array, prefix){ - var idx, out = []; - for(idx in array){ - var formatted = (prefix ? prefix : "") + "[]"; - if(prefix && typeof array[idx] === "object") - formatted = formatted.replace(/\[\]$/i, "[" + idx + "]"); - if(typeof array[idx] === "object" && JSON.stringify(array[idx]) === "{}"){ - continue; - } - if(array[idx] instanceof Array) - out.push(serializeArray(array[idx], formatted)); - else if(typeof array[idx] === "object") - out.push(serializeObject(array[idx], formatted)); - else - out.push(encodeURIComponent(formatted) + "=" + encodeURIComponent(array[idx])); + var idx, out = [] + for (idx in array) { + var formatted = (prefix ? prefix : "") + "[]" + if (prefix && typeof array[idx] === "object") formatted = formatted.replace(/\[\]$/i, "[" + idx + "]") + if (typeof array[idx] === "object" && JSON.stringify(array[idx]) === "{}") continue + if (array[idx] instanceof Array) out.push(serializeArray(array[idx], formatted)) + else if(typeof array[idx] === "object") out.push(serializeObject(array[idx], formatted)) + else out.push(encodeURIComponent(formatted) + "=" + encodeURIComponent(array[idx])) } - return out.join("&"); + return out.join("&") } function serializeObject(obj, prefix) { - var key, out = []; - for(key in obj){ - var formatted = prefix ? prefix + "[" + key + "]" : key; - if(obj[key] instanceof Array){ - if(obj[key].length < 1) - continue; - out.push(serializeArray(obj[key], formatted)); - }else if(typeof obj[key] === "object"){ - if(JSON.stringify(obj[key]) === "{}") - continue; - out.push(serializeObject(obj[key], formatted)); - }else{ - out.push(encodeURIComponent(formatted) + "=" + encodeURIComponent(obj[key])); + var key, out = [] + for (key in obj) { + var formatted = prefix ? prefix + "[" + key + "]" : key + if (obj[key] instanceof Array) { + if(obj[key].length < 1) continue + out.push(serializeArray(obj[key], formatted)) } - }; - return out.join("&"); + else if(typeof obj[key] === "object") { + if(JSON.stringify(obj[key]) === "{}") continue + out.push(serializeObject(obj[key], formatted)) + } + else out.push(encodeURIComponent(formatted) + "=" + encodeURIComponent(obj[key])) + } + return out.join("&") } function ajax(options) { if (options.dataType && options.dataType.toLowerCase() === "jsonp") { - var callbackKey = "mithril_callback_" + new Date().getTime() + "_" + (Math.round(Math.random() * 1e16)).toString(36); - var script = window.document.createElement("script"); + var callbackKey = "mithril_callback_" + new Date().getTime() + "_" + (Math.round(Math.random() * 1e16)).toString(36) + var script = window.document.createElement("script") window[callbackKey] = function(resp){ - delete window[callbackKey]; - window.document.body.removeChild(script); - options.onload({ type: "load", target: { - responseText: resp - } }); - }; + delete window[callbackKey] + window.document.body.removeChild(script) + options.onload({ + type: "load", + target: { + responseText: resp + } + }) + } - script.onerror = function(e){ - delete window[callbackKey]; - window.document.body.removeChild(script); + script.onerror = function(e) { + delete window[callbackKey] + window.document.body.removeChild(script) - options.onerror({ type: "error", target: { - status: 500, - responseText: JSON.stringify({ error: "Error making jsonp request" }) - } }); + options.onerror({ + type: "error", + target: { + status: 500, + responseText: JSON.stringify({error: "Error making jsonp request"}) + } + }) - e.preventDefault(); - e.stopPropagation(); - }; + return false + } - script.onload = function(e){ - e.preventDefault(); - e.stopPropagation(); - }; + script.onload = function(e) { + return false + } script.src = options.url + (options.url.indexOf("?") > 0 ? "&" : "?") + (options.callbackKey ? options.callbackKey : "callback") + "=" + callbackKey - + "&" + serializeObject(options.data || {}); - window.document.body.appendChild(script); - }else{ + + "&" + serializeObject(options.data || {}) + window.document.body.appendChild(script) + } + else { var xhr = new window.XMLHttpRequest xhr.open(options.method, options.url, true, options.user, options.password) xhr.onreadystatechange = function() { @@ -869,7 +869,7 @@ Mithril = m = new function app(window, undefined) { } } if (options.serialize == JSON.stringify && options.method != "GET") { - xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8"); + xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8") } if (typeof options.config == "function") { var maybeXhr = options.config(xhr, options)