Gestion de la récupération de l'attribut 'repeat' + gestion de la création de référence pour une valeur de tableau dans repeat, exemple: 'monTableau.maVar', référence qui sera modifiée à chaque itération avec la valeur du tableau

This commit is contained in:
xdrm-brackets 2016-09-19 19:22:51 +02:00
parent bb75d62412
commit ab97198abc
4 changed files with 91 additions and 56 deletions

View File

@ -1,16 +1,17 @@
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:{},ref_assoc:{}}; 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:{},ref_assoc:{}};
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".split(" ");FormBuilder.allowed_attr="node node_type next_nodes prev_nodes attributes children text repeat".split(" "); 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".split(" ");FormBuilder.allowed_attr="node node_type next_nodes prev_nodes attributes children text repeat".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)}; 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;a=FormBuilder.buildElements(this.ref_table,this.built_form);for(b in a.prev)this.parent_element.appendChild(a.prev[b]);this.parent_element.appendChild(a.node);for(b in a.next)this.parent_element.appendChild(a.next[b])}; 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;a=FormBuilder.buildElements(this.ref_table,this.built_form);for(b in a.prev)this.parent_element.appendChild(a.prev[b]);for(b in a.node)this.parent_element.appendChild(a.node[b]);for(b in a.next)this.parent_element.appendChild(a.next[b])};
FormBuilder.formatFormObject=function(a,b){if(a.hasOwnProperty("children"))for(var e in a.children)a.children[e].parent=a,FormBuilder.formatFormObject(a.children[e],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.formatFormObject=function(a,b){if(a.hasOwnProperty("children"))for(var e in a.children)a.children[e].hasOwnProperty("repeat")&&(a.children[e].parent=a,FormBuilder.formatFormObject(a.children[e],b));return a};
FormBuilder.replaceStatements=function(a,b,e,c){a=a instanceof Object?a:{};b=b instanceof Object?JSON.parse(JSON.stringify(b)):{};var d,f;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&&(f=FormBuilder.fetchNodeDefinition(a.node,e),f.hasOwnProperty("def")&&(a.node_link=JSON.parse(JSON.stringify(f.def)),f.hasOwnProperty("scope"))))for(d in f.scope)b[d]=f.scope[d];for(d in a)"string"==typeof a[d]&&(a[d]=[a[d]], FormBuilder.fetchNodeDefinition=function(a,b){var e,d;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)&&(d=new RegExp(e.slice(1,-1)),d.test(a))){matches={};for(d=1;d<RegExp.length&&10>d;d++)matches["$"+d]=RegExp["$"+d];return{def:b[e],scope:matches}}return{}};
f=FormBuilder.replaceStatementsFunction(c,a[d][0],b),!1!==f?a[d]=f:(f=FormBuilder.replaceStatementsArray(c,a[d][0],b),!1!==f?a[d]=f:(a[d]=FormBuilder.replaceStatementsRegex(a[d],b),a[d]=FormBuilder.replaceStatementsPrimary(a[d],b),a[d]=FormBuilder.replaceStatementsArrayValue(a[d],b))));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 FormBuilder.replaceStatements=function(a,b,e,d,f){a=a instanceof Object?a:{};b=b instanceof Object?JSON.parse(JSON.stringify(b)):{};var c,g;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&&(g=FormBuilder.fetchNodeDefinition(a.node,e),g.hasOwnProperty("def")&&(a.node_link=JSON.parse(JSON.stringify(g.def)),g.hasOwnProperty("scope"))))for(c in g.scope)b[c]=g.scope[c];for(c in a)"string"==typeof a[c]?(a[c]=[a[c]],
Array)for(var h in a[d])FormBuilder.replaceStatements(a[d][h],b,e,c);else a[d]instanceof Object&&FormBuilder.replaceStatements(a[d],b,e,c);return a};FormBuilder.replaceStatementsFunction=function(a,b,e){var c=null,c=FormBuilder.regex.fun_out_val.exec(b);if(null===c)return!1;e.hasOwnProperty(c[1])||(e[c[1]]=ref(a,function(){}));return e[c[1]]}; g=FormBuilder.replaceStatementsFunction(d,a[c][0],b),!1!==g?a[c]=g:(g=FormBuilder.replaceStatementsArray(a[c][0],b,d),!1!==g?a[c]=g:(a[c]=FormBuilder.replaceStatementsRegex(a[c],b),a[c]=FormBuilder.replaceStatementsPrimary(a[c],b),a[c]=FormBuilder.replaceStatementsArrayValue(a[c],b,d,f)))):"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));
FormBuilder.replaceStatementsArray=function(a,b,e){var c=null,c=FormBuilder.regex.arr_out_set.exec(b);if(null===c)return!1;e.hasOwnProperty(c[1])||(e[c[1]]=ref(a,[]));return e[c[1]]}; 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,e,d,f);else a[c]instanceof Object&&FormBuilder.replaceStatements(a[c],b,e,d,f);return a};FormBuilder.replaceStatementsFunction=function(a,b,e){var d=null,d=FormBuilder.regex.fun_out_val.exec(b);if(null===d)return!1;e.hasOwnProperty(d[1])||(e[d[1]]=ref(a,function(){}));return e[d[1]]};
FormBuilder.replaceStatementsRegex=function(a,b){var e=FormBuilder.regex.reg_out_val,c=null,d=[],f,h,g,k;for(g=0;g<a.length;g++){m=null;d=[];f=-1;for(h=[];null!==(c=e.exec(a[g]))&&!(f>=e.lastIndex);)f=e.lastIndex,d.push(c);for(c=f=0;c<d.length;c++)k=d[c][1],(0<f||0<d[c].index)&&h.push(a[g].substr(f,d[c].index-f)),b.hasOwnProperty(k)||(b[k]=""),h.push(b[k]),f=d[c].index+d[c][0].length;f<a[g].length&&h.push(a[g].substr(f,a[g].length));a=a.slice(0,g).concat(h).concat(a.slice(g+1))}return a}; 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]]};
FormBuilder.replaceStatementsPrimary=function(a,b){var e=FormBuilder.regex.pri_out_val,c=null,d=[],f,h,g,k;for(g=0;g<a.length;g++){m=null;d=[];f=0;for(h=[];null!==(c=e.exec(a[g]));)d.push(c);for(c=0;c<d.length;c++)k=d[c][1],(0<f||0<d[c].index)&&h.push(a[g].substr(f,d[c].index-f)),b.hasOwnProperty(k)||(b[k]=""),h.push(b[k]),f=d[c].index+d[c][0].length;f<a[g].length&&h.push(a[g].substr(f,a[g].length));a=a.slice(0,g).concat(h).concat(a.slice(g+1))}return a}; FormBuilder.replaceStatementsRegex=function(a,b){var e=FormBuilder.regex.reg_out_val,d=null,f=[],c,g,h,k;for(h=0;h<a.length;h++){m=null;f=[];c=-1;for(g=[];null!==(d=e.exec(a[h]))&&!(c>=e.lastIndex);)c=e.lastIndex,f.push(d);for(d=c=0;d<f.length;d++)k=f[d][1],(0<c||0<f[d].index)&&g.push(a[h].substr(c,f[d].index-c)),b.hasOwnProperty(k)||(b[k]=""),g.push(b[k]),c=f[d].index+f[d][0].length;c<a[h].length&&g.push(a[h].substr(c,a[h].length));a=a.slice(0,h).concat(g).concat(a.slice(h+1))}return a};
FormBuilder.replaceStatementsArrayValue=function(a,b){var e=FormBuilder.regex.arr_out_val,c=null,d=[],f,h,g,k;for(g=0;g<a.length;g++){m=null;d=[];f=0;for(h=[];null!==(c=e.exec(a[g]));)d.push(c);for(c=0;c<d.length;c++)k=d[c][1],(0<f||0<d[c].index)&&h.push(a[g].substr(f,d[c].index-f)),b.hasOwnProperty(k)||(b[k]=""),h.push(b[k]),f=d[c].index+d[c][0].length;f<a[g].length&&h.push(a[g].substr(f,a[g].length));a=a.slice(0,g).concat(h).concat(a.slice(g+1))}return a}; FormBuilder.replaceStatementsPrimary=function(a,b){var e=FormBuilder.regex.pri_out_val,d=null,f=[],c,g,h,k;for(h=0;h<a.length;h++){m=null;f=[];c=0;for(g=[];null!==(d=e.exec(a[h]));)f.push(d);for(d=0;d<f.length;d++)k=f[d][1],(0<c||0<f[d].index)&&g.push(a[h].substr(c,f[d].index-c)),b.hasOwnProperty(k)||(b[k]=""),g.push(b[k]),c=f[d].index+f[d][0].length;c<a[h].length&&g.push(a[h].substr(c,a[h].length));a=a.slice(0,h).concat(g).concat(a.slice(h+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.replaceStatementsArrayValue=function(a,b,e,d){b=FormBuilder.regex.arr_out_val;var f=null,c=[],g,h,k,l;for(k=0;k<a.length;k++){m=null;c=[];g=0;for(h=[];null!==(f=b.exec(a[k]));)c.push(f);for(f=0;f<c.length;f++)l=c[f][1]+"."+c[f][2],(0<g||0<c[f].index)&&h.push(a[k].substr(g,c[f].index-g)),d.hasOwnProperty(l)||(d[l]=ref(e)),h.push(d[l]),g=c[f].index+c[f][0].length;g<a[k].length&&h.push(a[k].substr(g,a[k].length));a=a.slice(0,k).concat(h).concat(a.slice(k+1))}return a};
FormBuilder.buildElements=function(a,b){var e={prev:[],node:null,next:[]},c,d,f;if(b.hasOwnProperty("prev_nodes"))for(c in b.prev_nodes)e.prev=FormBuilder.buildElements(a,b.prev_nodes[c]);if(b.hasOwnProperty("next_nodes"))for(c in b.next_nodes)e.next=FormBuilder.buildElements(a,b.next_nodes[c]);b.hasOwnProperty("node_type")?e.node=document.createElement(FormBuilder.readRef(a,b.node_type)):b.hasOwnProperty("node_link")&&(f=FormBuilder.buildElements(a,b.node_link),e.prev=e.prev.concat(f.prev),e.node= 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};
f.node,e.next=f.next.concat(e.next));if(b.hasOwnProperty("attributes"))for(c in b.attributes)e.node.setAttribute(c,FormBuilder.readRef(a,b.attributes[c]));b.hasOwnProperty("text")&&(e.node.innerHTML=b.text);if(b.hasOwnProperty("listeners"))for(c in b.listeners)e.node.addEventListener(c,FormBuilder.readRef(a,b.listeners[c]),!1),console.log("listener",c,FormBuilder.readRef(a,b.listeners[c]));if(b.hasOwnProperty("children"))for(c in b.children){f=FormBuilder.buildElements(a,b.children[c]);for(d in f.prev)e.node.appendChild(f.prev[d]); FormBuilder.buildElements=function(a,b){var e={prev:[],node:[],next:[]},d,f,c,g=[null];if(b.hasOwnProperty("repeat"))if(c=FormBuilder.readRef(a,b.repeat),isNaN(c))c instanceof Array&&(g=c);else for(g=[],d=0;d<c;d++)g[d]=d;console.log(g);if(b.hasOwnProperty("prev_nodes"))for(d in b.prev_nodes)e.prev=FormBuilder.buildElements(a,b.prev_nodes[d]);if(b.hasOwnProperty("next_nodes"))for(d in b.next_nodes)e.next=FormBuilder.buildElements(a,b.next_nodes[d]);b.hasOwnProperty("node_type")?e.node=document.createElement(FormBuilder.readRef(a,
e.node.appendChild(f.node);for(d in f.prev)e.node.appendChild(f.next[d])}return e}; b.node_type)):b.hasOwnProperty("node_link")&&(c=FormBuilder.buildElements(a,b.node_link),e.prev=e.prev.concat(c.prev),e.node=c.node,e.next=c.next.concat(e.next));if(b.hasOwnProperty("attributes"))for(d in b.attributes)e.node.setAttribute(d,FormBuilder.readRef(a,b.attributes[d]));b.hasOwnProperty("text")&&(e.node.innerHTML=b.text);if(b.hasOwnProperty("listeners"))for(d in b.listeners)e.node.addEventListener(d,FormBuilder.readRef(a,b.listeners[d]),!1);if(b.hasOwnProperty("children"))for(d in b.children){c=
FormBuilder.buildElements(a,b.children[d]);for(f in c.prev)e.node.appendChild(c.prev[f]);e.node.appendChild(c.node);for(f in c.prev)e.node.appendChild(c.next[f])}return e};

View File

@ -168,7 +168,7 @@ FormBuilder.prototype.build = function(scope){
/* [3] On remplace les valeurs /* [3] On remplace les valeurs
==================================================================*/ ==================================================================*/
this.built_form = FormBuilder.replaceStatements(this.built_form, scope, this.defs_object, this.ref_table); this.built_form = FormBuilder.replaceStatements(this.built_form, scope, this.defs_object, this.ref_table, this.ref_assoc);
}; };
@ -225,7 +225,8 @@ FormBuilder.prototype.attach = function(parent){
this.parent_element.appendChild( dom.prev[i] ); this.parent_element.appendChild( dom.prev[i] );
/* (2) On ajoute l'élément cible */ /* (2) On ajoute l'élément cible */
this.parent_element.appendChild( dom.node ); for( i in dom.node )
this.parent_element.appendChild( dom.node[i] );
/* (3) On ajoute les éléments suivants */ /* (3) On ajoute les éléments suivants */
for( i in dom.next ) for( i in dom.next )
@ -260,6 +261,8 @@ FormBuilder.formatFormObject = function(object, defs){
for( var child in object.children ){ for( var child in object.children ){
if( object.children[child].hasOwnProperty('repeat') ){
/* On ajoute le parent + on lance récursivement */ /* On ajoute le parent + on lance récursivement */
object.children[child].parent = object; object.children[child].parent = object;
FormBuilder.formatFormObject(object.children[child], defs); FormBuilder.formatFormObject(object.children[child], defs);
@ -268,6 +271,8 @@ FormBuilder.formatFormObject = function(object, defs){
} }
}
return object; return object;
}; };
@ -342,11 +347,12 @@ FormBuilder.fetchNodeDefinition = function(node, defs){
* @scope<Object> Ensemble des variables permettant le remplacement * @scope<Object> Ensemble des variables permettant le remplacement
* @definitions<Object> Définitions des éléments * @definitions<Object> Définitions des éléments
* @ref_table<Object> Table des références * @ref_table<Object> Table des références
* @ref_assoc<Object> Table des associations de références
* *
* @return replaced<Object> Objet avec les remplacements effectués * @return replaced<Object> Objet avec les remplacements effectués
* *
*/ */
FormBuilder.replaceStatements = function(object, scope, definitions, ref_table){ FormBuilder.replaceStatements = function(object, scope, definitions, ref_table, ref_assoc){
/* [0] Initialisation /* [0] Initialisation
==================================================================*/ ==================================================================*/
/* (1) Paramètres */ /* (1) Paramètres */
@ -414,7 +420,7 @@ FormBuilder.replaceStatements = function(object, scope, definitions, ref_table){
/* (2.2) On cherche tous les TABLEAUX à remplacer /* (2.2) On cherche tous les TABLEAUX à remplacer
---------------------------------------------------------*/ ---------------------------------------------------------*/
/* (1) On récupère le remplacement */ /* (1) On récupère le remplacement */
m_arr = FormBuilder.replaceStatementsArray(ref_table, object[key][0], scope); m_arr = FormBuilder.replaceStatementsArray(object[key][0], scope, ref_table);
/* (2) Si on un remplacement, on remplace, et on passe à la clé suivante */ /* (2) Si on un remplacement, on remplace, et on passe à la clé suivante */
if( m_arr !== false ){ if( m_arr !== false ){
@ -438,14 +444,16 @@ FormBuilder.replaceStatements = function(object, scope, definitions, ref_table){
/* (2.5) On cherche toutes les valeurs de tableaux à remplacer /* (2.5) On cherche toutes les valeurs de tableaux à remplacer
---------------------------------------------------------*/ ---------------------------------------------------------*/
/* (1) On récupère les remplacements */ /* (1) On récupère les remplacements */
object[key] = FormBuilder.replaceStatementsArrayValue(object[key], scope); object[key] = FormBuilder.replaceStatementsArrayValue(object[key], scope, ref_table, ref_assoc);
/* [1.2] Si c'est un type primitif, on le référencie
=========================================================*/
}else if( typeof object[key] === 'number' )
object[key] = [ ref(ref_table, object[key]) ];
} }
}
/* [3] On ajoute les variables '$var' et '$$arr' au scope suivant /* [3] On ajoute les variables '$var' et '$$arr' au scope suivant
==================================================================*/ ==================================================================*/
for( key in object ){ for( key in object ){
@ -477,13 +485,13 @@ FormBuilder.replaceStatements = function(object, scope, definitions, ref_table){
/* (1) Si c'est un tableau, on lance récursivement pour chaque item */ /* (1) Si c'est un tableau, on lance récursivement pour chaque item */
if( object[key] instanceof Array ) if( object[key] instanceof Array )
for( var i in object[key] ) for( var i in object[key] )
FormBuilder.replaceStatements(object[key][i], scope, definitions, ref_table); FormBuilder.replaceStatements(object[key][i], scope, definitions, ref_table, ref_assoc);
// console.log(object.node, key+'['+i+']'); // console.log(object.node, key+'['+i+']');
/* (2) Si c'est un objet, on lance récursivement */ /* (2) Si c'est un objet, on lance récursivement */
else if( object[key] instanceof Object ) else if( object[key] instanceof Object )
FormBuilder.replaceStatements(object[key], scope, definitions, ref_table); FormBuilder.replaceStatements(object[key], scope, definitions, ref_table, ref_assoc);
// console.log(object.node, key); // console.log(object.node, key);
@ -527,16 +535,17 @@ FormBuilder.replaceStatementsFunction = function(ref_table, statement, scope){
}; };
/* REMPLACE UN TABLEAU SOUS LA FORME "{{arrayName}}" par sa référence /* REMPLACE UN TABLEAU SOUS LA FORME "{{arrayName}}" par sa référence
* *
* @ref_table<Object> Objet contenant les références
* @statement<String> String contenant la chaine * @statement<String> String contenant la chaine
* @scope<Object> Objet contenant le scope * @scope<Object> Objet contenant le scope
* @ref_table<Object> Objet contenant les références
* *
* @return newVal<String> Retourne la nouvelle (ref) valeur ou FALSE si rien n'a été fais * @return newVal<String> Retourne la nouvelle (ref) valeur ou FALSE si rien n'a été fais
* *
*/ */
FormBuilder.replaceStatementsArray = function(ref_table, statement, scope){ FormBuilder.replaceStatementsArray = function(statement, scope, ref_table){
/* (1) On initialise les variables */ /* (1) On initialise les variables */
var match = null; var match = null;
var regex = FormBuilder.regex.arr_out_set; var regex = FormBuilder.regex.arr_out_set;
@ -712,11 +721,13 @@ FormBuilder.replaceStatementsPrimary = function(statements, scope){
* *
* @statements<Array> Tableau contenant les parties de la chaine * @statements<Array> Tableau contenant les parties de la chaine
* @scope<Object> Objet contenant le scope * @scope<Object> Objet contenant le scope
* @ref_table<Object> Objet contenant les références
* @ref_assoc<Object> Table des associations de références
* *
* @return splitVal<Array> Tableau contenant les parties de la chaine (références + chaine) * @return splitVal<Array> Tableau contenant les parties de la chaine (références + chaine)
* *
*/ */
FormBuilder.replaceStatementsArrayValue = function(statements, scope){ FormBuilder.replaceStatementsArrayValue = function(statements, scope, ref_table, ref_assoc){
/* [1] Initialisation /* [1] Initialisation
=========================================================*/ =========================================================*/
var regex = FormBuilder.regex.arr_out_val; var regex = FormBuilder.regex.arr_out_val;
@ -744,18 +755,18 @@ FormBuilder.replaceStatementsArrayValue = function(statements, scope){
=========================================================*/ =========================================================*/
for( i = 0 ; i < matches.length ; i++ ){ for( i = 0 ; i < matches.length ; i++ ){
key = matches[i][1]; key = matches[i][1] + '.' + matches[i][2];
/* (1) On met la chaine d'avant le match (si existe) */ /* (1) On met la chaine d'avant le match (si existe) */
if( lasti > 0 || matches[i].index > 0 ) if( lasti > 0 || matches[i].index > 0 )
parts.push( statements[p].substr(lasti, matches[i].index-lasti) ); parts.push( statements[p].substr(lasti, matches[i].index-lasti) );
/* (2) Si la var n'est pas dans le scope, on l'initialise vide */ /* (2) On initialise la référence pour la remplir (si n'existe pas)*/
if( !scope.hasOwnProperty(key) ) if( !ref_assoc.hasOwnProperty(key) )
scope[key] = ''; // on met une chaine vide ref_assoc[key] = ref(ref_table);
/* (3) On insère la valeur du scope */ /* (3) On insère la valeur du scope */
parts.push( scope[key] ); parts.push( ref_assoc[key] );
/* (4) On met à jour l'index de fin pour la suite */ /* (4) On met à jour l'index de fin pour la suite */
lasti = matches[i].index + matches[i][0].length; lasti = matches[i].index + matches[i][0].length;
@ -852,28 +863,52 @@ FormBuilder.buildElements = function(ref_table, definition){
===========================================================*/ ===========================================================*/
var built = { var built = {
prev: [], // Les éléments précédents prev: [], // Les éléments précédents
node: null, // L'éléments actuel et ses antécédents/+ prédécesseurs node: [], // Les éléments actuels
next: [] // Les éléments suivants next: [] // Les éléments suivants
}; };
var i, j, tmp; var i, j, tmp;
var repeater = [null];
/* [1] On construit les éléments @prev_nodes, s'ils existent /* [1] Gestion de l'attribut 'repeat'
=========================================================*/
if( definition.hasOwnProperty('repeat') ){
tmp = FormBuilder.readRef(ref_table, definition.repeat);
/* (1) Si on a un nombre */
if( !isNaN(tmp) ){
repeater = [];
for( i = 0 ; i < tmp ; i++ )
repeater[i] = i;
}
/* (2) Si c'est un tableau */
else if( tmp instanceof Array)
repeater = tmp;
}
console.log(repeater);
/* [2] On construit les éléments @prev_nodes, s'ils existent
===========================================================*/ ===========================================================*/
if( definition.hasOwnProperty('prev_nodes') ) if( definition.hasOwnProperty('prev_nodes') )
for( i in definition.prev_nodes ) for( i in definition.prev_nodes )
built.prev = FormBuilder.buildElements(ref_table, definition.prev_nodes[i]); built.prev = FormBuilder.buildElements(ref_table, definition.prev_nodes[i]);
/* [2] On construit les éléments @next_nodes s'ils existent /* [3] On construit les éléments @next_nodes s'ils existent
===========================================================*/ ===========================================================*/
if( definition.hasOwnProperty('next_nodes') ) if( definition.hasOwnProperty('next_nodes') )
for( i in definition.next_nodes ) for( i in definition.next_nodes )
built.next = FormBuilder.buildElements(ref_table, definition.next_nodes[i]); built.next = FormBuilder.buildElements(ref_table, definition.next_nodes[i]);
/* [3] On construit l'objet actuel /* [4] On construit l'objet actuel
===========================================================*/ ===========================================================*/
/* (1) On crée l'élément /* (1) On crée l'élément
@ -918,23 +953,22 @@ FormBuilder.buildElements = function(ref_table, definition){
---------------------------------------------------------*/ ---------------------------------------------------------*/
if( definition.hasOwnProperty('listeners') ){ if( definition.hasOwnProperty('listeners') ){
// Pour chaque listener
for( i in definition.listeners ){ for( i in definition.listeners ){
built.node.addEventListener( // Pour chaque noeud
i, // for( j in repeater )
FormBuilder.readRef(ref_table, definition.listeners[i]), built.node.addEventListener( i, FormBuilder.readRef(ref_table, definition.listeners[i]), false );
false
);
console.log('listener', i, FormBuilder.readRef(ref_table, definition.listeners[i]));
} }
} }
/* [4] On ajoute tous les enfants
/* [5] On ajoute tous les enfants
=========================================================*/ =========================================================*/
if( definition.hasOwnProperty('children') ) if( definition.hasOwnProperty('children') ){
/* (1) Pour chaque enfant */ /* (1) Pour chaque enfant */
for( i in definition.children ){ for( i in definition.children ){
@ -953,7 +987,7 @@ FormBuilder.buildElements = function(ref_table, definition){
} }
}
return built; return built;

View File

@ -1,5 +1,5 @@
document.body.innerHTML="";"use strict"; 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"}},custom_definition={"/^input.([a-z]+)$/":{node:"input",attributes:{type:"{$1}","data-name":"{name}",value:"{value}",placeholder:"{placeholder}"},next_nodes:[{node:"br"}],listeners:{focus:"{xx}()"}},"custom-select":{node:"span",attributes:{"class":"select-container nobold"},children:[{node:"select",attributes:{"data-name":"{name}"}, 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"}},custom_definition={"/^input.([a-z]+)$/":{node:"input",attributes:{type:"{$1}","data-name":"{name}",value:"{value}",placeholder:"{placeholder}"},next_nodes:[{node:"br"}],listeners:{focus:"{xx}()"}},"custom-select":{node:"span",attributes:{"class":"select-container nobold"},children:[{node:"select",attributes:{"data-name":"{name}"},
children:[{node:"option",attribute:{value:"{options.value}"},text:"{options.value}",repeat:"{{options}}"}]}],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}"},{node:"input.hidden",$name:"countcall",$value:"{countcall}"},{node:"input.hidden",$name:"countsms",$value:"{countsms}"},{node:"input.hidden", children:[{node:"option",attribute:{value:"{options.value}"},text:"{options.value}",repeat:"{{options}}"}]}],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}"},{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:"number",$value:"{number}",$xx:"{clicklistener}"},{node:"custom-select",$name:"existing",$$options:"{{options}}"}]},fb=new FormBuilder(form);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",options:["a","b","c","d"],clicklistener:function(a){console.log(a)}});console.log(fb.built_form); $name:"total",$value:"xx{countcall} calls + {countsms} sms to {number}xx"},{node:"input.text",$name:"count",$value:"{n}",repeat:10},{node:"input.text",$name:"number",$value:"{number}",$xx:"{clicklistener}"},{node:"custom-select",$name:"existing",$$options:"{{options}}"}]},fb=new FormBuilder(form);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",options:["a","b","c","d"],clicklistener:function(a){console.log(a)}});
var str=function(a){return FormBuilder.readRef(fb.ref_table,a)};fb.attach(document.body); console.log(fb.built_form);var str=function(a){return FormBuilder.readRef(fb.ref_table,a)};fb.attach(document.body);

View File

@ -61,8 +61,8 @@ var form = {
{ node: 'input.hidden', $name: 'sms', $value: '{sms}' }, { node: 'input.hidden', $name: 'sms', $value: '{sms}' },
{ node: 'input.hidden', $name: 'countcall', $value: '{countcall}' }, { node: 'input.hidden', $name: 'countcall', $value: '{countcall}' },
{ node: 'input.hidden', $name: 'countsms', $value: '{countsms}' }, { node: 'input.hidden', $name: 'countsms', $value: '{countsms}' },
{ node: 'input.hidden', $name: 'total', $value: 'xx{countcall} calls + {countsms} sms to {number}xx' }, { node: 'input.text', $name: 'total', $value: 'xx{countcall} calls + {countsms} sms to {number}xx' },
{ node: 'input.text', $name: 'count', $value: '{n}', repeat: 10 },
{ node: 'input.text', $name: 'number', $value: '{number}', $xx: '{clicklistener}' }, { node: 'input.text', $name: 'number', $value: '{number}', $xx: '{clicklistener}' },
{ node: 'custom-select', $name: 'existing', $$options: '{{options}}' } { node: 'custom-select', $name: 'existing', $$options: '{{options}}' }