Gestion de 'node_link' récursivement (pas uniquement au premier niveau)
This commit is contained in:
parent
d997066e36
commit
8d91b62bc6
|
@ -1,8 +1,7 @@
|
|||
var FormBuilder=function(a,c){this.parent_element=a;this.form_object=c};FormBuilder.prototype={form_object:this.form_object,defs_object:{},parent_element:this.parent_element,built_form:null};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-]+)$/};
|
||||
FormBuilder.spread_attr=["children","next_nodes","prev_nodes","attributes","node_link"];FormBuilder.allowed_attr="node node_type next_nodes prev_nodes attributes children text repeat".split(" ");FormBuilder.prototype.add_definition=function(a){for(var c in a)this.defs_object[c]=a[c]};FormBuilder.prototype.build=function(a){a=null==a?{}:a;this.built_form=FormBuilder.formatFormObject(this.form_object,this.defs_object);this.built_form=FormBuilder.replaceStatements(this.form_object,a)};
|
||||
FormBuilder.formatFormObject=function(a,c){if(a.hasOwnProperty("node")&&"string"==typeof a.node){var f=FormBuilder.fetchNodeDefinition(a.node,c);"undefined"!=typeof f.def&&(a.node_link=JSON.parse(JSON.stringify(f.def)),f.hasOwnProperty("scope")&&(a.scope=f.scope))}if(a.hasOwnProperty("children"))for(var b in a.children)a.children[b].parent=a,FormBuilder.formatFormObject(a.children[b],c);return a};
|
||||
FormBuilder.fetchNodeDefinition=function(a,c){var f,b;r=FormBuilder.regex.reg_in_key;if(null!=c&&c.hasOwnProperty(a))return{def:c[a]};for(f in c)if(r.test(f)&&(b=new RegExp(f.slice(1,-1)),b.test(a))){matches={};for(b=1;b<RegExp.length&&10>b;b++)matches["$"+b]=RegExp["$"+b];return{def:c[f],scope:matches}}return{}};
|
||||
FormBuilder.replaceStatements=function(a,c,f){a=a instanceof Object?a:{};c=c instanceof Object?JSON.parse(JSON.stringify(c)):{};f=null==f?"":f+" ";var b,g,e;if(a.hasOwnProperty("scope")&&a.scope instanceof Object)for(b in a.scope)c[b]=a.scope[b];a.received=JSON.parse(JSON.stringify(c));for(b in a)if("string"==typeof a[b]){g=FormBuilder.regex.arr_out_set;var d=[];(e=g.exec(a[b]))&&d.push(e[1]);for(e in d)c.hasOwnProperty(d[e])||(c[d[e]]=[]),a[b]=c[d[e]];if(!(0<d.length)){g=FormBuilder.regex.pri_out_val;
|
||||
for(d=[];null!==(e=g.exec(a[b]));)d.push(e[1]);for(e in d)c.hasOwnProperty(d[e])||(c[d[e]]=""),g=new RegExp("{"+d[e]+"}","g"),a[b]=a[b].replace(g,c[d[e]]);if(!(0<d.length)){g=FormBuilder.regex.reg_out_val;lasti=-1;for(d=[];null!==(e=g.exec(a[b]))&&!(lasti>=g.lastIndex);)lasti=g.lastIndex,d.push(e[1]);for(e in d)c.hasOwnProperty(d[e])||(c[d[e]]=""),g=new RegExp("{\\$"+d[e][1]+"}","g"),a[b]=a[b].replace(g,c[d[e]]);if(!(0<d.length)){g=FormBuilder.regex.arr_out_val;for(d=[];null!==(e=g.exec(a[b]));)d.push(e[1]);
|
||||
for(e in d)c.hasOwnProperty(d[e])||(c[d[e]]=""),g=new RegExp("/{"+d[e]+"}/","g"),a[b].replace(g,c[d[e]])}}}}for(b in a)FormBuilder.regex.pri_in_key.test(b)?c[b.substring(1)]=a[b]:FormBuilder.regex.arr_in_key.test(b)&&(c[b.substring(2)]=a[b]);c=JSON.parse(JSON.stringify(c));for(b in a)if(-1<FormBuilder.spread_attr.indexOf(b))if(a[b]instanceof Array)for(var h in a[b])console.log(f+""+a.node,b+"["+h+"]"),FormBuilder.replaceStatements(a[b][h],c,f);else a[b]instanceof Object&&(console.log(f+""+a.node,
|
||||
b),FormBuilder.replaceStatements(a[b],c,f));return a};
|
||||
FormBuilder.spread_attr=["children","next_nodes","prev_nodes","attributes","node_link"];FormBuilder.allowed_attr="node node_type next_nodes prev_nodes attributes children text repeat".split(" ");FormBuilder.prototype.add_definition=function(a){for(var c in a)this.defs_object[c]=a[c]};FormBuilder.prototype.build=function(a){a=null==a?{}:a;this.built_form=FormBuilder.formatFormObject(this.form_object,this.defs_object);this.built_form=FormBuilder.replaceStatements(this.form_object,a,this.defs_object)};
|
||||
FormBuilder.formatFormObject=function(a,c){if(a.hasOwnProperty("children"))for(var g in a.children)a.children[g].parent=a,FormBuilder.formatFormObject(a.children[g],c);return a};FormBuilder.fetchNodeDefinition=function(a,c){var g,b;r=FormBuilder.regex.reg_in_key;if("undefined"!=typeof c&&c.hasOwnProperty(a))return{def:c[a]};for(g in c)if(r.test(g)&&(b=new RegExp(g.slice(1,-1)),b.test(a))){matches={};for(b=1;b<RegExp.length&&10>b;b++)matches["$"+b]=RegExp["$"+b];return{def:c[g],scope:matches}}return{}};
|
||||
FormBuilder.replaceStatements=function(a,c,g){a=a instanceof Object?a:{};c=c instanceof Object?JSON.parse(JSON.stringify(c)):{};var b,f,e;if(a.hasOwnProperty("scope")&&a.scope instanceof Object)for(b in a.scope)c[b]=a.scope[b];if(a.hasOwnProperty("node")&&"string"==typeof a.node&&(f=FormBuilder.fetchNodeDefinition(a.node,g),f.hasOwnProperty("def")&&(a.node_link=JSON.parse(JSON.stringify(f.def)),f.hasOwnProperty("scope"))))for(b in f.scope)c[b]=f.scope[b];for(b in a)if("string"==typeof a[b]){f=FormBuilder.regex.arr_out_set;
|
||||
var d=[];(e=f.exec(a[b]))&&d.push(e[1]);for(e in d)c.hasOwnProperty(d[e])||(c[d[e]]=[]),a[b]=c[d[e]];if(!(0<d.length)){f=FormBuilder.regex.pri_out_val;for(d=[];null!==(e=f.exec(a[b]));)d.push(e[1]);for(e in d)c.hasOwnProperty(d[e])||(c[d[e]]=""),f=new RegExp("{"+d[e]+"}","g"),a[b]=a[b].replace(f,c[d[e]]);if(!(0<d.length)){f=FormBuilder.regex.reg_out_val;lasti=-1;for(d=[];null!==(e=f.exec(a[b]))&&!(lasti>=f.lastIndex);)lasti=f.lastIndex,d.push(e[1]);for(e in d)c.hasOwnProperty(d[e])||(c[d[e]]=""),
|
||||
f=new RegExp("{\\$"+d[e][1]+"}","g"),a[b]=a[b].replace(f,c[d[e]]);if(!(0<d.length)){f=FormBuilder.regex.arr_out_val;for(d=[];null!==(e=f.exec(a[b]));)d.push(e[1]);for(e in d)c.hasOwnProperty(d[e])||(c[d[e]]=""),f=new RegExp("/{"+d[e]+"}/","g"),a[b].replace(f,c[d[e]])}}}}for(b in a)FormBuilder.regex.pri_in_key.test(b)?c[b.substring(1)]=a[b]:FormBuilder.regex.arr_in_key.test(b)&&(c[b.substring(2)]=a[b]);c=JSON.parse(JSON.stringify(c));for(b in a)if(-1<FormBuilder.spread_attr.indexOf(b))if(a[b]instanceof
|
||||
Array)for(var h in a[b])FormBuilder.replaceStatements(a[b][h],c,g);else a[b]instanceof Object&&FormBuilder.replaceStatements(a[b],c,g);return a};
|
||||
|
|
|
@ -85,7 +85,7 @@ FormBuilder.allowed_attr = [ // Liste des attributs fixes aut
|
|||
FormBuilder.prototype.add_definition = function(def_object){
|
||||
|
||||
/* [1] On ajoute la définition
|
||||
=========================================================*/
|
||||
==================================================================*/
|
||||
for( var key in def_object )
|
||||
this.defs_object[key] = def_object[key];
|
||||
|
||||
|
@ -100,19 +100,19 @@ FormBuilder.prototype.add_definition = function(def_object){
|
|||
*/
|
||||
FormBuilder.prototype.build = function(scope){
|
||||
/* [0] Gestion du paramètres
|
||||
=========================================================*/
|
||||
==================================================================*/
|
||||
scope = (scope == null) ? {} : scope;
|
||||
|
||||
/* [1] On formatte l'objet
|
||||
=========================================================*/
|
||||
==================================================================*/
|
||||
// - Ajout des références children[n].parent vers élément parent
|
||||
// - Ajout des références element.node_link vers définition (en fonction de element.node)
|
||||
this.built_form = FormBuilder.formatFormObject(this.form_object, this.defs_object);
|
||||
|
||||
|
||||
/* [2] On remplace les valeurs
|
||||
=========================================================*/
|
||||
this.built_form = FormBuilder.replaceStatements(this.form_object, scope);
|
||||
==================================================================*/
|
||||
this.built_form = FormBuilder.replaceStatements(this.form_object, scope, this.defs_object);
|
||||
};
|
||||
|
||||
|
||||
|
@ -139,31 +139,9 @@ FormBuilder.prototype.build = function(scope){
|
|||
*/
|
||||
FormBuilder.formatFormObject = function(object, defs){
|
||||
|
||||
/* [1] Si a l'attribut 'node' <string>
|
||||
=========================================================*/
|
||||
if( object.hasOwnProperty('node') && typeof object.node == 'string' ){
|
||||
|
||||
/* On cherche une définition */
|
||||
var found_def = FormBuilder.fetchNodeDefinition(object.node, defs);
|
||||
|
||||
// Si on trouve
|
||||
if( typeof found_def.def != 'undefined' ){
|
||||
|
||||
// 1. On clone la définition dans l'attribut 'node_link'
|
||||
object.node_link = JSON.parse( JSON.stringify(found_def.def) );
|
||||
|
||||
// 2. On ajoute les matches dans l'attribut 'scope'
|
||||
if( found_def.hasOwnProperty('scope') )
|
||||
object.scope = found_def.scope;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
/* [2] Pour chaque enfant, s'il y a
|
||||
=========================================================*/
|
||||
/* [1] Pour chaque enfant, s'il y a
|
||||
==================================================================*/
|
||||
if( object.hasOwnProperty('children') ){
|
||||
|
||||
for( var child in object.children ){
|
||||
|
@ -191,20 +169,20 @@ FormBuilder.formatFormObject = function(object, defs){
|
|||
*/
|
||||
FormBuilder.fetchNodeDefinition = function(node, defs){
|
||||
/* [0] Initialisation
|
||||
=========================================================*/
|
||||
==================================================================*/
|
||||
var m = null, key, i, regex;
|
||||
r = FormBuilder.regex.reg_in_key;
|
||||
|
||||
|
||||
/* [1] Si la définition existe, on la retourne
|
||||
=========================================================*/
|
||||
if( defs != null && defs.hasOwnProperty(node) ){
|
||||
==================================================================*/
|
||||
if( typeof defs != 'undefined' && defs.hasOwnProperty(node) ){
|
||||
|
||||
return { def: defs[node] };
|
||||
|
||||
|
||||
/* [2] Sinon, on cherche une REGEX
|
||||
=========================================================*/
|
||||
==================================================================*/
|
||||
}else{
|
||||
|
||||
// Pour chaque définition
|
||||
|
@ -248,35 +226,57 @@ FormBuilder.fetchNodeDefinition = function(node, defs){
|
|||
*
|
||||
* @object<Object> Objet dans lequel remplacer les valeurs
|
||||
* @scope<Object> Ensemble des variables permettant le remplacement
|
||||
* @definitions<Object> Définitions des éléments
|
||||
*
|
||||
* @return replaced<Object> Objet avec les remplacements effectués
|
||||
*
|
||||
*/
|
||||
FormBuilder.replaceStatements = function(object, scope, pad){
|
||||
FormBuilder.replaceStatements = function(object, scope, definitions){
|
||||
/* [0] Initialisation
|
||||
=========================================================*/
|
||||
==================================================================*/
|
||||
/* (1) Paramètres */
|
||||
object = (object instanceof Object) ? object : {};
|
||||
scope = (scope instanceof Object) ? JSON.parse(JSON.stringify(scope)) : {};
|
||||
/*DEBUG*/pad= (pad==null) ? '' : pad + ' ';
|
||||
|
||||
/* (2) Variables */
|
||||
var key, r, tmpr, m;
|
||||
|
||||
var key, r, tmpr, m, found;
|
||||
|
||||
/* (3) On récupère le scope s'il est dans l'attribut 'scope' */
|
||||
if( object.hasOwnProperty('scope') && object.scope instanceof Object )
|
||||
for( key in object.scope )
|
||||
scope[key] = object.scope[key];
|
||||
|
||||
object.received = JSON.parse(JSON.stringify(scope));
|
||||
|
||||
/* [1] On remplace les valeurs
|
||||
=========================================================*/
|
||||
/* [1] On lie les définitions de l'attribut 'node' -> 'node_link'
|
||||
==================================================================*/
|
||||
if( object.hasOwnProperty('node') && typeof object.node == 'string' ){
|
||||
|
||||
/* On cherche une définition */
|
||||
found = FormBuilder.fetchNodeDefinition(object.node, definitions);
|
||||
|
||||
// Si on trouve
|
||||
if( found.hasOwnProperty('def') ){
|
||||
|
||||
// 1. On clone la définition dans l'attribut 'node_link'
|
||||
object.node_link = JSON.parse( JSON.stringify(found.def) );
|
||||
|
||||
// 2. On ajoute les matches dans l'attribut 'scope'
|
||||
if( found.hasOwnProperty('scope') )
|
||||
for( key in found.scope )
|
||||
scope[key] = found.scope[key];
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
/* [2] On remplace les valeurs
|
||||
==================================================================*/
|
||||
for( key in object ){
|
||||
|
||||
/* [1.1] Si c'est une string, on regarde s'il faut remplacer
|
||||
=========================================================*/
|
||||
==================================================================*/
|
||||
if( typeof object[key] == 'string' ){
|
||||
|
||||
|
||||
|
@ -403,8 +403,8 @@ FormBuilder.replaceStatements = function(object, scope, pad){
|
|||
}
|
||||
|
||||
|
||||
/* [2] 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 ){
|
||||
|
||||
/* (1) Ajout des variables de type '$nomVar'
|
||||
|
@ -420,12 +420,11 @@ FormBuilder.replaceStatements = function(object, scope, pad){
|
|||
}
|
||||
|
||||
|
||||
/* [3] On lance récursivement
|
||||
=========================================================*/
|
||||
/* (1) on clone le scope */
|
||||
/* [4] On lance récursivement
|
||||
==================================================================*/
|
||||
/* on clone le scope */
|
||||
scope = JSON.parse(JSON.stringify(scope));
|
||||
|
||||
|
||||
for( key in object ){
|
||||
|
||||
/* S'il ne s'agit d'un attribut interdit */
|
||||
|
@ -434,21 +433,22 @@ FormBuilder.replaceStatements = function(object, scope, pad){
|
|||
|
||||
/* (1) Si c'est un tableau, on lance récursivement pour chaque item */
|
||||
if( object[key] instanceof Array )
|
||||
for( var i in object[key] ){
|
||||
console.log(pad+''+object.node, key+'['+i+']');
|
||||
FormBuilder.replaceStatements(object[key][i], scope, pad);
|
||||
}
|
||||
for( var i in object[key] )
|
||||
FormBuilder.replaceStatements(object[key][i], scope, definitions);
|
||||
// console.log(object.node, key+'['+i+']');
|
||||
|
||||
|
||||
/* (2) Si c'est un objet, on lance récursivement */
|
||||
else if( object[key] instanceof Object ){
|
||||
console.log(pad+''+object.node, key);
|
||||
FormBuilder.replaceStatements(object[key], scope, pad);
|
||||
}
|
||||
else if( object[key] instanceof Object )
|
||||
FormBuilder.replaceStatements(object[key], scope, definitions);
|
||||
// console.log(object.node, key);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* [5] On retourne l'object courant
|
||||
==================================================================*/
|
||||
return object;
|
||||
};
|
||||
|
|
|
@ -1,4 +1,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"}},custom_definition={"/^input.([a-z]+)$/":{node:"input",attributes:{type:"{$1}","data-name":"{name}",value:"{value}",placeholder:"{placeholder}"},next_nodes:[{node:"br"}]},"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.a",$name:"uid",$value:"{uid}"},{node:"input.b",$name:"call",$value:"{call}"},{node:"input.c",$name:"sms",$value:"{sms}"},{node:"input.d",$name:"countcall",$value:"{countcall}"},{node:"input.e",$name:"countsms",$value:"{countsms}"},{node:"input.f",$name:"total",$value:"{countcall} calls + {countsms} sms to {number}"},
|
||||
{node:"input.g",$name:"number",$value:"{number}"},{node:"custom-select",$name:"existing",$$options:"{{options}}"}]},fb=new FormBuilder(document.body,form);fb.add_definition(default_definition);fb.add_definition(custom_definition);fb.build({});
|
||||
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",$name:"total",$value:"{countcall} calls + {countsms} sms to {number}"},
|
||||
{node:"input.text",$name:"number",$value:"{number}"},{node:"custom-select",$name:"existing",$$options:"{{options}}"}]},fb=new FormBuilder(document.body,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"]});
|
||||
|
|
|
@ -53,14 +53,14 @@ var form = {
|
|||
node: 'h4',
|
||||
attributes: { 'data-icon': 'o', 'class': 'new-contact color2' },
|
||||
children: [
|
||||
{ node: 'input.a', $name: 'uid', $value: '{uid}' },
|
||||
{ node: 'input.b', $name: 'call', $value: '{call}' },
|
||||
{ node: 'input.c', $name: 'sms', $value: '{sms}' },
|
||||
{ node: 'input.d', $name: 'countcall', $value: '{countcall}' },
|
||||
{ node: 'input.e', $name: 'countsms', $value: '{countsms}' },
|
||||
{ node: 'input.f', $name: 'total', $value: '{countcall} calls + {countsms} sms to {number}' },
|
||||
{ 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', $name: 'total', $value: '{countcall} calls + {countsms} sms to {number}' },
|
||||
|
||||
{ node: 'input.g', $name: 'number', $value: '{number}' },
|
||||
{ node: 'input.text', $name: 'number', $value: '{number}' },
|
||||
|
||||
{ node: 'custom-select', $name: 'existing', $$options: '{{options}}' }
|
||||
]
|
||||
|
@ -75,14 +75,12 @@ var form = {
|
|||
var fb = new FormBuilder(document.body, 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']
|
||||
// });
|
||||
|
||||
fb.build({});
|
||||
fb.build({
|
||||
uid: 1,
|
||||
call: 2,
|
||||
sms: 3,
|
||||
countcall: 4,
|
||||
countsms: 5,
|
||||
number: '01 02 03 04 05',
|
||||
options: ['a', 'b', 'c', 'd']
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue