forked from MithrilJS/mithril.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mithril.min.js
8 lines (8 loc) · 14.5 KB
/
mithril.min.js
1
2
3
4
5
6
7
8
/*
Mithril v0.1.24
http://github.com/lhorie/mithril.js
(c) Leo Horie
License: MIT
*/
Mithril=m=new function a(b,c){function d(a){D=a.document,E=a.location,G=a.cancelAnimationFrame||a.clearTimeout,F=a.requestAnimationFrame||a.setTimeout}function e(){for(var a,b=[].slice.call(arguments),c=(!(null==b[1]||L.call(b[1])!=H||"tag"in b[1]||"subtree"in b[1])),d=c?b[1]:{},e=("class"in d?"class":"className"),f={tag:"div",attrs:{}},g=[];a=M.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=N.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=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<l.length;F++)l[F]&&l[F].attrs&&null!=l[F].attrs.key&&(E=!0,B[l[F].attrs.key]={action:y,index:F});if(E){for(var F=0;F<i.length;F++)if(i[F]&&i[F].attrs)if(null!=i[F].attrs.key){var G=i[F].attrs.key;B[G]=B[G]?{action:A,index:F,from:B[G].index,element:a.childNodes[B[G].index]||D.createElement("div")}:{action:z,index:F}}else C.push({index:F,element:a.childNodes[F]||D.createElement("div")});for(var M,N=Object.keys(B).map(function(a){return B[a]}),P=N.sort(function(a,b){return a.action-b.action||a.index-b.index}),Q=l.slice(),F=0;M=P[F];F++){if(M.action==y&&(h(l[M.index].nodes,l[M.index]),Q.splice(M.index,1)),M.action==z){var R=D.createElement("div");R.key=i[M.index].attrs.key,a.insertBefore(R,a.childNodes[M.index]||null),Q.splice(M.index,0,{attrs:{key:i[M.index].attrs.key},nodes:[R]})}M.action==A&&(a.childNodes[M.index]!==M.element&&null!==M.element&&a.insertBefore(M.element,a.childNodes[M.index]||null),Q[M.index]=l[M.from])}for(var F=0;F<C.length;F++){var M=C[F];a.insertBefore(M.element,a.childNodes[M.index]||null),Q[M.index]=l[M.index]}l=Q,l.nodes=[];for(var S,F=0;S=a.childNodes[F];F++)l.nodes.push(S)}for(var F=0,T=0;F<i.length;F++){var U=f(a,b,l,n,i[F],l[T],m,n+x||x,o,p,q);U!==c&&(U.nodes.intact||(w=!1),x+=U.$trusted?(U.match(/<[^\/]|\>\s*[^<]/g)||[]).length:L.call(U)==I?U.length:1,l[T++]=U)}if(!w){for(var F=0;F<i.length;F++)null!=l[F]&&(v=v.concat(l[F].nodes));for(var V,F=0;V=l.nodes[F];F++)null!=V.parentNode&&v.indexOf(V)<0&&h([V],[l[F]]);for(var V,F=l.nodes.length;V=v[F];F++)null==V.parentNode&&a.appendChild(V);i.length<l.length&&(l.length=i.length),l.nodes=v}}else if(null!=i&&s==H){i.attrs||(i.attrs={}),l.attrs||(l.attrs={});var W=Object.keys(i.attrs);if((i.tag!=l.tag||W.join()!=Object.keys(l.attrs).join()||i.attrs.id!=l.attrs.id)&&(l.nodes.length&&h(l.nodes),l.configContext&&typeof l.configContext.onunload==K&&l.configContext.onunload()),L.call(i.tag)!=J)return;var V,X=0===l.nodes.length;if(i.attrs.xmlns?p=i.attrs.xmlns:"svg"===i.tag?p="http://www.w3.org/2000/svg":"math"===i.tag&&(p="http://www.w3.org/1998/Math/MathML"),X?(V=i.attrs.is?p===c?D.createElement(i.tag,i.attrs.is):D.createElementNS(p,i.tag,i.attrs.is):p===c?D.createElement(i.tag):D.createElementNS(p,i.tag),l={tag:i.tag,attrs:W.length?g(V,i.tag,i.attrs,{},p):{},children:null!=i.children&&i.children.length>0?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)"value"===f&&"input"===b&&a.value!==g&&(a.value=g);else{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;b<a.children.length;b++)i(a.children[b]);else a.children.tag&&i(a.children)}function j(a,b,c){var d=a.childNodes[b];if(d){var e=1!=d.nodeType,f=D.createElement("span");e?(a.insertBefore(f,d||null),f.insertAdjacentHTML("beforebegin",c),a.removeChild(f)):d.insertAdjacentHTML("beforebegin",c)}else a.insertAdjacentHTML("beforeend",c);for(var g=[];a.childNodes[b]!==d;)g.push(a.childNodes[b]),b++;return g}function k(a){var b=0;a:for(;;){for(var c=b;c<a.length;c++){{a[c]}if(L.call(a[c])==I){b=c,a=a.concat.apply([],a);continue a}}break}return a}function l(a,b){return function(c){c=c||event,e.redraw.strategy("diff"),e.startComputation();try{return a.call(b,c)}finally{e.endComputation()}}}function m(a){var b=R.indexOf(a);return 0>b?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<U.length;b++)W[b]&&"none"!=a&&e.render(U[b],V[b].view(W[b]),"all"==a);Z&&(Z(),Z=null),X=null,Y=new Date,e.redraw.strategy("diff")}function p(a){return a.slice(bb[e.route.mode].length)}function q(a,b,c){db={};var d=c.indexOf("?");-1!==d&&(db=u(c.substr(d+1,c.length)),c=c.substr(0,d));for(var f in b){if(f==c)return e.module(a,b[f]),!0;var g=new RegExp("^"+f.replace(/:[^\/]+?\.{3}/g,"(.*?)").replace(/:[^\/]+/g,"([^\\/]+)")+"/?$");if(g.test(c))return c.replace(g,function(){for(var c=f.match(/:[^\/]+/g)||[],d=[].slice.call(arguments,1,-2),g=0;g<c.length;g++)db[c[g].replace(/:|\./g,"")]=decodeURIComponent(d[g]);e.module(a,b[f])}),!0}}function r(a){if(a=a||event,!a.ctrlKey&&!a.metaKey&&2!=a.which){a.preventDefault?a.preventDefault():a.returnValue=!1;var b=a.currentTarget||this,c="pathname"==e.route.mode&&b.search?u(b.search.slice(1)):{};e.route(b[e.route.mode].slice(bb[e.route.mode].length),c)}}function s(){"hash"!=e.route.mode&&E.hash?E.hash=E.hash:b.scrollTo(0,0)}function t(a,b){var c=[];for(var d in a){var e=b?b+"["+d+"]":d,f=a[d];c.push(null!=f&&L.call(f)==H?t(f,e):encodeURIComponent(e)+"="+encodeURIComponent(f))}return c.join("&")}function u(a){for(var b=a.split("&"),c={},d=0;d<b.length;d++){var e=b[d].split("=");c[v(e[0])]=e[1]?v(e[1]):1===e.length?!0:""}return c}function v(a){return decodeURIComponent(a.replace(/\+/g," "))}function w(a){var b=m(a);h(a.childNodes,S[b]),S[b]=c}function x(a){var b=e.prop();return a.then(b),b.then=function(b,c){return x(a.then(b,c))},b}function y(a,b){function c(a){l=a||j,n.map(function(a){l==i&&a.resolve(m)||a.reject(m)})}function d(a,b,c,d){if((null!=m&&L.call(m)==H||typeof m==K)&&typeof a==K)try{var f=0;a.call(m,function(a){f++||(m=a,b())},function(a){f++||(m=a,c())})}catch(g){e.deferred.onerror(g),m=g,c()}else d()}function f(){var j;try{j=m&&m.then}catch(n){return e.deferred.onerror(n),m=n,l=h,f()}d(j,function(){l=g,f()},function(){l=h,f()},function(){try{l==g&&typeof a==K?m=a(m):l==h&&"function"==typeof b&&(m=b(m),l=g)}catch(f){return e.deferred.onerror(f),m=f,c()}m==k?(m=TypeError(),c()):d(j,function(){c(i)},c,function(){c(l==g&&i)})})}var g=1,h=2,i=3,j=4,k=this,l=0,m=0,n=[];k.promise={},k.resolve=function(a){return l||(m=a,l=g,f()),this},k.reject=function(a){return l||(m=a,l=h,f()),this},k.promise.then=function(a,b){var c=new y(a,b);return l==i?c.resolve(m):l==j?c.reject(m):n.push(c),c.promise}}function z(a){return a}function A(a){if(!a.dataType||"jsonp"!==a.dataType.toLowerCase()){var d=new b.XMLHttpRequest;if(d.open(a.method,a.url,!0,a.user,a.password),d.onreadystatechange=function(){4===d.readyState&&(d.status>=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)}var f="GET"!=a.method&&a.data?a.data:"";if(f&&L.call(f)!=J&&f.constructor!=b.FormData)throw"Request data should be either be a string or FormData. Check the `serialize` option in `m.request`";return d.send(f),d}var g="mithril_callback_"+(new Date).getTime()+"_"+Math.round(1e16*Math.random()).toString(36),h=D.createElement("script");b[g]=function(d){D.body.removeChild(h),a.onload({type:"load",target:{responseText:d}}),b[g]=c},h.onerror=function(){return D.body.removeChild(h),a.onerror({type:"error",target:{status:500,responseText:JSON.stringify({error:"Error making jsonp request"})}}),b[g]=c,!1},h.onload=function(){return!1},h.src=a.url+(a.url.indexOf("?")>0?"&":"?")+(a.callbackKey?a.callbackKey:"callback")+"="+g+"&"+t(a.data||{}),D.body.appendChild(h)}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;d<c.length;d++){var e=c[d].slice(1);a=a.replace(c[d],b[e]),delete b[e]}return a}var D,E,F,G,H="[object Object]",I="[object Array]",J="[object String]",K="function",L={}.toString,M=/(?:(^|#|\.)([^#\.\[\]]+))|(\[.+?\])/g,N=/\[(.+?)(?:=("|'|)(.*?)\2)?\]/,O=/AREA|BASE|BR|COL|COMMAND|EMBED|HR|IMG|INPUT|KEYGEN|LINK|META|PARAM|SOURCE|TRACK|WBR/;d(b);var P,Q={appendChild:function(a){P===c&&(P=D.createElement("html")),D.documentElement&&D.documentElement!==a?D.replaceChild(a,D.documentElement):D.appendChild(a),this.childNodes=D.childNodes},insertBefore:function(a){this.appendChild(a)},childNodes:[]},R=[],S={};e.render=function(a,b,d){var e=[];if(!a)throw new Error("Please ensure the DOM element exists before rendering a template into it.");var g=m(a),i=a==D,j=i||a==D.documentElement?Q:a;i&&"html"!=b.tag&&(b={tag:"html",attrs:{},children:b}),S[g]===c&&h(j.childNodes),d===!0&&w(a),S[g]=f(j,null,c,c,b,S[g],!1,0,null,c,e);for(var k=0;k<e.length;k++)e[k]()},e.trust=function(a){return a=new String(a),a.$trusted=!0,a},e.prop=function(a){return(null!=a&&L.call(a)==H||typeof a==K)&&typeof a.then==K?x(a):n(a)};var T,U=[],V=[],W=[],X=null,Y=0,Z=null,$=16;e.module=function(a,b){var c=U.indexOf(a);0>c&&(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<a.length;h++)a[h].then(b(h,!0),b(h,!1));else d.resolve([]);return d.promise},e.request=function(a){a.background!==!0&&e.startComputation();var b=e.deferred(),c=a.dataType&&"jsonp"===a.dataType.toLowerCase(),d=a.serialize=c?z:a.serialize||JSON.stringify,f=a.deserialize=c?z:a.deserialize||JSON.parse,g=a.extract||function(a){return 0===a.responseText.length&&f===JSON.parse?null:a.responseText};return a.url=C(a.url,a.data),a=B(a,a.data,d),a.onload=a.onerror=function(c){try{c=c||event;var d=("load"==c.type?a.unwrapSuccess:a.unwrapError)||z,h=d(f(g(c.target,a)));if("load"==c.type)if(L.call(h)==I&&a.type)for(var i=0;i<h.length;i++)h[i]=new a.type(h[i]);else a.type&&(h=new a.type(h));b["load"==c.type?"resolve":"reject"](h)}catch(c){e.deferred.onerror(c),b.reject(c)}a.background!==!0&&e.endComputation()},A(a),b.promise(a.initialValue),b.promise},e.deps=function(a){return d(b=a||b),b},e.deps.factory=a,e}("undefined"!=typeof window?window:{}),"undefined"!=typeof module&&null!==module&&(module.exports=m),"function"==typeof define&&define.amd&&define(function(){return m});
//# sourceMappingURL=mithril.min.js.map