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