NxTIC/public_html/js/lib/min/form-deflater.js

6 lines
1.7 KiB
JavaScript
Raw Permalink Normal View History

function FormDeflater(b,c,a){for(var d=0;d<c.length;d++)c[d]=c[d].toLowerCase();for(d=0;d<a.length;d++)a[d]=a[d].toLowerCase();this.container=b;this.tags=c;this.attr=a}FormDeflater.prototype={container:this.container,tags:this.tags,attr:this.attr};
FormDeflater.prototype.deflate=function(){for(var b=this.getChildren(this.container),b=this.filterElements(b),c={},a=0;a<b.length;a++)for(var d=0;d<this.attr.length;d++){var e=b[a].getAttribute(this.attr[d]);if(null!==e&&0<e.length){c.hasOwnProperty(e)?c[e]instanceof Array?c[e].push({target:b[a],attr:this.attr[d],value:b[a].value,checked:b[a].checked}):c[e]=[c[e],{target:b[a],attr:this.attr[d],value:b[a].value,checked:b[a].checked}]:c[e]={target:b[a],attr:this.attr[d],value:b[a].value,checked:b[a].checked};
break}}return c=this.cleanOutput(c)};FormDeflater.prototype.checkable=function(b){return"INPUT"!=b.tagName||-1==["radio","checkbox"].indexOf(b.getAttribute("type").toLowerCase())?!1:!0};
FormDeflater.prototype.cleanOutput=function(b){var c={},a;for(a in b)if(b[a]instanceof Array){var d=!0,e=[],f;for(f in b[a])if(this.checkable(b[a][f].target))!0===b[a][f].checked&&e.push(f);else{d=!1;break}if(d)if(1==e.length)c[a]=b[a][e[0]].value;else for(f in c[a]=[],e)c[a].push(b[a][e[f]].value);else for(f in c[a]=[],b[a])c[a].push(b[a][f].value)}else this.checkable(b[a].target)?c[a]=b[a].checked?b[a].value:null:c[a]=b[a].value;return c};
FormDeflater.prototype.getChildren=function(b){if(!(b instanceof Element))return[];for(var c=b=[].slice.call(b.children),a=0;a<b.length;a++)c=c.concat([].slice.call(this.getChildren(b[a])));return c};FormDeflater.prototype.filterElements=function(b){for(var c=[],a=0;a<b.length;a++)-1<this.tags.indexOf(b[a].tagName.toLowerCase())&&c.push(b[a]);return c};