Ajout observer sur les attributs

This commit is contained in:
xdrm-brackets 2016-09-23 17:15:18 +02:00
parent bdf1aa9ab1
commit a6c1c8c8cf
3 changed files with 25 additions and 12 deletions

1
js/lib/form-builder-min-min.js vendored Normal file
View File

@ -0,0 +1 @@

View File

@ -20,10 +20,11 @@ FormBuilder.createElements=function(a,b,d){var e=FormBuilder.debugStart("FormBui
Array)){n=[];p=[];q={};h=new RegExp(a.browse.id+".(.+)");for(g in d)h.test(g)&&p.push(RegExp.$1);if(a.browse.hasOwnProperty("funcs"))for(g in a.browse.funcs)h.test(g)&&(q[RegExp.$1]=FormBuilder.readRef(b,a.browse.funcs[g]));d.hasOwnProperty(a.browse.id+":i")||(d[a.browse.id+":i"]=ref(0));d.hasOwnProperty(a.browse.id+":n")||(d[a.browse.id+":n"]=ref(l));for(g=0;g<l.length;g++){n[g]={};for(c in p)q.hasOwnProperty(p[c])?n[g][a.browse.id+"."+p[c]]=q[p[c]](l[g]):l[g].hasOwnProperty(p[c])&&(n[g][a.browse.id+
"."+p[c]]=l[g][p[c]]);n[g][a.browse.id+":i"]=g;n[g][a.browse.id+":n"]=l.length}}for(c=0;c<n.length;c++){f[c]={prev:[],node:[],next:[]};if(n[c]instanceof Object)for(g in n[c])b[d[g]]=n[c][g];if(a.hasOwnProperty("prev_nodes"))for(g in a.prev_nodes)f[c].prev=FormBuilder.createElements(a.prev_nodes[g],b,d);if(a.hasOwnProperty("next_nodes"))for(g in a.next_nodes)f[c].next=FormBuilder.createElements(a.next_nodes[g],b,d);if(a.hasOwnProperty("node_link"))for(g in l=FormBuilder.createElements(a.node_link,
b,d),l)f[c].prev=f[c].prev.concat(l[g].prev),f[c].node=f[c].node.concat(l[g].node),f[c].next=l[g].next.concat(f[c].next);else a.hasOwnProperty("node_type")&&f[c].node.push(document.createElement(FormBuilder.readRef(b,a.node_type)))-1;if(a.hasOwnProperty("attributes"))for(g in a.attributes)for(k in f[c].node)f[c].node[k].setAttribute(g,FormBuilder.readRef(b,a.attributes[g]));if(a.hasOwnProperty("css"))for(k in f[c].node)FormBuilder.applyCSS(f[c].node[k],a.css);if(a.hasOwnProperty("text"))for(k in f[c].node)f[c].node[k].innerHTML=
FormBuilder.readRef(b,a.text);if(a.hasOwnProperty("listeners"))for(g in a.listeners)for(k in f[c].node)f[c].node[k].addEventListener(g,FormBuilder.readRef(b,a.listeners[g]),!1);if(a.hasOwnProperty("children"))for(g in a.children)for(k in l=FormBuilder.createElements(a.children[g],b,d),f[c].node)FormBuilder.attachElements(f[c].node[k],l);for(k in f[c].node){l="input"==FormBuilder.fetchNodeType(a)&&a.hasOwnProperty("_value");if("select"==FormBuilder.fetchNodeType(a)&&a.hasOwnProperty("_selected")){for(g=
0;g<f[c].node[k].children.length;g++)f[c].node[k].children[g].value==FormBuilder.readRef(b,a._selected)?f[c].node[k].children[g].setAttribute("selected",!0):f[c].node[k].children[g].removeAttribute("selected");f[c].node[k].addEventListener("change",function(c){if(a._selected instanceof Array&&!(1<a._selected.length)&&FormBuilder.regex.ref_pri.test(a._selected[0])){var d=a._selected[0];for(c=d;FormBuilder.regex.ref_pri.test(d);)c=d,d=b[d],d instanceof Array&&(d=d[0]);b[c]=this.value}},!1)}l&&f[c].node[k].addEventListener("input",
function(d){if(a._value instanceof Array&&!(1<a._value.length)&&FormBuilder.regex.ref_pri.test(a._value[0])){var c=a._value[0];for(d=c;FormBuilder.regex.ref_pri.test(c);)d=c,c=b[c],c instanceof Array&&(c=c[0]);b[d]=this.value}},!1)}}d=a.hasOwnProperty("node")?a.node:a.node_type;FormBuilder.debug("<"+d+">",2);FormBuilder.debugStop(e);return a.dom=f};
FormBuilder.readRef(b,a.text);if(a.hasOwnProperty("listeners"))for(g in a.listeners)for(k in f[c].node)f[c].node[k].addEventListener(g,FormBuilder.readRef(b,a.listeners[g]),!1);if(a.hasOwnProperty("children"))for(g in a.children)for(k in l=FormBuilder.createElements(a.children[g],b,d),f[c].node)FormBuilder.attachElements(f[c].node[k],l);for(k in f[c].node)if(l="input"==FormBuilder.fetchNodeType(a)&&a.hasOwnProperty("_value"),"select"==FormBuilder.fetchNodeType(a)&&a.hasOwnProperty("_selected")){for(g=
0;g<f[c].node[k].children.length;g++)f[c].node[k].children[g].value==FormBuilder.readRef(b,a._selected)?f[c].node[k].children[g].setAttribute("selected",!0):f[c].node[k].children[g].removeAttribute("selected");f[c].node[k].addEventListener("change",function(c){if(a._selected instanceof Array&&!(1<a._selected.length)&&FormBuilder.regex.ref_pri.test(a._selected[0])){var d=a._selected[0];for(c=d;FormBuilder.regex.ref_pri.test(d);)c=d,d=b[d],d instanceof Array&&(d=d[0]);b[c]=this.value}},!1)}else l?f[c].node[k].addEventListener("input",
function(d){if(a._value instanceof Array&&!(1<a._value.length)&&FormBuilder.regex.ref_pri.test(a._value[0])){var c=a._value[0];for(d=c;FormBuilder.regex.ref_pri.test(c);)d=c,c=b[c],c instanceof Array&&(c=c[0]);b[d]=this.value}},!1):(a.hasOwnProperty("observers")||(a.observers=[]),l=new MutationObserver(function(a){return function(b){return FormBuilder.DOMUpdates(a,b)}}(a)),l.observe(f[c].node[k],FormBuilder.observe.attributes),a.observers.push(l))}d=a.hasOwnProperty("node")?a.node:a.node_type;FormBuilder.debug("<"+
d+">",2);FormBuilder.debugStop(e);return a.dom=f};
FormBuilder.attachElements=function(a,b){var d=FormBuilder.debugStart("FormBuilder::attachElements",2),e,f;for(e in b){for(f in b[e].prev)b[e].prev[f]instanceof Element&&a.appendChild(b[e].prev[f]),b[e].prev[f]instanceof Object&&FormBuilder.attachElements(a,[b[e].prev[f]]);for(f in b[e].node)b[e].node[f]instanceof Element&&a.appendChild(b[e].node[f]),b[e].node[f]instanceof Object&&FormBuilder.attachElements(a,[b[e].node[f]]);for(f in b[e].next)b[e].next[f]instanceof Element&&a.appendChild(b[e].next[f]),
b[e].next[f]instanceof Object&&FormBuilder.attachElements(a,[b[e].next[f]])}FormBuilder.debugStop(d)};
FormBuilder.detachElements=function(a,b){var d,e;for(d in b){for(e in b[d].prev)b[d].prev[e]instanceof Element&&a.removeChild(b[d].prev[e]),b[d].prev[e]instanceof Object&&FormBuilder.detachElements(a,[b[d].prev[e]]);for(e in b[d].node)b[d].node[e]instanceof Element&&a.removeChild(b[d].node[e]),b[d].node[e]instanceof Object&&FormBuilder.detachElements(a,[b[d].node[e]]);for(e in b[d].next)b[d].next[e]instanceof Element&&a.removeChild(b[d].next[e]),b[d].next[e]instanceof Object&&FormBuilder.detachElements(a,
[b[d].next[e]])}};FormBuilder.applyCSS=function(a,b){for(var d in b)a.style[d]=b[d]};FormBuilder.fetchNodeType=function(a){if(a.hasOwnProperty("node_type"))return a.node_type;if(a.hasOwnProperty("node_link"))return FormBuilder.fetchNodeType(a.node_link)};FormBuilder.DOMUpdates=function(a){for(var b in a)console.log(a[b]),console.log(a[b].target),console.log(a[b].type)};
[b[d].next[e]])}};FormBuilder.applyCSS=function(a,b){for(var d in b)a.style[d]=b[d]};FormBuilder.fetchNodeType=function(a){if(a.hasOwnProperty("node_type"))return a.node_type;if(a.hasOwnProperty("node_link"))return FormBuilder.fetchNodeType(a.node_link)};FormBuilder.DOMUpdates=function(a,b){for(var d in b)console.log(a,b[d])};

