code style

This commit is contained in:
Leo Horie 2014-09-14 00:47:06 -04:00
parent 8c3467b819
commit 067da7eedc

View file

@ -324,7 +324,9 @@ Mithril = m = new function app(window, undefined) {
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) == "[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) else if (cached.children.tag) unload(cached.children)
} }
} }
@ -364,7 +366,7 @@ Mithril = m = new function app(window, undefined) {
m.startComputation() m.startComputation()
try {return callback.call(object, e)} try {return callback.call(object, e)}
finally { finally {
if (!lastRedrawId) lastRedrawId = -1; if (!lastRedrawId) lastRedrawId = -1
m.endComputation() m.endComputation()
} }
} }
@ -582,7 +584,8 @@ Mithril = m = new 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
e.preventDefault() if (e.preventDefault) e.preventDefault()
else e.returnValue = false
m.route(e.currentTarget[m.route.mode].slice(modes[m.route.mode].length)) m.route(e.currentTarget[m.route.mode].slice(modes[m.route.mode].length))
} }
function setScroll() { function setScroll() {
@ -784,82 +787,79 @@ Mithril = m = new function app(window, undefined) {
function identity(value) {return value} function identity(value) {return value}
function serializeArray(array, prefix){ function serializeArray(array, prefix){
var idx, out = []; var idx, out = []
for(idx in array){ for (idx in array) {
var formatted = (prefix ? prefix : "") + "[]"; var formatted = (prefix ? prefix : "") + "[]"
if(prefix && typeof array[idx] === "object") if (prefix && typeof array[idx] === "object") formatted = formatted.replace(/\[\]$/i, "[" + idx + "]")
formatted = formatted.replace(/\[\]$/i, "[" + idx + "]"); if (typeof array[idx] === "object" && JSON.stringify(array[idx]) === "{}") continue
if(typeof array[idx] === "object" && JSON.stringify(array[idx]) === "{}"){ if (array[idx] instanceof Array) out.push(serializeArray(array[idx], formatted))
continue; else if(typeof array[idx] === "object") out.push(serializeObject(array[idx], formatted))
} else out.push(encodeURIComponent(formatted) + "=" + encodeURIComponent(array[idx]))
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) { function serializeObject(obj, prefix) {
var key, out = []; var key, out = []
for(key in obj){ for (key in obj) {
var formatted = prefix ? prefix + "[" + key + "]" : key; var formatted = prefix ? prefix + "[" + key + "]" : key
if(obj[key] instanceof Array){ if (obj[key] instanceof Array) {
if(obj[key].length < 1) if(obj[key].length < 1) continue
continue; out.push(serializeArray(obj[key], formatted))
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]));
} }
}; else if(typeof obj[key] === "object") {
return out.join("&"); 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) { function ajax(options) {
if (options.dataType && options.dataType.toLowerCase() === "jsonp") { if (options.dataType && options.dataType.toLowerCase() === "jsonp") {
var callbackKey = "mithril_callback_" + new Date().getTime() + "_" + (Math.round(Math.random() * 1e16)).toString(36); var callbackKey = "mithril_callback_" + new Date().getTime() + "_" + (Math.round(Math.random() * 1e16)).toString(36)
var script = window.document.createElement("script"); var script = window.document.createElement("script")
window[callbackKey] = function(resp){ window[callbackKey] = function(resp){
delete window[callbackKey]; delete window[callbackKey]
window.document.body.removeChild(script); window.document.body.removeChild(script)
options.onload({ type: "load", target: { options.onload({
responseText: resp type: "load",
} }); target: {
}; responseText: resp
}
})
}
script.onerror = function(e){ script.onerror = function(e) {
delete window[callbackKey]; delete window[callbackKey]
window.document.body.removeChild(script); window.document.body.removeChild(script)
options.onerror({ type: "error", target: { options.onerror({
status: 500, type: "error",
responseText: JSON.stringify({ error: "Error making jsonp request" }) target: {
} }); status: 500,
responseText: JSON.stringify({error: "Error making jsonp request"})
}
})
e.preventDefault(); return false
e.stopPropagation(); }
};
script.onload = function(e){ script.onload = function(e) {
e.preventDefault(); return false
e.stopPropagation(); }
};
script.src = options.url script.src = options.url
+ (options.url.indexOf("?") > 0 ? "&" : "?") + (options.url.indexOf("?") > 0 ? "&" : "?")
+ (options.callbackKey ? options.callbackKey : "callback") + (options.callbackKey ? options.callbackKey : "callback")
+ "=" + callbackKey + "=" + callbackKey
+ "&" + serializeObject(options.data || {}); + "&" + serializeObject(options.data || {})
window.document.body.appendChild(script); window.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() {
@ -869,7 +869,7 @@ Mithril = m = new function app(window, undefined) {
} }
} }
if (options.serialize == JSON.stringify && options.method != "GET") { 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") { if (typeof options.config == "function") {
var maybeXhr = options.config(xhr, options) var maybeXhr = options.config(xhr, options)