minified version added
authorDaniel Abrecht <daniel.abrecht@hotmail.com>
Tue, 20 Jan 2015 19:19:54 +0000 (20:19 +0100)
committerDaniel Abrecht <daniel.abrecht@hotmail.com>
Tue, 20 Jan 2015 19:19:54 +0000 (20:19 +0100)
MVSync.js
MVSync.min.js [new file with mode: 0644]

index 9232621..60ed54a 100644 (file)
--- a/MVSync.js
+++ b/MVSync.js
@@ -11,12 +11,14 @@ Object.unobserve;
 var templates = {};
 var requiredTemplates = {};
 
-Object.observe(requiredTemplates,function(changes){
-  changes.forEach(function(ch){
-    if(ch.type=="add")
-      loadTemplate(ch.name);
+function init(){
+  Object.observe(requiredTemplates,function(changes){
+    changes.forEach(function(ch){
+      if(ch.type=="add")
+        loadTemplate(ch.name);
+    });
   });
-});
+}
 
 /**
  * @constructor
@@ -440,6 +442,7 @@ addEventListener("load",function(){
   }else if(document.querySelector("[data-template-root]")){
     base = document.querySelector("[data-template-root]").getAttribute("data-template-root") + "/";
   }
+  init();
   compileTemplates(document);
   var t = compileTemplate(document.documentElement);
   t.instance(model);
diff --git a/MVSync.min.js b/MVSync.min.js
new file mode 100644 (file)
index 0000000..88a53a9
--- /dev/null
@@ -0,0 +1,12 @@
+(function(){function v(){Object.observe(n,function(c){c.forEach(function(c){"add"==c.type&&C(c.name)})})}function D(c){function h(h){function q(a){a.forEach(function(a){r(a.name)})}function w(a){try{return(new Function("v","return v."+a+";"))(h)}catch(g){return null}}function r(a){if(a in k)for(var g in k[a])(function(){var b=k[a][g],e=null;b.a&&(e=w(b.a));var f=null;switch(b.type){case "getter":var d=y[a+b.a]=l[b.which]=y[a+b.a]||l[b.which]||{};p[b.which]&&(d.k=p[b.which]);var c=function(a){for(d.i=
+a;d.b.length;){var e=d.b.pop();(new Function("s","n","v","s[n]"+b.a+"=v;")).call(h,h,e,a)}};d.b=d.b||[];d.b.length||(f=e(),f instanceof Function?f(c):c(f));d.b.push(b.which);break;case "setter":f=z[a+b.a]=p[b.which]=z[a+b.a]||p[b.which]||{};(l[b.which]||l[b.which]||{}).k=f;f.d=e;break;case "value":f=e;a in l&&l[a].i==f||(e=null,a in l&&(e=l[a].k.d,delete l[a].i),!e&&a in p&&(e=p[a].d),e&&(e=e(f))&&e());"attribute"==b.e?t(b.element,b.which,f):"content"==b.e&&A(b.element,f);break;case "map":b.update()}})()}
+function t(a,g,b){g in a?a[g]=b:a.setAttribute(g,b)}function A(a,g){for(;a.childNodes.length;){var b=a.childNodes[a.childNodes.length-1];b.l&&b.l.f();a.removeChild(b)}"_content"in a&&delete a.c;g instanceof Node?(b=g.cloneNode(!0),x(b),a.appendChild(b)):a.appendChild(document.createTextNode(g||""))}function x(a){var g=a.getAttribute("data-bind");if(g)for(var g=g.split("\u00a6"),b=0;b<g.length;b++){var e=g[b].split(":"),f=e[1],d=f.match(/^([a-zA-Z_$][a-zA-Z0-9_$]*)(.*)/)[1],e=e[0],c=w(f);t(a,e,c);
+k[d]=k[d]||[];k[d].push({which:e,e:"attribute",type:"value",a:f,element:a})}if(b=a.getAttribute("data-content"))f=b.match(/^([a-zA-Z_$][a-zA-Z0-9_$]*)(.*)/),d=f[1],f=b,c=w(f),A(a,c),k[d]=k[d]||[],k[d].push({e:"content",type:"value",a:f,element:a});if(g=a.getAttribute("data-getter"))for(g=g.split("\u00a6"),b=0;b<g.length;b++)e=g[b].split(":"),f=e[1],d=f.match(/^([a-zA-Z_$][a-zA-Z0-9_$]*)(.*)/)[1],e=e[0],k[d]=k[d]||[],k[d].push({which:e,type:"getter",a:f,element:a});if(g=a.getAttribute("data-setter"))for(g=
+g.split("\u00a6"),b=0;b<g.length;b++)e=g[b].split(":"),f=e[1],d=f.match(/^([a-zA-Z_$][a-zA-Z0-9_$]*)(.*)/)[1],e=e[0],k[d]=k[d]||[],k[d].push({which:e,type:"setter",a:f,element:a});(new MutationObserver(function(b){b.forEach(function(b){b=b.attributeName;var d=a.getAttribute(b),e;for(e in k){var f=k[e],c;for(c in f){var g=f[c];"value"==g.type&&g.which==b&&(new Function("s","v","if(s."+g.a+".toString()!=v)s."+g.a+"=Object(s."+g.a+").constructor(v);")).call(h,h,d)}}})})).observe(a,{attributes:!0});"value"in
+a&&a.addEventListener("change",function(){a.setAttribute("value",a.value)});if(b=a.getAttribute("data-map")){var m=b.split(":"),f=m[0].match(/^([a-zA-Z_$][a-zA-Z0-9_$]*)(.*)/),d=f[1],f=f[2],l=function(){if(m[1]in u){var b=h[d];b&&(b=Object((new Function("v","return v"+f+";"))(b)));b||(b=[]);"length"in b||(b=[b]);a.c=a.c||{o:[],n:[]};Object.observe(b,function(){B(a.c,b,a,u[m[1]])});B(a.c,b,a,u[m[1]])}else(n[m[1]]=n[m[1]]||[]).push(l)};(k[m[0]]=k[m[0]]||[]).push({type:"map",update:l});l()}if(!b)for(b=
+0;b<a.children.length;b++)x(a.children[b])}function B(a,g,b,e){var f=a.o;a=a.n;for(var d=f.length;d--;)-1==g.indexOf(f[d])&&(a[d].element.parentNode&&a[d].element.parentNode.removeChild(a[d].element),f.splice(d,1),a.splice(d,1));for(d=0;d<g.length;d++){var c=f.indexOf(g[d]);if(-1!=c)a[c].index=d;else{c={element:e.h(g[d]),index:d};if(a.length){var h=a[a.length-1].element;h.parentNode==b?b.insertBefore(c.element,h.nextSibling):b.appendChild(c.element)}else b.appendChild(c.element);f.push(g[d]);a.push(c)}}for(d=
+0;d<a.length;d++)e=a[d],e.index!=d&&(g=e.element,b=a[e.index].element,c=e.index,a[d]=[a[c],a[c]=a[d]][0],e=e.index,f[d]=[f[e],f[e]=f[d]][0],e=g.parentNode,c=b.parentNode,e&&c&&(h=g.nextSibling,c.insertBefore(g,b.nextSibling),e.insertBefore(b,h)))}Object.defineProperty(h,"this",{configurable:!0,writable:!0,enumerable:!1,value:h});var k={},y={},l={},z={},p={};this.g=[];this.root=c instanceof HTMLHtmlElement?c:c.cloneNode(!0);this.root.l=this;this.root.classList.add(E);this.f=function(){Object.r(this.j.m,
+this.j.d);for(var a=0;a<this.g.length;a++)this.g[a].f()};x(this.root);this.j={m:Object.observe(h,q),d:q};for(var v in h)r(v)}c.removeAttribute("data-template");var E=c.p;c.q=this;this.h=function(c){return(new h(c)).root}}function r(c){var h=c.getAttribute("data-template");!h&&c instanceof HTMLHtmlElement&&(h="root");!c.parentNode||c instanceof HTMLHtmlElement||c.parentNode.removeChild(c);c.p=h;c=u[h]=new D(c);if(h in n){for(;n[h].length;)n[h].pop()();delete n[h]}return c}function t(c){"querySelectorAll"in
+c&&(c=c.querySelectorAll("[data-template]"));for(var h=0;h<c.length;h++)r(c[h])}function C(c){var h=new XMLHttpRequest;h.open("GET",q+"templates/"+c+".html",!0);h.onload=function(){var c=null;if(!c){var c=document.createDocumentFragment(),h=document.createElement("div");h.innerHTML=this.responseText;c.appendChild(h)}t(c)};h.send()}window.model=window.model||{};var u={},n={},q="/";addEventListener("load",function(){window.templateRoot?q=window.templateRoot+"/":document.querySelector("[data-template-root]")&&
+(q=document.querySelector("[data-template-root]").getAttribute("data-template-root")+"/");v();t(document);r(document.documentElement).h(model)})})();
+