Gestion de la création d'un 'repeat' ou 'browse' dans un 'node_link', possibilité d'imbrication de 'repeat'/'browse'

This commit is contained in:
xdrm-brackets 2016-09-20 19:15:33 +02:00
parent ca3c151867
commit f4f749ffcc
4 changed files with 81 additions and 64 deletions

View File

@ -1,18 +1,20 @@
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.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.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 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.replaceStatements=function(a,b,f,d,e){a=a instanceof Object?a:{};b=b instanceof Object?JSON.parse(JSON.stringify(b)):{};var c,k;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&&(k=FormBuilder.fetchNodeDefinition(a.node,f),k.hasOwnProperty("def")&&(a.node_link=JSON.parse(JSON.stringify(k.def)),k.hasOwnProperty("scope"))))for(c in k.scope)b[c]=k.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]],k=FormBuilder.replaceStatementsFunction(d,a[c][0],b),!1!==k?a[c]=k:(k=FormBuilder.replaceStatementsArray(a[c][0],b,d),!1!==k?a[c]=k:(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 h in a[c])FormBuilder.replaceStatements(a[c][h],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.replaceStatementsRegex=function(a,b){var f=FormBuilder.regex.reg_out_val,d=null,e=[],c,k,h,g;for(h=0;h<a.length;h++){m=null;e=[];c=-1;for(k=[];null!==(d=f.exec(a[h]))&&!(c>=f.lastIndex);)c=f.lastIndex,e.push(d);for(d=c=0;d<e.length;d++)g=e[d][1],(0<c||0<e[d].index)&&k.push(a[h].substr(c,e[d].index-c)),b.hasOwnProperty(g)||(b[g]=""),k.push(b[g]),c=e[d].index+e[d][0].length;c<a[h].length&&k.push(a[h].substr(c,a[h].length));a=a.slice(0,h).concat(k).concat(a.slice(h+1))}return a};
FormBuilder.replaceStatementsPrimary=function(a,b){var f=FormBuilder.regex.pri_out_val,d=null,e=[],c,k,h,g;for(h=0;h<a.length;h++){m=null;e=[];c=0;for(k=[];null!==(d=f.exec(a[h]));)e.push(d);for(d=0;d<e.length;d++)g=e[d][1],(0<c||0<e[d].index)&&k.push(a[h].substr(c,e[d].index-c)),b.hasOwnProperty(g)||(b[g]=""),k.push(b[g]),c=e[d].index+e[d][0].length;c<a[h].length&&k.push(a[h].substr(c,a[h].length));a=a.slice(0,h).concat(k).concat(a.slice(h+1))}return a};
FormBuilder.replaceStatementsArrayValue=function(a,b,f,d){b=FormBuilder.regex.arr_out_val;var e=null,c=[],k,h,g,l;for(g=0;g<a.length;g++){m=null;c=[];k=0;for(h=[];null!==(e=b.exec(a[g]));)c.push(e);for(e=0;e<c.length;e++)l=c[e][1]+c[e][2]+c[e][3],(0<k||0<c[e].index)&&h.push(a[g].substr(k,c[e].index-k)),d.hasOwnProperty(l)||(d[l]=ref(f)),h.push(d[l]),k=c[e].index+c[e][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 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]])}};
FormBuilder.createElements=function(a,b,f){var d=[],e,c,k,h,g,l=["NULL"],n,p;if(a.hasOwnProperty("repeat")&&(g=FormBuilder.readRef(b,a.repeat.n),!isNaN(g)))for(l=[],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++)l[e]={},l[e][a.repeat.id+":i"]=e,l[e][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(e in f)k.test(e)&&n.push(RegExp.$1);if(a.browse.hasOwnProperty("funcs"))for(e in a.browse.funcs)k.test(e)&&(p[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++){l[e]={};for(c in n)g[e].hasOwnProperty(n[c])?l[e][a.browse.id+"."+n[c]]=g[e][n[c]]:p.hasOwnProperty(n[c])&&(l[e][a.browse.id+"."+n[c]]=p[n[c]](g[e]));l[e][a.browse.id+":i"]=e;l[e][a.browse.id+
":n"]=g.length}}for(c=0;c<l.length;c++){d[c]={prev:[],node:[],next:[]};if(l[c]instanceof Object)for(e in l[c])b[f[e]]=l[c][e];if(a.hasOwnProperty("prev_nodes"))for(e in a.prev_nodes)d[c].prev=FormBuilder.createElements(a.prev_nodes[e],b,f);if(a.hasOwnProperty("next_nodes"))for(e in a.next_nodes)d[c].next=FormBuilder.createElements(a.next_nodes[e],b,f);if(a.hasOwnProperty("node_link"))for(e in g=FormBuilder.createElements(a.node_link,b,f),g)d[c].prev=d[c].prev.concat(g[e].prev),d[c].node=d[c].node.concat(g[e].node),
d[c].next=g[e].next.concat(d[c].next);else a.hasOwnProperty("node_type")&&d[c].node.push(document.createElement(FormBuilder.readRef(b,a.node_type)));if(a.hasOwnProperty("attributes"))for(e in a.attributes)for(h in d[c].node)d[c].node[h].setAttribute(e,FormBuilder.readRef(b,a.attributes[e]));if(a.hasOwnProperty("text"))for(h in d[c].node)d[c].node[h].innerHTML=FormBuilder.readRef(b,a.text);if(a.hasOwnProperty("listeners"))for(e in a.listeners)for(h in d[c].node)d[c].node[h].addEventListener(e,FormBuilder.readRef(b,
a.listeners[e]),!1);if(a.hasOwnProperty("children"))for(e in a.children)for(h in g=FormBuilder.createElements(a.children[e],b,f),d[c].node)FormBuilder.attachElements(d[c].node[h],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]]);for(d in b[f].node)b[f].node[d]instanceof Element&&a.appendChild(b[f].node[d]),b[f].node[d]instanceof Object&&FormBuilder.attachElements(a,[b[f].node[d]]);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]])}};

