Gestion de la modification par référence fonctionnelle [FCT]

This commit is contained in:
xdrm-brackets 2016-09-19 10:24:34 +02:00
parent 1f0155e109
commit 0867417929
4 changed files with 62 additions and 24 deletions

View File

@ -1,12 +1,12 @@
var ref=function(a,c){for(var e=null;null==e||a.hasOwnProperty(e);)e="$"+(1048576+Math.floor(15728639*Math.random())).toString(16)+"$";a[e]=c;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:{}}; var ref=function(a,b){for(var e=null;null==e||a.hasOwnProperty(e);)e="$"+(1048576+Math.floor(15728639*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-]+)$/,ref_pri:/^\$[a-f0-9]{6}\$$/};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.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-]+)$/,ref_pri:/^\$[a-f0-9]{6}\$$/};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;for(var c in a)a[c]=ref(this.ref_table,a[c]);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)};
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 e in a.children)a.children[e].parent=a,FormBuilder.formatFormObject(a.children[e],c);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};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,c){var e,d;r=FormBuilder.regex.reg_in_key;if("undefined"!=typeof c&&c.hasOwnProperty(a))return{def:c[a]};for(e in c)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:c[e],scope:matches}}return{}}; 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{}};
FormBuilder.replaceStatements=function(a,c,e,d){a=a instanceof Object?a:{};c=c instanceof Object?JSON.parse(JSON.stringify(c)):{};var b,f;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,e),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)"string"==typeof a[b]&&(a[b]=[a[b]], FormBuilder.replaceStatements=function(a,b,e,d){a=a instanceof Object?a:{};b=b instanceof Object?JSON.parse(JSON.stringify(b)):{};var c,f;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&&(f=FormBuilder.fetchNodeDefinition(a.node,e),f.hasOwnProperty("def")&&(a.node_link=JSON.parse(JSON.stringify(f.def)),f.hasOwnProperty("scope"))))for(c in f.scope)b[c]=f.scope[c];for(c in a)"string"==typeof a[c]&&(a[c]=[a[c]],
f=FormBuilder.replaceStatementsArray(a[b][0],c),!1!==f?a[b]=f:(a[b]=FormBuilder.replaceStatementsRegex(a[b],c),a[b]=FormBuilder.replaceStatementsPrimary(a[b],c),a[b]=FormBuilder.replaceStatementsArrayValue(a[b],c)));for(b in a)FormBuilder.regex.pri_in_key.test(b)?c[b.substring(1)]=ref(d,a[b]):FormBuilder.regex.arr_in_key.test(b)&&(c[b.substring(2)]=ref(d,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], f=FormBuilder.replaceStatementsArray(a[c][0],b),!1!==f?a[c]=f:(a[c]=FormBuilder.replaceStatementsRegex(a[c],b),a[c]=FormBuilder.replaceStatementsPrimary(a[c],b),a[c]=FormBuilder.replaceStatementsArrayValue(a[c],b)));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],
c,e,d);else a[b]instanceof Object&&FormBuilder.replaceStatements(a[b],c,e,d);return a};FormBuilder.replaceStatementsArray=function(a,c){var e=null,e=FormBuilder.regex.arr_out_set.exec(a);if(null===e)return!1;c.hasOwnProperty(e[1])||(c[e[1]]=[]);return c[e[1]]}; b,e,d);else a[c]instanceof Object&&FormBuilder.replaceStatements(a[c],b,e,d);return a};FormBuilder.replaceStatementsArray=function(a,b){var e=null,e=FormBuilder.regex.arr_out_set.exec(a);if(null===e)return!1;b.hasOwnProperty(e[1])||(b[e[1]]=[]);return b[e[1]]};
FormBuilder.replaceStatementsRegex=function(a,c){var e=FormBuilder.regex.reg_out_val,d=null,b=[],f,h,g,k;for(g=0;g<a.length;g++){m=null;b=[];f=-1;for(h=[];null!==(d=e.exec(a[g]))&&!(f>=e.lastIndex);)f=e.lastIndex,b.push(d);for(d=f=0;d<b.length;d++)k=b[d][1],(0<f||0<b[d].index)&&h.push(a[g].substr(f,b[d].index-f)),c.hasOwnProperty(k)||(c[k]=""),h.push(c[k]),f=b[d].index+b[d][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,c=[],f,h,g,k;for(g=0;g<a.length;g++){m=null;c=[];f=-1;for(h=[];null!==(d=e.exec(a[g]))&&!(f>=e.lastIndex);)f=e.lastIndex,c.push(d);for(d=f=0;d<c.length;d++)k=c[d][1],(0<f||0<c[d].index)&&h.push(a[g].substr(f,c[d].index-f)),b.hasOwnProperty(k)||(b[k]=""),h.push(b[k]),f=c[d].index+c[d][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,c){var e=FormBuilder.regex.pri_out_val,d=null,b=[],f,h,g,k;for(g=0;g<a.length;g++){m=null;b=[];f=0;for(h=[];null!==(d=e.exec(a[g]));)b.push(d);for(d=0;d<b.length;d++)k=b[d][1],(0<f||0<b[d].index)&&h.push(a[g].substr(f,b[d].index-f)),c.hasOwnProperty(k)||(c[k]=""),h.push(c[k]),f=b[d].index+b[d][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,c=[],f,h,g,k;for(g=0;g<a.length;g++){m=null;c=[];f=0;for(h=[];null!==(d=e.exec(a[g]));)c.push(d);for(d=0;d<c.length;d++)k=c[d][1],(0<f||0<c[d].index)&&h.push(a[g].substr(f,c[d].index-f)),b.hasOwnProperty(k)||(b[k]=""),h.push(b[k]),f=c[d].index+c[d][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.replaceStatementsArrayValue=function(a,c){var e=FormBuilder.regex.arr_out_val,d=null,b=[],f,h,g,k;for(g=0;g<a.length;g++){m=null;b=[];f=0;for(h=[];null!==(d=e.exec(a[g]));)b.push(d);for(d=0;d<b.length;d++)k=b[d][1],(0<f||0<b[d].index)&&h.push(a[g].substr(f,b[d].index-f)),c.hasOwnProperty(k)||(c[k]=""),h.push(c[k]),f=b[d].index+b[d][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.replaceStatementsArrayValue=function(a,b){var e=FormBuilder.regex.arr_out_val,d=null,c=[],f,h,g,k;for(g=0;g<a.length;g++){m=null;c=[];f=0;for(h=[];null!==(d=e.exec(a[g]));)c.push(d);for(d=0;d<c.length;d++)k=c[d][1],(0<f||0<c[d].index)&&h.push(a[g].substr(f,c[d].index-f)),b.hasOwnProperty(k)||(b[k]=""),h.push(b[k]),f=c[d].index+c[d][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.buildString=function(a,c){var e,d="";if("string"==typeof c&&FormBuilder.regex.ref_pri.test(c))return a[c];for(e in c)d=FormBuilder.regex.ref_pri.test(c[e])&&a.hasOwnProperty(c[e])?a[c[e]]instanceof Array?d+(""+FormBuilder.buildString(a,a[c[e]])):d+(""+a[c[e]]):d+(""+c[e].toString());return d}; 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];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};

View File

@ -68,7 +68,8 @@ FormBuilder.prototype = {
parent_element: null, // element qui contiendra le formulaire parent_element: null, // element qui contiendra le formulaire
built_form: null, // Object correspondant au formulaire construit built_form: null, // Object correspondant au formulaire construit
root_element: null, // Element correspondant à l'objet construit root_element: null, // Element correspondant à l'objet construit
ref_table: {} // Tableau contenant la liste des références ref_table: {}, // Tableau contenant la liste des références
ref_assoc: {} // Association entre table de référence et scope d'entrée
}; };
@ -134,10 +135,9 @@ FormBuilder.prototype.add_definition = function(def_object){
}; };
/* ON CONSTRUIT L'OBJET (avant la construction) /* ON CONSTRUIT L'OBJET
* *
* @scope<Object> for( key in object ){ * @scope<Object> [OPT] Objet contenant les variables à passer
[OPT] Objet contenant les variables à passer
* *
*/ */
FormBuilder.prototype.build = function(scope){ FormBuilder.prototype.build = function(scope){
@ -147,9 +147,14 @@ FormBuilder.prototype.build = function(scope){
scope = (scope == null) ? {} : scope; scope = (scope == null) ? {} : scope;
/* (2) On transforme toutes les variables en références */ /* (2) On transforme toutes les variables en références */
for( var key in scope ) for( var key in scope ){
// On enregistre la référence
scope[key] = ref(this.ref_table, scope[key]); scope[key] = ref(this.ref_table, scope[key]);
// On enregistre l'association, pour modifier plus tard
this.ref_assoc[key] = scope[key];
}
/* [1] On clone l'object /* [1] On clone l'object
=========================================================*/ =========================================================*/
this.built_form = JSON.parse(JSON.stringify(this.form_object)); this.built_form = JSON.parse(JSON.stringify(this.form_object));
@ -167,6 +172,33 @@ FormBuilder.prototype.build = function(scope){
}; };
/* ON MODIFIE L'OBJET (scope uniquement)
*
* @scope<Object> [OPT] Objet contenant les variables à modifier
*
*/
FormBuilder.prototype.update = function(scope){
/* [0] Gestion du paramètres
==================================================================*/
/* (1) On initialise le scope vide s'il n'est pas donné */
scope = (scope == null) ? {} : scope;
/* (2) On récupère toutes les références des variables */
for( var key in scope ){
// si on trouve l'association
if( this.ref_assoc.hasOwnProperty(key) ){
// On modifie la valeur
this.ref_table[ this.ref_assoc[key] ] = scope[key];
}
}
};
/* CONSTRUIT LES DOM ASSOCIE A L'OBJET CONSTRUIT /* CONSTRUIT LES DOM ASSOCIE A L'OBJET CONSTRUIT
* *
* @parent<Element> Element auquel rattacher le formulaire * @parent<Element> Element auquel rattacher le formulaire
@ -180,7 +212,7 @@ FormBuilder.prototype.attach = function(parent){
return false; return false;
this.parent_element = (parent instanceof Element) ? parent : this.parent_element; this.parent_element = (parent instanceof Element) ? parent : this.parent_element;
// TODO: Gestion de la création des éléments HTML
/* [1] Chapter Title /* [1] Chapter Title
=========================================================*/ =========================================================*/
@ -700,7 +732,7 @@ FormBuilder.replaceStatementsArrayValue = function(statements, scope){
* @return built<String> Chaine recomposée * @return built<String> Chaine recomposée
* *
*/ */
FormBuilder.buildString = function(ref_table, parts){ FormBuilder.readRef = function(ref_table, parts){
/* [0] Initialisation /* [0] Initialisation
=========================================================*/ =========================================================*/
var i, built = ""; var i, built = "";
@ -708,8 +740,14 @@ FormBuilder.buildString = function(ref_table, parts){
/* [1] Si c'est un tableau, on le retourne /* [1] Si c'est un tableau, on le retourne
=========================================================*/ =========================================================*/
if( typeof parts == 'string' && FormBuilder.regex.ref_pri.test(parts) ) if( typeof parts == 'string' && FormBuilder.regex.ref_pri.test(parts) ){
return ref_table[parts];
// tant qu'on a une référence
while( typeof parts == 'string' && FormBuilder.regex.ref_pri.test(parts) )
parts = ref_table[parts];
return parts;
}
/* [2] Sinon, on remplace par les valeurs /* [2] Sinon, on remplace par les valeurs
@ -721,7 +759,7 @@ FormBuilder.buildString = function(ref_table, parts){
// si le résultat est un tableau, on lance récursivement // si le résultat est un tableau, on lance récursivement
if( ref_table[parts[i]] instanceof Array ) if( ref_table[parts[i]] instanceof Array )
built += ''+ FormBuilder.buildString(ref_table, ref_table[parts[i]]); built += ''+ FormBuilder.readRef(ref_table, ref_table[parts[i]]);
// sinon // sinon
else else

View File

@ -1,4 +1,4 @@
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"}]},"custom-select":{node:"span",attributes:{"class":"select-container nobold"},children:[{node:"select",attributes:{"data-name":"{name}"},children:[{node:"option", 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:"xx{countcall} calls + {countsms} sms to {number}xx"}, 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:"xx{countcall} calls + {countsms} sms to {number}xx"},
{node:"input.text",$name:"number",$value:"{number}"},{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"]});console.log(fb.built_form);var str=function(a){return FormBuilder.buildString(fb.ref_table,a)}; {node:"input.text",$name:"number",$value:"{number}"},{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"]});console.log(fb.built_form);var str=function(a){return FormBuilder.readRef(fb.ref_table,a)};

View File

@ -86,4 +86,4 @@ fb.build({
}); });
console.log(fb.built_form); console.log(fb.built_form);
var str = function(attr){ return FormBuilder.buildString(fb.ref_table, attr); }; var str = function(attr){ return FormBuilder.readRef(fb.ref_table, attr); };