forked from srchulo/jQuery-Facets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.deserialize.js
142 lines (110 loc) · 3.98 KB
/
jquery.deserialize.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
* @author Kyle Florence <kyle[dot]florence[at]gmail[dot]com>
* @website https://github.com/kflorence/jquery-deserialize/
* @version 1.2.1
*
* Dual licensed under the MIT and GPLv2 licenses.
*/
(function( jQuery, undefined ) {
var push = Array.prototype.push,
rcheck = /^(?:radio|checkbox)$/i,
rplus = /\+/g,
rselect = /^(?:option|select-one|select-multiple)$/i,
rvalue = /^(?:button|color|date|datetime|datetime-local|email|hidden|month|number|password|range|reset|search|submit|tel|text|textarea|time|url|week)$/i;
function getElements( elements ) {
return elements.map(function() {
return this.elements ? jQuery.makeArray( this.elements ) : this;
}).filter( ":input:not(:disabled)" ).get();
}
function getElementsByName( elements ) {
var current,
elementsByName = {};
jQuery.each( elements, function( i, element ) {
current = elementsByName[ element.name ];
elementsByName[ element.name ] = current === undefined ? element :
( jQuery.isArray( current ) ? current.concat( element ) : [ current, element ] );
});
return elementsByName;
}
jQuery.fn.deserialize = function( data, options ) {
var i, length,
elements = getElements( this ),
normalized = [];
if ( !data || !elements.length ) {
return this;
}
if ( jQuery.isArray( data ) ) {
normalized = data;
} else if ( jQuery.isPlainObject( data ) ) {
var key, value;
for ( key in data ) {
jQuery.isArray( value = data[ key ] ) ?
push.apply( normalized, jQuery.map( value, function( v ) {
return { name: key, value: v };
})) : push.call( normalized, { name: key, value: value } );
}
} else if ( typeof data === "string" ) {
var parts;
data = data.split( "&" );
for ( i = 0, length = data.length; i < length; i++ ) {
parts = data[ i ].split( "=" );
push.call( normalized, {
name: decodeURIComponent( parts[ 0 ] ),
value: decodeURIComponent( parts[ 1 ].replace( rplus, "%20" ) )
});
}
}
if ( !( length = normalized.length ) ) {
return this;
}
var current, element, j, len, name, property, type, value,
change = jQuery.noop,
complete = jQuery.noop,
names = {};
options = options || {};
elements = getElementsByName( elements );
// Backwards compatible with old arguments: data, callback
if ( jQuery.isFunction( options ) ) {
complete = options;
} else {
change = jQuery.isFunction( options.change ) ? options.change : change;
complete = jQuery.isFunction( options.complete ) ? options.complete : complete;
}
for ( i = 0; i < length; i++ ) {
current = normalized[ i ];
name = current.name;
value = current.value;
if ( !( element = elements[ name ] ) ) {
continue;
}
type = ( len = element.length ) ? element[ 0 ] : element;
type = ( type.type || type.nodeName ).toLowerCase();
property = null;
if ( rvalue.test( type ) ) {
if ( len ) {
j = names[ name ];
element = element[ names[ name ] = ( j == undefined ) ? 0 : ++j ];
}
change.call( element, ( element.value = value ) );
} else if ( rcheck.test( type ) ) {
property = "checked";
} else if ( rselect.test( type ) ) {
property = "selected";
}
if ( property ) {
if ( !len ) {
element = [ element ];
len = 1;
}
for ( j = 0; j < len; j++ ) {
current = element[ j ];
if ( current.value == value ) {
change.call( current, ( current[ property ] = true ) && value );
}
}
}
}
complete.call( this );
return this;
};
})( jQuery );