View File

@ -210,7 +210,7 @@ FormBuilder.prototype.attach = function(parent){
/* [1] On construit le DOM
=========================================================*/
var dom = FormBuilder.buildElements(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);
@ -843,12 +843,12 @@ FormBuilder.readRef = function(ref_table, parts){
* @return built<Element[]> Retourne les éléments construits
*
*/
FormBuilder.buildElements = function(definition, ref_table, ref_assoc){
FormBuilder.createElements = function(definition, ref_table, ref_assoc){
/* [0] Initialisation
===========================================================*/
var built = [];
var i, j, k, tmp, b,
var i, j, k, n, tmp, b,
repeated = ['NULL'],
scope, funcs;
@ -969,7 +969,7 @@ FormBuilder.buildElements = function(definition, ref_table, ref_assoc){
/* (1) On initialise l'élément courant */
built[b] = { prev: [], node: null, next: [] };
built[b] = { prev: [], node: [], next: [] };
/* (2) Gestion des variables de 'repeat' et 'browse' */
if( repeated[b] instanceof Object ){
@ -980,47 +980,44 @@ FormBuilder.buildElements = function(definition, ref_table, ref_assoc){
}
/* [3] On construit les éléments @prev_nodes, s'ils existent
===========================================================*/
if( definition.hasOwnProperty('prev_nodes') )
for( i in definition.prev_nodes )
built[b].prev = FormBuilder.buildElements(definition.prev_nodes[i], ref_table, ref_assoc);
built[b].prev = FormBuilder.createElements(definition.prev_nodes[i], ref_table, ref_assoc);
/* [4] On construit les éléments @next_nodes s'ils existent
===========================================================*/
if( definition.hasOwnProperty('next_nodes') )
for( i in definition.next_nodes )
built[b].next = FormBuilder.buildElements(definition.next_nodes[i], ref_table, ref_assoc);
built[b].next = FormBuilder.createElements(definition.next_nodes[i], ref_table, ref_assoc);
/* [5] On construit l'objet actuel
===========================================================*/
/* (1) Si c'est un sub-node, on récupère la définition récursivement */
if( definition.hasOwnProperty('node_link') ){
/* (1) On crée l'élément
---------------------------------------------------------*/
/* (1) Si on a le type de <tag> hmtl */
if( definition.hasOwnProperty('node_type') )
built[b].node = document.createElement( FormBuilder.readRef(ref_table, definition.node_type) );
/* (2) Si c'est un sub-node, on récupère la définition récursivement */
else if( definition.hasOwnProperty('node_link') ){
tmp = FormBuilder.buildElements(definition.node_link, ref_table, ref_assoc);
tmp = FormBuilder.createElements(definition.node_link, ref_table, ref_assoc);
for( i in tmp ){
/* On ajoute les éléments à l'instance actuelle */
built[b].prev = built[b].prev.concat( tmp[i].prev );
built[b].node = tmp[i].node;
built[b].node = built[b].node.concat( tmp[i].node );
built[b].next = tmp[i].next.concat( built[b].next );
}
}
/* (2) Si on a le type de <tag> hmtl */
else if( definition.hasOwnProperty('node_type') )
built[b].node.push( document.createElement( FormBuilder.readRef(ref_table, definition.node_type) ) );
/* (2) On ajoute les attributs
---------------------------------------------------------*/
@ -1028,7 +1025,9 @@ FormBuilder.buildElements = function(definition, ref_table, ref_assoc){
// Pour chaque attribut, on définit
for( i in definition.attributes )
built[b].node.setAttribute( i, FormBuilder.readRef(ref_table, definition.attributes[i]) );
// Pour chaque node
for( n in built[b].node )
built[b].node[n].setAttribute( i, FormBuilder.readRef(ref_table, definition.attributes[i]) );
}
@ -1038,7 +1037,8 @@ FormBuilder.buildElements = function(definition, ref_table, ref_assoc){
if( definition.hasOwnProperty('text') ){
// Note: Override les enfants
built[b].node.innerHTML = FormBuilder.readRef(ref_table, definition.text);
for( n in built[b].node )
built[b].node[n].innerHTML = FormBuilder.readRef(ref_table, definition.text);
}
@ -1049,7 +1049,10 @@ FormBuilder.buildElements = function(definition, ref_table, ref_assoc){
// Pour chaque listener
for( i in definition.listeners )
built[b].node.addEventListener( i, FormBuilder.readRef(ref_table, definition.listeners[i]), false );
// Pour chaque node
for( n in built[b].node )
built[b].node[n].addEventListener( i, FormBuilder.readRef(ref_table, definition.listeners[i]), false );
}
@ -1061,9 +1064,12 @@ FormBuilder.buildElements = function(definition, ref_table, ref_assoc){
/* (1) Pour chaque enfant */
for( i in definition.children ){
tmp = FormBuilder.buildElements(definition.children[i], ref_table, ref_assoc);
FormBuilder.attachElements(built[b].node, tmp);
tmp = FormBuilder.createElements(definition.children[i], ref_table, ref_assoc);
// Pour chaque node
for( n in built[b].node )
FormBuilder.attachElements(built[b].node[n], tmp);
}
@ -1098,32 +1104,43 @@ FormBuilder.attachElements = function(parent, set){
---------------------------------------------------------*/
for( a in set[c].prev ){
/* (1) Si on a un élément */
/* (1) Si c'est un Element */
if( set[c].prev[a] instanceof Element )
parent.appendChild( set[c].prev[a] );
/* (2) Si c'est un autre élément -> récursion */
else if( set[c].prev[a] instanceof Object )
/* (2) On lance récursivement */
if( set[c].prev[a] instanceof Object )
FormBuilder.attachElements(parent, [ set[c].prev[a] ]);
}
/* (2) On ajoute l'élément actuel
---------------------------------------------------------*/
parent.appendChild( set[c].node );
for( a in set[c].node ){
/* (1) Si c'est un Element */
if( set[c].node[a] instanceof Element )
parent.appendChild( set[c].node[a] );
/* (2) On lance récursivement */
if( set[c].node[a] instanceof Object )
FormBuilder.attachElements(parent, [ set[c].node[a] ]);
}
/* (3) On attache les éléments suivants
---------------------------------------------------------*/
for( a in set[c].next ){
/* (1) Si on a un élément */
/* (1) Si c'est un Element */
if( set[c].next[a] instanceof Element )
parent.appendChild( set[c].next[a] );
/* (2) Si c'est un autre élément -> récursion */
else if( set[c].next[a] instanceof Object )
/* (2) On lance récursivement */
if( set[c].next[a] instanceof Object )
FormBuilder.attachElements(parent, [ set[c].next[a] ]);
}

View File

@ -1,7 +1,7 @@
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_type:"br",repeat:{n:"{$1}",id:"brs"}}},custom_definition={"/^input.([a-z]+)$/":{node:"input",attributes:{type:"{$1}","data-name":"{name}",value:"{value}",placeholder:"{placeholder}"},next_nodes:[{node:"br"}],listeners:{click:"{xx()}"}},"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()}"}}],next_nodes:[{node:"br"}]}},form={node:"h4",attributes:{"data-icon":"o","class":"new-contact color2"},children:[{node:"input.hidden",$name:"uid",$value:"{uid}"},{node:"input.hidden",$name:"call",$value:"{call}"},{node:"input.hidden",$name:"sms",$value:"{sms}"},
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}"},next_nodes:[{node:"br"}],listeners:{click:"{xx()}"}},"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()}"}}],next_nodes:[{node:"br2"}]}},form={node:"h4",attributes:{"data-icon":"o","class":"new-contact color2"},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:"{countcall}"},{node:"input.hidden",$name:"countsms",$value:"{countsms}"},{node:"input.text",$name:"total",$value:"xx{countcall} calls + {countsms} sms to {number}xx"},{node:"input.text",$name:"count{rep:i}",$value:"{rep:i} sur {rep:n}",repeat:{n:10,id:"rep"}},{node:"input.text",$name:"number",$value:"{number}",$xx:"{clicklistener}"},{node:"custom-select",$name:"existing",$options:"{options[]}"}]},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:"{countcall}"},{node:"input.hidden",$name:"countsms",$value:"{countsms}"},{node:"input.text",$name:"number",$value:"{number}"},{node:"custom-select",$name:"existing",$options:"{existing[]}",$listener:"{existingsel()}"},{node:"br2"},{node:"input.text",$name:"username",$placeholder:"{usernameph}",
$value:"{username}"},{node:"input.submit",$value:"Enregistrer",attributes:{"class":"primary sub-number"}}],next_nodes:[{node:"br2"}]},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",
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:"{countcall}"},{node:"input.hidden",$name:"countsms",$value:"{countsms}"},{node:"input.text",$name:"number",$value:"{number}"},{node:"custom-select",$name:"existing",$options:"{existing[]}",$listener:"{existingsel()}"},{node:"input.text",$name:"username",$placeholder:"{usernameph}",$value:"{username}"},
{node:"input.submit",$value:"Enregistrer",attributes:{"class":"primary sub-number"}}],next_nodes:[{node:"br4"}]},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,countcall:4,countsms:5,number:"01 02 03 04 05",existing:["Jean","Archibald","Daniel","Maurice"],username:"moi-meme",usernameph:"Pseudo",clicklistener:function(b){console.log("clicked",b)},existingsel:function(b){console.log("selected",b)},getoptval:function(b){return b}});fb.attach(document.body);

View File

@ -12,7 +12,7 @@ var default_definition = {
'option': { node_type: 'option' },
'select': { node_type: 'select' },
'span': { node_type: 'span' },
'/^br([0-9]+)$/': { node_type: 'br', repeat: { n: '{$1}', id: 'brs' } }
'/^br([0-9]+)$/': { node: 'br', repeat: { n: '{$1}', id: 'brs' } }
};
var custom_definition = {
@ -50,7 +50,7 @@ var custom_definition = {
listeners: { 'change': '{listener()}' }
}
],
next_nodes: [{ node: 'br' }]
next_nodes: [{ node: 'br2' }]
}
};
@ -86,12 +86,10 @@ var contactForm = {
{ node: 'custom-select', $name: 'existing', $options: '{existing[]}', $listener: '{existingsel()}' },
{ node: 'br2' },
{ node: 'input.text', $name: 'username', $placeholder: '{usernameph}', $value: '{username}'},
{ node: 'input.submit', $value: 'Enregistrer', attributes: { class: 'primary sub-number'} }
],
next_nodes: [ { node: 'br2' } ]
next_nodes: [ { node: 'br4' } ]
};
var a = ("<h4 data-icon='o' class='new-contact color2'> \n"+