-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjQuery.form_params.js
174 lines (154 loc) · 4.67 KB
/
jQuery.form_params.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
;(function($){
$.fn.extend({
/**
* @parent dom
* @download http://jmvcsite.heroku.com/pluginify?plugins[]=jquery/dom/form_params/form_params.js
* @plugin jquery/dom/form_params
* @test jquery/dom/form_params/qunit.html
*
* Returns an object of name-value pairs that represents values in a form.
* It is able to nest values whose element's name has square brackets.
*
* When convert is set to true strings that represent numbers and booleans will
* be converted and empty string will not be added to the object.
*
* Example html:
* @codestart html
* <form>
* <input name="foo[bar]" value='2'/>
* <input name="foo[ced]" value='4'/>
* </form>
* @codeend
* Example code:
*
* $('form').formParams() //-> { foo:{bar:'2', ced: '4'} }
*
*
* @demo jquery/dom/form_params/form_params.html
*
* @param {Object} [params] If an object is passed, the form will be repopulated
* with the values of the object based on the name of the inputs within
* the form
* @param {Boolean} [convert=false] True if strings that look like numbers
* and booleans should be converted and if empty string should not be added
* to the result. Defaults to false.
* @return {Object} An object of name-value pairs.
*/
formParams: function( params ) {
var convert;
var keyBreaker = /[^\[\]]+/g;
// Quick way to determine if something is a boolean
if ( !! params === params ) {
convert = params;
params = null;
}
return params ? setParams( params ) : getParams( convert );
function setParams( params ) {
// Find all the inputs
this.find("[name]").each(function() {
var name = $(this).attr("name");
var parts = name.match( keyBreaker );
var value;
for (var i = 0; i < parts.length; i++) {
value = typeof value === 'object' ? value[parts[i]] : params[parts[i]];
};
var $this;
// Don't do all this work if there's no value
if ( value !== undefined ) {
$this = $(this);
// Nested these if statements for performance
if ( $this.is(":radio") ) {
if ( $this.val() == value ) {
$this.attr("checked", true);
}
} else if ( $this.is(":checkbox") ) {
// Convert single value to an array to reduce
// complexity
value = $.isArray( value ) ? value : [value];
if ( $.inArray( $this.val(), value ) > -1) {
$this.attr("checked", true);
}
} else {
$this.val( value );
}
}
});
return this;
}
function getParams( convert ) {
var convertValue = function( value ) {
if ( $.isNumeric( value )) {
return parseFloat( value );
} else if ( value === 'true') {
return true;
} else if ( value === 'false' ) {
return false;
} else if ( value === '' ) {
return undefined;
}
return value;
},
nestData = function( elem, type, data, parts, value, seen ) {
var name = parts.shift();
if ( parts.length ) {
if ( ! data[ name ] ) {
data[ name ] = {};
}
// Recursive call
nestData( elem, type, data[ name ], parts, value, seen );
} else {
// Handle same name case, as well as "last checkbox checked"
// case
if ( name in seen && type != "radio" && ! $.isArray( data[ name ] )) {
if ( name in data ) {
data[ name ] = [ data[name] ];
} else {
data[ name ] = [];
}
} else {
seen[ name ] = true;
}
// Finally, assign data
if ( ( type == "radio" || type == "checkbox" ) && ! elem.is(":checked") ) {
return
}
if ( ! data[ name ] ) {
data[ name ] = value;
} else {
data[ name ].push( value );
}
}
};
var data = {},
// This is used to keep track of the checkbox names that we've
// already seen, so we know that we should return an array if
// we see it multiple times. Fixes last checkbox checked bug.
seen = {},
current;
this.find("[name]").each(function() {
var $this = $(this),
type = $this.attr("type"),
name = $this.attr("name"),
value = $this.val(),
parts;
// Don't accumulate submit buttons and nameless elements
if ( type == "submit" || ! name ) {
return;
}
// Figure out name parts
parts = name.match( keyBreaker );
if ( ! parts.length ) {
parts = [name];
}
// Convert the value
if ( convert ) {
value = convertValue( value );
}
// Assign data recursively
nestData( $this, type, data, parts, value, seen );
});
return data;
}
}
});
})(jQuery);