Gestion des rérérences en cours 'pri_replacement'

This commit is contained in:
xdrm-brackets 2016-09-18 12:37:21 +02:00
parent 8d91b62bc6
commit ca63da8d61
4 changed files with 138 additions and 32 deletions

View File

@ -1,7 +1,9 @@
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,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};
var refObject=function(a){this.value=a};refObject.prototype={value:this.value,get:function(){return this.value},set:function(a){this.value=a},valueOf:function(){return this.get()}};var ref=function(a){return new refObject(a)},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};
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=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)};
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};FormBuilder.formatFormObject=function(a,c){if(a.hasOwnProperty("children"))for(var h in a.children)a.children[h].parent=a,FormBuilder.formatFormObject(a.children[h],c);return a};
FormBuilder.fetchNodeDefinition=function(a,c){var h,b;r=FormBuilder.regex.reg_in_key;if("undefined"!=typeof c&&c.hasOwnProperty(a))return{def:c[a]};for(h in c)if(r.test(h)&&(b=new RegExp(h.slice(1,-1)),b.test(a))){matches={};for(b=1;b<RegExp.length&&10>b;b++)matches["$"+b]=RegExp["$"+b];return{def:c[h],scope:matches}}return{}};
FormBuilder.replaceStatements=function(a,c,h){a=a instanceof Object?a:{};c=c instanceof Object?JSON.parse(JSON.stringify(c)):{};var b,e,d,f,k;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&&(e=FormBuilder.fetchNodeDefinition(a.node,h),e.hasOwnProperty("def")&&(a.node_link=JSON.parse(JSON.stringify(e.def)),e.hasOwnProperty("scope"))))for(b in e.scope)c[b]=e.scope[b];for(b in a)if("string"==typeof a[b]){e=
FormBuilder.regex.arr_out_set;f=[];(d=e.exec(a[b]))&&f.push(d[1]);for(d in f)c.hasOwnProperty(f[d])||(c[f[d]]=[]),a[b]=c[f[d]];if(!(0<f.length)){e=FormBuilder.regex.pri_out_val;for(var g=[];null!==(d=e.exec(a[b]));)g.push(d);f=0;k=[];for(d in g)k.push(a[b].substring(f,g[d].index)),e=a[b].substring(g[d].index+1,g[d].index+g[d][0].length-1),c.hasOwnProperty(e)||k.push(c[e]),f=g[d].index+g[d][0].length;a[b]=FormBuilder.buildString(k);if(!(0<g.length)){e=FormBuilder.regex.reg_out_val;f=-1;for(g=[];null!==
(d=e.exec(a[b]))&&!(f>=e.lastIndex);)f=e.lastIndex,g.push(d[1]);for(d in g)c.hasOwnProperty(g[d])||(c[g[d]]=""),e=new RegExp("{\\$"+g[d][1]+"}","g"),a[b]=a[b].replace(e,c[g[d]]);if(!(0<g.length)){e=FormBuilder.regex.arr_out_val;for(f=[];null!==(d=e.exec(a[b]));)f.push(d[1]);for(d in f)c.hasOwnProperty(f[d])||(c[f[d]]=""),e=new RegExp("/{"+f[d]+"}/","g"),a[b].replace(e,c[f[d]])}}}}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 l in a[b])FormBuilder.replaceStatements(a[b][l],c,h);else a[b]instanceof Object&&FormBuilder.replaceStatements(a[b],c,h);return a};FormBuilder.buildString=function(a){var c,h="";for(c in a)console.log(a[c]),h=a[c]instanceof refObject?h+(""+a[c].get()):h+(""+a[c].toString());return h};

View File

