cell.min.js 12 KB

1
  1. (function($root){var Membrane={inject:function($host,gene,namespace,replace){var $node=null;var $replacement;if(replace&&$host){$replacement=Phenotype.$type(gene,namespace);if(gene.hasOwnProperty("$cell")){$node=$host;if($node.parentNode){$node.parentNode.replaceChild($replacement,$node)}}$node=$replacement}else if(gene.$type&&(gene.$type==="head"||gene.$type==="body")&&$root.document.getElementsByTagName(gene.$type)){$node=$root.document.getElementsByTagName(gene.$type)[0]}else if(gene.id&&$root.document.getElementById(gene.id)){$node=$root.document.getElementById(gene.id);if($node.nodeName.toLowerCase()!==(gene.$type||"div")){$replacement=Phenotype.$type(gene,namespace);$node.parentNode.replaceChild($replacement,$node);$node=$replacement}}if($node&&!$node.Meta){$node.Meta={}}return $node},add:function($parent,gene,index,namespace){var $node=Phenotype.$type(gene,namespace);if(index!==null&&index!==undefined&&$parent.childNodes&&$parent.childNodes[index]){$parent.insertBefore($node,$parent.childNodes[index])}else{$parent.appendChild($node)}return $node},build:function($parent,gene,index,namespace,replace){var $existing=Membrane.inject($parent,gene,namespace,replace);if($existing){return $existing;}else{return Membrane.add($parent,gene,index,namespace)}}};var Genotype={set:function($node,key,val){if(["$init"].indexOf(key)===-1){$node.Genotype[key]=Nucleus.bind($node,val)}else{val.snapshot=val;$node.Genotype[key]=val}},update:function($node,key,val){Nucleus.queue($node,key,"w");Genotype.set($node,key,val)},build:function($node,gene,inheritance){$node.Genotype={};$node.Inheritance=inheritance;for(var key in gene){Genotype.set($node,key,gene[key])}},infect:function(gene){var virus=gene.$virus;if(!virus){return gene}var mutations=Array.isArray(virus)?virus:[virus];delete gene.$virus;return mutations.reduce(function(g,mutate){var mutated=mutate(g);if(mutated===null||typeof mutated!=="object"){throw new Error("$virus mutations must return an object")}mutated.$type=mutated.$type||"div";return mutated},gene)}};var Gene={freeze:function(gene){var cache=[];var res=JSON.stringify(gene,function(key,val){if(typeof val==="function"){return val.toString()}if(typeof val==="object"&&val!==null){if(cache.indexOf(val)!==-1){return "[Circular]"}cache.push(val)}return val});cache=null;return res},LCS:function(a,b){var m=a.length,n=b.length,C=[],i,j,af=[],bf=[];for(i=0;i<m;i+=1){af.push(Gene.freeze(a[i]))}for(j=0;j<n;j+=1){bf.push(Gene.freeze(b[j]))}for(i=0;i<=m;i+=1){C.push([0])}for(j=0;j<n;j+=1){C[0].push(0)}for(i=0;i<m;i+=1){for(j=0;j<n;j+=1){C[i+1][j+1]=af[i]===bf[j]?C[i][j]+1:Math.max(C[i+1][j],C[i][j+1])}}return(function bt(i,j){if(i*j===0){return[]}if(af[i-1]===bf[j-1]){return bt(i-1,j-1).concat([{item:a[i-1],_old:i-1,_new:j-1}],)}return C[i][j-1]>C[i-1][j]?bt(i,j-1):bt(i-1,j)}(m,n))},diff:function(_old,_new){var lcs=Gene.LCS(_old,_new);var old_common=lcs.map(function(i){return i._old});var minus=_old.map(function(item,index){return{item:item,index:index}}).filter(function(item,index){return old_common.indexOf(index)===-1});var new_common=lcs.map(function(i){return i._new});var plus=_new.map(function(item,index){return{item:item,index:index}}).filter(function(item,index){return new_common.indexOf(index)===-1});return{"-":minus,"+":plus}}};var Phenotype={build:function($node,genotype){Phenotype.$init($node);for(var key in genotype){if(genotype[key]!==null&&genotype[key]!==undefined){Phenotype.set($node,key,genotype[key])}}},multiline:function(fn){return/\/\*!?(?:@preserve)?[ \t]*(?:\r\n|\n)([\s\S]*?)(?:\r\n|\n)[ \t]*\*\//.exec(fn.toString())[1]},get:function(key){return Object.getOwnPropertyDescriptor($root.HTMLElement.prototype,key,)||Object.getOwnPropertyDescriptor($root.Element.prototype,key)},set:function($node,key,val){if(key[0]==="$"){if(key==="$type"){var tag=$node.tagName?$node.tagName.toLowerCase():"text";if(val.toLowerCase()!==tag){var fragment=Phenotype.$type({$type:"fragment"});var replacement=fragment.$build($node.Genotype,$node.Inheritance,null,$node.Meta.namespace,);$node.parentNode.replaceChild(replacement,$node);$node=replacement}}else if(key==="$text"){if(typeof val==="function"){val=Phenotype.multiline(val)}$node.textContent=val}else if(key==="$html"){if(typeof val==="function"){val=Phenotype.multiline(val)}$node.innerHTML=val}else if(key==="$components"){Phenotype.$components($node,val)}}else if(key[0]==="_"){}else if(key==="value"){$node[key]=val}else if(key==="style"&&typeof val==="object"){var CSSStyleDeclaration=Phenotype.get(key).get.call($node);for(var attr in val){CSSStyleDeclaration[attr]=val[attr]}}else if(typeof val==="number"||typeof val==="string"||typeof val==="boolean"){if($node.setAttribute){$node.setAttribute(key,val)}}else if(typeof val==="function"){var prop=Phenotype.get(key);if(prop){prop.set.call($node,val)}}},$type:function(model,namespace){var meta={};var $node;if(model.$type==="svg"){$node=$root.document.createElementNS("http://www.w3.org/2000/svg",model.$type,);meta.namespace=$node.namespaceURI}else if(namespace){$node=$root.document.createElementNS(namespace,model.$type);meta.namespace=$node.namespaceURI}else if(model.$type==="fragment"){$node=$root.document.createDocumentFragment()}else if(model.$type==="text"){if(model.$text&&typeof model.$text==="function"){model.$text=Phenotype.multiline(model.$text)}$node=$root.document.createTextNode(model.$text)}else{$node=$root.document.createElement(model.$type||"div")}$node.Meta=meta;return $node},$components:function($parent,components){if(!components){components=[]}var old=[].map.call($parent.childNodes,function($node){return $node.Genotype}).filter(function(item){return item;});if(old.length>0){var diff=Gene.diff(old,components);diff["-"].forEach(function(item){$parent.childNodes[item.index].Kill=true});[].filter.call($parent.childNodes,function($node){return $node.Kill}).forEach(function($node){$parent.removeChild($node);setTimeout(function(){Nucleus.disconnect($node)},0)});diff["+"].forEach(function(item){var inheritance=$parent.Inheritance;for(var key in $parent.Genotype){if(key[0]==="_"){inheritance=inheritance.concat([key])}}$parent.$build(item.item,inheritance,item.index,$parent.Meta.namespace,);$parent.$components[item.index]=$parent.childNodes[item.index].Genotype})}else{var $fragment=Phenotype.$type({$type:"fragment"});var inheritance=$parent.Inheritance;for(var key in $parent.Genotype){if(key[0]==="_"){inheritance=inheritance.concat([key])}}while($parent.firstChild){$parent.removeChild($parent.firstChild);}components.forEach(function(component){$fragment.$build(component,inheritance,null,$parent.Meta.namespace,)});$parent.appendChild($fragment);$parent.$components=[].map.call($parent.childNodes,function($node){return $node.Genotype})}},$init:function($node){Nucleus.tick.call($root,function(){if($node.Genotype&&$node.Genotype.$init){Nucleus.bind($node,$node.Genotype.$init)()}})},$update:function($node){if($node.parentNode&&!$node.Meta.$updated&&$node.$update){$node.Meta.$updated=true;$node.$update();for(var key in $node.Dirty){Phenotype.set($node,key,$node.Genotype[key])}$node.Meta.$updated=false;$node.Dirty=null}}};var Nucleus={tick:$root.requestAnimationFrame||$root.webkitRequestAnimationFrame||$root.mozRequestAnimationFrame||$root.msRequestAnimationFrame||function(cb){return $root.setTimeout(cb,1000/60)},set:function($node,key){try{Object.defineProperty($node,key,{configurable:true,get:function(){if(key[0]==="$"||key[0]==="_"){if(key in $node.Genotype){Nucleus.queue($node,key,"r");return $node.Genotype[key]}else if(key[0]==="_"){var $current=$node;while($current=$current.parentNode){if($current&&$current.Genotype&&(key in $current.Genotype)){Nucleus.queue($current,key,"r");return $current.Genotype[key]}}}else{return null}}else{if(key==="value"){return Object.getOwnPropertyDescriptor(Object.getPrototypeOf($node),key,).get.call($node)}else if(key==="style"){return Phenotype.get(key).get.call($node)}else if(key in $node.Genotype){return $node.Genotype[key]}else{return Phenotype.get(key).get.call($node)}}},set:function(val){var $current=$node;if(!(key in $node.Genotype)&&key[0]==="_"){while($current=$current.parentNode){if($current&&$current.Genotype&&(key in $current.Genotype)){break}}}Genotype.update($current,key,val);if(key[0]!=="$"&&key[0]!=="_"){if(key==="value"){return Object.getOwnPropertyDescriptor(Object.getPrototypeOf($node),key,).set.call($node,val)}else if(key==="style"&&typeof val==="object"){Phenotype.get(key).set.call($node,val)}else if(typeof val==="number"||typeof val==="string"||typeof val==="boolean"){$node.setAttribute(key,val)}else if(typeof val==="function"){Phenotype.get(key).set.call($node,val)}}}})}catch(e){}},build:function($node){["$type","$text","$html","$components"].forEach(function(key){if(!(key in $node.Genotype)){Nucleus.set($node,key)}});if($node.Inheritance){$node.Inheritance.forEach(function(key){Nucleus.set($node,key)})}for(var key in $node.Genotype){Nucleus.set($node,key)}},_queue:[],bind:function($node,v){if(typeof v==="function"){var fun=function(){Nucleus.tick.call($root,function(){Nucleus._queue.forEach(function($node){var needs_update=false;for(var key in $node.Dirty){if(Gene.freeze($node.Genotype[key])!==$node.Dirty[key]){Phenotype.set($node,key,$node.Genotype[key]);if(key[0]==="_"){needs_update=true;}}}if(needs_update&&"$update"in $node.Genotype&&(typeof $node.Genotype.$update==="function")){Phenotype.$update($node)}else{$node.Dirty=null}});var index=Nucleus._queue.indexOf($node);if(index!==-1){Nucleus._queue.splice(index,1)}});return v.apply($node,arguments)};fun.snapshot=v;return fun}else{return v}},queue:function($node,key,mode){var val=$node.Genotype[key];if(mode==="r"){if(typeof val!=="object"&&!Array.isArray(val)){return}}if(Nucleus._queue.indexOf($node)===-1){Nucleus._queue.push($node)}if(!$node.Dirty){$node.Dirty={}}if(!(key in $node.Dirty)){$node.Dirty[key]=Gene.freeze($node.Genotype[key]);}},disconnect:function($node){var index=Nucleus._queue.indexOf($node);if(index!==-1){Nucleus._queue.splice(index,1)}$node.childNodes.forEach(Nucleus.disconnect)}};var God={detect:function($context){if($context===undefined){$context=this}return Object.keys($context).filter(function(k){if(k==="onmozfullscreenerror"||k==="onmozfullscreenchange"){return false;}try{if(/webkitStorageInfo|webkitIndexedDB/.test(k)||$context[k]instanceof $root.Element){return false;}return $context[k]&&Object.prototype.hasOwnProperty.call($context[k],"$cell")}catch(e){return false}}).map(function(k){return $context[k]})},plan:function($context){if($context===undefined){$context=$root}else{$root=$context}$context.DocumentFragment.prototype.$build=$context.Element.prototype.$build=function(healthy_gene,inheritance,index,namespace,replace,){var gene=Genotype.infect(healthy_gene);var $node=Membrane.build(this,gene,index,namespace,replace);Genotype.build($node,gene,inheritance||[],index);Nucleus.build($node);Phenotype.build($node,$node.Genotype);return $node};$context.DocumentFragment.prototype.$cell=$context.Element.prototype.$cell=function(gene,options){return this.$build(gene,[],null,(options&&options.namespace)||null,true,)};$context.DocumentFragment.prototype.$snapshot=$context.Element.prototype.$snapshot=function(){var json=JSON.stringify(this.Genotype,function(k,v){if(typeof v==="function"&&v.snapshot){return "("+v.snapshot.toString()+")"}return v});return JSON.parse(json,function(k,v){if(typeof v==="string"&&v.indexOf("function")>=0){return eval(v)}return v})};if($root.NodeList&&!$root.NodeList.prototype.forEach){$root.NodeList.prototype.forEach=Array.prototype.forEach;}},create:function($context){return God.detect($context).map(function(gene){return $context.document.body.$build(gene,[])})}};if(typeof exports!=="undefined"){var x={Phenotype:Phenotype,Genotype:Genotype,Nucleus:Nucleus,Gene:Gene,Membrane:Membrane,God:God,plan:God.plan.bind(God),create:God.create.bind(God)};if(typeof module!=="undefined"&&module.exports){exports=module.exports=x}exports=x}else{God.plan(this);if(this.addEventListener){this.addEventListener("load",function(){God.create(this)})}}}(this));