Gestion de 'browse' avec les valeurs d'un tableau, possibilité de créer des fonctions custom qui retournerons une valeur pour chaque élément, sinon on peut utiliser les attributs propres

This commit is contained in:
xdrm-brackets 2016-09-20 14:37:40 +02:00
parent 7e26e80604
commit 410e3f2359
4 changed files with 16 additions and 11 deletions

View File

@ -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.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<g;e++)l[e]={},l[e][a.repeat.id+":i"]=e,l[e][a.repeat.id+":n"]=g;if(a.hasOwnProperty("browse")&&(g=FormBuilder.readRef(b,a.browse.array),g instanceof Array)){l=[];h=[];n={};k=new RegExp(a.browse.id+".(.+)"); 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<g;e++)l[e]={},l[e][a.repeat.id+":i"]=e,l[e][a.repeat.id+":n"]=g;if(a.hasOwnProperty("browse")&&(g=FormBuilder.readRef(b,a.browse.array),g instanceof Array)){l=[];h=[];n={};k=new RegExp(a.browse.id+".(.+)");
for(e in f)k.test(e)&&h.push(RegExp.$1);if(a.browse.hasOwnProperty("funcs"))for(e in a.browse.funcs)k.test(e)&&(n[RegExp.$1]=FormBuilder.readRef(b,a.browse.funcs[e]));f.hasOwnProperty(a.browse.id+":i")||(f[a.browse.id+":i"]=ref(0));f.hasOwnProperty(a.browse.id+":n")||(f[a.browse.id+":n"]=ref(g));for(e=0;e<g.length;e++){l[e]={};for(c in h)g[e].hasOwnProperty(h[c])?l[e][a.browse.id+"."+h[c]]=g[e][h[c]]:n.hasOwnProperty(h[c])&&(l[e][a.browse.id+"."+h[c]]=n[h[c]](g[e]));l[e][a.browse.id+":i"]=e;l[e][a.browse.id+ for(e in f)k.test(e)&&h.push(RegExp.$1);if(a.browse.hasOwnProperty("funcs"))for(e in a.browse.funcs)k.test(e)&&(n[RegExp.$1]=FormBuilder.readRef(b,a.browse.funcs[e]));f.hasOwnProperty(a.browse.id+":i")||(f[a.browse.id+":i"]=ref(0));f.hasOwnProperty(a.browse.id+":n")||(f[a.browse.id+":n"]=ref(g));for(e=0;e<g.length;e++){l[e]={};for(c in h)g[e].hasOwnProperty(h[c])?l[e][a.browse.id+"."+h[c]]=g[e][h[c]]:n.hasOwnProperty(h[c])&&(l[e][a.browse.id+"."+h[c]]=n[h[c]](g[e]));l[e][a.browse.id+":i"]=e;l[e][a.browse.id+
":n"]=g.length}}for(h=0;h<l.length;h++){d[h]={prev:[],node:null,next:[]};for(e in l[h])b[f[e]]=l[h][e];if(a.hasOwnProperty("prev_nodes"))for(e in a.prev_nodes)d[h].prev=FormBuilder.buildElements(a.prev_nodes[e],b,f);if(a.hasOwnProperty("next_nodes"))for(e in a.next_nodes)d[h].next=FormBuilder.buildElements(a.next_nodes[e],b,f);if(a.hasOwnProperty("node_type"))d[h].node=document.createElement(FormBuilder.readRef(b,a.node_type));else if(a.hasOwnProperty("node_link"))for(e in g=FormBuilder.buildElements(a.node_link, ":n"]=g.length}}for(h=0;h<l.length;h++){d[h]={prev:[],node:null,next:[]};if(l[h]instanceof Object)for(e in l[h])b[f[e]]=l[h][e];if(a.hasOwnProperty("prev_nodes"))for(e in a.prev_nodes)d[h].prev=FormBuilder.buildElements(a.prev_nodes[e],b,f);if(a.hasOwnProperty("next_nodes"))for(e in a.next_nodes)d[h].next=FormBuilder.buildElements(a.next_nodes[e],b,f);if(a.hasOwnProperty("node_type"))d[h].node=document.createElement(FormBuilder.readRef(b,a.node_type));else if(a.hasOwnProperty("node_link"))for(e in g=
b,f),g)d[h].prev=d[h].prev.concat(g[e].prev),d[h].node=g[e].node,d[h].next=g[e].next.concat(d[h].next);if(a.hasOwnProperty("attributes"))for(e in a.attributes)d[h].node.setAttribute(e,FormBuilder.readRef(b,a.attributes[e]));a.hasOwnProperty("text")&&(d[h].node.innerHTML=a.text);if(a.hasOwnProperty("listeners"))for(e in a.listeners)d[h].node.addEventListener(e,FormBuilder.readRef(b,a.listeners[e]),!1);if(a.hasOwnProperty("children"))for(e in a.children)for(k in g=FormBuilder.buildElements(a.children[e], FormBuilder.buildElements(a.node_link,b,f),g)d[h].prev=d[h].prev.concat(g[e].prev),d[h].node=g[e].node,d[h].next=g[e].next.concat(d[h].next);if(a.hasOwnProperty("attributes"))for(e in a.attributes)d[h].node.setAttribute(e,FormBuilder.readRef(b,a.attributes[e]));a.hasOwnProperty("text")&&(d[h].node.innerHTML=FormBuilder.readRef(b,a.text));if(a.hasOwnProperty("listeners"))for(e in a.listeners)d[h].node.addEventListener(e,FormBuilder.readRef(b,a.listeners[e]),!1);if(a.hasOwnProperty("children"))for(e in a.children)for(k in g=
b,f),g){for(c in g[k].prev)d[h].node.appendChild(g[k].prev[c]);d[h].node.appendChild(g[k].node);for(c in g[k].prev)d[h].node.appendChild(g[k].next[c])}}return a.dom=d}; FormBuilder.buildElements(a.children[e],b,f),g){for(c in g[k].prev)d[h].node.appendChild(g[k].prev[c]);d[h].node.appendChild(g[k].node);for(c in g[k].prev)d[h].node.appendChild(g[k].next[c])}}return a.dom=d};

