Gestion des 'getter' et 'setter' dynamiques de variables directement sur l'objet, soit l'objet A notre 'FormBuilder' et 'someVar' une de ses variables de construction, 'someVar' est accessible via 'A.' en GET et en SET

This commit is contained in:
xdrm-brackets 2016-09-22 15:01:01 +02:00
parent 6b325f7198
commit f1c32cabdb
4 changed files with 111 additions and 28 deletions

View File

@ -1,25 +1,26 @@
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$"}}; 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;this.observer=new MutationObserver(FormBuilder.DOMUpdates)};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$"},observer:this.observer,scope:{}};
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.debug_time=!1;FormBuilder.debug_time_details=!1; 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.debug_time=!1;FormBuilder.debug_time_details=!1;
FormBuilder.debugStart=function(a,b){if(1!=b||!FormBuilder.debug_time)if(2!=b||!FormBuilder.debug_time_details)return{type:0};var e="["+parseInt(64424509439*Math.random()).toString(16)+"]";console.time(e);console.group(a);return{type:b,time_r:e,group:a}};FormBuilder.debug=function(a,b){if(1!=b||!FormBuilder.debug_time)if(2!=b||!FormBuilder.debug_time_details)return{type:0};console.debug(a)}; FormBuilder.debugStart=function(a,b){if(1!=b||!FormBuilder.debug_time)if(2!=b||!FormBuilder.debug_time_details)return{type:0};var e="["+parseInt(64424509439*Math.random()).toString(16)+"]";console.time(e);console.group(a);return{type:b,time_r:e,group:a}};FormBuilder.debug=function(a,b){if(1!=b||!FormBuilder.debug_time)if(2!=b||!FormBuilder.debug_time_details)return{type:0};console.debug(a)};
FormBuilder.debugStop=function(a){if(1!=a.type||!FormBuilder.debug_time)if(2!=a.type||!FormBuilder.debug_time_details)return null;console.timeEnd(a.time_r);console.groupEnd(a.group)};FormBuilder.prototype.add_definition=function(a){var b=FormBuilder.debugStart("FormBuilder.add_definition",1),e;for(e in a)this.defs_object[e]=a[e];FormBuilder.debugStop(b)}; FormBuilder.debugStop=function(a){if(1!=a.type||!FormBuilder.debug_time)if(2!=a.type||!FormBuilder.debug_time_details)return null;console.timeEnd(a.time_r);console.groupEnd(a.group)};FormBuilder.prototype.add_definition=function(a){var b=FormBuilder.debugStart("FormBuilder.add_definition",1),e;for(e in a)this.defs_object[e]=a[e];FormBuilder.debugStop(b)};
FormBuilder.prototype.build=function(a){var b=FormBuilder.debugStart("FormBuilder.build",1);a=null==a?{}:a;for(var e in a)a[e]=ref(this.ref_table,a[e]),this.ref_assoc[e]=a[e];this.built_form=JSON.parse(JSON.stringify(this.form_object));this.built_form=FormBuilder.replaceStatements(this.built_form,a,this.defs_object,this.ref_table,this.ref_assoc);FormBuilder.debugStop(b)}; FormBuilder.prototype.build=function(a){var b=FormBuilder.debugStart("FormBuilder.build",1);a=null==a?{}:a;var e={},c;for(c in a)a[c]=ref(this.ref_table,a[c]),this.ref_assoc[c]=a[c],this.scope["$"+c]=this.ref_assoc[c],e["$"+c]={get:function(a,b){return function(){return a.ref_table[a.scope["$"+b]]}}(this,c),set:function(a,b){return function(c){a.ref_table[a.scope["$"+b]]=c}}(this,c)},propName=null;Object.defineProperties(this,e);this.built_form=JSON.parse(JSON.stringify(this.form_object));this.built_form=
FormBuilder.prototype.update=function(a){var b=FormBuilder.debugStart("FormBuilder.update",1);a=null==a?{}:a;for(var e in a)this.ref_assoc.hasOwnProperty(e)&&(this.ref_table[this.ref_assoc[e]]=a[e]);FormBuilder.debugStop(b)}; FormBuilder.replaceStatements(this.built_form,a,this.defs_object,this.ref_table,this.ref_assoc);FormBuilder.debugStop(b)};FormBuilder.prototype.update=function(a){var b=FormBuilder.debugStart("FormBuilder.update",1);a=null==a?{}:a;for(var e in a)this.ref_assoc.hasOwnProperty(e)&&(this.ref_table[this.ref_assoc[e]]=a[e]);FormBuilder.debugStop(b)};
FormBuilder.prototype.attach=function(a){var b=FormBuilder.debugStart("FormBuilder.attach",1);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.debugStop(b)}; FormBuilder.prototype.attach=function(a){var b=FormBuilder.debugStart("FormBuilder.attach",1);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")&&(this.observer.disconnect(),FormBuilder.detachElements(this.parent_element,this.built_form.dom));FormBuilder.createElements(this.built_form,this.ref_table,this.ref_assoc);FormBuilder.attachElements(this.parent_element,this.built_form.dom);this.observer.observe(this.parent_element,
FormBuilder.fetchNodeDefinition=function(a,b){var e=FormBuilder.debugStart("FormBuilder::fetchNodeDefinition",2);FormBuilder.debug('node = "'+a+'"',2);var d,f;r=FormBuilder.regex.reg_in_key;if("undefined"!=typeof b&&b.hasOwnProperty(a))return FormBuilder.debugStop(e),{def:b[a]};for(d in b)if(r.test(d)&&(f=new RegExp(d.slice(1,-1)),f.test(a))){matches={};for(f=1;f<RegExp.length&&10>f;f++)matches["$"+f]=RegExp["$"+f];FormBuilder.debugStop(e);return{def:b[d],scope:matches}}FormBuilder.debugStop(e);return{}}; {childList:!0,attributes:!0,characterData:!0,subtree:!0,attributeOldValue:!0,characterDataOldValue:!0});FormBuilder.debugStop(b)};FormBuilder.prototype.getter=function(a){return this.ref_table[this.scope[a]]};FormBuilder.prototype.setter=function(a,b){this.ref_table[this.scope[a]]=b};
FormBuilder.replaceStatements=function(a,b,e,d,f){var g=FormBuilder.debugStart("FormBuilder::replaceStatements",2);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,e),h.hasOwnProperty("def")&&(a.node_link=JSON.parse(JSON.stringify(h.def)),h.hasOwnProperty("scope"))))for(c in h.scope)b[c]= FormBuilder.fetchNodeDefinition=function(a,b){var e=FormBuilder.debugStart("FormBuilder::fetchNodeDefinition",2);FormBuilder.debug('node = "'+a+'"',2);var c,f;r=FormBuilder.regex.reg_in_key;if("undefined"!=typeof b&&b.hasOwnProperty(a))return FormBuilder.debugStop(e),{def:b[a]};for(c in b)if(r.test(c)&&(f=new RegExp(c.slice(1,-1)),f.test(a))){matches={};for(f=1;f<RegExp.length&&10>f;f++)matches["$"+f]=RegExp["$"+f];FormBuilder.debugStop(e);return{def:b[c],scope:matches}}FormBuilder.debugStop(e);return{}};
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(a[c][0],b,d),!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,f)))): FormBuilder.replaceStatements=function(a,b,e,c,f){var g=FormBuilder.debugStart("FormBuilder::replaceStatements",2);a=a instanceof Object?a:{};b=b instanceof Object?JSON.parse(JSON.stringify(b)):{};var d,h;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&&(h=FormBuilder.fetchNodeDefinition(a.node,e),h.hasOwnProperty("def")&&(a.node_link=JSON.parse(JSON.stringify(h.def)),h.hasOwnProperty("scope"))))for(d in h.scope)b[d]=
"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 k in a[c])FormBuilder.debug("**"+c+"["+k+"]",2),FormBuilder.replaceStatements(a[c][k],b,e,d,f);else a[c]instanceof Object&&(FormBuilder.debug("**"+c,2),FormBuilder.replaceStatements(a[c],b,e,d, h.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]],h=FormBuilder.replaceStatementsFunction(a[d][0],b,c),!1!==h?a[d]=h:(h=FormBuilder.replaceStatementsArray(a[d][0],b,c),!1!==h?a[d]=h:(a[d]=FormBuilder.replaceStatementsRegex(a[d],b),a[d]=FormBuilder.replaceStatementsPrimary(a[d],b),a[d]=FormBuilder.replaceStatementsArrayValue(a[d],b,c,f)))):
f));FormBuilder.debugStop(g);return a};FormBuilder.replaceStatementsFunction=function(a,b,e){var d=null,d=FormBuilder.regex.fun_out_val.exec(a);if(null===d)return!1;b.hasOwnProperty(d[1])||(b[d[1]]=ref(e,function(){}));return b[d[1]]};FormBuilder.replaceStatementsArray=function(a,b,e){var d=null,d=FormBuilder.regex.arr_out_set.exec(a);if(null===d)return!1;b.hasOwnProperty(d[1])||(b[d[1]]=ref(e,[]));return b[d[1]]}; "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 k in a[d])FormBuilder.debug("**"+d+"["+k+"]",2),FormBuilder.replaceStatements(a[d][k],b,e,c,f);else a[d]instanceof Object&&(FormBuilder.debug("**"+d,2),FormBuilder.replaceStatements(a[d],b,e,c,
FormBuilder.replaceStatementsRegex=function(a,b){var e=FormBuilder.regex.reg_out_val,d=null,f=[],g,c,h,k;for(h=0;h<a.length;h++){m=null;f=[];g=-1;for(c=[];null!==(d=e.exec(a[h]))&&!(g>=e.lastIndex);)g=e.lastIndex,f.push(d);for(d=g=0;d<f.length;d++)k=f[d][1],(0<g||0<f[d].index)&&c.push(a[h].substr(g,f[d].index-g)),b.hasOwnProperty(k)||(b[k]=""),c.push(b[k]),g=f[d].index+f[d][0].length;g<a[h].length&&c.push(a[h].substr(g,a[h].length));a=a.slice(0,h).concat(c).concat(a.slice(h+1))}return a}; f));FormBuilder.debugStop(g);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.replaceStatementsPrimary=function(a,b){var e=FormBuilder.regex.pri_out_val,d=null,f=[],g,c,h,k;for(h=0;h<a.length;h++){m=null;f=[];g=0;for(c=[];null!==(d=e.exec(a[h]));)f.push(d);for(d=0;d<f.length;d++)k=f[d][1],(0<g||0<f[d].index)&&c.push(a[h].substr(g,f[d].index-g)),b.hasOwnProperty(k)||(b[k]=""),c.push(b[k]),g=f[d].index+f[d][0].length;g<a[h].length&&c.push(a[h].substr(g,a[h].length));a=a.slice(0,h).concat(c).concat(a.slice(h+1))}return a}; FormBuilder.replaceStatementsRegex=function(a,b){var e=FormBuilder.regex.reg_out_val,c=null,f=[],g,d,h,k;for(h=0;h<a.length;h++){m=null;f=[];g=-1;for(d=[];null!==(c=e.exec(a[h]))&&!(g>=e.lastIndex);)g=e.lastIndex,f.push(c);for(c=g=0;c<f.length;c++)k=f[c][1],(0<g||0<f[c].index)&&d.push(a[h].substr(g,f[c].index-g)),b.hasOwnProperty(k)||(b[k]=""),d.push(b[k]),g=f[c].index+f[c][0].length;g<a[h].length&&d.push(a[h].substr(g,a[h].length));a=a.slice(0,h).concat(d).concat(a.slice(h+1))}return a};
FormBuilder.replaceStatementsArrayValue=function(a,b,e,d){b=FormBuilder.regex.arr_out_val;var f=null,g=[],c,h,k,l;for(k=0;k<a.length;k++){m=null;g=[];c=0;for(h=[];null!==(f=b.exec(a[k]));)g.push(f);for(f=0;f<g.length;f++)l=g[f][1]+g[f][2]+g[f][3],(0<c||0<g[f].index)&&h.push(a[k].substr(c,g[f].index-c)),d.hasOwnProperty(l)||(d[l]=ref(e)),h.push(d[l]),c=g[f].index+g[f][0].length;c<a[k].length&&h.push(a[k].substr(c,a[k].length));a=a.slice(0,k).concat(h).concat(a.slice(k+1))}return a}; FormBuilder.replaceStatementsPrimary=function(a,b){var e=FormBuilder.regex.pri_out_val,c=null,f=[],g,d,h,k;for(h=0;h<a.length;h++){m=null;f=[];g=0;for(d=[];null!==(c=e.exec(a[h]));)f.push(c);for(c=0;c<f.length;c++)k=f[c][1],(0<g||0<f[c].index)&&d.push(a[h].substr(g,f[c].index-g)),b.hasOwnProperty(k)||(b[k]=""),d.push(b[k]),g=f[c].index+f[c][0].length;g<a[h].length&&d.push(a[h].substr(g,a[h].length));a=a.slice(0,h).concat(d).concat(a.slice(h+1))}return a};
FormBuilder.readRef=function(a,b){var e,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(e in b)d=FormBuilder.regex.ref_pri.test(b[e])&&a.hasOwnProperty(b[e])?a[b[e]]instanceof Array?d+FormBuilder.readRef(a,a[b[e]]):d+a[b[e]]:d+b[e].toString();return d}; FormBuilder.replaceStatementsArrayValue=function(a,b,e,c){b=FormBuilder.regex.arr_out_val;var f=null,g=[],d,h,k,l;for(k=0;k<a.length;k++){m=null;g=[];d=0;for(h=[];null!==(f=b.exec(a[k]));)g.push(f);for(f=0;f<g.length;f++)l=g[f][1]+g[f][2]+g[f][3],(0<d||0<g[f].index)&&h.push(a[k].substr(d,g[f].index-d)),c.hasOwnProperty(l)||(c[l]=ref(e)),h.push(c[l]),d=g[f].index+g[f][0].length;d<a[k].length&&h.push(a[k].substr(d,a[k].length));a=a.slice(0,k).concat(h).concat(a.slice(k+1))}return a};
FormBuilder.createElements=function(a,b,e){var d=FormBuilder.debugStart("FormBuilder::createElements",2),f=[],g,c,h,k,l,n=["NULL"],p,q;if(a.hasOwnProperty("repeat")&&(l=FormBuilder.readRef(b,a.repeat.n),!isNaN(l)))for(n=[],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(l)),g=0;g<l;g++)n[g]={},n[g][a.repeat.id+":i"]=g,n[g][a.repeat.id+":n"]=l;if(a.hasOwnProperty("browse")&&(l=FormBuilder.readRef(b,a.browse.array),l instanceof 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};
Array)){n=[];p=[];q={};h=new RegExp(a.browse.id+".(.+)");for(g in e)h.test(g)&&p.push(RegExp.$1);if(a.browse.hasOwnProperty("funcs"))for(g in a.browse.funcs)h.test(g)&&(q[RegExp.$1]=FormBuilder.readRef(b,a.browse.funcs[g]));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(l));for(g=0;g<l.length;g++){n[g]={};for(c in p)q.hasOwnProperty(p[c])?n[g][a.browse.id+"."+p[c]]=q[p[c]](l[g]):l[g].hasOwnProperty(p[c])&&(n[g][a.browse.id+ FormBuilder.createElements=function(a,b,e){var c=FormBuilder.debugStart("FormBuilder::createElements",2),f=[],g,d,h,k,l,n=["NULL"],p,q;if(a.hasOwnProperty("repeat")&&(l=FormBuilder.readRef(b,a.repeat.n),!isNaN(l)))for(n=[],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(l)),g=0;g<l;g++)n[g]={},n[g][a.repeat.id+":i"]=g,n[g][a.repeat.id+":n"]=l;if(a.hasOwnProperty("browse")&&(l=FormBuilder.readRef(b,a.browse.array),l instanceof
"."+p[c]]=l[g][p[c]]);n[g][a.browse.id+":i"]=g;n[g][a.browse.id+":n"]=l.length}}for(c=0;c<n.length;c++){f[c]={prev:[],node:[],next:[]};if(n[c]instanceof Object)for(g in n[c])b[e[g]]=n[c][g];if(a.hasOwnProperty("prev_nodes"))for(g in a.prev_nodes)f[c].prev=FormBuilder.createElements(a.prev_nodes[g],b,e);if(a.hasOwnProperty("next_nodes"))for(g in a.next_nodes)f[c].next=FormBuilder.createElements(a.next_nodes[g],b,e);if(a.hasOwnProperty("node_link"))for(g in l=FormBuilder.createElements(a.node_link, Array)){n=[];p=[];q={};h=new RegExp(a.browse.id+".(.+)");for(g in e)h.test(g)&&p.push(RegExp.$1);if(a.browse.hasOwnProperty("funcs"))for(g in a.browse.funcs)h.test(g)&&(q[RegExp.$1]=FormBuilder.readRef(b,a.browse.funcs[g]));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(l));for(g=0;g<l.length;g++){n[g]={};for(d in p)q.hasOwnProperty(p[d])?n[g][a.browse.id+"."+p[d]]=q[p[d]](l[g]):l[g].hasOwnProperty(p[d])&&(n[g][a.browse.id+
b,e),l)f[c].prev=f[c].prev.concat(l[g].prev),f[c].node=f[c].node.concat(l[g].node),f[c].next=l[g].next.concat(f[c].next);else a.hasOwnProperty("node_type")&&f[c].node.push(document.createElement(FormBuilder.readRef(b,a.node_type)));if(a.hasOwnProperty("attributes"))for(g in a.attributes)for(k in f[c].node)f[c].node[k].setAttribute(g,FormBuilder.readRef(b,a.attributes[g]));if(a.hasOwnProperty("css"))for(k in f[c].node)FormBuilder.applyCSS(f[c].node[k],a.css);if(a.hasOwnProperty("text"))for(k in f[c].node)f[c].node[k].innerHTML= "."+p[d]]=l[g][p[d]]);n[g][a.browse.id+":i"]=g;n[g][a.browse.id+":n"]=l.length}}for(d=0;d<n.length;d++){f[d]={prev:[],node:[],next:[]};if(n[d]instanceof Object)for(g in n[d])b[e[g]]=n[d][g];if(a.hasOwnProperty("prev_nodes"))for(g in a.prev_nodes)f[d].prev=FormBuilder.createElements(a.prev_nodes[g],b,e);if(a.hasOwnProperty("next_nodes"))for(g in a.next_nodes)f[d].next=FormBuilder.createElements(a.next_nodes[g],b,e);if(a.hasOwnProperty("node_link"))for(g in l=FormBuilder.createElements(a.node_link,
FormBuilder.readRef(b,a.text);if(a.hasOwnProperty("listeners"))for(g in a.listeners)for(k in f[c].node)f[c].node[k].addEventListener(g,FormBuilder.readRef(b,a.listeners[g]),!1);if(a.hasOwnProperty("children"))for(g in a.children)for(k in l=FormBuilder.createElements(a.children[g],b,e),f[c].node)FormBuilder.attachElements(f[c].node[k],l)}b=a.hasOwnProperty("node")?a.node:a.node_type;FormBuilder.debug("<"+b+">",2);FormBuilder.debugStop(d);return a.dom=f}; b,e),l)f[d].prev=f[d].prev.concat(l[g].prev),f[d].node=f[d].node.concat(l[g].node),f[d].next=l[g].next.concat(f[d].next);else a.hasOwnProperty("node_type")&&f[d].node.push(document.createElement(FormBuilder.readRef(b,a.node_type)));if(a.hasOwnProperty("attributes"))for(g in a.attributes)for(k in f[d].node)f[d].node[k].setAttribute(g,FormBuilder.readRef(b,a.attributes[g]));if(a.hasOwnProperty("css"))for(k in f[d].node)FormBuilder.applyCSS(f[d].node[k],a.css);if(a.hasOwnProperty("text"))for(k in f[d].node)f[d].node[k].innerHTML=
FormBuilder.attachElements=function(a,b){var e=FormBuilder.debugStart("FormBuilder::attachElements",2),d,f;for(d in b){for(f in b[d].prev)b[d].prev[f]instanceof Element&&a.appendChild(b[d].prev[f]),b[d].prev[f]instanceof Object&&FormBuilder.attachElements(a,[b[d].prev[f]]);for(f in b[d].node)b[d].node[f]instanceof Element&&a.appendChild(b[d].node[f]),b[d].node[f]instanceof Object&&FormBuilder.attachElements(a,[b[d].node[f]]);for(f in b[d].next)b[d].next[f]instanceof Element&&a.appendChild(b[d].next[f]), FormBuilder.readRef(b,a.text);if(a.hasOwnProperty("listeners"))for(g in a.listeners)for(k in f[d].node)f[d].node[k].addEventListener(g,FormBuilder.readRef(b,a.listeners[g]),!1);if(a.hasOwnProperty("children"))for(g in a.children)for(k in l=FormBuilder.createElements(a.children[g],b,e),f[d].node)FormBuilder.attachElements(f[d].node[k],l)}b=a.hasOwnProperty("node")?a.node:a.node_type;FormBuilder.debug("<"+b+">",2);FormBuilder.debugStop(c);return a.dom=f};
b[d].next[f]instanceof Object&&FormBuilder.attachElements(a,[b[d].next[f]])}FormBuilder.debugStop(e)}; FormBuilder.attachElements=function(a,b){var e=FormBuilder.debugStart("FormBuilder::attachElements",2),c,f;for(c in b){for(f in b[c].prev)b[c].prev[f]instanceof Element&&a.appendChild(b[c].prev[f]),b[c].prev[f]instanceof Object&&FormBuilder.attachElements(a,[b[c].prev[f]]);for(f in b[c].node)b[c].node[f]instanceof Element&&a.appendChild(b[c].node[f]),b[c].node[f]instanceof Object&&FormBuilder.attachElements(a,[b[c].node[f]]);for(f in b[c].next)b[c].next[f]instanceof Element&&a.appendChild(b[c].next[f]),
FormBuilder.detachElements=function(a,b){var e,d;for(e in b){for(d in b[e].prev)b[e].prev[d]instanceof Element&&a.removeChild(b[e].prev[d]),b[e].prev[d]instanceof Object&&FormBuilder.detachElements(a,[b[e].prev[d]]);for(d in b[e].node)b[e].node[d]instanceof Element&&a.removeChild(b[e].node[d]),b[e].node[d]instanceof Object&&FormBuilder.detachElements(a,[b[e].node[d]]);for(d in b[e].next)b[e].next[d]instanceof Element&&a.removeChild(b[e].next[d]),b[e].next[d]instanceof Object&&FormBuilder.detachElements(a, b[c].next[f]instanceof Object&&FormBuilder.attachElements(a,[b[c].next[f]])}FormBuilder.debugStop(e)};
[b[e].next[d]])}};FormBuilder.applyCSS=function(a,b){for(var e in b)a.style[e]=b[e]}; 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]};FormBuilder.DOMUpdates=function(a){console.log(a)};