@ -1,15 +1,49 @@
/****************************************************/
/* _ _ _____ ___ _ ___ _______ __ */
/* | | | |_ _|_ _| | |_ _|_ _\ \ / / */
/* | | | | | | | || | | | | | \ V / */
/* | |_| | | | | || |___ | | | | | | */
/* \___/ |_| |___|_____|___| |_| |_| */
/****************************************************/
var refObject = function(variable){ this.value = variable; };
refObject.prototype = {
value: this.value,
get: function(){ return this.value; },
set: function(variable){ this.value = variable; },
valueOf: function(){ return this.get(); }
};
// alias
var ref = function(variable){ return new refObject(variable); };
/************************************************************************************/
/* _____ ___ ____ __ __ ____ _ _ ___ _ ____ _____ ____ */
/* | ___/ _ \| _ \| \/ | | __ )| | | |_ _| | | _ \| ____| _ \ */
/* | |_ | | | | |_) | |\/| |_____| _ \| | | || || | | | | | _| | |_) | */
/* | _|| |_| | _ <| | | |_____| |_) | |_| || || |___| |_| | |___| _ < */
/* |_| \___/|_| \_\_| |_| |____/ \___/|___|_____|____/|_____|_| \_\ */
/************************************************************************************/
/* CONSTRUCTEUR -> INITIALISE UNE L'INSTANCE
*
* @parent_element<Element> Element qui contiendra le formulaire
* @form_object<Object> Objet définissant le formulaire
*
*/
var FormBuilder = function(parent_element, form_object){
/* (1) On définit le parent */
this.parent_element = parent_element;
var FormBuilder = function(form_object){
/* (2) 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;
};
@ -27,8 +61,9 @@ var FormBuilder = function(parent_element, form_object){
FormBuilder.prototype = {
form_object: this.form_object, // objet permettant la construction du formulaire
defs_object: {}, // objet des définitions des éléments
parent_element: this.parent_element, // element qui contiendra le formulaire
built_form: null // Element correspondant au formulaire construit
parent_element: null, // element qui contiendra le formulaire
built_form: null, // Object correspondant au formulaire construit
root_element: null // Element correspondant à l'objet construit
};
@ -103,19 +138,42 @@ FormBuilder.prototype.build = function(scope){
==================================================================*/
scope = (scope == null) ? {} : scope;
/* [1] On formatte l'objet
/* [1] On clone l'object
=========================================================*/
this.built_form = JSON.parse(JSON.stringify(this.form_object));
/* [2] 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);
this.built_form = FormBuilder.formatFormObject(this.built_form, this.defs_object);
/* [2] On remplace les valeurs
/* [3] On remplace les valeurs
==================================================================*/
this.built_form = FormBuilder.replaceStatements(this.form_object, scope, this.defs_object);
this.built_form = FormBuilder.replaceStatements(this.built_form, scope, this.defs_object);
};
/* CONSTRUIT LES DOM ASSOCIE A L'OBJET CONSTRUIT
*
* @parent<Element> Element auquel rattacher le formulaire
*
*/
FormBuilder.prototype.attach = function(parent){
/* [0] Initialisation
=========================================================*/
/* (1) Gestion du paramètre @parent */
if( !(parent instanceof Element) && this.parent_element == null )
return false;
this.parent_element = (parent instanceof Element) ? parent : this.parent_element;
/* [1] Chapter Title
=========================================================*/
}
/************************************************/
@ -239,7 +297,7 @@ FormBuilder.replaceStatements = function(object, scope, definitions){
scope = (scope instanceof Object) ? JSON.parse(JSON.stringify(scope)) : {};
/* (2) Variables */
var key, r, tmpr, m, found;
var key, r, tmp, m, found, lasti, parts;
/* (3) On récupère le scope s'il est dans l'attribut 'scope' */
if( object.hasOwnProperty('scope') && object.scope instanceof Object )
@ -317,21 +375,28 @@ FormBuilder.replaceStatements = function(object, scope, definitions){
/* (1.2) Tant que ça match */
while( (m=r.exec(object[key])) !== null )
m_pri.push( m[1] );
m_pri.push( m );
/* (1.3) Pour chaque match */
lasti = 0, parts = [];
for( m in m_pri ){
// {1} Si la var n'est pas dans le scope //
if( !scope.hasOwnProperty(m_pri[m]) )
scope[m_pri[m]] = ''; // on met une chaine vide
// {2} on remplace toutes les occurences par la valeur //
tmpr = new RegExp( "\{"+m_pri[m]+"\}", 'g' );
object[key] = object[key].replace(tmpr, scope[m_pri[m]]);
// {1} On construit les valeurs pour le 'string-builder' //
parts.push( object[key].substring(lasti, m_pri[m].index) );
// {2} Sa la valeur n'existe pas dans le scope, on la crée //
tmp = object[key].substring(m_pri[m].index+1, m_pri[m].index+m_pri[m][0].length-1);
if( !scope.hasOwnProperty(tmp) )
parts.push( scope[tmp] );
lasti = m_pri[m].index + m_pri[m][0].length;
// object[key] = object[key].replace(tmp, scope[m_pri[m]]);
// console.log('pri', m_pri[m], scope[m_pri[m]]);
}
// On met la fonction à amorcer pour construire la valeur
object[key] = FormBuilder.buildString(parts);
/* (1.4) Si on a trouvé qqch, on passe à la clé suivante */
if( m_pri.length > 0 )
@ -362,8 +427,8 @@ FormBuilder.replaceStatements = function(object, scope, definitions){
scope[m_reg[m]] = ''; // on met une chaine vide
// {2} on remplace toutes les occurences par la valeur //
tmpr = new RegExp( "\{\\$"+m_reg[m][1]+"\}", 'g' );
object[key] = object[key].replace(tmpr, scope[m_reg[m]]);
tmp = new RegExp( "\{\\$"+m_reg[m][1]+"\}", 'g' );
object[key] = object[key].replace(tmp, scope[m_reg[m]]);
// console.log('reg', m_reg[m], scope);
@ -392,8 +457,8 @@ FormBuilder.replaceStatements = function(object, scope, definitions){
scope[m_aval[m]] = ''; // on met une chaine vide
// {2} on remplace toutes les occurences par la valeur //
tmpr = new RegExp( "/\{"+m_aval[m]+"\}/", 'g' );
object[key].replace(tmpr, scope[m_aval[m]]);
tmp = new RegExp( "/\{"+m_aval[m]+"\}/", 'g' );
object[key].replace(tmp, scope[m_aval[m]]);
}
@ -452,3 +517,41 @@ FormBuilder.replaceStatements = function(object, scope, definitions){
==================================================================*/
return object;
};
/* CONSTRUIT UNE CHAINE A PARTIR DE SES PARTIES (VARIABLES OU TEXTE BRUT)
*
* @parts<Array> Tableau contenant les parties
*
* @return built<String> Chaine recomposée
*
*/
FormBuilder.buildString = function(parts){
/* [0] Initialisation
=========================================================*/
/* (1) Variables */
var i;
var built = "";
/* [1] On remplace par les valeurs
=========================================================*/
for( i in parts ){
console.log(parts[i]);
/* (1) Si <refObject>, on ajoute la valeur */
if( parts[i] instanceof refObject )
built += '' + parts[i].get();
/* (2) Sinon, on ajoute simplement */
else
built += '' + parts[i].toString();
}
/* [2] On retourne le résultat
=========================================================*/
return built;
};

View File

@ -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.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"]});
{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"]});console.log(fb.built_form);

View File

@ -84,3 +84,4 @@ fb.build({
number: '01 02 03 04 05',
options: ['a', 'b', 'c', 'd']
});
console.log(fb.built_form);