NxTIC/js/lib/form-builder-min.js

19 lines
8.1 KiB
JavaScript

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