View File

@ -43,9 +43,16 @@ var ref = function(ref_table, variable){
*/ */
var FormBuilder = function(form_object){ var FormBuilder = function(form_object){
/* [1] Enregistrement du formulaire
=========================================================*/
/* On définit le formulaire (sous forme de description formelle) */ /* On définit le formulaire (sous forme de description formelle) */
this.form_object = form_object; this.form_object = form_object;
/* [2] On initialise notre Observer
=========================================================*/
this.observer = new MutationObserver( FormBuilder.DOMUpdates );
}; };
@ -67,7 +74,9 @@ FormBuilder.prototype = {
built_form: null, // Object correspondant au formulaire construit built_form: null, // Object correspondant au formulaire construit
root_element: null, // Element correspondant à l'objet construit root_element: null, // Element correspondant à l'objet construit
ref_table: {'$00000000$': null}, // Tableau contenant la liste des références ref_table: {'$00000000$': null}, // Tableau contenant la liste des références
ref_assoc: {'NULL': '$00000000$'} // Association entre table de référence et scope d'entrée ref_assoc: {'NULL': '$00000000$'}, // Association entre table de référence et scope d'entrée
observer: this.observer, // Observeur du sous-DOM qui sera créé
scope: {} // Scope pour les variables (getters + setters)
}; };
@ -191,15 +200,43 @@ FormBuilder.prototype.build = function(scope){
/* (1) On initialise le scope vide s'il n'est pas donné */ /* (1) On initialise le scope vide s'il n'est pas donné */
scope = (scope == null) ? {} : scope; scope = (scope == null) ? {} : scope;
var properties = {};
/* (2) On transforme toutes les variables en références */ /* (2) On transforme toutes les variables en références */
for( var key in scope ){ for( var key in scope ){
// On enregistre la référence // On enregistre la référence
scope[key] = ref(this.ref_table, scope[key]); scope[key] = ref(this.ref_table, scope[key]);
// On enregistre l'association, pour modifier plus tard // On enregistre l'association, pour modifier plus tard
this.ref_assoc[key] = scope[key]; this.ref_assoc[key] = scope[key];
// On ajoute une entrée dans le @scope
// this.scope['$'+key] = this.ref_assoc[key];
this.scope['$'+key] = this.ref_assoc[key];
var fset = function(o, k, v){ o.ref_table[ o.scope['$'+k] ] = v; };
properties['$'+key] = {
get: (function(o, k){ return function(){
return o.ref_table[ o.scope['$'+k] ];
}; })(this, key),
set: (function(o, k){ return function(v){
o.ref_table[ o.scope['$'+k] ] = v;
}; })(this, key)
};
propName = null;
} }
Object.defineProperties(this, properties);
/* [1] On clone l'object /* [1] On clone l'object
=========================================================*/ =========================================================*/
this.built_form = JSON.parse(JSON.stringify(this.form_object)); this.built_form = JSON.parse(JSON.stringify(this.form_object));
@ -265,6 +302,10 @@ FormBuilder.prototype.attach = function(parent){
=========================================================*/ =========================================================*/
if( this.built_form.hasOwnProperty('dom') ){ if( this.built_form.hasOwnProperty('dom') ){
/* (1) On déconnecte l'observer */
this.observer.disconnect();
/* (2) On détache les éléments */
FormBuilder.detachElements(this.parent_element, this.built_form.dom); FormBuilder.detachElements(this.parent_element, this.built_form.dom);
} }
@ -273,14 +314,48 @@ FormBuilder.prototype.attach = function(parent){
/* [1] On construit le DOM /* [1] On construit le DOM
=========================================================*/ =========================================================*/
/* (1) On récupère les éléments */
var dom = FormBuilder.createElements(this.built_form, this.ref_table, this.ref_assoc); var dom = FormBuilder.createElements(this.built_form, this.ref_table, this.ref_assoc);
FormBuilder.attachElements(this.parent_element, dom); /* (2) On attache les éléments */
FormBuilder.attachElements(this.parent_element, this.built_form.dom);
/* (3) On lance l'observer */
this.observer.observe(this.parent_element, {
childList: true,
attributes: true,
characterData: true,
subtree: true,
attributeOldValue: true,
characterDataOldValue: true
});
FormBuilder.debugStop(dbg); FormBuilder.debugStop(dbg);
}; };
/* CUSTOM GETTER DE VARIABLE
*
* @varName<String> Nom de la variable
*
* @return varValue<mixed> Retourne la valeur de la variable
*
*/
FormBuilder.prototype.getter = function(varName){
return this.ref_table[ this.scope[varName] ];
};
/* CUSTOM SETTER DE VARIABLE
*
* @varName<String> Nom de la variable
* @newValue<mixed> Nouvelle valeur de la variable
*
*/
FormBuilder.prototype.setter = function(varName, newValue){
this.ref_table[ this.scope[varName] ] = newValue;
};
/************************************************/ /************************************************/
/* ____ _____ _ _____ ___ ____ */ /* ____ _____ _ _____ ___ ____ */
/* / ___|_ _|/ \|_ _|_ _/ ___| */ /* / ___|_ _|/ \|_ _|_ _/ ___| */
@ -1242,7 +1317,7 @@ FormBuilder.attachElements = function(parent, set){
}; };
/* DETACHE UN JEU D'ELEMENTS A UN PARENT /* DETACHE UN JEU D'ELEMENTS D'UN PARENT
* *
* @parent<Element> Element parent * @parent<Element> Element parent
* @set<Array> Set d'enfants à attacher * @set<Array> Set d'enfants à attacher
@ -1321,3 +1396,9 @@ FormBuilder.applyCSS = function(element, css){
element.style[r] = css[r]; element.style[r] = css[r];
}; };
FormBuilder.DOMUpdates = function(events){
// TODO
console.log(events);
};

View File

@ -2,4 +2,4 @@ document.body.innerHTML="";"use strict";
var default_definition={input:{node_type:"input"},"/^h([1-6])$/":{node_type:"h{$1}"},br:{node_type:"br"},option:{node_type:"option"},select:{node_type:"select"},span:{node_type:"span"},"/^br([0-9]+)$/":{node:"br",repeat:{n:"{$1}",id:"brs"}}},custom_definition={"/^input.([a-z]+)$/":{node:"input",attributes:{type:"{$1}","data-name":"{name}",value:"{value}",placeholder:"{placeholder}"},listeners:{click:"{click_listener()}"}},"custom-select":{node:"span",attributes:{"class":"select-container nobold"},children:[{node:"select", var default_definition={input:{node_type:"input"},"/^h([1-6])$/":{node_type:"h{$1}"},br:{node_type:"br"},option:{node_type:"option"},select:{node_type:"select"},span:{node_type:"span"},"/^br([0-9]+)$/":{node:"br",repeat:{n:"{$1}",id:"brs"}}},custom_definition={"/^input.([a-z]+)$/":{node:"input",attributes:{type:"{$1}","data-name":"{name}",value:"{value}",placeholder:"{placeholder}"},listeners:{click:"{click_listener()}"}},"custom-select":{node:"span",attributes:{"class":"select-container nobold"},children:[{node:"select",
attributes:{"data-name":"{name}"},children:[{node:"option",attributes:{value:"{options:i}"},text:"{options.value}",browse:{array:"{options[]}",funcs:{"options.value":"{getoptval()}"}}}],listeners:{change:"{listener()}"}}]}},contactForm={node:"h4",attributes:{"data-icon":"o","class":"new-contact colo2"},children:[{node:"input.hidden",$name:"uid",$value:"{uid}"},{node:"input.hidden",$name:"call",$value:"{call}"},{node:"input.hidden",$name:"sms",$value:"{sms}"},{node:"input.hidden",$name:"countcall", attributes:{"data-name":"{name}"},children:[{node:"option",attributes:{value:"{options:i}"},text:"{options.value}",browse:{array:"{options[]}",funcs:{"options.value":"{getoptval()}"}}}],listeners:{change:"{listener()}"}}]}},contactForm={node:"h4",attributes:{"data-icon":"o","class":"new-contact colo2"},children:[{node:"input.hidden",$name:"uid",$value:"{uid}"},{node:"input.hidden",$name:"call",$value:"{call}"},{node:"input.hidden",$name:"sms",$value:"{sms}"},{node:"input.hidden",$name:"countcall",
$value:"{count_call}",$click_listener:"{my_listener()}"},{node:"input.hidden",$name:"countsms",$value:"{count_sms}"},{node:"input.text",$name:"number",$value:"{number}"},{node:"custom-select",$name:"existing",$options:"{existing[]}",$listener:"{existingsel()}"},{node:"input.text",$name:"username",$placeholder:"{username_ph}",$value:"{username}"},{node:"input.submit",$value:"Enregistrer",attributes:{"class":"primary sub-number"}}],next_nodes:[{node:"br"}]},a="<h4 data-icon='o' class='new-contact color2'> \n\t<input type='hidden' data-name='uid' value='@uid'>\n\t<input type='hidden' data-name='call' value='@call'>\n\t<input type='hidden' data-name='sms' value='@sms'>\n\t<input type='hidden' data-name='countcall' value='@countcall'>\n\t<input type='hidden' data-name='countsms' value='@countsms'>\n\t<input type='text' data-name='number' placeholder='Num\u00e9ro de t\u00e9l\u00e9phone' value='@number' > \n\t<span class='select-container nobold'><select data-name='existing'>\n\t\t<option value='.'>Utiliser pseudo</option>\n@contacts\t</select></span>\n\t&nbsp;&nbsp;&nbsp;&nbsp;ou&nbsp;&nbsp;&nbsp;&nbsp;\n\t<input type='text' data-name='username' placeholder='Pseudo' value='@username' > \n\t<input type='submit' class='primary sub-number' value='Enregistrer'>\n</h4>\n\n", $value:"{count_call}",$click_listener:"{my_listener()}"},{node:"input.hidden",$name:"countsms",$value:"{count_sms}"},{node:"input.text",$name:"number",$value:"{number}"},{node:"custom-select",$name:"existing",$options:"{existing[]}",$listener:"{existingsel()}"},{node:"input.text",$name:"username",$placeholder:"{username_ph}",$value:"{username}"},{node:"input.submit",$value:"Enregistrer",attributes:{"class":"primary sub-number"}}],next_nodes:[{node:"br"}]},a="<h4 data-icon='o' class='new-contact color2'> \n\t<input type='hidden' data-name='uid' value='@uid'>\n\t<input type='hidden' data-name='call' value='@call'>\n\t<input type='hidden' data-name='sms' value='@sms'>\n\t<input type='hidden' data-name='countcall' value='@countcall'>\n\t<input type='hidden' data-name='countsms' value='@countsms'>\n\t<input type='text' data-name='number' placeholder='Num\u00e9ro de t\u00e9l\u00e9phone' value='@number' > \n\t<span class='select-container nobold'><select data-name='existing'>\n\t\t<option value='.'>Utiliser pseudo</option>\n@contacts\t</select></span>\n\t&nbsp;&nbsp;&nbsp;&nbsp;ou&nbsp;&nbsp;&nbsp;&nbsp;\n\t<input type='text' data-name='username' placeholder='Pseudo' value='@username' > \n\t<input type='submit' class='primary sub-number' value='Enregistrer'>\n</h4>\n\n",
fb=new FormBuilder(contactForm);fb.add_definition(default_definition);fb.add_definition(custom_definition);fb.build({uid:1,call:2,sms:3,count_call:4,count_sms:5,number:"01 02 03 04 05",existing:["Jean","Archibald","Daniel","Maurice"],username:"moi-meme",my_listener:function(b){console.log("clicked",b)},existingsel:function(b){console.log("selected",b)},getoptval:function(b){return b}});fb.attach(document.body); fb=new FormBuilder(contactForm);fb.add_definition(default_definition);fb.add_definition(custom_definition);fb.build({uid:1,call:2,sms:3,count_call:4,count_sms:5,number:"01 02 03 04 05",existing:["Jean","Archibald","Daniel","Maurice"],username:"moi-meme",username_ph:"Pseudo",my_listener:function(b){console.log("clicked",b)},existingsel:function(b){console.log("selected",b)},getoptval:function(b){return b}});fb.attach(document.body);

View File

@ -103,6 +103,7 @@ var a = ("<h4 data-icon='o' class='new-contact color2'> \n"+
// FormBuilder.debug_time = true; // FormBuilder.debug_time = true;
// FormBuilder.debug_time_details = true; // FormBuilder.debug_time_details = true;
var fb = new FormBuilder(contactForm); var fb = new FormBuilder(contactForm);
fb.add_definition(default_definition); fb.add_definition(default_definition);
fb.add_definition(custom_definition); fb.add_definition(custom_definition);
@ -115,8 +116,8 @@ fb.build({
number: '01 02 03 04 05', number: '01 02 03 04 05',
existing: ['Jean', 'Archibald', 'Daniel', 'Maurice'], existing: ['Jean', 'Archibald', 'Daniel', 'Maurice'],
username: 'moi-meme', username: 'moi-meme',
// username_ph: 'Pseudo', username_ph: 'Pseudo',
my_listener: function(e){ console.log('clicked', e); }, my_listener: function(e){ console.log('clicked', e); },
existingsel: function(e){ console.log('selected', e); }, existingsel: function(e){ console.log('selected', e); },
getoptval: function(opt){ return opt; } getoptval: function(opt){ return opt; }
}); });