mithril
Version:
A framework for building brilliant applications
8 lines • 10.1 kB
JavaScript
/*
Mithril v0.1.14
http://github.com/lhorie/mithril.js
(c) Leo Horie
License: MIT
*/
Mithril=m=new function a(b){function c(){var a=arguments,b=!("[object Object]"!=w.call(a[1])||"tag"in a[1]||"subtree"in a[1]),c=b?a[1]:{},d="class"in c?"class":"className",e=v[a[0]];if(void 0===e){v[a[0]]=e={tag:"div",attrs:{}};for(var f,g=[];f=x.exec(a[0]);)if(""==f[1])e.tag=f[2];else if("#"==f[1])e.attrs.id=f[2];else if("."==f[1])g.push(f[2]);else if("["==f[3][0]){var i=y.exec(f[3]);e.attrs[i[1]]=i[3]||!0}g.length>0&&(e.attrs[d]=g.join(" "))}e=h(e),e.attrs=h(e.attrs),e.children=b?a[2]:a[1];for(var j in c)e.attrs[j]=j==d?(e.attrs[j]||"")+" "+c[j]:c[j];return e}function d(a,c,h,i,j,k,l,m){if((null===h||void 0===h)&&(h=""),"retain"!==h.subtree){var n=w.call(i),o=w.call(h);if(n!=o&&(null!==i&&void 0!==i&&f(i.nodes),i=new h.constructor,i.nodes=[]),"[object Array]"==o){for(var p=[],q=i.length===h.length,r=0,s=0,t=0;s<h.length;s++){var u=d(a,null,h[s],i[t],j,k+r||r,l,m);void 0!==u&&(u.nodes.intact||(q=!1),r+=u instanceof Array?u.length:1,i[t++]=u)}if(!q){p.length<i.nodes.length&&(i.nodes=i.nodes.filter(function(a){return null!==a.parentNode}));for(var s=0;s<h.length;s++)void 0!==i[s]&&(p=p.concat(i[s].nodes));for(var v,s=p.length;v=i.nodes[s];s++)null!==v.parentNode&&v.parentNode.removeChild(v);for(var v,s=i.nodes.length;v=p[s];s++)null===v.parentNode&&a.appendChild(v);h.length<i.length&&(i.length=h.length),i.nodes=p}}else if("[object Object]"==o){if((h.tag!=i.tag||Object.keys(h.attrs).join()!=Object.keys(i.attrs).join()||h.attrs.id!=i.attrs.id)&&f(i.nodes),"string"!=typeof h.tag)return;var v,x=0===i.nodes.length;h.attrs.xmlns?m=h.attrs.xmlns:"svg"===h.tag&&(m="http://www.w3.org/2000/svg"),x?(v=void 0===m?b.document.createElement(h.tag):b.document.createElementNS(m,h.tag),i={tag:h.tag,attrs:e(v,h.tag,h.attrs,{},m),children:d(v,h.tag,h.children,i.children,!0,0,h.attrs.contenteditable?v:l,m),nodes:[v]},a.insertBefore(v,a.childNodes[k]||null)):(v=i.nodes[0],e(v,h.tag,h.attrs,i.attrs,m),i.children=d(v,h.tag,h.children,i.children,!1,0,h.attrs.contenteditable?v:l,m),i.nodes.intact=!0,j===!0&&a.insertBefore(v,a.childNodes[k]||null)),"[object Function]"==w.call(h.attrs.config)&&h.attrs.config(v,!x)}else{var v;if(0===i.nodes.length)h.$trusted?v=g(a,k,h):(v=b.document.createTextNode(h),a.insertBefore(v,a.childNodes[k]||null)),i="string number boolean".indexOf(typeof h)>-1?new h.constructor(h):h,i.nodes=[v];else if(i.valueOf()!==h.valueOf()||j===!0){if(!l||l!==b.document.activeElement)if(h.$trusted){var y=i.nodes[0],p=[y];if(y){for(;y=y.nextSibling;)p.push(y);f(p),v=g(a,k,h)}else a.innerHTML=h}else v=i.nodes[0],"textarea"===c?a.value=h:l?l.innerHTML=h:(a.insertBefore(v,a.childNodes[k]||null),v.nodeValue=h);i=new h.constructor(h),i.nodes=[v]}else i.nodes.intact=!0}return i}}function e(a,c,d,e,f){for(var g in d){var h=d[g],j=e[g];if(!(g in e)||j!==h||a===b.document.activeElement){if(e[g]=h,"config"===g)continue;if("function"==typeof h&&0==g.indexOf("on"))a[g]=i(h,a);else if("style"===g&&"object"==typeof h){for(var k in h)(void 0===j||j[k]!==h[k])&&(a.style[k]=h[k]);for(var k in j)k in h||(a.style[k]="")}else void 0!==f?"href"===g?a.setAttributeNS("http://www.w3.org/1999/xlink","href",h):"className"===g?a.setAttribute("class",h):a.setAttribute(g,h):"value"===g&&"input"===c?a.value!==h&&(a.value=h):g in a&&"list"!=g&&"style"!=g?a[g]=h:a.setAttribute(g,h)}}return e}function f(a){for(var b=a.length-1;b>-1;b--)a[b]&&a[b].parentNode.removeChild(a[b]);a.length=0}function g(a,b,c){var d=a.childNodes[b];return d?d.insertAdjacentHTML("beforebegin",c):a.insertAdjacentHTML("beforeend",c),d?d.previousSibling:a.firstChild}function h(a){var b={};for(var c in a)b[c]=a[c];return b}function i(a,b){return function(d){d=d||event,c.startComputation();try{return a.call(b,d)}finally{c.endComputation()}}}function j(){for(var a=0;a<C.length;a++)c.render(C[a],D[a].view(E[a]));I&&(I(),I=null),G=F}function k(a,b,d){N={};var e=d.indexOf("?");-1!==e&&(N=o(d.substr(e+1,d.length)),d=d.substr(0,e));for(var f in b){if(f==d)return!void c.module(a,b[f]);var g=new RegExp("^"+f.replace(/:[^\/]+?\.{3}/g,"(.*?)").replace(/:[^\/]+/g,"([^\\/]+)")+"/?$");if(g.test(d))return!void d.replace(g,function(){for(var d=f.match(/:[^\/]+/g)||[],e=[].slice.call(arguments,1,-2),g=0;g<d.length;g++)N[d[g].replace(/:|\./g,"")]=p(e[g]);c.module(a,b[f])})}}function l(a){a=a||event,a.ctrlKey||a.metaKey||2==a.which||(a.preventDefault(),c.route(a.currentTarget[c.route.mode].slice(L[c.route.mode].length)))}function m(){"hash"!=c.route.mode&&b.location.hash&&(b.location.hash=b.location.hash)}function n(a,b){var c=[];for(var d in a){var e=b?b+"["+d+"]":d,f=a[d];c.push("object"==typeof f?n(f,e):encodeURIComponent(e)+"="+encodeURIComponent(f))}return c.join("&")}function o(a){for(var b=a.split("&"),c={},d=0;d<b.length;d++){var e=b[d].split("=");c[p(e[0])]=e[1]?p(e[1]):1===e.length?!0:""}return c}function p(a){return decodeURIComponent(a.replace(/\+/g," "))}function q(a){return a}function r(a){var c=b.XDomainRequest?new b.XDomainRequest:new b.XMLHttpRequest;return c.open(a.method,a.url,!0,a.user,a.password),c.onreadystatechange=function(){4===c.readyState&&(c.status>=200&&c.status<300?a.onload({type:"load",target:c}):a.onerror({type:"error",target:c}))},"function"==typeof a.config&&a.config(c,a),c.send(a.data),c}function s(a,b,c){return b&&Object.keys(b).length>0&&("GET"==a.method?a.url=a.url+(a.url.indexOf("?")<0?"?":"&")+n(b):a.data=c(b)),a}function t(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 u,v={},w={}.toString,x=/(?:(^|#|\.)([^#\.\[\]]+))|(\[.+?\])/g,y=/\[(.+?)(?:=("|'|)(.+?)\2)?\]/,z={insertAdjacentHTML:function(a,c){b.document.write(c),b.document.close()},appendChild:function(a){void 0===u&&(u=b.document.createElement("html")),"HTML"==a.nodeName?u=a:u.appendChild(a),b.document.documentElement!==u&&b.document.replaceChild(u,b.document.documentElement)},insertBefore:function(a){this.appendChild(a)},childNodes:[]},A=[],B={};c.render=function(a,c){if(!a)throw new Error("Please ensure the DOM element exists before rendering a template into it.");var e=A.indexOf(a),f=0>e?A.push(a)-1:e,g=a==b.document||a==b.document.documentElement?z:a;B[f]=d(g,null,c,B[f],!1,0,null,void 0)},c.trust=function(a){return a=new String(a),a.$trusted=!0,a};var C=[],D=[],E=[],F=0,G=0,H=0,I=null;c.module=function(a,b){c.startComputation();var d=C.indexOf(a);0>d&&(d=C.length),C[d]=a,D[d]=b,E[d]&&"function"==typeof E[d].onunload&&E[d].onunload(),E[d]=new b.controller,c.endComputation()},c.redraw=function(){if(F=b.performance&&b.performance.now?b.performance.now():(new b.Date).getTime(),F-G>16)j();else{var a=b.cancelAnimationFrame||b.clearTimeout,c=b.requestAnimationFrame||b.setTimeout;a(H),H=c(j,0)}};var J=0;c.startComputation=function(){J++},c.endComputation=function(){J=Math.max(J-1,0),0==J&&c.redraw()},c.withAttr=function(a,b){return function(c){c=c||event,b(a in c.currentTarget?c.currentTarget[a]:c.currentTarget.getAttribute(a))}};var K,L={pathname:"",hash:"#",search:"?"},M=function(){},N={};c.route=function(){if(0===arguments.length)return K;if(3===arguments.length){K=b.location[c.route.mode].slice(L[c.route.mode].length);var a=arguments[0],d=arguments[1],e=arguments[2];M=function(b){var f=K=b.slice(L[c.route.mode].length);k(a,e,f)||c.route(d,!0)};var f="hash"==c.route.mode?"onhashchange":"onpopstate";b[f]=function(){M(b.location[c.route.mode])},I=m,b[f]()}else if(arguments[0].addEventListener){var g=arguments[0],h=arguments[1];g.href.indexOf(L[c.route.mode])<0&&(g.href=location.pathname+L[c.route.mode]+g.pathname),h||(g.removeEventListener("click",l),g.addEventListener("click",l))}else if("string"==typeof arguments[0]){K=arguments[0];var i="object"==typeof arguments[1]?n(arguments[1]):null;i&&(K+=(-1===K.indexOf("?")?"?":"&")+i);var j=(3==arguments.length?arguments[2]:arguments[1])===!0;b.history.pushState?(I=function(){b.history[j?"replaceState":"pushState"](null,b.document.title,L[c.route.mode]+K),m()},M(L[c.route.mode]+K)):b.location[c.route.mode]=K}},c.route.param=function(a){return N[a]},c.route.mode="search",c.prop=function(a){var b=function(){return arguments.length&&(a=arguments[0]),a};return b.toJSON=function(){return a},b};var O={};return c.deferred=function(){var a=[],b=[],d=O,e=O,f=c.prop(),g={resolve:function(c){d===O&&f(d=c);for(var e=0;e<a.length;e++)a[e](c);a.length=b.length=0},reject:function(c){e===O&&(e=c);for(var d=0;d<b.length;d++)b[d](c);a.length=b.length=0},promise:f};return g.promise.resolvers=a,g.promise.then=function(f,g){function h(a,b){return function(c){try{var d=b(c);d&&"function"==typeof d.then?d.then(i[a],g):i[a](void 0!==d?d:c)}catch(e){if(e instanceof Error&&e.constructor!==Error)throw e;i.reject(e)}}}var i=c.deferred();return f||(f=q),g||(g=q),d!==O?h("resolve",f)(d):e!==O?h("reject",g)(e):(a.push(h("resolve",f)),b.push(h("reject",g))),i.promise},g},c.sync=function(a){function b(b){return function(c){return f.push(c),b||(d="reject"),f.length==a.length&&(e.promise(f),e[d](f)),c}}for(var d="resolve",e=c.deferred(),f=[],g=0;g<a.length;g++)a[g].then(b(!0),b(!1));return e.promise},c.request=function(a){a.background!==!0&&c.startComputation();var b=c.deferred(),d=a.serialize||JSON.stringify,e=a.deserialize||JSON.parse,f=a.extract||function(a){return 0===a.responseText.length&&e===JSON.parse?null:a.responseText};return a.url=t(a.url,a.data),a=s(a,a.data,d),a.onload=a.onerror=function(d){try{d=d||event;var g=("load"==d.type?a.unwrapSuccess:a.unwrapError)||q,h=g(e(f(d.target,a)));if(h instanceof Array&&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"==d.type?"resolve":"reject"](h)}catch(d){if(d instanceof Error&&d.constructor!==Error)throw d;b.reject(d)}a.background!==!0&&c.endComputation()},r(a),b.promise},c.deps=function(a){return b=a},c.deps.factory=a,c}(this),"undefined"!=typeof module&&null!==module&&(module.exports=m),"function"==typeof define&&define.amd&&define(function(){return m});
//# sourceMappingURL=mithril.min.map