8 lines
No EOL
8.4 KiB
JavaScript
8 lines
No EOL
8.4 KiB
JavaScript
/*
|
|
Mithril v0.1.10
|
|
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,l){if(null===h||void 0===h)return void(i&&e(i.nodes));if("retain"!==h.subtree){var m=u.call(i),n=u.call(h);if(m!=n&&(null!==i&&void 0!==i&&e(i.nodes),i=new h.constructor,i.nodes=[]),"[object Array]"==n){for(var o=[],p=i.length===h.length,q=0,r=0,s=0;r<h.length;r++){var t=c(b,null,h[r],i[s],j,k+q||q,l);void 0!==t&&(t.nodes.intact||(p=!1),q+=t instanceof Array?t.length:1,i[s++]=t)}if(!p){for(var r=0;r<h.length;r++)void 0!==i[r]&&(o=o.concat(i[r].nodes));for(var v,r=o.length;v=i.nodes[r];r++)null!==v.parentNode&&v.parentNode.removeChild(v);for(var v,r=i.nodes.length;v=o[r];r++)null===v.parentNode&&b.appendChild(v);h.length<i.length&&(i.length=h.length),i.nodes=o}}else if("[object Object]"==n){if((h.tag!=i.tag||Object.keys(h.attrs).join()!=Object.keys(i.attrs).join()||h.attrs.id!=i.attrs.id)&&e(i.nodes),"string"!=typeof h.tag)return;var v,w=0===i.nodes.length;"svg"===h.tag&&(l="http://www.w3.org/2000/svg"),w?(v=void 0===l?a.document.createElement(h.tag):a.document.createElementNS(l,h.tag),i={tag:h.tag,attrs:d(v,h.tag,h.attrs,{},l),children:c(v,h.tag,h.children,i.children,!0,0,l),nodes:[v]},b.insertBefore(v,b.childNodes[k]||null)):(v=i.nodes[0],d(v,h.tag,h.attrs,i.attrs,l),i.children=c(v,h.tag,h.children,i.children,!1,0,l),i.nodes.intact=!0,j===!0&&b.insertBefore(v,b.childNodes[k]||null)),"[object Function]"==u.call(h.attrs.config)&&h.attrs.config(v,!w)}else{var v;if(0===i.nodes.length)h.$trusted?v=f(b,k,h):(v=a.document.createTextNode(h),b.insertBefore(v,b.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(h.$trusted){var x=i.nodes[0],o=[x];if(x){for(;x=x.nextSibling;)o.push(x);e(o),v=f(b,k,h)}else b.innerHTML=h}else v=i.nodes[0],"textarea"===g?b.value=h:b.insertBefore(v,b.childNodes[k]||null),v.nodeValue=h;i=new h.constructor(h),i.nodes=[v]}else i.nodes.intact=!0}return i}}function d(b,c,d,e,f){for(var g in d){var i=d[g],j=e[g];if(!(g in e)||j!==i||b===a.document.activeElement){if(e[g]=i,"config"===g)continue;if("function"==typeof i&&0==g.indexOf("on"))b[g]=h(i,b);else if("style"===g)for(var k in i)(void 0===j||j[k]!==i[k])&&(b.style[k]=i[k]);else void 0!==f?"href"===g?b.setAttributeNS("http://www.w3.org/1999/xlink","href",i):"className"===g?b.setAttribute("class",i):b.setAttribute(g,i):"value"===g&&"input"===c?b.value!==i&&(b.value=i):g in b?b[g]=i:b.setAttribute(g,i)}}return e}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,null,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
|