-
Notifications
You must be signed in to change notification settings - Fork 11
/
mag.min.js
7 lines (7 loc) · 11.9 KB
/
mag.min.js
1
2
3
4
5
6
7
/*
Mag.JS v0.15.3
http://github.com/magnumjs/mag.js
(c) Michael Glazer
License: MIT
*/
!function(e,n,t,r){function o(e){return"[object Array]"===Object.prototype.toString.call(e)}function i(e){if(""!==e.id)return'id("'+e.id+'")';if(e===t.body)return e.tagName;var n=0;if(e.parentNode)for(var r=e.parentNode.childNodes,o=0;o<r.length;o++){var a=r[o];if(a===e)return i(e.parentNode)+"/"+e.tagName+"["+(n+1)+"]";1===a.nodeType&&a.tagName===e.tagName&&n++}}function a(e){var n=i(e);e.parentNode.removeChild(e),_[n+"-config"]&&_[n+"-config"].configContext&&typeof _[n+"-config"].configContext.onunload===N&&_[n+"-config"].configContext.onunload(_[n+"-config"].configContext,e,n)}function l(e){return(e=e&&parseInt(e.split("[").pop().slice(0,-1)))?parseInt(e)-1:0}function f(e,n,t){var l,u,s;if(e){null==n&&(n={_text:""});var h=d(e);if(s=o(n)){if(x[t]&&0===h.length&&(x[t].parent.insertAdjacentHTML("beforeend",x[t].node),h=d(x[t].parent.children)),0===h.length)return r;for(u=h[0].parentNode,x[t]||(x[t]={node:h[0].cloneNode(!0).outerHTML,parent:u});h.length<n.length;)x[t]?(u.insertAdjacentHTML("beforeend",x[t].node),l=u.lastChild):l=h[0].cloneNode(!0),h.push(l),u&&u.appendChild(l);if(h.map(function(e){return e.__key}),t=n.map(function(e){return e[w]}),(h.length==n.length||-1!==t.indexOf(r))&&(n=n.map(function(e,n){if("object"==typeof e){if(h[n].__key&&typeof e[w]===C)return e[w]=h[n].__key,e;typeof e[w]===C&&(e[w]=b+n),h[n].__key=e[w]}return e})),h.length>n.length)if(0===n.length||"object"!=typeof n[0])for(;h.length>n.length;)l=h.pop(),(u=l.parentNode)&&a(l);else var p=[],v=n.map(function(e){return e[w]}),h=(h.map(function(e){return e.__key}),h.filter(function(e){return-1===v.indexOf(e.__key)||-1!==p.indexOf(e.__key)?(p.push(e.__key),a(e),!1):(p.push(e.__key),!0)}))}for(t=0;t<h.length;t++)i(h[t]),s?h[t]&&f(h[t],n[t]):(n&&"object"==typeof n&&-1!==Object.keys(n).indexOf(w)&&(h[t][b]=h[t][b]||{},h[t][b].isChildOfArray=!0,h[t][b].dataPass=n),c(h[t],n));return e}}function u(e){return e.parentNode&&e.parentNode[b]&&e.parentNode[b].isChildOfArray?e.parentNode:e.parentNode?u(e.parentNode):r}function c(e,n){var t,l;if(typeof n!==N||typeof n()._html===N){if("object"!=typeof n&&typeof n===N&&typeof n()._html===N)return c(e,{_html:n()._html});if("object"!=typeof n)return c(e,{_text:n});for(var u in n){var d=n[u];d===r?d="":null===d&&-1===["onunload"].indexOf(u)&&g(e,u).forEach(function(e){a(e)}),"_"===u[0]&&(t=t||{},t[u.substr(1)]=d)}var h=i(e);t&&s(e,t),t=0;var p=["willupdate","didupdate","didload","willload","isupdate"];for(u in n)-1===p.indexOf(u)&&(d=n[u],"_"!==u[0]&&(l=g(e,u),typeof d===N&&"fun"==d.type&&(d=d()),o(d)&&(l=g(e,"$"+u)),f(l,d,h+"/"+u),t++))}}function d(e){var n;if(null==e.length&&(e=[e]),!o(e)){n=[];for(var t=0;t<e.length;t+=1)e[t]&&n.push(e[t]);e=n}return e}function s(t,r){var o,a=i(t),f=l(a);for(o in r)if("text"!==o&&"html"!==o)if(0==o.indexOf("on")){var c=function(n,t,r){var o=u(t),a=o&&i(o),f=l(a),c=i(t),c=l(c);return n=n.call(t,r,c,t,{path:a,data:((o||{})[b]||[]).dataPass,node:o,index:f}),e.redraw(),n}.bind({},r[o],t);t[o]=c}else if("config"==o){c=!0,_[a+"-config"]?c=!1:_[a+"-config"]={};var d=_[a+"-config"].configContext=_[a+"-config"].configContext||{};n.push(function(e,n){return function(){return e.apply(e,n)}}(r[o],[t,c,d,f]))}else c={key:o,value:r[o],node:t},e.hook("attributes",o,c),c.change&&(o=c.key,r[o]=c.value),null===r[o]?t.removeAttribute(o):t.setAttribute(o,""+r[o]);h(t,r.text),v(t,r.html,f)}function h(e,n){var t,r;if(e&&null!=n){if(!r){r=[];for(var o=0;o<e.childNodes.length;o+=1)t=e.childNodes[o],t.nodeType===y&&r.push(t)}for(;e.firstChild;)e.removeChild(e.firstChild);for("INPUT"===e.nodeName?e.setAttribute("value",""+n):e.appendChild(e.ownerDocument.createTextNode(""+n)),o=0;o<r.length;o+=1)e.appendChild(r[o]);"SELECT"===e.nodeName&&(e.value=""+n)}}function p(e,n){if(e.cloner){var t,r=b+e.id.split(b).pop();t=e.id,t=-1!==t.indexOf(n,t.length-n.length),e.id=r+(t?"":n)}}function v(e,n,t){e&&null!=n&&(typeof n===N&&1===n().nodeType?(n=n(),p(n,t),e.firstChild?e.replaceChild(n,e.firstChild):e.appendChild(n)):1===n.nodeType?(p(n,t),e.firstChild?e.replaceChild(n,e.firstChild):e.appendChild(n)):e.innerHTML=n)}function g(n,t,r){var o=[];if(n){var i=n.childNodes;if(i)for(var a=0;a<i.length;a+=1)i[a].nodeType===y&&o.push(i[a])}var i=[],a=t,l="$"===t[0];"$"===a[0]&&(a=a.substr(1));for(var f=0;f<o.length;f+=1){var u=o[f],c=a,d=" "+u.className+" ";(u.id===c||-1<d.indexOf(" "+c+" ")||u.name===c||u.nodeName.toLowerCase()===c.toLowerCase()||u.getAttribute("data-bind")===c)&&i.push(o[f])}if(!i.length||l)for(f=0;f<o.length&&(i=i.concat(g(o[f],t,!0)),!i.length||l);f++);return r||i.length||(n={key:t,value:i,node:n},e.hook("elementMatcher",t,n),n.change&&(i=n.value)),i}function m(e,n){if(n={tag:e.tagName},n.children=[],e.firstChild||e.children[0]){var t=e.firstChild||e.childNodes[0];(t=t.nodeValue||t.value||t.innerText)&&(t=t.replace(/\u00a0/g,"x").trim()),t&&n.children.push(t)}for(t=0,n.attrs={},t;t<e.attributes.length;t++)n.attrs[e.attributes[t].name]=e.attributes[t].value;var r=e.children;if(r.length)for(t=0;t<r.length;t++)n.children.push(m(r[t],n.children));return n}var y=1,_=[],b="__magnum__",N="function",C="undefined",w="_key",x={};e.fill={fill:f,elementToObject:m,cached:_,find:g,configs:n},window.mag=e}(window.mag||{},[],document),function(e){function n(e){for(var n in e)if(e[n]&&-1!==["INPUT","SELECT","TEXTAREA"].indexOf(e[n].tagName))return e[n];return!1}function t(n,r,o){for(var i in r){var l=r[i];Array.isArray(l)&&l[0]&&!l[0].__$$i&&void 0!==l[0].then&&"fun"!=typeof l[0].type?(l[0].__$$i=1,Promise.all(l).then(function(n,t,r){r&&(0==l[0].__$$i,n[t]=r,e.redraw())}.bind(this,r,i))):"object"!=typeof l||l&&void 0!==l.then?(!l||Array.isArray(l)||l.__$$i||void 0===l.then||"fun"==typeof l.type||(l.__$$i=1,l.then(function(n,t,r){r&&(n[t].__$$i=0,n[t]=r,e.redraw())}.bind(this,r,i))),a(n,i,r,o)):t(n+"."+i,l,o)}}var r={modules:[],promises:[],deferreds:[],controllers:[],elements:[],getController:function(e){return new e.controller},submodule:function(e,n){var t=function(){return(e.controller||function(){}).apply(this,n)||this},r={controller:t,view:function(t){if(1<arguments.length)var r=n.concat([].slice.call(arguments,1));e.view.apply(e,r?[t].concat(r):[t])}};return t.$$args=n,r},getArgs:function(e){var n=r.modules[e]&&r.modules[e].controller&&r.modules[e].controller.$$args?[r.controllers[e]].concat(r.modules[e].controller.$$args):[r.controllers[e]];return t(e,n[0],this.elements[e]),n}},o=function(n,t){for(var r=1;r<n.length;r+=2){var o=n[r+1];t=e.fill.find(t,n[r]),t=t[o]}return t},i=function(t,r,i,a){return t=(""+i).split("."),3>t.length?(t=parseInt(t.pop()),t=isNaN(t)?0:t,r=e.fill.find(a[t]?a[t]:a,r)):(a=o(t,a),r=e.fill.find(a,r)),(r=n(r))&&!r.eventOnFocus&&(r.addEventListener("focus",function(){this.classList.contains("mag-dirty")||this.classList.add("mag-dirty")},!1),r.eventOnFocus=!0),r},a=function(t,r,o,a){var l=o[r];"_value"===r&&(r=t.split(".").pop());var f=e.fill.find(a,r),f=n(f);if("function"!=typeof l&&f){var u=i.bind({},o,r,t,a);u(),Object.defineProperty(o,r,{configurable:!0,get:function(){var e=u();return e&&"undefined"!==e.value&&e.classList.contains("mag-dirty")&&e.value!==l?(l=e.value,e.value):l},set:function(e){var n=u();n&&"undefined"!==n.value&&n.value!==e&&e!==l&&(n.value=e,l=e)}})}};e.mod=r}(window.mag||{}),function(e){function n(e,n,t){var r=n.getArgs(t),o=n.modules[t];n.controllers[t],o&&o.view(r[0],e)}function t(e,n,t){var r;return function(){var o=this,i=arguments;r?f(r):t&&e.apply(o,i),r=u(function(){t||e.apply(o,i),r=null},n)}}function r(e){var n,t=Object.getNotifier(e);for(n in e){var o=e[n];if(!o||"object"!=typeof o)break;Object.observe(o,function(e){e.forEach(function(e){t.notify(e)})}),r(o)}}function o(e,n){if(e&&void 0!==Object.observe){var o=t(n,16);r(e),Object.observe(e,o),Object.unobserve(e,o)}}var i={roots:[],contexts:[],templates:{},unloaders:[],cache:{}},a=function(e,n){return i.cache[e]&&i.cache[e]===JSON.stringify(n)?!0:void(i.cache[e]=JSON.stringify(n))};i.callLCEvent=function(e,n,t,r){var o=!1,a={preventDefault:function(){o=!0}};if(n.controllers[t]&&n.controllers[t][e]&&(n.controllers[t][e].call({},a,n.elements[t]),r&&(n.controllers[t][e]=0)),o)for(r=0;e=i.unloaders[r];r++)e.controller.onunload&&(e.handler.call(e.controller,n.elements[t]),e.controller.onunload=0);return o},i.callConfigs=function(e){for(var n=0,t=e.length;t>n;n++)e[n]()},i.redraw=function(e,n,t){e=e||i.module||{},t&&(i.cache={}),this.fun=this.fun||c(function(){n.configs.splice(0,n.configs.length),i.doLoop(e,n)}),this.fun()},i.doLoop=function(n,t){for(var r=0;i.roots[r];r++)if(n.controllers[r]&&n.elements[r]){if(i.callLCEvent("willload",n,r,1))break;if(i.innerLoop(n,t,r)){n.deferreds[r][1]({_html:e.prop(n.elements[r])}),i.callLCEvent("didload",n,r,1),i.callConfigs(t.configs);var o=n,a=t,l=r,f=void 0;for(f in a.cached)-1!==f.indexOf('id("'+o.elements[l].id+'")/')&&-1!==f.indexOf("-config")&&a.cached[f].configContext&&i.unloaders.push({controller:a.cached[f].configContext,handler:a.cached[f].configContext.onunload});n.deferreds[r][0]&&(delete n.elements[r],delete n.modules[r],delete n.controllers[r],delete n.promises[r],delete n.deferreds[r])}}},i.clear=function(e){-1!==e&&i.cache[e]&&delete i.cache[e]},i.innerLoop=function(e,t,r){var o=e.elements[r],l=e.getArgs(r);return a(r,l)?!1:(n(o,e,r),i.setupWatch(l,t,o,r,e),t.fill(o,l[0]),!0)};var l;i.doWatch=function(e,t,r,o,f,u){if(u!=l){if(l=u,i.callLCEvent("willupdate",o,r,1),f=o.getArgs(r),a(r,f))return void i.callLCEvent("didupdate",o,r);i.callLCEvent("isupdate",o,r),n(t,o,r),e.fill(t,f[0])}},i.setupWatch=function(e,n,t,r,a){o(e[0],function(o){o.forEach(function(e){"update"==e.type&&e.oldValue&&"fun"==e.oldValue.type&&e.oldValue.data&&"module"==e.oldValue.data.type&&!e.object[e.name].data&&i.callLCEvent("onunload",a,e.oldValue.data.id,1)}),a.controllers[r]=e[0],c(i.doWatch.bind({},n,t,r,a,o))()})};var f=window.cancelAnimationFrame||window.clearTimeout,u=window.requestAnimationFrame||window.setTimeout,c=function(n,t){var r,o,i=t||16;return function(){var t=arguments;if(u===window.requestAnimationFrame||+new Date-r>i)o>0&&f(o),o=u(function(){r=+new Date;var o=[].slice.call(arguments).concat([].slice.call(t));n.apply(this,o),e.redrawing=!1},i);else{r=+new Date;var a=[].slice.call(arguments).concat([].slice.call(t));n.apply(this,a),o=u(function(){o=null,e.redrawing=!1},i)}}};e.render=i}(window.mag||{}),function(e,n){function t(n,t){var r=function(){return arguments.length&&(n=arguments[0],e.redraw()),n};return r.type="fun",r.data=t?t:null,r.toJSON=function(){return n&&n.nodeType?a.elementToObject(n):n},r}function r(n,t,o){return o=e.prop(t,o),n.then(o),o.then=function(e,o){return r(n.then(e,o),t)},o}var o=e.mod,i=e.render,a=e.fill,l={}.toString,f={attributes:[],elementMatcher:[]};e.redrawing=!1,e.redraw=function(n){e.redrawing||(e.redrawing=!0,i.redraw(o||i.module||{},a,n))},e.withProp=function(e,n){return function(t){t=t||event,t=t.currentTarget||this,n(e in t?t[e]:t.getAttribute(e))}},e.prop=function(e,n){return(null!=e&&"[object Object]"===l.call(e)||"function"==typeof e)&&"function"==typeof e.then?r(e,n):t(e,n)},e.hookin=function(e,n,t){f[e].push({context:{},handler:t,key:n})},e.hook=function(e,n,t){for(var r=0,o=f[e].length;o>r;r++){var i=n,a=e,l=r,u=t,c=void 0;u.change=!1,f[a][l].key==i&&(c=JSON.stringify({v:u.value,k:u.key}),f[a][l].handler.call(f[a][l].context,u),c!==JSON.stringify({v:u.value,k:u.key})&&(u.change=!0))}},e.module=function(t,l,f,u){var c,d=i.roots.indexOf(t);if((c=-1>=d)||(i.callLCEvent("onreload",o,d),c=!0),c){if(f&&!f.retain&&i.clear(d,t,a),(0>d||u)&&(d=i.roots.length),c=n.getElementById(t),!c)throw Error("Mag.JS Module - invalid node id: "+t);if(i.roots[d]=t,!l.view)throw Error("Mag.JS module - requires a view: "+t+l);return t=o.submodule(l,[f||{}]),l=o.getController(t,c,a),o.controllers[d]=l,l.onunload&&i.unloaders.push({controller:l,handler:l.onunload}),o.modules[d]=t,o.elements[d]=u?c.cloneNode(!0):c,o.elements[d].cloner=u,o.promises[d]=new Promise(function(){o.deferreds[d]=arguments}.bind({},u)),e.redraw(),r(o.promises[d],{_html:e.prop(o.elements[d])},{type:"module",id:d})}}}(window.mag||{},document);