Permalink
Browse files

sort out two-dimensional names and multiple checkbox values

  • Loading branch information...
1 parent 0ea6edd commit 1f756110f9f84b0fff47f37ac02fc6783479077e @matzko committed Mar 29, 2011
Showing with 36 additions and 13 deletions.
  1. +35 −12 client-files/js/filosofo-js.dev.js
  2. +1 −1 client-files/js/filosofo-js.js
@@ -121,6 +121,28 @@ var FilosofoJS = function(scope) {
},
/**
+ * Set the input value, even if that means a multi-dimensional object.
+ */
+ _setValueFromInputName = function( name, value ) {
+ var match = /([^\[]*)\[([^\]]*)\]/.exec( name );
+ if ( match && match[0] && match[1] && match[2] ) {
+ if ( ! this[match[1]] )
+ this[match[1]] = {};
+ this[match[1]][match[2]] = value;
+
+ // checkboxes with attributes like: name="myname[]"
+ } else if ( match && match[0] && match[1] && '' === match[2] ) {
+ if ( ! this[match[1]] || ! this[match[1]][0] ) {
+ this[match[1]] = [];
+ }
+
+ this[match[1]][this[match[1]].length] = value;
+ } else {
+ this[name] = value;
+ }
+ },
+
+ /**
* @todo better handling of different elements
* @todo better handling of diff submit buttons, for example
*/
@@ -131,7 +153,8 @@ var FilosofoJS = function(scope) {
i, j = elTypes.length, k = 0,
objType,
data = {},
- fields;
+ fields,
+ fieldValue;
while ( j-- ) {
fields = form.getElementsByTagName( elTypes[j] );
@@ -143,34 +166,33 @@ var FilosofoJS = function(scope) {
k = fields[i].options.length;
if ( -1 < fields[i].selectedIndex ) {
data[fields[i].name] = [];
+ fieldValue = [];
while ( k-- ) {
if ( fields[i].options[k].selected ) {
- data[fields[i].name][data[fields[i].name].length] = fields[i].options[k].value;
+ fieldValue[fieldValue.length] = fields[i].options[k].value;
}
}
+ _setValueFromInputName.call(data, fields[i].name, fieldValue);
}
} else if ( 'select' == fields[i].name ) {
if ( fields[i].options && fields[i].options[fields[i].selectedIndex] ) {
- data[fields[i].name] = fields[i].options[fields[i].selectedIndex];
+ _setValueFromInputName.call(data, fields[i].name, fields[i].options[fields[i].selectedIndex]);
} else if ( fields[i].value ) {
- data[fields[i].name] = fields[i].value;
+ _setValueFromInputName.call(data, fields[i].name, fields[i].value);
}
} else if ( 'button' == fields[i].nodeName.toLowerCase() ) {
if ( fields[i].name ) {
if ( fields[i].getAttribute('value') ) {
- data[fields[i].name] = fields[i].getAttribute('value');
+ _setValueFromInputName.call(data, fields[i].name, fields[i].getAttribute('value'));
} else if ( fields[i].value ) {
- data[fields[i].name] = fields[i].value;
+ _setValueFromInputName.call(data, fields[i].name, fields[i].value);
} else if ( fields[i].innerText || fields[i].textContent ) {
- data[fields[i].name] = ( fields[i].innerText || fields[i].textContent );
+ _setValueFromInputName.call(data, fields[i].name, ( fields[i].innerText || fields[i].textContent ) );
}
}
} else if ( 'checkbox' == objType ) {
- if ( ! data[fields[i].name] || ! data[fields[i].name][0] ) {
- data[fields[i].name] = [];
- }
if ( fields[i].checked ) {
- data[fields[i].name][data[fields[i].name].length] = fields[i].value;
+ _setValueFromInputName.call(data, fields[i].name, fields[i].value);
}
} else if (
! objType ||
@@ -180,11 +202,12 @@ var FilosofoJS = function(scope) {
fields[i].checked
)
) {
- data[fields[i].name] = fields[i].value;
+ _setValueFromInputName.call(data, fields[i].name, fields[i].value);
}
}
}
}
+ console.log(data)
return data;
},
@@ -1 +1 @@
-var FilosofoJS=function(a){var p=function(x,w,d){if(x.addEventListener){x.addEventListener(w,d,false)}else{if(x.attachEvent){x.attachEvent("on"+w,function(){return d.call(x,window.event)})}}},t=document,j=(function(){var w,d=[function(){return new a.ActiveXObject("Microsoft.XMLHTTP")},function(){return new a.ActiveXObject("Msxml2.XMLHTTP.3.0")},function(){return new a.ActiveXObject("Msxml2.XMLHTTP.6.0")},function(){return new XMLHttpRequest()}];for(w=d.length;w--;){try{if(d[w]()){return d[w]}}catch(x){}}})(),v=function(d,y,A){d=d||"admin-ajax.php";y=y||{};var w,x=new j;w=n(y);try{if("undefined"==typeof A){A=function(){}}x.open("POST",d,true);x.setRequestHeader("Content-Type","application/x-www-form-urlencoded");x.onreadystatechange=function(){if(4==x.readyState){x.onreadystatechange=function(){};if(200<=x.status&&300>x.status||("undefined"==typeof x.status)){A(x.responseText)}}};x.send(w)}catch(z){}},b=function(x,d){var w=x.constructor.prototype[d];return("undefined"==typeof w||d!==x[w])},n=function(d){var x,w,y=[];for(x in d){if(b(d,x)){if("[]"==x.substr(x.length-2,x.length)){for(w=0;w<d[x].length;w++){y[y.length]=k(x)+"="+k(d[x][w])}}else{y[y.length]=k(x)+"="+k(d[x])}}}return y.join("&")},k=(function(){var d=function(w){return encodeURIComponent(w).replace(/%20/,"+").replace(/(.{0,3})(%0A)/g,function(y,z,x){return z+(z=="%0D"?"":"%0D")+x}).replace(/(%0D)(.{0,3})/g,function(y,z,x){return z+(x=="%0A"?"":"%0A")+x})};if(typeof encodeURIComponent!="undefined"&&String.prototype.replace&&d("\n \r")=="%0D%0A+%0D%0A"){return d}})(),s=function(d){d=d||window.event;return d.target||d.srcElement},o=function(A){if(!A){return{}}var y=["button","input","select","textarea"],z,x=y.length,w=0,C,B={},d;while(x--){d=A.getElementsByTagName(y[x]);z=d.length;while(z--){if(d[z]&&d[z].name){C=(d[z].type+"").toLowerCase();if("select-multiple"==C){w=d[z].options.length;if(-1<d[z].selectedIndex){B[d[z].name]=[];while(w--){if(d[z].options[w].selected){B[d[z].name][B[d[z].name].length]=d[z].options[w].value}}}}else{if("select"==d[z].name){if(d[z].options&&d[z].options[d[z].selectedIndex]){B[d[z].name]=d[z].options[d[z].selectedIndex]}else{if(d[z].value){B[d[z].name]=d[z].value}}}else{if("button"==d[z].nodeName.toLowerCase()){if(d[z].name){if(d[z].getAttribute("value")){B[d[z].name]=d[z].getAttribute("value")}else{if(d[z].value){B[d[z].name]=d[z].value}else{if(d[z].innerText||d[z].textContent){B[d[z].name]=(d[z].innerText||d[z].textContent)}}}}}else{if("checkbox"==C){if(!B[d[z].name]||!B[d[z].name][0]){B[d[z].name]=[]}if(d[z].checked){B[d[z].name][B[d[z].name].length]=d[z].value}}else{if(!C||"radio"!=C||("radio"==C&&d[z].checked)){B[d[z].name]=d[z].value}}}}}}}}return B},h=function(x,d,w){return((1-w)*x)+(w*d)},e=function(y,d,x){var w=h(y,d,x*x*(3-2*x));return w},f=false,c=function(x){if(f){return}var d=x,E=0,C=0,B=25,z=400,D=z/B,w,y,A;while(d.offsetParent&&d!=t.dElement){E+=d.offsetTop;d=d.offsetParent}E=E-30;E=0>E?0:E;if(t.documentElement&&t.documentElement.scrollTop){C=t.documentElement.scrollTop}else{if(t.body&&t.body.scrollTop){C=t.body.scrollTop}else{if(t.getElementsByTagName("body")){C=t.getElementsByTagName("body")[0].scrollTop}}}w=C-E;y=w/D;for(A=0;A<D;A++){(function(){var H=Math.ceil(C-(e(0,1,(A/D))*w)),F=A,G=(A+1)<D?false:true;setTimeout(function(){if(G){f=false}scrollTo(0,H)},F*B)})()}},u=function(d,w){return{animate:function(){if(this.inProgress){return}this.inProgress=true;w=w||function(){};var z=20,D=500,x=D/z,y,B=false,C,A=this;for(y=0;y<x;y++){B=(y+1)<x?false:true;C=0<d?e(0,1,(y/x))*d:e(1,0,(y/x))*d;(function(E){var G=y,F=B,H=C;setTimeout(function(){if(F){A.inProgress=false}E.apply(A,[H,F])},G*z)})(w)}}}},r=function(y,w,z){if(!y){return}w=w||-1;z=z||function(){};if(-1===w){y.style.opacity=1;y.style.filter="alpha(opacity=100)"}else{if(1===w){y.style.opacity=0;y.style.filter="alpha(opacity=0)";y.style.display="block"}}var x=function(B,A){var C=100+B*w;y.style.opacity=C/100;y.style.filter="alpha(opacity="+C+")";if(A){z.call(y);if(-1===w){y.style.display="none"}else{y.style.display="block"}}},d;if(y){if(-1===w){d=new u(100,x),d.animate()}else{d=new u(-100,x);d.animate()}}},q=function(w,x,d){if(!d){d=t}if(!w||!x){return false}(function(A,B,z){var y=new RegExp("\\b"+A+"\\b");p(z,"click",function(E){var C=true,D=s(E);do{if(D.className&&y.exec(D.className)){C=B.call(D,E);if(!C){if(E.stopPropagation){E.stopPropagation()}if(E.preventDefault){E.preventDefault()}E.cancelBubble=true;E.returnValue=false;return false}else{return true}}else{D=D.parentNode}}while(D&&D!=z)})})(w,x,d)},l=function(d){if(d){m=d}p(t,"DOMContentLoaded",i);p(window,"load",i)},g=false,m=function(){},i=function(){if(g){return false}g=true;m()};return{addEvent:p,Animation:u,attachClassClickListener:q,doWhenReady:l,fade:r,getEventTarget:s,getFormData:o,isObjProperty:b,postReq:v,scrollToElement:c}};
+var FilosofoJS=function(a){var p=function(y,x,d){if(y.addEventListener){y.addEventListener(x,d,false)}else{if(y.attachEvent){y.attachEvent("on"+x,function(){return d.call(y,window.event)})}}},t=document,j=(function(){var x,d=[function(){return new a.ActiveXObject("Microsoft.XMLHTTP")},function(){return new a.ActiveXObject("Msxml2.XMLHTTP.3.0")},function(){return new a.ActiveXObject("Msxml2.XMLHTTP.6.0")},function(){return new XMLHttpRequest()}];for(x=d.length;x--;){try{if(d[x]()){return d[x]}}catch(y){}}})(),w=function(d,z,B){d=d||"admin-ajax.php";z=z||{};var x,y=new j;x=n(z);try{if("undefined"==typeof B){B=function(){}}y.open("POST",d,true);y.setRequestHeader("Content-Type","application/x-www-form-urlencoded");y.onreadystatechange=function(){if(4==y.readyState){y.onreadystatechange=function(){};if(200<=y.status&&300>y.status||("undefined"==typeof y.status)){B(y.responseText)}}};y.send(x)}catch(A){}},b=function(y,d){var x=y.constructor.prototype[d];return("undefined"==typeof x||d!==y[x])},n=function(d){var y,x,z=[];for(y in d){if(b(d,y)){if("[]"==y.substr(y.length-2,y.length)){for(x=0;x<d[y].length;x++){z[z.length]=k(y)+"="+k(d[y][x])}}else{z[z.length]=k(y)+"="+k(d[y])}}}return z.join("&")},k=(function(){var d=function(x){return encodeURIComponent(x).replace(/%20/,"+").replace(/(.{0,3})(%0A)/g,function(z,A,y){return A+(A=="%0D"?"":"%0D")+y}).replace(/(%0D)(.{0,3})/g,function(z,A,y){return A+(y=="%0A"?"":"%0A")+y})};if(typeof encodeURIComponent!="undefined"&&String.prototype.replace&&d("\n \r")=="%0D%0A+%0D%0A"){return d}})(),s=function(d){d=d||window.event;return d.target||d.srcElement},u=function(x,y){var d=/([^\[]*)\[([^\]]*)\]/.exec(x);if(d&&d[0]&&d[1]&&d[2]){if(!this[d[1]]){this[d[1]]={}}this[d[1]][d[2]]=y}else{if(d&&d[0]&&d[1]&&""===d[2]){if(!this[d[1]]||!this[d[1]][0]){this[d[1]]=[]}this[d[1]][this[d[1]].length]=y}else{this[x]=y}}},o=function(x){if(!x){return{}}var z=["button","input","select","textarea"],D,B=z.length,A=0,y,C={},E,d;while(B--){E=x.getElementsByTagName(z[B]);D=E.length;while(D--){if(E[D]&&E[D].name){y=(E[D].type+"").toLowerCase();if("select-multiple"==y){A=E[D].options.length;if(-1<E[D].selectedIndex){C[E[D].name]=[];d=[];while(A--){if(E[D].options[A].selected){d[d.length]=E[D].options[A].value}}u.call(C,E[D].name,d)}}else{if("select"==E[D].name){if(E[D].options&&E[D].options[E[D].selectedIndex]){u.call(C,E[D].name,E[D].options[E[D].selectedIndex])}else{if(E[D].value){u.call(C,E[D].name,E[D].value)}}}else{if("button"==E[D].nodeName.toLowerCase()){if(E[D].name){if(E[D].getAttribute("value")){u.call(C,E[D].name,E[D].getAttribute("value"))}else{if(E[D].value){u.call(C,E[D].name,E[D].value)}else{if(E[D].innerText||E[D].textContent){u.call(C,E[D].name,(E[D].innerText||E[D].textContent))}}}}}else{if("checkbox"==y){if(E[D].checked){u.call(C,E[D].name,E[D].value)}}else{if(!y||"radio"!=y||("radio"==y&&E[D].checked)){u.call(C,E[D].name,E[D].value)}}}}}}}}console.log(C);return C},h=function(y,d,x){return((1-x)*y)+(x*d)},e=function(z,d,y){var x=h(z,d,y*y*(3-2*y));return x},f=false,c=function(y){if(f){return}var d=y,F=0,D=0,C=25,A=400,E=A/C,x,z,B;while(d.offsetParent&&d!=t.dElement){F+=d.offsetTop;d=d.offsetParent}F=F-30;F=0>F?0:F;if(t.documentElement&&t.documentElement.scrollTop){D=t.documentElement.scrollTop}else{if(t.body&&t.body.scrollTop){D=t.body.scrollTop}else{if(t.getElementsByTagName("body")){D=t.getElementsByTagName("body")[0].scrollTop}}}x=D-F;z=x/E;for(B=0;B<E;B++){(function(){var I=Math.ceil(D-(e(0,1,(B/E))*x)),G=B,H=(B+1)<E?false:true;setTimeout(function(){if(H){f=false}scrollTo(0,I)},G*C)})()}},v=function(d,x){return{animate:function(){if(this.inProgress){return}this.inProgress=true;x=x||function(){};var A=20,E=500,y=E/A,z,C=false,D,B=this;for(z=0;z<y;z++){C=(z+1)<y?false:true;D=0<d?e(0,1,(z/y))*d:e(1,0,(z/y))*d;(function(F){var H=z,G=C,I=D;setTimeout(function(){if(G){B.inProgress=false}F.apply(B,[I,G])},H*A)})(x)}}}},r=function(z,x,A){if(!z){return}x=x||-1;A=A||function(){};if(-1===x){z.style.opacity=1;z.style.filter="alpha(opacity=100)"}else{if(1===x){z.style.opacity=0;z.style.filter="alpha(opacity=0)";z.style.display="block"}}var y=function(C,B){var D=100+C*x;z.style.opacity=D/100;z.style.filter="alpha(opacity="+D+")";if(B){A.call(z);if(-1===x){z.style.display="none"}else{z.style.display="block"}}},d;if(z){if(-1===x){d=new v(100,y),d.animate()}else{d=new v(-100,y);d.animate()}}},q=function(x,y,d){if(!d){d=t}if(!x||!y){return false}(function(B,C,A){var z=new RegExp("\\b"+B+"\\b");p(A,"click",function(F){var D=true,E=s(F);do{if(E.className&&z.exec(E.className)){D=C.call(E,F);if(!D){if(F.stopPropagation){F.stopPropagation()}if(F.preventDefault){F.preventDefault()}F.cancelBubble=true;F.returnValue=false;return false}else{return true}}else{E=E.parentNode}}while(E&&E!=A)})})(x,y,d)},l=function(d){if(d){m=d}p(t,"DOMContentLoaded",i);p(window,"load",i)},g=false,m=function(){},i=function(){if(g){return false}g=true;m()};return{addEvent:p,Animation:v,attachClassClickListener:q,doWhenReady:l,fade:r,getEventTarget:s,getFormData:o,isObjProperty:b,postReq:w,scrollToElement:c}};

0 comments on commit 1f75611

Please sign in to comment.