From 8d91b62bc618180585baa0fae6efc5696a7260f7 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sun, 18 Sep 2016 11:29:46 +0200 Subject: [PATCH] =?UTF-8?q?Gestion=20de=20'node=5Flink'=20r=C3=A9cursiveme?= =?UTF-8?q?nt=20(pas=20uniquement=20au=20premier=20niveau)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- js/lib/form-builder-min.js | 13 ++-- js/lib/form-builder.js | 112 ++++++++++++++++---------------- js/lib/form-builder/main-min.js | 4 +- js/lib/form-builder/main.js | 34 +++++----- 4 files changed, 80 insertions(+), 83 deletions(-) diff --git a/js/lib/form-builder-min.js b/js/lib/form-builder-min.js index 9769645..54d40e8 100644 --- a/js/lib/form-builder-min.js +++ b/js/lib/form-builder-min.js @@ -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;bb;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(!(0b;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=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 - =========================================================*/ - 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 Objet dans lequel remplacer les valeurs * @scope Ensemble des variables permettant le remplacement +* @definitions Définitions des éléments * * @return replaced 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; }; diff --git a/js/lib/form-builder/main-min.js b/js/lib/form-builder/main-min.js index 6004c2d..205d7b6 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.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"]}); diff --git a/js/lib/form-builder/main.js b/js/lib/form-builder/main.js index 1a036ac..0bc3495 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.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'] +});