From 410e3f2359236bac955b2f98a2bebd94e1f76096 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Tue, 20 Sep 2016 14:37:40 +0200 Subject: [PATCH] =?UTF-8?q?Gestion=20de=20'browse'=20avec=20les=20valeurs?= =?UTF-8?q?=20d'un=20tableau,=20possibilit=C3=A9=20de=20cr=C3=A9er=20des?= =?UTF-8?q?=20fonctions=20custom=20qui=20retournerons=20une=20valeur=20pou?= =?UTF-8?q?r=20chaque=20=C3=A9l=C3=A9ment,=20sinon=20on=20peut=20utiliser?= =?UTF-8?q?=20les=20attributs=20propres?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- js/lib/form-builder-min.js | 6 +++--- js/lib/form-builder.js | 15 ++++++++++----- js/lib/form-builder/main-min.js | 4 ++-- js/lib/form-builder/main.js | 2 +- 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/js/lib/form-builder-min.js b/js/lib/form-builder-min.js index e1f2c90..893b720 100644 --- a/js/lib/form-builder-min.js +++ b/js/lib/form-builder-min.js @@ -14,6 +14,6 @@ FormBuilder.replaceStatementsArrayValue=function(a,b,f,d){b=FormBuilder.regex.ar FormBuilder.readRef=function(a,b){var f,d="";if("string"==typeof b&&FormBuilder.regex.ref_pri.test(b)){for(;"string"==typeof b&&FormBuilder.regex.ref_pri.test(b);)b=a[b],b instanceof Array&&1==b.length&&(b=b[0]);return b}for(f in b)d=FormBuilder.regex.ref_pri.test(b[f])&&a.hasOwnProperty(b[f])?a[b[f]]instanceof Array?d+FormBuilder.readRef(a,a[b[f]]):d+a[b[f]]:d+b[f].toString();return d}; FormBuilder.buildElements=function(a,b,f){var d=[],e,c,k,g,h,l=["NULL"],n;if(a.hasOwnProperty("repeat")&&(g=FormBuilder.readRef(b,a.repeat.n),!isNaN(g)))for(l=[],f.hasOwnProperty(a.repeat.id+":i")||(f[a.repeat.id+":i"]=ref(0)),f.hasOwnProperty(a.repeat.id+":n")||(f[a.repeat.id+":n"]=ref(g)),e=0;e, on ajoute la valeur (si elle existe) */ + /* (1) Si référence, on ajoute la valeur (si elle existe) */ if( FormBuilder.regex.ref_pri.test(parts[i]) && ref_table.hasOwnProperty(parts[i]) ){ // si le résultat est un tableau, on lance récursivement @@ -953,6 +953,7 @@ FormBuilder.buildElements = function(definition, ref_table, ref_assoc){ repeated[i] = {}; + // Pour chaque champ, on récupère/calcule la valeur for( j in scope ){ // {1} Si la valeur est un attribut // @@ -961,7 +962,7 @@ FormBuilder.buildElements = function(definition, ref_table, ref_assoc){ // {2} Si c'est une fonction custom // else if( funcs.hasOwnProperty(scope[j]) ) - repeated[i][definition.browse.id+'.'+scope[j]] = funcs[scope[j]](tmp[i]); + repeated[i][definition.browse.id+'.'+scope[j]] = funcs[ scope[j] ]( tmp[i] ); } @@ -984,8 +985,12 @@ FormBuilder.buildElements = function(definition, ref_table, ref_assoc){ built[b] = { prev: [], node: null, next: [] }; /* (2) Gestion des variables de 'repeat' et 'browse' */ - for( i in repeated[b] ) - ref_table[ ref_assoc[i] ] = repeated[b][i]; + if( repeated[b] instanceof Object ){ + + for( i in repeated[b] ) + ref_table[ ref_assoc[i] ] = repeated[b][i]; + + } @@ -1046,7 +1051,7 @@ FormBuilder.buildElements = function(definition, ref_table, ref_assoc){ if( definition.hasOwnProperty('text') ){ // Note: Override les enfants - built[b].node.innerHTML = definition.text; + built[b].node.innerHTML = FormBuilder.readRef(ref_table, definition.text); } diff --git a/js/lib/form-builder/main-min.js b/js/lib/form-builder/main-min.js index d9a530c..8b054e6 100644 --- a/js/lib/form-builder/main-min.js +++ b/js/lib/form-builder/main-min.js @@ -1,5 +1,5 @@ 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"}],listeners:{focus:"{xx()}"}},"custom-select":{node:"span",attributes:{"class":"select-container nobold"},children:[{node:"select",attributes:{"data-name":"{name}"}, -children:[{node:"option",attributes:{value:"{options.id}"},text:"{options.value}",browse:{array:"{options[]}",funcs:{"options.value":"{getval()}","options.id":"{getnam()}"}}}]}],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:"total",$value:"xx{countcall} calls + {countsms} sms to {number}xx"},{node:"input.text",$name:"count{rep:i}",$value:"{rep:i} sur {rep:n}",repeat:{n:10,id:"rep"}},{node:"input.text",$name:"number",$value:"{number}",$xx:"{clicklistener}"},{node:"custom-select",$name:"existing",$options:"{options[]}"}]},fb=new FormBuilder(form);fb.add_definition(default_definition);fb.add_definition(custom_definition); +children:[{node:"option",attributes:{value:"{options.id}"},text:"{options.value} - {options:i}/{options:n}",browse:{array:"{options[]}",funcs:{"options.value":"{getval()}","options.id":"{getnam()}"}}}]}],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:"total",$value:"xx{countcall} calls + {countsms} sms to {number}xx"},{node:"input.text",$name:"count{rep:i}",$value:"{rep:i} sur {rep:n}",repeat:{n:10,id:"rep"}},{node:"input.text",$name:"number",$value:"{number}",$xx:"{clicklistener}"},{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"],clicklistener:function(a){console.log("clicked",a)},getval:function(a){return a},getnam:function(a){return"["+a+"]"}});console.log(fb.built_form);var str=function(a){return FormBuilder.readRef(fb.ref_table,a)};fb.attach(document.body); diff --git a/js/lib/form-builder/main.js b/js/lib/form-builder/main.js index e43af50..83f8445 100644 --- a/js/lib/form-builder/main.js +++ b/js/lib/form-builder/main.js @@ -41,7 +41,7 @@ var custom_definition = { { node: 'option', attributes: { value: '{options.id}' }, - text: '{options.value}', + text: '{options.value} - {options:i}/{options:n}', browse: { array: '{options[]}', funcs: {