2016-09-20 08:41:56 +00:00
|
|
|
var ref=function(a,b){for(var g=null;null==g||a.hasOwnProperty(g);)g="$"+(268435456+Math.floor(4026531839*Math.random())).toString(16)+"$";a[g]=b;return g},FormBuilder=function(a){this.form_object=a};FormBuilder.prototype={form_object:this.form_object,defs_object:{},parent_element:null,built_form:null,root_element:null,ref_table:{$00000000$:null},ref_assoc:{NULL:"$00000000$"}};
|
|
|
|
FormBuilder.regex={reg_in_key:/^\/\^(.+)\$\/$/,reg_out_val:/\{(\$[1-9])\}/,pri_out_val:/\{([a-z-]+)\}/g,pri_in_key:/^\$([a-z-]+)$/,arr_out_set:/^\{([a-z-]+)\[\]\}$/,arr_out_val:/\{([a-z-]+)\.([a-z-]+)\}/g,arr_in_key:/^\$([a-z-]+)$/,fun_out_val:/^\{([a-z-]+)\(\)\}$/,ref_pri:/^\$[a-f0-9]{8}\$$/};FormBuilder.spread_attr="children next_nodes prev_nodes attributes node_link listeners repeat browse".split(" ");FormBuilder.prototype.add_definition=function(a){for(var b in a)this.defs_object[b]=a[b]};
|
|
|
|
FormBuilder.prototype.build=function(a){a=null==a?{}:a;for(var b in a)a[b]=ref(this.ref_table,a[b]),this.ref_assoc[b]=a[b];this.built_form=JSON.parse(JSON.stringify(this.form_object));this.built_form=FormBuilder.formatFormObject(this.built_form,this.defs_object);this.built_form=FormBuilder.replaceStatements(this.built_form,a,this.defs_object,this.ref_table,this.ref_assoc)};
|
|
|
|
FormBuilder.prototype.update=function(a){a=null==a?{}:a;for(var b in a)this.ref_assoc.hasOwnProperty(b)&&(this.ref_table[this.ref_assoc[b]]=a[b])};
|
|
|
|
FormBuilder.prototype.attach=function(a){if(!(a instanceof Element)&&null===this.parent_element)return!1;this.parent_element=a instanceof Element?a:this.parent_element;var b,g;a=FormBuilder.buildElements(this.built_form,this.ref_table,this.ref_assoc);for(b in a){for(g in a[b].prev)this.parent_element.appendChild(a[b].prev[g]);this.parent_element.appendChild(a[b].node);for(g in a[b].next)this.parent_element.appendChild(a[b].next[g])}};FormBuilder.formatFormObject=function(a,b){return a};
|
|
|
|
FormBuilder.fetchNodeDefinition=function(a,b){var g,c;r=FormBuilder.regex.reg_in_key;if("undefined"!=typeof b&&b.hasOwnProperty(a))return{def:b[a]};for(g in b)if(r.test(g)&&(c=new RegExp(g.slice(1,-1)),c.test(a))){matches={};for(c=1;c<RegExp.length&&10>c;c++)matches["$"+c]=RegExp["$"+c];return{def:b[g],scope:matches}}return{}};
|
|
|
|
FormBuilder.replaceStatements=function(a,b,g,c,e){a=a instanceof Object?a:{};b=b instanceof Object?JSON.parse(JSON.stringify(b)):{};var d,k;if(a.hasOwnProperty("scope")&&a.scope instanceof Object)for(d in a.scope)b[d]=a.scope[d];if(a.hasOwnProperty("node")&&"string"==typeof a.node&&(k=FormBuilder.fetchNodeDefinition(a.node,g),k.hasOwnProperty("def")&&(a.node_link=JSON.parse(JSON.stringify(k.def)),k.hasOwnProperty("scope"))))for(d in k.scope)b[d]=k.scope[d];a.hasOwnProperty("browse")&&(a.browse.hasOwnProperty("array")&&
|
|
|
|
FormBuilder.regex.arr_out_set.test(a.browse.array)?a.browse.id=RegExp.$1:delete a.browse);for(d in a)"string"==typeof a[d]?(a[d]=[a[d]],k=FormBuilder.replaceStatementsFunction(c,a[d][0],b),!1!==k?a[d]=k:(k=FormBuilder.replaceStatementsArray(a[d][0],b,c),!1!==k?a[d]=k:(a[d]=FormBuilder.replaceStatementsRegex(a[d],b),a[d]=FormBuilder.replaceStatementsPrimary(a[d],b),a[d]=FormBuilder.replaceStatementsArrayValue(a[d],b,c,e)))):"number"===typeof a[d]&&(a[d]=[ref(c,a[d])]);for(d in a)FormBuilder.regex.pri_in_key.test(d)?
|
|
|
|
b[d.substring(1)]=ref(c,a[d]):FormBuilder.regex.arr_in_key.test(d)&&(b[d.substring(2)]=ref(c,a[d]));b=JSON.parse(JSON.stringify(b));for(d in a)if(-1<FormBuilder.spread_attr.indexOf(d))if(a[d]instanceof Array)for(var f in a[d])FormBuilder.replaceStatements(a[d][f],b,g,c,e);else a[d]instanceof Object&&FormBuilder.replaceStatements(a[d],b,g,c,e);return a};
|
|
|
|
FormBuilder.replaceStatementsFunction=function(a,b,g){var c=null,c=FormBuilder.regex.fun_out_val.exec(b);if(null===c)return!1;g.hasOwnProperty(c[1])||(g[c[1]]=ref(a,function(){}));return g[c[1]]};FormBuilder.replaceStatementsArray=function(a,b,g){var c=null,c=FormBuilder.regex.arr_out_set.exec(a);if(null===c)return!1;b.hasOwnProperty(c[1])||(b[c[1]]=ref(g,[]));return b[c[1]]};
|
|
|
|
FormBuilder.replaceStatementsRegex=function(a,b){var g=FormBuilder.regex.reg_out_val,c=null,e=[],d,k,f,h;for(f=0;f<a.length;f++){m=null;e=[];d=-1;for(k=[];null!==(c=g.exec(a[f]))&&!(d>=g.lastIndex);)d=g.lastIndex,e.push(c);for(c=d=0;c<e.length;c++)h=e[c][1],(0<d||0<e[c].index)&&k.push(a[f].substr(d,e[c].index-d)),b.hasOwnProperty(h)||(b[h]=""),k.push(b[h]),d=e[c].index+e[c][0].length;d<a[f].length&&k.push(a[f].substr(d,a[f].length));a=a.slice(0,f).concat(k).concat(a.slice(f+1))}return a};
|
|
|
|
FormBuilder.replaceStatementsPrimary=function(a,b){var g=FormBuilder.regex.pri_out_val,c=null,e=[],d,k,f,h;for(f=0;f<a.length;f++){m=null;e=[];d=0;for(k=[];null!==(c=g.exec(a[f]));)e.push(c);for(c=0;c<e.length;c++)h=e[c][1],(0<d||0<e[c].index)&&k.push(a[f].substr(d,e[c].index-d)),b.hasOwnProperty(h)||(b[h]=""),k.push(b[h]),d=e[c].index+e[c][0].length;d<a[f].length&&k.push(a[f].substr(d,a[f].length));a=a.slice(0,f).concat(k).concat(a.slice(f+1))}return a};
|
|
|
|
FormBuilder.replaceStatementsArrayValue=function(a,b,g,c){b=FormBuilder.regex.arr_out_val;var e=null,d=[],k,f,h,l;for(h=0;h<a.length;h++){m=null;d=[];k=0;for(f=[];null!==(e=b.exec(a[h]));)d.push(e);for(e=0;e<d.length;e++)l=d[e][1]+"."+d[e][2],(0<k||0<d[e].index)&&f.push(a[h].substr(k,d[e].index-k)),c.hasOwnProperty(l)||(c[l]=ref(g)),f.push(c[l]),k=d[e].index+d[e][0].length;k<a[h].length&&f.push(a[h].substr(k,a[h].length));a=a.slice(0,h).concat(f).concat(a.slice(h+1))}return a};
|
|
|
|
FormBuilder.readRef=function(a,b){var g,c="";if("string"==typeof b&&FormBuilder.regex.ref_pri.test(b)){for(;"string"==typeof b&&FormBuilder.regex.ref_pri.test(b);)b=a[b],b instanceof Array&&1==b.length&&(b=b[0]);return b}for(g in b)c=FormBuilder.regex.ref_pri.test(b[g])&&a.hasOwnProperty(b[g])?a[b[g]]instanceof Array?c+FormBuilder.readRef(a,a[b[g]]):c+a[b[g]]:c+b[g].toString();return c};
|
|
|
|
FormBuilder.buildElements=function(a,b,g){var c=[],e,d,k,f,h;h=null;var l=["NULL"];if(a.hasOwnProperty("repeat")){f=FormBuilder.readRef(b,a.repeat);if(isNaN(f))f instanceof Array&&(h=f);else for(h=[],e=0;e<f;e++)h[e]=e;if(h instanceof Array){console.log("repeat",h);console.log(a);delete a.repeat;l=[];f=g["options.value"];for(e=0;e<h.length;e++){b[f]=h[e];l[e]=FormBuilder.buildElements(a,b,g);for(d in l[e].prev)parent.dom.appendChild(l[e].prev[d]);parent.dom.appendChild(l[e].node);for(d in l[e].next)parent.dom.appendChild(l[e].next[d])}console.log(l)}}a.hasOwnProperty("browse");
|
|
|
|
for(h=0;h<l.length;h++){c[h]={prev:[],node:null,next:[]};if(a.hasOwnProperty("prev_nodes"))for(e in a.prev_nodes)c[h].prev=FormBuilder.buildElements(a.prev_nodes[e],b,g);if(a.hasOwnProperty("next_nodes"))for(e in a.next_nodes)c[h].next=FormBuilder.buildElements(a.next_nodes[e],b,g);if(a.hasOwnProperty("node_type"))c[h].node=document.createElement(FormBuilder.readRef(b,a.node_type));else if(a.hasOwnProperty("node_link"))for(e in f=FormBuilder.buildElements(a.node_link,b,g),f)c[h].prev=c[h].prev.concat(f[e].prev),
|
|
|
|
c[h].node=f[e].node,c[h].next=f[e].next.concat(c[h].next);if(a.hasOwnProperty("attributes"))for(e in a.attributes)c[h].node.setAttribute(e,FormBuilder.readRef(b,a.attributes[e]));a.hasOwnProperty("text")&&(c[h].node.innerHTML=a.text);if(a.hasOwnProperty("listeners"))for(e in a.listeners)c[h].node.addEventListener(e,FormBuilder.readRef(b,a.listeners[e]),!1);if(a.hasOwnProperty("children"))for(e in a.children)for(k in f=FormBuilder.buildElements(a.children[e],b,g),f){for(d in f[k].prev)c[h].node.appendChild(f[k].prev[d]);
|
|
|
|
c[h].node.appendChild(f[k].node);for(d in f[k].prev)c[h].node.appendChild(f[k].next[d])}}return a.dom=c};
|