UNPKG

mithril

Version:

A framework for building brilliant applications

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