From 504ee83d336db8992c2dd18ba0ac3752d77da230 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sun, 18 Sep 2016 10:44:21 +0200 Subject: [PATCH] =?UTF-8?q?Gestion=20de=20'node=5Flink'=20en=20clonage=20(?= =?UTF-8?q?et=20non=20r=C3=A9f=C3=A9rence)=20+=20gestion=20du=20passage=20?= =?UTF-8?q?de=20'scope'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- js/lib/form-builder-min.js | 12 +++++----- js/lib/form-builder.js | 40 +++++++++++++++++---------------- js/lib/form-builder/main-min.js | 4 ++-- js/lib/form-builder/main.js | 34 +++++++++++++++------------- 4 files changed, 47 insertions(+), 43 deletions(-) diff --git a/js/lib/form-builder-min.js b/js/lib/form-builder-min.js index b0a319b..bd2db51 100644 --- a/js/lib/form-builder-min.js +++ b/js/lib/form-builder-min.js @@ -1,8 +1,8 @@ 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 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.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;ee;e++)matches["$"+e]=RegExp["$"+e];return{def:c[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=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(!(0b;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=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 Objet avec les remplacements effectués * */ -FormBuilder.replaceStatements = function(object, scope){ +FormBuilder.replaceStatements = function(object, scope, pad){ /* [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 next_scope = {}; - object.received = scope; /* (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 ) - next_scope[key] = object.scope[key]; + scope[key] = object.scope[key]; + + object.received = JSON.parse(JSON.stringify(scope)); /* [1] On remplace les valeurs =========================================================*/ @@ -409,38 +410,39 @@ FormBuilder.replaceStatements = function(object, scope){ /* (1) Ajout des variables de type '$nomVar' ---------------------------------------------------------*/ if( FormBuilder.regex.pri_in_key.test(key) ) - next_scope[ key.substring(1) ] = object[key]; + scope[ key.substring(1) ] = object[key]; /* (2) Ajout des tableaux de type '$$nomArr' ---------------------------------------------------------*/ else if( FormBuilder.regex.arr_in_key.test(key) ) - next_scope[ key.substring(2) ] = object[key]; + scope[ key.substring(2) ] = object[key]; } /* [3] On lance récursivement =========================================================*/ + /* (1) on clone le scope */ + scope = JSON.parse(JSON.stringify(scope)); + + for( key in object ){ /* S'il ne s'agit d'un attribut interdit */ if( FormBuilder.spread_attr.indexOf(key) > -1 ){ - console.log('spreads to', key); - /* (1) on envoie aussi le scope actuel (le tout cloné) */ - Object.assign( next_scope, scope ); - /* (3) 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 ) for( var i in object[key] ){ - console.log(object, key+'['+i+']', scope); - FormBuilder.replaceStatements(object[key][i], next_scope); + console.log(pad+''+object.node, key+'['+i+']'); + FormBuilder.replaceStatements(object[key][i], scope, pad); } /* (2) Si c'est un objet, on lance récursivement */ else if( object[key] instanceof Object ){ - console.warn(object, key, scope); - FormBuilder.replaceStatements(object[key], next_scope); + console.log(pad+''+object.node, key); + FormBuilder.replaceStatements(object[key], scope, pad); } diff --git a/js/lib/form-builder/main-min.js b/js/lib/form-builder/main-min.js index 205d7b6..6004c2d 100644 --- a/js/lib/form-builder/main-min.js +++ b/js/lib/form-builder/main-min.js @@ -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"]}); +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({}); diff --git a/js/lib/form-builder/main.js b/js/lib/form-builder/main.js index 0bc3495..1a036ac 100644 --- a/js/lib/form-builder/main.js +++ b/js/lib/form-builder/main.js @@ -53,14 +53,14 @@ var 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.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.text', $name: 'number', $value: '{number}' }, + { node: 'input.g', $name: 'number', $value: '{number}' }, { node: 'custom-select', $name: 'existing', $$options: '{{options}}' } ] @@ -75,12 +75,14 @@ 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({ +// uid: 1, +// call: 2, +// sms: 3, +// countcall: 4, +// countsms: 5, +// number: '01 02 03 04 05', +// options: ['a', 'b', 'c', 'd'] +// }); + +fb.build({});