-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathunserialize.jquery.1.0.2.js
121 lines (113 loc) · 4.43 KB
/
unserialize.jquery.1.0.2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the Lesser GNU General Public License as
* published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
* 2013, Daniel Cantarín, omega_canta@yahoo.com
*/
jQuery.unserialize = function(str){
var items = str.split('&');
var ret = "{";
var arrays = [];
var index = "";
for (var i = 0; i < items.length; i++) {
var parts = items[i].split(/=/);
//console.log(parts[0], parts[0].indexOf("%5B"), parts[0].indexOf("["));
if (parts[0].indexOf("%5B") > -1 || parts[0].indexOf("[") > -1){
//Array serializado
index = (parts[0].indexOf("%5B") > -1) ? parts[0].replace("%5B","").replace("%5D","") : parts[0].replace("[","").replace("]","");
//console.log("array detectado:", index);
//console.log(arrays[index] === undefined);
if (arrays[index] === undefined){
arrays[index] = [];
}
arrays[index].push( decodeURIComponent(parts[1].replace(/\+/g," ")));
//console.log("arrays:", arrays);
} else {
//console.log("common item (not array)");
if (parts.length > 1){
ret += "\""+parts[0] + "\": \"" + decodeURIComponent(parts[1].replace(/\+/g," ")).replace(/\n/g,"\\n").replace(/\r/g,"\\r") + "\", ";
}
}
};
ret = (ret != "{") ? ret.substr(0,ret.length-2) + "}" : ret + "}";
//console.log(ret, arrays);
var ret2 = JSON.parse(ret);
//proceso los arrays
for (arr in arrays){
ret2[arr] = arrays[arr];
}
return ret2;
}
jQuery.fn.unserialize = function(parm){
//If not string, JSON is assumed.
var items = (typeof parm == "string") ? parm.split('&') : parm;
if (typeof items !== "object"){
throw new Error("unserialize: string or JSON object expected.");
}
//Check for the need of building an array from some item.
//May return a false positive, but it's still better than looping twice.
//TODO: confirm if it's ok to simplify this method by always calling
//$.unserialize(parm) without any extra checking.
var need_to_build = ((typeof parm == "string") && decodeURIComponent(parm).indexOf("[]=") > -1);
items = (need_to_build) ? $.unserialize(parm) : items;
for (var i in items){
var parts = (items instanceof Array) ? items[i].split(/=/) : [i, (items[i] instanceof Array) ? items[i] : "" + items[i]];
parts[0] = decodeURIComponent(parts[0]);
if (parts[0].indexOf("[]") == -1 && parts[1] instanceof Array){
parts[0] += "[]";
}
obj = this.find('[name=\''+ parts[0] +'\']');
if (obj.length == 0){
try{
obj = this.parent().find('[name=\''+ parts[0] +'\']');
} catch(e){}
}
if (typeof obj.attr("type") == "string" && ( obj.attr("type").toLowerCase() == "radio" || obj.attr("type").toLowerCase() == "checkbox")){
obj.each(function(index, coso) {
coso = $(coso);
//if the value is an array, i gotta search the item with that value.
if (parts[1] instanceof Array){
for (var i2 in parts[1]){
var val = ""+parts[1][i2];
if (coso.attr("value") == decodeURIComponent(val.replace(/\+/g," "))){
coso.prop("checked",true);
} else {
if (!$.inArray(coso.val(),parts[1])){
coso.prop("checked",false);
}
}
}
} else {
val = "" + parts[1];
if (coso.attr("value") == decodeURIComponent(val.replace(/\+/g," "))){
coso.prop("checked",true);
} else {
coso.prop("checked",false);
}
}
});
} else if (obj.length > 0 && obj[0].tagName == "SELECT" && parts[1] instanceof Array && obj.prop("multiple")){
//Here, i have an array for a multi-select.
obj.val(parts[1]);
} else {
//When the value is an array, we join without delimiter
var val = (parts[1] instanceof Array) ? parts[1].join("") : parts[1];
//when the value is an object, we set the value to ""
val = (typeof val == "object") ? "" : val;
obj.val(decodeURIComponent(val.replace(/\+/g," ")));
}
};
return this;
}