diff --git a/mithril.js b/mithril.js index a4f4f97a..274430a1 100755 --- a/mithril.js +++ b/mithril.js @@ -4,6 +4,20 @@ Mithril = m = new function app(window, undefined) { 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){ + $document = window.document; + $location = window.location; + $cancelAnimationFrame = window.cancelAnimationFrame || window.clearTimeout; + $requestAnimationFrame = window.requestAnimationFrame || window.setTimeout; + } + + initialize(window); + + /* * @typedef {String} Tag * A string that looks like -> div.classname#id[param=one][param2=two] @@ -121,10 +135,10 @@ Mithril = m = new function app(window, undefined) { action: MOVE, index: i, from: existing[key].index, - element: parentElement.childNodes[existing[key].index] || window.document.createElement("div") + element: parentElement.childNodes[existing[key].index] || $document.createElement("div") } } - else unkeyed.push({index: i, element: parentElement.childNodes[i] || window.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]}); @@ -137,7 +151,7 @@ Mithril = m = new function app(window, undefined) { newCached.splice(change.index, 1) } if (change.action == INSERTION) { - var dummy = window.document.createElement("div"); + 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]}) @@ -198,7 +212,7 @@ Mithril = m = new function app(window, undefined) { else if (data != null && dataType == sObj) { if (!data.attrs) data.attrs = {}; if (!cached.attrs) cached.attrs = {}; - + var dataAttrKeys = Object.keys(data.attrs); //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) { @@ -212,7 +226,7 @@ Mithril = m = new function app(window, undefined) { 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) { - node = namespace === undefined ? window.document.createElement(data.tag) : window.document.createElementNS(namespace, data.tag); + node = namespace === undefined ? $document.createElement(data.tag) : $document.createElementNS(namespace, data.tag); cached = { tag: data.tag, //set attributes first, then create children @@ -255,7 +269,7 @@ Mithril = m = new function app(window, undefined) { nodes = injectHTML(parentElement, index, data) } else { - nodes = [window.document.createTextNode(data)]; + nodes = [$document.createTextNode(data)]; 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; @@ -263,7 +277,7 @@ Mithril = m = new function app(window, undefined) { } else if (cached.valueOf() !== data.valueOf() || shouldReattach === true) { nodes = cached.nodes; - if (!editable || editable !== window.document.activeElement) { + if (!editable || editable !== $document.activeElement) { if (data.$trusted) { clear(nodes, cached); nodes = injectHTML(parentElement, index, data) @@ -276,7 +290,7 @@ Mithril = m = new function app(window, undefined) { else { if (nodes[0].nodeType == 1 || nodes.length > 1) { //was a trusted string clear(cached.nodes, cached); - nodes = [window.document.createTextNode(data)] + nodes = [$document.createTextNode(data)] } parentElement.insertBefore(nodes[0], parentElement.childNodes[index] || null); nodes[0].nodeValue = data @@ -359,7 +373,7 @@ Mithril = m = new function app(window, undefined) { var nextSibling = parentElement.childNodes[index]; if (nextSibling) { var isElement = nextSibling.nodeType != 1; - var placeholder = window.document.createElement("span"); + var placeholder = $document.createElement("span"); if (isElement) { parentElement.insertBefore(placeholder, nextSibling || null); placeholder.insertAdjacentHTML("beforebegin", data); @@ -405,12 +419,12 @@ Mithril = m = new function app(window, undefined) { var html; var documentNode = { appendChild: function(node) { - if (html === undefined) html = window.document.createElement("html"); - if (window.document.documentElement && window.document.documentElement !== node) { - window.document.replaceChild(node, window.document.documentElement) + if (html === undefined) html = $document.createElement("html"); + if ($document.documentElement && $document.documentElement !== node) { + $document.replaceChild(node, $document.documentElement) } - else window.document.appendChild(node); - this.childNodes = window.document.childNodes + else $document.appendChild(node); + this.childNodes = $document.childNodes }, insertBefore: function(node) { this.appendChild(node) @@ -422,8 +436,8 @@ Mithril = m = new function app(window, undefined) { var configs = []; if (!root) throw new Error("Please ensure the DOM element exists before rendering a template into it."); var id = getCellCacheKey(root); - var isDocumentRoot = root == window.document; - var node = isDocumentRoot || root == window.document.documentElement ? documentNode : 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); @@ -492,21 +506,19 @@ Mithril = m = new function app(window, undefined) { } }; m.redraw = function(force) { - var cancel = window.cancelAnimationFrame || window.clearTimeout; - var defer = window.requestAnimationFrame || window.setTimeout; //lastRedrawId is a positive number if a second redraw is requested before the next animation frame //lastRedrawID is null if it's the first redraw and not an event handler if (lastRedrawId && force !== true) { //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 || defer == window.requestAnimationFrame) { - if (lastRedrawId > 0) cancel(lastRedrawId); - lastRedrawId = defer(redraw, FRAME_BUDGET) + if (new Date - lastRedrawCallTime > FRAME_BUDGET || $requestAnimationFrame == window.requestAnimationFrame) { + if (lastRedrawId > 0) $cancelAnimationFrame(lastRedrawId); + lastRedrawId = $requestAnimationFrame(redraw, FRAME_BUDGET) } } else { redraw(); - lastRedrawId = defer(function() {lastRedrawId = null}, FRAME_BUDGET) + lastRedrawId = $requestAnimationFrame(function() {lastRedrawId = null}, FRAME_BUDGET) } }; m.redraw.strategy = m.prop(); @@ -558,8 +570,8 @@ Mithril = m = new function app(window, undefined) { }; var listener = m.route.mode == "hash" ? "onhashchange" : "onpopstate"; window[listener] = function() { - if (currentRoute != normalizeRoute(window.location[m.route.mode])) { - redirect(window.location[m.route.mode]) + if (currentRoute != normalizeRoute($location[m.route.mode])) { + redirect($location[m.route.mode]) } }; computePostRedrawHook = setScroll; @@ -570,7 +582,7 @@ Mithril = m = new function app(window, undefined) { var element = arguments[0]; var isInitialized = arguments[1]; var context = arguments[2]; - element.href = (m.route.mode !== 'pathname' ? window.location.pathname : '') + modes[m.route.mode] + this.attrs.href; + element.href = (m.route.mode !== 'pathname' ? $location.pathname : '') + modes[m.route.mode] + this.attrs.href; element.removeEventListener("click", routeUnobtrusive); element.addEventListener("click", routeUnobtrusive) } @@ -584,12 +596,12 @@ Mithril = m = new function app(window, undefined) { if (window.history.pushState) { computePostRedrawHook = function() { - window.history[shouldReplaceHistoryEntry ? "replaceState" : "pushState"](null, window.document.title, modes[m.route.mode] + currentRoute); + window.history[shouldReplaceHistoryEntry ? "replaceState" : "pushState"](null, $document.title, modes[m.route.mode] + currentRoute); setScroll() }; redirect(modes[m.route.mode] + currentRoute) } - else window.location[m.route.mode] = currentRoute + else $location[m.route.mode] = currentRoute } }; m.route.param = function(key) {return routeParams[key]}; @@ -633,7 +645,7 @@ Mithril = m = new function app(window, undefined) { m.route(currentTarget[m.route.mode].slice(modes[m.route.mode].length), args) } function setScroll() { - if (m.route.mode != "hash" && window.location.hash) window.location.hash = window.location.hash; + if (m.route.mode != "hash" && $location.hash) $location.hash = $location.hash; else window.scrollTo(0, 0) } function buildQueryString(object, prefix) { @@ -698,7 +710,7 @@ Mithril = m = new function app(window, undefined) { if (!state) { promiseValue = value; state = REJECTING; - + fire() } return this @@ -717,7 +729,7 @@ Mithril = m = new function app(window, undefined) { } return deferred.promise }; - + function finish(type) { state = type || REJECTED; next.map(function(deferred) { @@ -833,10 +845,10 @@ Mithril = m = new function app(window, undefined) { 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 script = $document.createElement("script"); + window[callbackKey] = function(resp) { - window.document.body.removeChild(script); + $document.body.removeChild(script); options.onload({ type: "load", target: { @@ -847,7 +859,7 @@ Mithril = m = new function app(window, undefined) { }; script.onerror = function(e) { - window.document.body.removeChild(script); + $document.body.removeChild(script); options.onerror({ type: "error", @@ -870,7 +882,7 @@ Mithril = m = new function app(window, undefined) { + (options.callbackKey ? options.callbackKey : "callback") + "=" + callbackKey + "&" + buildQueryString(options.data || {}); - window.document.body.appendChild(script) + $document.body.appendChild(script) } else { var xhr = new window.XMLHttpRequest; @@ -953,7 +965,10 @@ Mithril = m = new function app(window, undefined) { }; //testing API - m.deps = function(mock) {return window = mock || window}; + m.deps = function(mock) { + initialize(window = mock || window); + return window; + }; //for internal testing only, do not use `m.deps.factory` m.deps.factory = app; @@ -961,6 +976,4 @@ Mithril = m = new function app(window, undefined) { }(typeof window != "undefined" ? window : {}); if (typeof module != "undefined" && module !== null) module.exports = m; -if (typeof define == "function" && define.amd) define(function() {return m}) - -;;; +if (typeof define == "function" && define.amd) define(function() {return m}); diff --git a/mithril.min.js b/mithril.min.js index 4e346e9c..7dc6b0f3 100755 --- a/mithril.min.js +++ b/mithril.min.js @@ -4,5 +4,5 @@ http://github.com/lhorie/mithril.js (c) Leo Horie License: MIT */ -Mithril=m=new function a(b,c){function d(){for(var a,b=[].slice.call(arguments),c=(!(null==b[1]||H.call(b[1])!=D||"tag"in b[1]||"subtree"in b[1])),d=c?b[1]:{},e=("class"in d?"class":"className"),f={tag:"div",attrs:{}},g=[];a=I.exec(b[0]);)if(""==a[1]&&a[2])f.tag=a[2];else if("#"==a[1])f.attrs.id=a[2];else if("."==a[1])g.push(a[2]);else if("["==a[3][0]){var h=J.exec(a[3]);f.attrs[h[1]]=h[3]||(h[2]?"":!0)}g.length>0&&(f.attrs[e]=g.join(" "));var i=c?b[2]:b[1];f.children=H.call(i)==E?i:b.slice(c?2:1);for(var j in d)f.attrs[j]=j==e?(f.attrs[j]||"")+" "+d[j]:d[j];return f}function e(a,d,h,k,l,m,n,o,p,q,r){if(null==l&&(l=""),"retain"===l.subtree)return m;var s=H.call(m),t=H.call(l);if(null==m||s!=t){if(null!=m)if(h&&h.nodes){var u=o-k,v=u+(t==E?l:m.nodes).length;g(h.nodes.slice(u,v),h.slice(u,v))}else m.nodes&&g(m.nodes,m);m=new l.constructor,m.tag&&(m={}),m.nodes=[]}if(t==E){l=j(l);for(var w=[],x=m.length===l.length,y=0,z=1,A=2,B=3,C={},I=[],J=!1,L=0;L\s*[^<]/g)||[]).length:H.call(U)==E?U.length:1,m[T++]=U)}if(!x){for(var L=0;L0?e(V,l.tag,c,c,l.children,m.children,!0,0,l.attrs.contenteditable?V:p,q,r):l.children,nodes:[V]},m.children&&!m.children.nodes&&(m.children.nodes=[]),"select"==l.tag&&l.attrs.value&&f(V,l.tag,{value:l.attrs.value},{},q),a.insertBefore(V,a.childNodes[o]||null)):(V=m.nodes[0],W.length&&f(V,l.tag,l.attrs,m.attrs,q),m.children=e(V,l.tag,c,c,l.children,m.children,!1,0,l.attrs.contenteditable?V:p,q,r),m.nodes.intact=!0,n===!0&&null!=V&&a.insertBefore(V,a.childNodes[o]||null)),typeof l.attrs.config==G){var Y=m.configContext=m.configContext||{},Z=function(a,b){return function(){return a.attrs.config.apply(a,b)}};r.push(Z(l,[V,!X,Y,m]))}}else if(typeof t!=G){var w;0===m.nodes.length?(l.$trusted?w=i(a,o,l):(w=[b.document.createTextNode(l)],a.nodeName.match(K)||a.insertBefore(w[0],a.childNodes[o]||null)),m="string number boolean".indexOf(typeof l)>-1?new l.constructor(l):l,m.nodes=w):m.valueOf()!==l.valueOf()||n===!0?(w=m.nodes,p&&p===b.document.activeElement||(l.$trusted?(g(w,m),w=i(a,o,l)):"textarea"===d?a.value=l:p?p.innerHTML=l:((1==w[0].nodeType||w.length>1)&&(g(m.nodes,m),w=[b.document.createTextNode(l)]),a.insertBefore(w[0],a.childNodes[o]||null),w[0].nodeValue=l)),m=new l.constructor(l),m.nodes=w):m.nodes.intact=!0}return m}function f(a,b,c,d,e){for(var f in c){var g=c[f],h=d[f];if(!(f in d)||h!==g){d[f]=g;try{if("config"===f)continue;if(typeof g==G&&0==f.indexOf("on"))a[f]=k(g,a);else if("style"===f&&null!=g&&H.call(g)==D){for(var i in g)(null==h||h[i]!==g[i])&&(a.style[i]=g[i]);for(var i in h)i in g||(a.style[i]="")}else null!=e?"href"===f?a.setAttributeNS("http://www.w3.org/1999/xlink","href",g):"className"===f?a.setAttribute("class",g):a.setAttribute(f,g):f in a&&"list"!=f&&"style"!=f&&"form"!=f?a[f]=g:a.setAttribute(f,g)}catch(j){if(j.message.indexOf("Invalid argument")<0)throw j}}}return d}function g(a,b){for(var c=a.length-1;c>-1;c--)a[c]&&a[c].parentNode&&(a[c].parentNode.removeChild(a[c]),b=[].concat(b),b[c]&&h(b[c]));0!=a.length&&(a.length=0)}function h(a){if(a.configContext&&typeof a.configContext.onunload==G&&a.configContext.onunload(),a.children)if(H.call(a.children)==E)for(var b=0;bb?M.push(a)-1:b}function m(a){var b=function(){return arguments.length&&(a=arguments[0]),a};return b.toJSON=function(){return a},b}function n(){for(var a=d.redraw.strategy(),b=0;b=200&&d.status<300?a.onload({type:"load",target:d}):a.onerror({type:"error",target:d}))},a.serialize==JSON.stringify&&a.data&&"GET"!=a.method&&d.setRequestHeader("Content-Type","application/json; charset=utf-8"),a.deserialize==JSON.parse&&d.setRequestHeader("Accept","application/json, text/*"),typeof a.config==G){var e=a.config(d,a);null!=e&&(d=e)}if(a.data&&H.call(a.data)!=F&&a.data.constructor!=b.FormData)throw"Request data should be either be a string or FormData. Check the `serialize` option in `m.request`";return d.send("GET"!=a.method&&a.data?a.data:""),d}var f="mithril_callback_"+(new Date).getTime()+"_"+Math.round(1e16*Math.random()).toString(36),g=b.document.createElement("script");b[f]=function(d){b.document.body.removeChild(g),a.onload({type:"load",target:{responseText:d}}),b[f]=c},g.onerror=function(){return b.document.body.removeChild(g),a.onerror({type:"error",target:{status:500,responseText:JSON.stringify({error:"Error making jsonp request"})}}),b[f]=c,!1},g.onload=function(){return!1},g.src=a.url+(a.url.indexOf("?")>0?"&":"?")+(a.callbackKey?a.callbackKey:"callback")+"="+f+"&"+s(a.data||{}),b.document.body.appendChild(g)}function A(a,b,c){if("GET"==a.method&&"jsonp"!=a.dataType){var d=a.url.indexOf("?")<0?"?":"&",e=s(b);a.url=a.url+(e?d+e:"")}else a.data=c(b);return a}function B(a,b){var c=a.match(/:[a-z]\w+/gi);if(c&&b)for(var d=0;dc&&(c=P.length);var e=!1;if(R[c]&&typeof R[c].onunload==G){var f={preventDefault:function(){e=!0}};R[c].onunload(f)}if(!e){d.redraw.strategy("all"),d.startComputation(),P[c]=a;var g=O=b,h=new b.controller;return g==O&&(R[c]=h,Q[c]=b),d.endComputation(),R[c]}},d.redraw=function(a){var c=b.cancelAnimationFrame||b.clearTimeout,d=b.requestAnimationFrame||b.setTimeout;S&&a!==!0?(new Date-T>V||d==b.requestAnimationFrame)&&(S>0&&c(S),S=d(n,V)):(n(),S=d(function(){S=null},V))},d.redraw.strategy=d.prop();var W=0;d.startComputation=function(){W++},d.endComputation=function(){W=Math.max(W-1,0),0==W&&d.redraw()},d.withAttr=function(a,b){return function(c){c=c||event;var d=c.currentTarget||this;b(a in d?d[a]:d.getAttribute(a))}};var X,Y={pathname:"",hash:"#",search:"?"},Z=function(){},$={};return d.route=function(){if(0===arguments.length)return X;if(3===arguments.length&&H.call(arguments[1])==F){var a=arguments[0],c=arguments[1],e=arguments[2];Z=function(b){var f=X=o(b);p(a,e,f)||d.route(c,!0)};var f="hash"==d.route.mode?"onhashchange":"onpopstate";b[f]=function(){X!=o(b.location[d.route.mode])&&Z(b.location[d.route.mode])},U=r,b[f]()}else if(arguments[0].addEventListener){{var g=arguments[0];arguments[1],arguments[2]}g.href=("pathname"!==d.route.mode?b.location.pathname:"")+Y[d.route.mode]+this.attrs.href,g.removeEventListener("click",q),g.addEventListener("click",q)}else if(H.call(arguments[0])==F){X=arguments[0];var h=null!=arguments[1]&&H.call(arguments[1])==D?s(arguments[1]):null;h&&(X+=(-1===X.indexOf("?")?"?":"&")+h);var i=(3==arguments.length?arguments[2]:arguments[1])===!0;b.history.pushState?(U=function(){b.history[i?"replaceState":"pushState"](null,b.document.title,Y[d.route.mode]+X),r()},Z(Y[d.route.mode]+X)):b.location[d.route.mode]=X}},d.route.param=function(a){return $[a]},d.route.mode="search",d.deferred=function(){var a=new x;return a.promise=w(a.promise),a},d.deferred.onerror=function(a){if("[object Error]"==H.call(a)&&!a.constructor.toString().match(/ Error/))throw a},d.sync=function(a){function b(a,b){return function(d){return g[a]=d,b||(c="reject"),0==--f&&(e.promise(g),e[c](g)),d}}var c="resolve",e=d.deferred(),f=a.length,g=new Array(f);if(a.length>0)for(var h=0;h0&&(f.attrs[e]=g.join(" "));var i=c?b[2]:b[1];f.children=L.call(i)==I?i:b.slice(c?2:1);for(var j in d)f.attrs[j]=j==e?(f.attrs[j]||"")+" "+d[j]:d[j];return f}function f(a,b,d,e,i,l,m,n,o,p,q){if(null==i&&(i=""),"retain"===i.subtree)return l;var r=L.call(l),s=L.call(i);if(null==l||r!=s){if(null!=l)if(d&&d.nodes){var t=n-e,u=t+(s==I?i:l.nodes).length;h(d.nodes.slice(t,u),d.slice(t,u))}else l.nodes&&h(l.nodes,l);l=new i.constructor,l.tag&&(l={}),l.nodes=[]}if(s==I){i=k(i);for(var v=[],w=l.length===i.length,x=0,y=1,z=2,A=3,B={},C=[],E=!1,F=0;F\s*[^<]/g)||[]).length:L.call(U)==I?U.length:1,l[T++]=U)}if(!w){for(var F=0;F0?f(V,i.tag,c,c,i.children,l.children,!0,0,i.attrs.contenteditable?V:o,p,q):i.children,nodes:[V]},l.children&&!l.children.nodes&&(l.children.nodes=[]),"select"==i.tag&&i.attrs.value&&g(V,i.tag,{value:i.attrs.value},{},p),a.insertBefore(V,a.childNodes[n]||null)):(V=l.nodes[0],W.length&&g(V,i.tag,i.attrs,l.attrs,p),l.children=f(V,i.tag,c,c,i.children,l.children,!1,0,i.attrs.contenteditable?V:o,p,q),l.nodes.intact=!0,m===!0&&null!=V&&a.insertBefore(V,a.childNodes[n]||null)),typeof i.attrs.config==K){var Y=l.configContext=l.configContext||{},Z=function(a,b){return function(){return a.attrs.config.apply(a,b)}};q.push(Z(i,[V,!X,Y,l]))}}else if(typeof s!=K){var v;0===l.nodes.length?(i.$trusted?v=j(a,n,i):(v=[D.createTextNode(i)],a.nodeName.match(O)||a.insertBefore(v[0],a.childNodes[n]||null)),l="string number boolean".indexOf(typeof i)>-1?new i.constructor(i):i,l.nodes=v):l.valueOf()!==i.valueOf()||m===!0?(v=l.nodes,o&&o===D.activeElement||(i.$trusted?(h(v,l),v=j(a,n,i)):"textarea"===b?a.value=i:o?o.innerHTML=i:((1==v[0].nodeType||v.length>1)&&(h(l.nodes,l),v=[D.createTextNode(i)]),a.insertBefore(v[0],a.childNodes[n]||null),v[0].nodeValue=i)),l=new i.constructor(i),l.nodes=v):l.nodes.intact=!0}return l}function g(a,b,c,d,e){for(var f in c){var g=c[f],h=d[f];if(!(f in d)||h!==g){d[f]=g;try{if("config"===f)continue;if(typeof g==K&&0==f.indexOf("on"))a[f]=l(g,a);else if("style"===f&&null!=g&&L.call(g)==H){for(var i in g)(null==h||h[i]!==g[i])&&(a.style[i]=g[i]);for(var i in h)i in g||(a.style[i]="")}else null!=e?"href"===f?a.setAttributeNS("http://www.w3.org/1999/xlink","href",g):"className"===f?a.setAttribute("class",g):a.setAttribute(f,g):f in a&&"list"!=f&&"style"!=f&&"form"!=f?a[f]=g:a.setAttribute(f,g)}catch(j){if(j.message.indexOf("Invalid argument")<0)throw j}}}return d}function h(a,b){for(var c=a.length-1;c>-1;c--)a[c]&&a[c].parentNode&&(a[c].parentNode.removeChild(a[c]),b=[].concat(b),b[c]&&i(b[c]));0!=a.length&&(a.length=0)}function i(a){if(a.configContext&&typeof a.configContext.onunload==K&&a.configContext.onunload(),a.children)if(L.call(a.children)==I)for(var b=0;bb?R.push(a)-1:b}function n(a){var b=function(){return arguments.length&&(a=arguments[0]),a};return b.toJSON=function(){return a},b}function o(){for(var a=e.redraw.strategy(),b=0;b=200&&d.status<300?a.onload({type:"load",target:d}):a.onerror({type:"error",target:d}))},a.serialize==JSON.stringify&&a.data&&"GET"!=a.method&&d.setRequestHeader("Content-Type","application/json; charset=utf-8"),a.deserialize==JSON.parse&&d.setRequestHeader("Accept","application/json, text/*"),typeof a.config==K){var e=a.config(d,a);null!=e&&(d=e)}if(a.data&&L.call(a.data)!=J&&a.data.constructor!=b.FormData)throw"Request data should be either be a string or FormData. Check the `serialize` option in `m.request`";return d.send("GET"!=a.method&&a.data?a.data:""),d}var f="mithril_callback_"+(new Date).getTime()+"_"+Math.round(1e16*Math.random()).toString(36),g=D.createElement("script");b[f]=function(d){D.body.removeChild(g),a.onload({type:"load",target:{responseText:d}}),b[f]=c},g.onerror=function(){return D.body.removeChild(g),a.onerror({type:"error",target:{status:500,responseText:JSON.stringify({error:"Error making jsonp request"})}}),b[f]=c,!1},g.onload=function(){return!1},g.src=a.url+(a.url.indexOf("?")>0?"&":"?")+(a.callbackKey?a.callbackKey:"callback")+"="+f+"&"+t(a.data||{}),D.body.appendChild(g)}function B(a,b,c){if("GET"==a.method&&"jsonp"!=a.dataType){var d=a.url.indexOf("?")<0?"?":"&",e=t(b);a.url=a.url+(e?d+e:"")}else a.data=c(b);return a}function C(a,b){var c=a.match(/:[a-z]\w+/gi);if(c&&b)for(var d=0;dc&&(c=U.length);var d=!1;if(W[c]&&typeof W[c].onunload==K){var f={preventDefault:function(){d=!0}};W[c].onunload(f)}if(!d){e.redraw.strategy("all"),e.startComputation(),U[c]=a;var g=T=b,h=new b.controller;return g==T&&(W[c]=h,V[c]=b),e.endComputation(),W[c]}},e.redraw=function(a){X&&a!==!0?(new Date-Y>$||F==b.requestAnimationFrame)&&(X>0&&G(X),X=F(o,$)):(o(),X=F(function(){X=null},$))},e.redraw.strategy=e.prop();var _=0;e.startComputation=function(){_++},e.endComputation=function(){_=Math.max(_-1,0),0==_&&e.redraw()},e.withAttr=function(a,b){return function(c){c=c||event;var d=c.currentTarget||this;b(a in d?d[a]:d.getAttribute(a))}};var ab,bb={pathname:"",hash:"#",search:"?"},cb=function(){},db={};return e.route=function(){if(0===arguments.length)return ab;if(3===arguments.length&&L.call(arguments[1])==J){var a=arguments[0],c=arguments[1],d=arguments[2];cb=function(b){var f=ab=p(b);q(a,d,f)||e.route(c,!0)};var f="hash"==e.route.mode?"onhashchange":"onpopstate";b[f]=function(){ab!=p(E[e.route.mode])&&cb(E[e.route.mode])},Z=s,b[f]()}else if(arguments[0].addEventListener){{var g=arguments[0];arguments[1],arguments[2]}g.href=("pathname"!==e.route.mode?E.pathname:"")+bb[e.route.mode]+this.attrs.href,g.removeEventListener("click",r),g.addEventListener("click",r)}else if(L.call(arguments[0])==J){ab=arguments[0];var h=null!=arguments[1]&&L.call(arguments[1])==H?t(arguments[1]):null;h&&(ab+=(-1===ab.indexOf("?")?"?":"&")+h);var i=(3==arguments.length?arguments[2]:arguments[1])===!0;b.history.pushState?(Z=function(){b.history[i?"replaceState":"pushState"](null,D.title,bb[e.route.mode]+ab),s()},cb(bb[e.route.mode]+ab)):E[e.route.mode]=ab}},e.route.param=function(a){return db[a]},e.route.mode="search",e.deferred=function(){var a=new y;return a.promise=x(a.promise),a},e.deferred.onerror=function(a){if("[object Error]"==L.call(a)&&!a.constructor.toString().match(/ Error/))throw a},e.sync=function(a){function b(a,b){return function(e){return g[a]=e,b||(c="reject"),0==--f&&(d.promise(g),d[c](g)),e}}var c="resolve",d=e.deferred(),f=a.length,g=new Array(f);if(a.length>0)for(var h=0;h