Correction du scope (mais erreur persistante) v2
This commit is contained in:
parent
01649b00bd
commit
3d346f5efe
|
@ -1,3 +1,3 @@
|
||||||
var ElementObject={node:"node name defined earlier",attributes:{attr_name_1:"attr_value_1",attr_name_2:"attr_value_2"},children:[ElementObject],next_nodes:[ElementObject],prev_nodes:[ElementObject],text:"some text to be the innerHTML",repeat:"number to repeat ElementObject || {{array}} to repeat ElementObject",node_type:"html tag name",listeners:[EventListener]},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"},
|
var ElementObject={node:"node name defined earlier",attributes:{attr_name_1:"attr_value_1",attr_name_2:"attr_value_2"},children:[ElementObject],next_nodes:[ElementObject],prev_nodes:[ElementObject],text:"some text to be the innerHTML",repeat:"number to repeat ElementObject || {{array}} to repeat ElementObject",node_type:"html tag name",listeners:[EventListener]},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",
|
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:text",$name:"number",$value:"{number}",$placeholder:"{Num\u00e9ro de t\u00e9l\u00e9phone}"},{node:"custom-select",$name:"existing",$$options:"{{options}}"}]};
|
"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:"number",$value:"{number}",$placeholder:"{Num\u00e9ro de t\u00e9l\u00e9phone}"},{node:"custom-select",$name:"existing",$$options:"{{options}}"}]};
|
||||||
|
|
|
@ -102,7 +102,7 @@ var default_definition = {
|
||||||
};
|
};
|
||||||
|
|
||||||
var custom_definition = {
|
var custom_definition = {
|
||||||
'/^input:([a-z]+)$/': {
|
'/^input\.([a-z]+)$/': {
|
||||||
node: 'input',
|
node: 'input',
|
||||||
attributes: {
|
attributes: {
|
||||||
'type': '{$1}',
|
'type': '{$1}',
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
var FormBuilder=function(a,d){this.parent_element=a;this.form_object=d};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-]+)$/};
|
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.no_recursion=["parent","scope","received"];FormBuilder.prototype.add_definition=function(a){for(var d in a)this.defs_object[d]=a[d]};
|
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.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 b=FormBuilder.fetchNodeDefinition(a.node,c);null!==b&&(a.node_link=b.def,b.hasOwnProperty("scope")&&(a.scope=b.scope))}if(a.hasOwnProperty("children"))for(var e in a.children)a.children[e].parent=a,FormBuilder.formatFormObject(a.children[e],c);return a};
|
||||||
FormBuilder.formatFormObject=function(a,d){if(a.hasOwnProperty("node")&&"string"==typeof a.node){var b=FormBuilder.fetchNodeDefinition(a.node,d);null!==b&&(a.node_link=b.def,b.hasOwnProperty("scope")&&(a.scope=b.scope))}if(a.hasOwnProperty("children"))for(var f in a.children)a.children[f].parent=a,FormBuilder.formatFormObject(a.children[f],d);return a};
|
FormBuilder.fetchNodeDefinition=function(a,c){var b,e;r=FormBuilder.regex.reg_in_key;if(null!=c&&c.hasOwnProperty(a))return{def:c.node,scope:null};for(b in c)if(r.test(b)&&(e=new RegExp(b.slice(1,-1)),e.test(a))){matches={};for(e=1;e<RegExp.length&&10>e;e++)matches["$"+e]=RegExp["$"+e];return{def:c[b],scope:matches}}return null};
|
||||||
FormBuilder.fetchNodeDefinition=function(a,d){var b,f;r=FormBuilder.regex.reg_in_key;if(null!=d&&d.hasOwnProperty(a))return{def:d.node,scope:null};for(b in d)if(r.test(b)&&(f=new RegExp(b.slice(1,-1)),f.test(a))){matches={};for(f=1;f<RegExp.length&&10>f;f++)matches["$"+f]=RegExp["$"+f];return{def:d[b],scope:matches}}return null};
|
FormBuilder.replaceStatements=function(a,c){a=a instanceof Object?a:{};c=c instanceof Object?JSON.parse(JSON.stringify(c)):{};var b,e,f,g={};a.received=c;if(a.hasOwnProperty("scope")&&a.scope instanceof Object)for(b in a.scope)g[b]=a.scope[b];for(b in a)if("string"==typeof a[b]){e=FormBuilder.regex.arr_out_set;var d=[];(f=e.exec(a[b]))&&d.push(f[1]);for(f in d)c.hasOwnProperty(d[f])||(c[d[f]]=[]),a[b]=c[d[f]];if(!(0<d.length)){e=FormBuilder.regex.pri_out_val;for(d=[];null!==(f=e.exec(a[b]));)d.push(f[1]);
|
||||||
FormBuilder.replaceStatements=function(a,d){a=a instanceof Object?a:{};d=d instanceof Object?d:{};var b,f,c,g={};if(a.hasOwnProperty("scope")&&a.scope instanceof Object)for(b in a.scope)g[b]=a.scope[b];for(b in a)if("string"==typeof a[b]){f=FormBuilder.regex.arr_out_set;var e=[];(c=f.exec(a[b]))&&e.push(c[1]);for(c in e)d.hasOwnProperty(e[c])||(d[e[c]]=[]),a[b]=d[e[c]];if(!(0<e.length)){f=FormBuilder.regex.pri_out_val;for(e=[];null!==(c=f.exec(a[b]));)e.push(c[1]);for(c in e)d.hasOwnProperty(e[c])||
|
for(f in d)c.hasOwnProperty(d[f])||(c[d[f]]=""),e=new RegExp("{"+d[f]+"}","g"),a[b]=a[b].replace(e,c[d[f]]);if(!(0<d.length)){e=FormBuilder.regex.reg_out_val;lasti=-1;for(d=[];null!==(f=e.exec(a[b]))&&!(lasti>=e.lastIndex);)lasti=e.lastIndex,d.push(f[1]);for(f in d)c.hasOwnProperty(d[f])||(c[d[f]]=""),e=new RegExp("{\\$"+d[f][1]+"}","g"),a[b]=a[b].replace(e,c[d[f]]);if(!(0<d.length)){e=FormBuilder.regex.arr_out_val;for(d=[];null!==(f=e.exec(a[b]));)d.push(f[1]);for(f in d)c.hasOwnProperty(d[f])||
|
||||||
(d[e[c]]=""),f=new RegExp("{"+e[c]+"}","g"),a[b]=a[b].replace(f,d[e[c]]);if(!(0<e.length)){f=FormBuilder.regex.reg_out_val;lasti=-1;for(e=[];null!==(c=f.exec(a[b]))&&!(lasti>=f.lastIndex);)lasti=f.lastIndex,e.push(c[1]);for(c in e)d.hasOwnProperty(e[c])||(d[e[c]]=""),f=new RegExp("{\\$"+e[c][1]+"}","g"),a[b]=a[b].replace(f,d[e[c]]);if(!(0<e.length)){f=FormBuilder.regex.arr_out_val;for(e=[];null!==(c=f.exec(a[b]));)e.push(c[1]);for(c in e)d.hasOwnProperty(e[c])||(d[e[c]]=""),f=new RegExp("/{"+e[c]+
|
(c[d[f]]=""),e=new RegExp("/{"+d[f]+"}/","g"),a[b].replace(e,c[d[f]])}}}}for(b in a)FormBuilder.regex.pri_in_key.test(b)?g[b.substring(1)]=a[b]:FormBuilder.regex.arr_in_key.test(b)&&(g[b.substring(2)]=a[b]);for(b in a)if(-1<FormBuilder.spread_attr.indexOf(b))if(console.log("spreads to",b),Object.assign(g,c),a[b]instanceof Array)for(var h in a[b])console.log(a,b+"["+h+"]",c),FormBuilder.replaceStatements(a[b][h],g);else a[b]instanceof Object&&(console.warn(a,b,c),FormBuilder.replaceStatements(a[b],
|
||||||
"}/","g"),a[b].replace(f,d[e[c]])}}}}for(b in a)FormBuilder.regex.pri_in_key.test(b)?g[b.substring(1)]=a[b]:FormBuilder.regex.arr_in_key.test(b)&&(g[b.substring(2)]=a[b]);for(b in a)if(-1<FormBuilder.spread_attr.indexOf(b))if(console.log("spreads to",b),c=[JSON.stringify(d).slice(0,-1),JSON.stringify(g).slice(1)],2<c[0].length&&2<c[1].length&&(c[1]=","+c[1]),g=JSON.parse(c[0]+c[1]),a[b]instanceof Array)for(var h in a[b])console.log(a,b+"["+h+"]",d),FormBuilder.replaceStatements(a[b][h],g);else a[b]instanceof
|
g));return a};
|
||||||
Object&&(console.log(a,b,d),FormBuilder.replaceStatements(a[b],g));return a};
|
|
||||||
|
|
|
@ -67,12 +67,6 @@ FormBuilder.allowed_attr = [ // Liste des attributs fixes aut
|
||||||
'repeat' /* nombre/tableau déterminant qu'il faut répéter @n fois ou en fonction de la taille du tableau */
|
'repeat' /* nombre/tableau déterminant qu'il faut répéter @n fois ou en fonction de la taille du tableau */
|
||||||
];
|
];
|
||||||
|
|
||||||
FormBuilder.no_recursion = [ // Attributs à ne pas lancer récursivement (car inutile)
|
|
||||||
'parent', /* ne pas lancer récursivement sur parent car sinon récursion infinie */
|
|
||||||
'scope', /* ne pas lancer sur scope, car fait partie des méta-données */
|
|
||||||
'received'
|
|
||||||
];
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/************************************************************/
|
/************************************************************/
|
||||||
|
@ -263,18 +257,19 @@ FormBuilder.replaceStatements = function(object, scope){
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
/* (1) Paramètres */
|
/* (1) Paramètres */
|
||||||
object = (object instanceof Object) ? object : {};
|
object = (object instanceof Object) ? object : {};
|
||||||
scope = (scope instanceof Object) ? scope : {};
|
scope = (scope instanceof Object) ? JSON.parse(JSON.stringify(scope)) : {};
|
||||||
|
|
||||||
/* (2) Variables */
|
/* (2) Variables */
|
||||||
var key, r, tmpr, m;
|
var key, r, tmpr, m;
|
||||||
var next_scope = {};
|
var next_scope = {};
|
||||||
|
|
||||||
|
object.received = scope;
|
||||||
|
|
||||||
/* (3) On récupère le scope s'il est dans l'attribut 'scope' */
|
/* (3) On récupère le scope s'il est dans l'attribut 'scope' */
|
||||||
if( object.hasOwnProperty('scope') && object.scope instanceof Object )
|
if( object.hasOwnProperty('scope') && object.scope instanceof Object )
|
||||||
for( key in object.scope )
|
for( key in object.scope )
|
||||||
next_scope[key] = object.scope[key];
|
next_scope[key] = object.scope[key];
|
||||||
|
|
||||||
|
|
||||||
/* [1] On remplace les valeurs
|
/* [1] On remplace les valeurs
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
for( key in object ){
|
for( key in object ){
|
||||||
|
@ -433,16 +428,7 @@ FormBuilder.replaceStatements = function(object, scope){
|
||||||
console.log('spreads to', key);
|
console.log('spreads to', key);
|
||||||
|
|
||||||
/* (1) on envoie aussi le scope actuel (le tout cloné) */
|
/* (1) on envoie aussi le scope actuel (le tout cloné) */
|
||||||
var tmp = [
|
Object.assign( next_scope, scope );
|
||||||
JSON.stringify(scope).slice(0,-1),
|
|
||||||
JSON.stringify(next_scope).slice(1)
|
|
||||||
];
|
|
||||||
|
|
||||||
// si au moins une valeur @scope ET @next_scope
|
|
||||||
if( tmp[0].length > 2 && tmp[1].length > 2 )
|
|
||||||
tmp[1] = ',' + tmp[1];
|
|
||||||
|
|
||||||
next_scope = JSON.parse( tmp[0] + tmp[1] );
|
|
||||||
|
|
||||||
/* (3) Si c'est un tableau, on lance récursivement pour chaque item */
|
/* (3) Si c'est un tableau, on lance récursivement pour chaque item */
|
||||||
if( object[key] instanceof Array )
|
if( object[key] instanceof Array )
|
||||||
|
@ -453,7 +439,7 @@ FormBuilder.replaceStatements = function(object, scope){
|
||||||
|
|
||||||
/* (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 ){
|
||||||
console.log(object, key, scope);
|
console.warn(object, key, scope);
|
||||||
FormBuilder.replaceStatements(object[key], next_scope);
|
FormBuilder.replaceStatements(object[key], next_scope);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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:"{countcall} calls + {countsms} sms to {number}"},
|
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"]});
|
||||||
|
|
|
@ -15,7 +15,7 @@ var default_definition = {
|
||||||
};
|
};
|
||||||
|
|
||||||
var custom_definition = {
|
var custom_definition = {
|
||||||
'/^input:([a-z]+)$/': {
|
'/^input\.([a-z]+)$/': {
|
||||||
node: 'input',
|
node: 'input',
|
||||||
attributes: {
|
attributes: {
|
||||||
'type': '{$1}',
|
'type': '{$1}',
|
||||||
|
@ -53,14 +53,14 @@ var form = {
|
||||||
node: 'h4',
|
node: 'h4',
|
||||||
attributes: { 'data-icon': 'o', 'class': 'new-contact color2' },
|
attributes: { 'data-icon': 'o', 'class': 'new-contact color2' },
|
||||||
children: [
|
children: [
|
||||||
{ node: 'input:hidden', $name: 'uid', $value: '{uid}' },
|
{ node: 'input.hidden', $name: 'uid', $value: '{uid}' },
|
||||||
{ node: 'input:hidden', $name: 'call', $value: '{call}' },
|
{ node: 'input.hidden', $name: 'call', $value: '{call}' },
|
||||||
{ 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: '{countcall} calls + {countsms} sms to {number}' },
|
{ node: 'input.hidden', $name: 'total', $value: '{countcall} calls + {countsms} sms to {number}' },
|
||||||
|
|
||||||
{ node: 'input:text', $name: 'number', $value: '{number}' },
|
{ node: 'input.text', $name: 'number', $value: '{number}' },
|
||||||
|
|
||||||
{ node: 'custom-select', $name: 'existing', $$options: '{{options}}' }
|
{ node: 'custom-select', $name: 'existing', $$options: '{{options}}' }
|
||||||
]
|
]
|
||||||
|
|
Loading…
Reference in New Issue