From 7e26e80604e4475e796fcd5f319ac38215cf01be Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Tue, 20 Sep 2016 14:18:38 +0200 Subject: [PATCH] Remplacement de 'repeater.i' et 'repeater.n' en 'repeater:i' et 'repeater:n' --- js/lib/form-builder-min.js | 28 +++++---- js/lib/form-builder.js | 108 +++++++++++++++++++++++++------- js/lib/form-builder/main-min.js | 4 +- js/lib/form-builder/main.js | 22 +++---- 4 files changed, 115 insertions(+), 47 deletions(-) diff --git a/js/lib/form-builder-min.js b/js/lib/form-builder-min.js index 2c8f9f8..e1f2c90 100644 --- a/js/lib/form-builder-min.js +++ b/js/lib/form-builder-min.js @@ -1,17 +1,19 @@ var ref=function(a,b){for(var f=null;null==f||a.hasOwnProperty(f);)f="$"+(268435456+Math.floor(4026531839*Math.random())).toString(16)+"$";a[f]=b;return f},FormBuilder=function(a){this.form_object=a};FormBuilder.prototype={form_object:this.form_object,defs_object:{},parent_element:null,built_form:null,root_element:null,ref_table:{$00000000$:null},ref_assoc:{NULL:"$00000000$"}}; -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-]+)$/,fun_out_val:/^\{([a-z-]+)\(\)\}$/,ref_pri:/^\$[a-f0-9]{8}\$$/,ref_arv:/^(.+)\.(.+)$/};FormBuilder.spread_attr="children next_nodes prev_nodes attributes node_link listeners repeat browse".split(" "); +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-]+)$/,fun_out_val:/^\{([a-z-]+)\(\)\}$/,ref_pri:/^\$[a-f0-9]{8}\$$/};FormBuilder.spread_attr="children next_nodes prev_nodes attributes node_link listeners repeat browse funcs".split(" "); FormBuilder.prototype.add_definition=function(a){for(var b in a)this.defs_object[b]=a[b]};FormBuilder.prototype.build=function(a){a=null==a?{}:a;for(var b in a)a[b]=ref(this.ref_table,a[b]),this.ref_assoc[b]=a[b];this.built_form=JSON.parse(JSON.stringify(this.form_object));this.built_form=FormBuilder.formatFormObject(this.built_form,this.defs_object);this.built_form=FormBuilder.replaceStatements(this.built_form,a,this.defs_object,this.ref_table,this.ref_assoc)}; FormBuilder.prototype.update=function(a){a=null==a?{}:a;for(var b in a)this.ref_assoc.hasOwnProperty(b)&&(this.ref_table[this.ref_assoc[b]]=a[b])}; FormBuilder.prototype.attach=function(a){if(!(a instanceof Element)&&null===this.parent_element)return!1;this.parent_element=a instanceof Element?a:this.parent_element;var b,f;a=FormBuilder.buildElements(this.built_form,this.ref_table,this.ref_assoc);for(b in a){for(f in a[b].prev)this.parent_element.appendChild(a[b].prev[f]);this.parent_element.appendChild(a[b].node);for(f in a[b].next)this.parent_element.appendChild(a[b].next[f])}};FormBuilder.formatFormObject=function(a,b){return a}; -FormBuilder.fetchNodeDefinition=function(a,b){var f,c;r=FormBuilder.regex.reg_in_key;if("undefined"!=typeof b&&b.hasOwnProperty(a))return{def:b[a]};for(f in b)if(r.test(f)&&(c=new RegExp(f.slice(1,-1)),c.test(a))){matches={};for(c=1;cc;c++)matches["$"+c]=RegExp["$"+c];return{def:b[f],scope:matches}}return{}}; -FormBuilder.replaceStatements=function(a,b,f,c,e){a=a instanceof Object?a:{};b=b instanceof Object?JSON.parse(JSON.stringify(b)):{};var d,k;if(a.hasOwnProperty("scope")&&a.scope instanceof Object)for(d in a.scope)b[d]=a.scope[d];if(a.hasOwnProperty("node")&&"string"==typeof a.node&&(k=FormBuilder.fetchNodeDefinition(a.node,f),k.hasOwnProperty("def")&&(a.node_link=JSON.parse(JSON.stringify(k.def)),k.hasOwnProperty("scope"))))for(d in k.scope)b[d]=k.scope[d];a.hasOwnProperty("browse")&&(a.browse.hasOwnProperty("array")&& -FormBuilder.regex.arr_out_set.test(a.browse.array)?a.browse.id=RegExp.$1:delete a.browse);for(d in a)"string"==typeof a[d]?(a[d]=[a[d]],k=FormBuilder.replaceStatementsFunction(c,a[d][0],b),!1!==k?a[d]=k:(k=FormBuilder.replaceStatementsArray(a[d][0],b,c),!1!==k?a[d]=k:(a[d]=FormBuilder.replaceStatementsRegex(a[d],b),a[d]=FormBuilder.replaceStatementsPrimary(a[d],b),a[d]=FormBuilder.replaceStatementsArrayValue(a[d],b,c,e)))):"number"===typeof a[d]&&(a[d]=[ref(c,a[d])]);for(d in a)FormBuilder.regex.pri_in_key.test(d)? -b[d.substring(1)]=ref(c,a[d]):FormBuilder.regex.arr_in_key.test(d)&&(b[d.substring(2)]=ref(c,a[d]));b=JSON.parse(JSON.stringify(b));for(d in a)if(-1=f.lastIndex);)d=f.lastIndex,e.push(c);for(c=d=0;cd;d++)matches["$"+d]=RegExp["$"+d];return{def:b[f],scope:matches}}return{}}; +FormBuilder.replaceStatements=function(a,b,f,d,e){a=a instanceof Object?a:{};b=b instanceof Object?JSON.parse(JSON.stringify(b)):{};var c,k;if(a.hasOwnProperty("scope")&&a.scope instanceof Object)for(c in a.scope)b[c]=a.scope[c];if(a.hasOwnProperty("node")&&"string"==typeof a.node&&(k=FormBuilder.fetchNodeDefinition(a.node,f),k.hasOwnProperty("def")&&(a.node_link=JSON.parse(JSON.stringify(k.def)),k.hasOwnProperty("scope"))))for(c in k.scope)b[c]=k.scope[c];a.hasOwnProperty("browse")&&(a.browse.hasOwnProperty("array")&& +FormBuilder.regex.arr_out_set.test(a.browse.array)?a.browse.id=RegExp.$1:delete a.browse);for(c in a)"string"==typeof a[c]?(a[c]=[a[c]],k=FormBuilder.replaceStatementsFunction(d,a[c][0],b),!1!==k?a[c]=k:(k=FormBuilder.replaceStatementsArray(a[c][0],b,d),!1!==k?a[c]=k:(a[c]=FormBuilder.replaceStatementsRegex(a[c],b),a[c]=FormBuilder.replaceStatementsPrimary(a[c],b),a[c]=FormBuilder.replaceStatementsArrayValue(a[c],b,d,e)))):"number"===typeof a[c]&&(a[c]=[ref(d,a[c])]);for(c in a)FormBuilder.regex.pri_in_key.test(c)? +b[c.substring(1)]=ref(d,a[c]):FormBuilder.regex.arr_in_key.test(c)&&(b[c.substring(2)]=ref(d,a[c]));b=JSON.parse(JSON.stringify(b));for(c in a)if(-1=f.lastIndex);)c=f.lastIndex,e.push(d);for(d=c=0;d 0 || matches[i].index > 0 ) @@ -863,12 +862,11 @@ FormBuilder.buildElements = function(definition, ref_table, ref_assoc){ var i, j, k, tmp, b, repeated = ['NULL'], - scope; + scope, funcs; /* [1] Gestion de l'attribut 'repeat' =========================================================*/ if( definition.hasOwnProperty('repeat') ){ - tmp = FormBuilder.readRef( ref_table, definition.repeat.n ); // Si c'est bien un nombre @@ -879,19 +877,19 @@ FormBuilder.buildElements = function(definition, ref_table, ref_assoc){ /* (2) Vérification des variables */ - if( !ref_assoc.hasOwnProperty(definition.repeat.id+'.i') ) - ref_assoc[definition.repeat.id+'.i'] = ref(0); + if( !ref_assoc.hasOwnProperty(definition.repeat.id+':i') ) + ref_assoc[definition.repeat.id+':i'] = ref(0); - if( !ref_assoc.hasOwnProperty(definition.repeat.id+'.n') ) - ref_assoc[definition.repeat.id+'.n'] = ref(tmp); + if( !ref_assoc.hasOwnProperty(definition.repeat.id+':n') ) + ref_assoc[definition.repeat.id+':n'] = ref(tmp); /* (2) On construit le scope pour chaque valeur */ for( i = 0 ; i < tmp ; i++ ){ repeated[i] = {}; - repeated[i][definition.repeat.id+'.i'] = i; - repeated[i][definition.repeat.id+'.n'] = tmp; + repeated[i][definition.repeat.id+':i'] = i; + repeated[i][definition.repeat.id+':n'] = tmp; } @@ -904,7 +902,75 @@ FormBuilder.buildElements = function(definition, ref_table, ref_assoc){ /* [2] Gestion de l'attribut 'browse' =========================================================*/ if( definition.hasOwnProperty('browse') ){ + tmp = FormBuilder.readRef( ref_table, definition.browse.array ); + // Si c'est bien un tableau + if( tmp instanceof Array ){ + + /* (1) On initialise le tableau et le scope */ + repeated = []; + scope = []; + funcs = {}; + + + /* (2) On cherche les variables */ + k = new RegExp( definition.browse.id+'.(.+)' ); + + // Si on trouve une valeur du type 'monTableauId.xxxxx' + for( i in ref_assoc ) + if( k.test(i) ) + scope.push( RegExp.$1 ); + + /* (2) On récupère les fonctions custom */ + if( definition.browse.hasOwnProperty('funcs') ){ + + // Pour chaque fonction + for( i in definition.browse.funcs ){ + + // Si elle a le bon nom + if( k.test(i) ){ + + // on ajoute la fonction à la liste de fonctions + funcs[RegExp.$1] = FormBuilder.readRef( ref_table, definition.browse.funcs[i] ); + + } + + } + + } + + + /* (3) Vérification des variables */ + if( !ref_assoc.hasOwnProperty(definition.browse.id+':i') ) + ref_assoc[definition.browse.id+':i'] = ref(0); + + if( !ref_assoc.hasOwnProperty(definition.browse.id+':n') ) + ref_assoc[definition.browse.id+':n'] = ref(tmp); + + + /* (4) On construit le scope pour chaque valeur */ + for( i = 0 ; i < tmp.length ; i++ ){ + + repeated[i] = {}; + + for( j in scope ){ + + // {1} Si la valeur est un attribut // + if( tmp[i].hasOwnProperty(scope[j]) ) + repeated[i][definition.browse.id+'.'+scope[j]] = tmp[i][scope[j]]; + + // {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+':i'] = i; + repeated[i][definition.browse.id+':n'] = tmp.length; + + } + + } } diff --git a/js/lib/form-builder/main-min.js b/js/lib/form-builder/main-min.js index 939a3e0..d9a530c 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:{"option.value":"{getval()}","option.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:100,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}",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 1c21209..e43af50 100644 --- a/js/lib/form-builder/main.js +++ b/js/lib/form-builder/main.js @@ -45,8 +45,8 @@ var custom_definition = { browse: { array: '{options[]}', funcs: { - 'option.value': '{getval()}', - 'option.id': '{getnam()}' + 'options.value': '{getval()}', + 'options.id': '{getnam()}' } } } @@ -62,16 +62,16 @@ 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.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: '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[]}' } + { node: 'custom-select', $name: 'existing', $options: '{options[]}' } ] };