24 lines
9.1 KiB
JavaScript
24 lines
9.1 KiB
JavaScript
var ref=function(a,b){for(var e=null;null==e||a.hasOwnProperty(e);)e="$"+(268435456+Math.floor(4026531839*Math.random())).toString(16)+"$";a[e]=b;return e},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 funcs".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;this.built_form.hasOwnProperty("dom")&&FormBuilder.detachElements(this.parent_element,this.built_form.dom);a=FormBuilder.createElements(this.built_form,this.ref_table,this.ref_assoc);FormBuilder.attachElements(this.parent_element,a)};FormBuilder.formatFormObject=function(a,b){return a};
|
|
FormBuilder.fetchNodeDefinition=function(a,b){var e,c;r=FormBuilder.regex.reg_in_key;if("undefined"!=typeof b&&b.hasOwnProperty(a))return{def:b[a]};for(e in b)if(r.test(e)&&(c=new RegExp(e.slice(1,-1)),c.test(a))){matches={};for(c=1;c<RegExp.length&&10>c;c++)matches["$"+c]=RegExp["$"+c];return{def:b[e],scope:matches}}return{}};
|
|
FormBuilder.replaceStatements=function(a,b,e,c,f){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,e),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(a[d][0],b,c),!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,f)))):"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 h in a[d])FormBuilder.replaceStatements(a[d][h],b,e,c,f);else a[d]instanceof Object&&FormBuilder.replaceStatements(a[d],b,e,c,f);return a};
|
|
FormBuilder.replaceStatementsFunction=function(a,b,e){var c=null,c=FormBuilder.regex.fun_out_val.exec(a);if(null===c)return!1;b.hasOwnProperty(c[1])||(b[c[1]]=ref(e,function(){}));return b[c[1]]};FormBuilder.replaceStatementsArray=function(a,b,e){var c=null,c=FormBuilder.regex.arr_out_set.exec(a);if(null===c)return!1;b.hasOwnProperty(c[1])||(b[c[1]]=ref(e,[]));return b[c[1]]};
|
|
FormBuilder.replaceStatementsRegex=function(a,b){var e=FormBuilder.regex.reg_out_val,c=null,f=[],d,k,h,g;for(h=0;h<a.length;h++){m=null;f=[];d=-1;for(k=[];null!==(c=e.exec(a[h]))&&!(d>=e.lastIndex);)d=e.lastIndex,f.push(c);for(c=d=0;c<f.length;c++)g=f[c][1],(0<d||0<f[c].index)&&k.push(a[h].substr(d,f[c].index-d)),b.hasOwnProperty(g)||(b[g]=""),k.push(b[g]),d=f[c].index+f[c][0].length;d<a[h].length&&k.push(a[h].substr(d,a[h].length));a=a.slice(0,h).concat(k).concat(a.slice(h+1))}return a};
|
|
FormBuilder.replaceStatementsPrimary=function(a,b){var e=FormBuilder.regex.pri_out_val,c=null,f=[],d,k,h,g;for(h=0;h<a.length;h++){m=null;f=[];d=0;for(k=[];null!==(c=e.exec(a[h]));)f.push(c);for(c=0;c<f.length;c++)g=f[c][1],(0<d||0<f[c].index)&&k.push(a[h].substr(d,f[c].index-d)),b.hasOwnProperty(g)||(b[g]=""),k.push(b[g]),d=f[c].index+f[c][0].length;d<a[h].length&&k.push(a[h].substr(d,a[h].length));a=a.slice(0,h).concat(k).concat(a.slice(h+1))}return a};
|
|
FormBuilder.replaceStatementsArrayValue=function(a,b,e,c){b=FormBuilder.regex.arr_out_val;var f=null,d=[],k,h,g,l;for(g=0;g<a.length;g++){m=null;d=[];k=0;for(h=[];null!==(f=b.exec(a[g]));)d.push(f);for(f=0;f<d.length;f++)l=d[f][1]+d[f][2]+d[f][3],(0<k||0<d[f].index)&&h.push(a[g].substr(k,d[f].index-k)),c.hasOwnProperty(l)||(c[l]=ref(e)),h.push(c[l]),k=d[f].index+d[f][0].length;k<a[g].length&&h.push(a[g].substr(k,a[g].length));a=a.slice(0,g).concat(h).concat(a.slice(g+1))}return a};
|
|
FormBuilder.readRef=function(a,b){var e,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(e in b)c=FormBuilder.regex.ref_pri.test(b[e])&&a.hasOwnProperty(b[e])?a[b[e]]instanceof Array?c+FormBuilder.readRef(a,a[b[e]]):c+a[b[e]]:c+b[e].toString();return c};
|
|
FormBuilder.createElements=function(a,b,e){var c=[],f,d,k,h,g,l=["NULL"],n,p;if(a.hasOwnProperty("repeat")&&(g=FormBuilder.readRef(b,a.repeat.n),!isNaN(g)))for(l=[],e.hasOwnProperty(a.repeat.id+":i")||(e[a.repeat.id+":i"]=ref(0)),e.hasOwnProperty(a.repeat.id+":n")||(e[a.repeat.id+":n"]=ref(g)),f=0;f<g;f++)l[f]={},l[f][a.repeat.id+":i"]=f,l[f][a.repeat.id+":n"]=g;if(a.hasOwnProperty("browse")&&(g=FormBuilder.readRef(b,a.browse.array),g instanceof Array)){l=[];n=[];p={};k=new RegExp(a.browse.id+".(.+)");
|
|
for(f in e)k.test(f)&&n.push(RegExp.$1);if(a.browse.hasOwnProperty("funcs"))for(f in a.browse.funcs)k.test(f)&&(p[RegExp.$1]=FormBuilder.readRef(b,a.browse.funcs[f]));e.hasOwnProperty(a.browse.id+":i")||(e[a.browse.id+":i"]=ref(0));e.hasOwnProperty(a.browse.id+":n")||(e[a.browse.id+":n"]=ref(g));for(f=0;f<g.length;f++){l[f]={};for(d in n)p.hasOwnProperty(n[d])?l[f][a.browse.id+"."+n[d]]=p[n[d]](g[f]):g[f].hasOwnProperty(n[d])&&(l[f][a.browse.id+"."+n[d]]=g[f][n[d]]);l[f][a.browse.id+":i"]=f;l[f][a.browse.id+
|
|
":n"]=g.length}}for(d=0;d<l.length;d++){c[d]={prev:[],node:[],next:[]};if(l[d]instanceof Object)for(f in l[d])b[e[f]]=l[d][f];if(a.hasOwnProperty("prev_nodes"))for(f in a.prev_nodes)c[d].prev=FormBuilder.createElements(a.prev_nodes[f],b,e);if(a.hasOwnProperty("next_nodes"))for(f in a.next_nodes)c[d].next=FormBuilder.createElements(a.next_nodes[f],b,e);if(a.hasOwnProperty("node_link"))for(f in g=FormBuilder.createElements(a.node_link,b,e),g)c[d].prev=c[d].prev.concat(g[f].prev),c[d].node=c[d].node.concat(g[f].node),
|
|
c[d].next=g[f].next.concat(c[d].next);else a.hasOwnProperty("node_type")&&c[d].node.push(document.createElement(FormBuilder.readRef(b,a.node_type)));if(a.hasOwnProperty("attributes"))for(f in a.attributes)for(h in c[d].node)c[d].node[h].setAttribute(f,FormBuilder.readRef(b,a.attributes[f]));if(a.hasOwnProperty("css"))for(h in c[d].node)FormBuilder.applyCSS(c[d].node[h],a.css);if(a.hasOwnProperty("text"))for(h in c[d].node)c[d].node[h].innerHTML=FormBuilder.readRef(b,a.text);if(a.hasOwnProperty("listeners"))for(f in a.listeners)for(h in c[d].node)c[d].node[h].addEventListener(f,
|
|
FormBuilder.readRef(b,a.listeners[f]),!1);if(a.hasOwnProperty("children"))for(f in a.children)for(h in g=FormBuilder.createElements(a.children[f],b,e),c[d].node)FormBuilder.attachElements(c[d].node[h],g)}return a.dom=c};
|
|
FormBuilder.attachElements=function(a,b){var e,c;for(e in b){for(c in b[e].prev)b[e].prev[c]instanceof Element&&a.appendChild(b[e].prev[c]),b[e].prev[c]instanceof Object&&FormBuilder.attachElements(a,[b[e].prev[c]]);for(c in b[e].node)b[e].node[c]instanceof Element&&a.appendChild(b[e].node[c]),b[e].node[c]instanceof Object&&FormBuilder.attachElements(a,[b[e].node[c]]);for(c in b[e].next)b[e].next[c]instanceof Element&&a.appendChild(b[e].next[c]),b[e].next[c]instanceof Object&&FormBuilder.attachElements(a,
|
|
[b[e].next[c]])}};
|
|
FormBuilder.detachElements=function(a,b){var e,c;for(e in b){for(c in b[e].prev)b[e].prev[c]instanceof Element&&a.removeChild(b[e].prev[c]),b[e].prev[c]instanceof Object&&FormBuilder.detachElements(a,[b[e].prev[c]]);for(c in b[e].node)b[e].node[c]instanceof Element&&a.removeChild(b[e].node[c]),b[e].node[c]instanceof Object&&FormBuilder.detachElements(a,[b[e].node[c]]);for(c in b[e].next)b[e].next[c]instanceof Element&&a.removeChild(b[e].next[c]),b[e].next[c]instanceof Object&&FormBuilder.detachElements(a,[b[e].next[c]])}};
|
|
FormBuilder.applyCSS=function(a,b){for(var e in b)a.style[e]=b[e]};
|