View File

@ -821,7 +821,7 @@ FormBuilder.readRef = function(ref_table, parts){
=========================================================*/ =========================================================*/
for( i in parts ){ for( i in parts ){
/* (1) Si <refObject>, 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]) ){ if( FormBuilder.regex.ref_pri.test(parts[i]) && ref_table.hasOwnProperty(parts[i]) ){
// si le résultat est un tableau, on lance récursivement // 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] = {}; repeated[i] = {};
// Pour chaque champ, on récupère/calcule la valeur
for( j in scope ){ for( j in scope ){
// {1} Si la valeur est un attribut // // {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 // // {2} Si c'est une fonction custom //
else if( funcs.hasOwnProperty(scope[j]) ) 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,9 +985,13 @@ FormBuilder.buildElements = function(definition, ref_table, ref_assoc){
built[b] = { prev: [], node: null, next: [] }; built[b] = { prev: [], node: null, next: [] };
/* (2) Gestion des variables de 'repeat' et 'browse' */ /* (2) Gestion des variables de 'repeat' et 'browse' */
if( repeated[b] instanceof Object ){
for( i in repeated[b] ) for( i in repeated[b] )
ref_table[ ref_assoc[i] ] = repeated[b][i]; ref_table[ ref_assoc[i] ] = repeated[b][i];
}
/* [3] On construit les éléments @prev_nodes, s'ils existent /* [3] On construit les éléments @prev_nodes, s'ils existent
@ -1046,7 +1051,7 @@ FormBuilder.buildElements = function(definition, ref_table, ref_assoc){
if( definition.hasOwnProperty('text') ){ if( definition.hasOwnProperty('text') ){
// Note: Override les enfants // Note: Override les enfants
built[b].node.innerHTML = definition.text; built[b].node.innerHTML = FormBuilder.readRef(ref_table, definition.text);
} }

View File

@ -1,5 +1,5 @@
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"}],listeners:{focus:"{xx()}"}},"custom-select":{node:"span",attributes:{"class":"select-container nobold"},children:[{node:"select",attributes:{"data-name":"{name}"}, 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", 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",
$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); $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); 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);

View File

@ -41,7 +41,7 @@ var custom_definition = {
{ {
node: 'option', node: 'option',
attributes: { value: '{options.id}' }, attributes: { value: '{options.id}' },
text: '{options.value}', text: '{options.value} - {options:i}/{options:n}',
browse: { browse: {
array: '{options[]}', array: '{options[]}',
funcs: { funcs: {