-
Notifications
You must be signed in to change notification settings - Fork 7
/
iugo.min.js
3 lines (3 loc) · 6.08 KB
/
iugo.min.js
1
2
3
/* Author Chris Scott, MIT license */
window.$iugo={};$iugo.$internals={};$iugo.$internals.registerModelMember=function(a,b){Object.defineProperty(a,b,{get:function(){return this.$[b]},set:function(a){this.updateView(b,a);this.$[b]=a;$iugo.$internals.applySetters(a,this,[b],a)}})};$iugo.$internals.applySetters=function(a,b,c,f){if(a instanceof Object||a instanceof Array){for(var d in a){$iugo.$internals.registerProperty(a,d,b,c);var e=c.concat([d]);$iugo.$internals.applySetters(a[d],b,e,f)}a instanceof Array&&$iugo.$internals.registerArray(a,b,c)}};$iugo.$internals.registerProperty=function(a,b,c,f){c.$[f.concat([b]).join(".")]=a[b];Object.defineProperty(a,b,{get:function(){return c.$[f.concat([b]).join(".")]},set:function(d){c.$[f.concat([b]).join(".")]=d;$iugo.$internals.setChildMembers(a,c,f);c.updateView(f[0],c[f[0]])}})};$iugo.$internals.setChildMembers=function(a,b,c){if(a instanceof Object||a instanceof Array)for(var f in a){var d=c.concat([f]);b.$[d.join(".")]=a[f];$iugo.$internals.setChildMembers(a[f],b,d)}};$iugo.$internals.registerArray=function(a,b,c){a.push=function(){var a=Array.prototype.push.apply(this,arguments);b[c[0]]=b[c[0]];return a};a.pop=function(){var f=$iugo.$internals.clone(Array.prototype.pop.call(a));b[c[0]]=b[c[0]];return f};a.unshift=function(){for(var a=$iugo.$internals.clone(this),d=0;d<arguments.length;d++)this[d]=arguments[d];for(var e=0;e<a.length;e++)this[d++]=a[e];b[c[0]]=b[c[0]];return this.length};a.shift=function(){var a=$iugo.$internals.clone(Array.prototype.shift.call(this));b[c[0]]=b[c[0]];return a};a.reverse=function(){for(var a=$iugo.$internals.clone(this),b=0;b<this.length;b++)this[b]=a[this.length-1-b];return this};a.sort=function(){var a=$iugo.$internals.clone(this);Array.prototype.sort.apply(a,arguments);for(var b=0;b<this.length;b++)this[b]=a[b];return this};a.splice=function(){for(var a=$iugo.$internals.clone(this),d=0;d<a.length,d<arguments[0];d++)this[d]=a[d];for(var e=2;e<arguments.length;e++)this[d++]=arguments[e];for(e=arguments[0]+arguments[1];e<a.length;e++)this[d++]=a[e];for(e=d;e<a.length;e++)this.pop();b[c[0]]=b[c[0]];return this}};$iugo.$internals.clone=function(a){var b;b=a instanceof Array?[]:{};for(var c in a)b[c]=a[c]instanceof Object||a[c]instanceof Array?$iugo.$internals.clone(a[c]):a[c];return b};$iugo.$internals.MVVC=function(a,b,c){this.view=b?b:document.body;this.viewcontroller=c?c:{};for(b=0;b<this.initializers.length;b++)if(this.initializers[b]instanceof Function)this.initializers[b](this.view);this.model=a};$iugo.$internals.MVVC.prototype={$:{},set model(a){for(var b in a)$iugo.$internals.registerModelMember(this,b),this[b]=a[b]},updateView:function(a,b){for(var c=0;c<this.defaultViewcontrollers.length;c++)if(this.defaultViewcontrollers[c]instanceof Function)this.defaultViewcontrollers[c](a,b,this.view);if("undefined"!==typeof this.viewcontroller[a]&&this.viewcontroller[a]instanceof Function)this.viewcontroller[a](b,this.view)},defaultViewcontrollers:[],initializers:[]};$iugo.defaultViewcontrollers=$iugo.$internals.MVVC.prototype.defaultViewcontrollers;$iugo.initializers=$iugo.$internals.MVVC.prototype.initializers;$iugo.store={};window.Iugo=function(a,b,c){return new $iugo.$internals.MVVC(a,b,c)};
$iugo.store.BD={a:[],b:{},f:0};$iugo.initializers.push(function(a){for(var l=/(>[^<]*)\$\{([^:.}<]+:)?([^}<]*)\}([^<]*<)/g;a.innerHTML.match(l);)a.innerHTML=a.innerHTML.replace(l,function(h,a,p,d,f){h=a+"<span ";p&&(h+='class="bindto-'+p.substr(0,p.length-1)+'" ');return h+('data-bind_key="'+d+'"></span>'+f)});a.innerHTML=a.innerHTML.replace(/<[^>]+ [^ =]+="[^"]*\$\{[^}<"]+\}[^"]*"[^>]*>/g,function(a){var e=$iugo.store.BD.f++;$iugo.store.BD.a[e]={d:[],c:{},e:{}};a=a.replace(/([^ =]+)="([^"]*\$\{[^}"]+\}[^"]*)"/g,function(a,d,f){"data-iugo_alias-"==d.substr(0,16)&&(d=d.substr(16));$iugo.store.BD.a[e].d.push(d);$iugo.store.BD.a[e].c[d]=f;f.replace(/\$\{([^"}:]+):[^}"]+\}/g,function(a,d){$iugo.store.BD.b[d]||($iugo.store.BD.b[d]=[]);-1==$iugo.store.BD.b[d].indexOf(e)&&$iugo.store.BD.b[d].push(e)});return d+'=""'});return a=a.replace(/ ?>$/,' data-iugo_id="'+e+'">')})});$iugo.defaultViewcontrollers.push(function(a,l,h){function e(n){for(var c=document.querySelector('[data-iugo_id="'+n+'"]'),g=$iugo.store.BD.a[n].d,b=0;b<g.length;b++){var a=$iugo.store.BD.a[n].c[g[b]].replace(s,function(b){return $iugo.store.BD.a[n].e[b]?$iugo.store.BD.a[n].e[b]:""});c.setAttribute(g[b],a)}}function p(n,c,a){if(c.hasAttribute("data-iugo_id")){var b=c.getAttribute("data-iugo_id");c=$iugo.store.BD.a[b].d;for(var d=0;d<c.length;d++)$iugo.store.BD.a[b].c[c[d]].replace(s,function(c,d,f){if(!d&&!a||d&&d.substr(0,d.length-1)==a){d=f.split(".");f=n;for(var e=0;e<d.length;e++)""!=d[e]&&(f=f[d[e]]);$iugo.store.BD.a[b].e[c]=f}});e(b)}}function d(d){var c=d.getAttribute("data-iugo_id"),a=$iugo.store.BD.f++,c=$iugo.store.BD.a[c];$iugo.store.BD.a[a]={d:c.d,c:c.c,e:{}};d.setAttribute("data-iugo_id",a)}function f(a,c,g){p(a,c);if(a instanceof Array){g=c.children.length;for(var b=g-1;0<=b;b--)c.children[b].classList.contains("iugo_cloned")&&c.removeChild(c.children[b]);g=c.children.length;for(b=0;b<g;b++){var e;if(c.children[b].hasAttribute("data-bind_each")){e=c.children[b];0==a.length?(e.setAttribute("data-iugo_display",e.style.display),e.style.display="none"):e.style.display=e.getAttribute("data-iugo_display");for(var h=0;h<a.length;h++){var m;if(1<=h){m=e.cloneNode(!0);m.classList.add("iugo_cloned");m.hasAttribute("data-iugo_id")&&d(m);for(var k=m.querySelectorAll("[data-iugo_id]"),l=0;l<k.length;l++)d(k[l]);c.appendChild(m)}else m=e;f(a[h],m)}}}}else if(a instanceof Object)if(b=c.getAttribute("data-bind_key"),null!=b&&""!=b)g=null==g?"":g+".",b=b.slice(g.length).split(".")[0],f(a[b],c,g+b);else for(b=0;b<c.children.length;b++)c.children[b].className.match("bindto-")||f(a,c.children[b],g);else if(0<c.children.length)for(b=0;b<c.children.length;b++)f(a,c.children[b],g);else"INPUT"==c.tagName?c.value=a:c.innerHTML=a}var s=/\$\{([^:}]+:)?([^}]+)\}/g;if($iugo.store.BD.b[a])for(var t=$iugo.store.BD.b[a],k=0;k<t.length;k++)for(var q=h.querySelectorAll('[data-iugo_id="'+t[k]+'"]'),r=0;r<q.length;r++)p(l,q[r],a);q=h.getElementsByClassName("bindto-"+a);for(k=0;k<q.length;k++)f(l,q[k])});