View File

@ -1284,7 +1284,7 @@ FormBuilder.createElements = function(definition, ref_table, ref_assoc){
}
// {4} Si <input> de type texte //
if( input ){
else if( input ){
// On met à jour la valeur
built[b].node[n].addEventListener('input', function(e){
@ -1315,10 +1315,23 @@ FormBuilder.createElements = function(definition, ref_table, ref_assoc){
}, false);
// {5} Sinon gestion des attributs //
}else{
if( !definition.hasOwnProperty('observers') )
definition.observers = [];
// On met un observer sur les attributs + on lui passe la definition actuelle
var obs = new MutationObserver(
(function(def){
return function(ev){ return FormBuilder.DOMUpdates(def, ev) };
})(definition)
);
obs.observe(built[b].node[n], FormBuilder.observe.attributes);
definition.observers.push(obs);
}
// TODO: Gestion de la transmission de 'value' / 'selected' lors de la construction
}
}
@ -1504,7 +1517,7 @@ FormBuilder.fetchNodeType = function(definition){
};
FormBuilder.DOMUpdates = function(events){
FormBuilder.DOMUpdates = function(definition, events){
var e;
@ -1512,9 +1525,7 @@ FormBuilder.DOMUpdates = function(events){
=========================================================*/
for( e in events){
console.log( events[e] );
console.log( events[e].target );
console.log( events[e].type );
console.log( definition, events[e] );