corrade-http-templates – Blame information for rev 9

Subversion Repositories:
Rev:
Rev Author Line No. Line
2 eva 1 /*!
2 SerializeJSON jQuery plugin.
3 https://github.com/marioizquierdo/jquery.serializeJSON
4 version 2.6.1 (May, 2015)
5  
6 Copyright (c) 2012, 2015 Mario Izquierdo
7 Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
8 and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
9 */
10 (function ($) {
11 "use strict";
12  
13 // jQuery('form').serializeJSON()
14 $.fn.serializeJSON = function (options) {
15 var serializedObject, formAsArray, keys, type, value, _ref, f, opts;
16 f = $.serializeJSON;
17 opts = f.setupOpts(options); // calculate values for options {parseNumbers, parseBoolens, parseNulls}
18 formAsArray = this.serializeArray(); // array of objects {name, value}
19 f.readCheckboxUncheckedValues(formAsArray, this, opts); // add {name, value} of unchecked checkboxes if needed
20  
21 serializedObject = {};
22 $.each(formAsArray, function (i, input) {
23 keys = f.splitInputNameIntoKeysArray(input.name, opts);
24 type = keys.pop(); // the last element is always the type ("string" by default)
25 if (type !== 'skip') { // easy way to skip a value
26 value = f.parseValue(input.value, type, opts); // string, number, boolean or null
27 if (opts.parseWithFunction && type === '_') { // allow for custom parsing
28 value = opts.parseWithFunction(value, input.name);
29 }
30 f.deepSet(serializedObject, keys, value, opts);
31 }
32 });
33 return serializedObject;
34 };
35  
36 // Use $.serializeJSON as namespace for the auxiliar functions
37 // and to define defaults
38 $.serializeJSON = {
39  
40 defaultOptions: {
41 checkboxUncheckedValue: undefined, // to include that value for unchecked checkboxes (instead of ignoring them)
42  
43 parseNumbers: false, // convert values like "1", "-2.33" to 1, -2.33
44 parseBooleans: false, // convert "true", "false" to true, false
45 parseNulls: false, // convert "null" to null
46 parseAll: false, // all of the above
47 parseWithFunction: null, // to use custom parser, a function like: function(val){ return parsed_val; }
48  
49 customTypes: {}, // override defaultTypes
50 defaultTypes: {
51 "string": function(str) { return String(str); },
52 "number": function(str) { return Number(str); },
53 "boolean": function(str) { var falses = ["false", "null", "undefined", "", "0"]; return falses.indexOf(str) === -1; },
54 "null": function(str) { var falses = ["false", "null", "undefined", "", "0"]; return falses.indexOf(str) === -1 ? str : null; },
55 "array": function(str) { return JSON.parse(str); },
56 "object": function(str) { return JSON.parse(str); },
57 "auto": function(str) { return $.serializeJSON.parseValue(str, null, {parseNumbers: true, parseBooleans: true, parseNulls: true}); } // try again with something like "parseAll"
58 },
59  
60 useIntKeysAsArrayIndex: false // name="foo[2]" value="v" => {foo: [null, null, "v"]}, instead of {foo: ["2": "v"]}
61 },
62  
63 // Merge option defaults into the options
64 setupOpts: function(options) {
65 var opt, validOpts, defaultOptions, optWithDefault, parseAll, f;
66 f = $.serializeJSON;
67  
68 if (options == null) { options = {}; } // options ||= {}
69 defaultOptions = f.defaultOptions || {}; // defaultOptions
70  
71 // Make sure that the user didn't misspell an option
72 validOpts = ['checkboxUncheckedValue', 'parseNumbers', 'parseBooleans', 'parseNulls', 'parseAll', 'parseWithFunction', 'customTypes', 'defaultTypes', 'useIntKeysAsArrayIndex']; // re-define because the user may override the defaultOptions
73 for (opt in options) {
74 if (validOpts.indexOf(opt) === -1) {
75 throw new Error("serializeJSON ERROR: invalid option '" + opt + "'. Please use one of " + validOpts.join(', '));
76 }
77 }
78  
79 // Helper to get the default value for this option if none is specified by the user
80 optWithDefault = function(key) { return (options[key] !== false) && (options[key] !== '') && (options[key] || defaultOptions[key]); };
81  
82 // Return computed options (opts to be used in the rest of the script)
83 parseAll = optWithDefault('parseAll');
84 return {
85 checkboxUncheckedValue: optWithDefault('checkboxUncheckedValue'),
86  
87 parseNumbers: parseAll || optWithDefault('parseNumbers'),
88 parseBooleans: parseAll || optWithDefault('parseBooleans'),
89 parseNulls: parseAll || optWithDefault('parseNulls'),
90 parseWithFunction: optWithDefault('parseWithFunction'),
91  
92 typeFunctions: $.extend({}, optWithDefault('defaultTypes'), optWithDefault('customTypes')),
93  
94 useIntKeysAsArrayIndex: optWithDefault('useIntKeysAsArrayIndex')
95 };
96 },
97  
98 // Given a string, apply the type or the relevant "parse" options, to return the parsed value
99 parseValue: function(str, type, opts) {
100 var typeFunction, f;
101 f = $.serializeJSON;
102  
103 // Parse with a type if available
104 typeFunction = opts.typeFunctions && opts.typeFunctions[type];
105 if (typeFunction) { return typeFunction(str); } // use specific type
106  
107 // Otherwise, check if there is any auto-parse option enabled and use it.
108 if (opts.parseNumbers && f.isNumeric(str)) { return Number(str); } // auto: number
109 if (opts.parseBooleans && (str === "true" || str === "false")) { return str === "true"; } // auto: boolean
110 if (opts.parseNulls && str == "null") { return null; } // auto: null
111  
112 // If none applies, just return the str
113 return str;
114 },
115  
116 isObject: function(obj) { return obj === Object(obj); }, // is it an Object?
117 isUndefined: function(obj) { return obj === void 0; }, // safe check for undefined values
118 isValidArrayIndex: function(val) { return /^[0-9]+$/.test(String(val)); }, // 1,2,3,4 ... are valid array indexes
119 isNumeric: function(obj) { return obj - parseFloat(obj) >= 0; }, // taken from jQuery.isNumeric implementation. Not using jQuery.isNumeric to support old jQuery and Zepto versions
120  
121 optionKeys: function(obj) { if (Object.keys) { return Object.keys(obj); } else { var key, keys = []; for(key in obj){ keys.push(key); } return keys;} }, // polyfill Object.keys to get option keys in IE<9
122  
123 // Split the input name in programatically readable keys.
124 // The last element is always the type (default "_").
125 // Examples:
126 // "foo" => ['foo', '_']
127 // "foo:string" => ['foo', 'string']
128 // "foo:boolean" => ['foo', 'boolean']
129 // "[foo]" => ['foo', '_']
130 // "foo[inn][bar]" => ['foo', 'inn', 'bar', '_']
131 // "foo[inn[bar]]" => ['foo', 'inn', 'bar', '_']
132 // "foo[inn][arr][0]" => ['foo', 'inn', 'arr', '0', '_']
133 // "arr[][val]" => ['arr', '', 'val', '_']
134 // "arr[][val]:null" => ['arr', '', 'val', 'null']
135 splitInputNameIntoKeysArray: function(name, opts) {
136 var keys, nameWithoutType, type, _ref, f;
137 f = $.serializeJSON;
138 _ref = f.extractTypeFromInputName(name, opts); nameWithoutType = _ref[0]; type = _ref[1];
139 keys = nameWithoutType.split('['); // split string into array
140 keys = $.map(keys, function (key) { return key.replace(/\]/g, ''); }); // remove closing brackets
141 if (keys[0] === '') { keys.shift(); } // ensure no opening bracket ("[foo][inn]" should be same as "foo[inn]")
142 keys.push(type); // add type at the end
143 return keys;
144 },
145  
146 // Returns [name-without-type, type] from name.
147 // "foo" => ["foo", '_']
148 // "foo:boolean" => ["foo", 'boolean']
149 // "foo[bar]:null" => ["foo[bar]", 'null']
150 extractTypeFromInputName: function(name, opts) {
151 var match, validTypes, f;
152 if (match = name.match(/(.*):([^:]+)$/)){
153 f = $.serializeJSON;
154  
155 validTypes = f.optionKeys(opts ? opts.typeFunctions : f.defaultOptions.defaultTypes);
156 validTypes.push('skip'); // skip is a special type that makes it easy to remove
157 if (validTypes.indexOf(match[2]) !== -1) {
158 return [match[1], match[2]];
159 } else {
160 throw new Error("serializeJSON ERROR: Invalid type " + match[2] + " found in input name '" + name + "', please use one of " + validTypes.join(', '));
161 }
162 } else {
163 return [name, '_']; // no defined type, then use parse options
164 }
165 },
166  
167 // Set a value in an object or array, using multiple keys to set in a nested object or array:
168 //
169 // deepSet(obj, ['foo'], v) // obj['foo'] = v
170 // deepSet(obj, ['foo', 'inn'], v) // obj['foo']['inn'] = v // Create the inner obj['foo'] object, if needed
171 // deepSet(obj, ['foo', 'inn', '123'], v) // obj['foo']['arr']['123'] = v //
172 //
173 // deepSet(obj, ['0'], v) // obj['0'] = v
174 // deepSet(arr, ['0'], v, {useIntKeysAsArrayIndex: true}) // arr[0] = v
175 // deepSet(arr, [''], v) // arr.push(v)
176 // deepSet(obj, ['arr', ''], v) // obj['arr'].push(v)
177 //
178 // arr = [];
179 // deepSet(arr, ['', v] // arr => [v]
180 // deepSet(arr, ['', 'foo'], v) // arr => [v, {foo: v}]
181 // deepSet(arr, ['', 'bar'], v) // arr => [v, {foo: v, bar: v}]
182 // deepSet(arr, ['', 'bar'], v) // arr => [v, {foo: v, bar: v}, {bar: v}]
183 //
184 deepSet: function (o, keys, value, opts) {
185 var key, nextKey, tail, lastIdx, lastVal, f;
186 if (opts == null) { opts = {}; }
187 f = $.serializeJSON;
188 if (f.isUndefined(o)) { throw new Error("ArgumentError: param 'o' expected to be an object or array, found undefined"); }
189 if (!keys || keys.length === 0) { throw new Error("ArgumentError: param 'keys' expected to be an array with least one element"); }
190  
191 key = keys[0];
192  
193 // Only one key, then it's not a deepSet, just assign the value.
194 if (keys.length === 1) {
195 if (key === '') {
196 o.push(value); // '' is used to push values into the array (assume o is an array)
197 } else {
198 o[key] = value; // other keys can be used as object keys or array indexes
199 }
200  
201 // With more keys is a deepSet. Apply recursively.
202 } else {
203 nextKey = keys[1];
204  
205 // '' is used to push values into the array,
206 // with nextKey, set the value into the same object, in object[nextKey].
207 // Covers the case of ['', 'foo'] and ['', 'var'] to push the object {foo, var}, and the case of nested arrays.
208 if (key === '') {
209 lastIdx = o.length - 1; // asume o is array
210 lastVal = o[lastIdx];
211 if (f.isObject(lastVal) && (f.isUndefined(lastVal[nextKey]) || keys.length > 2)) { // if nextKey is not present in the last object element, or there are more keys to deep set
212 key = lastIdx; // then set the new value in the same object element
213 } else {
214 key = lastIdx + 1; // otherwise, point to set the next index in the array
215 }
216 }
217  
218 // '' is used to push values into the array "array[]"
219 if (nextKey === '') {
220 if (f.isUndefined(o[key]) || !$.isArray(o[key])) {
221 o[key] = []; // define (or override) as array to push values
222 }
223 } else {
224 if (opts.useIntKeysAsArrayIndex && f.isValidArrayIndex(nextKey)) { // if 1, 2, 3 ... then use an array, where nextKey is the index
225 if (f.isUndefined(o[key]) || !$.isArray(o[key])) {
226 o[key] = []; // define (or override) as array, to insert values using int keys as array indexes
227 }
228 } else { // for anything else, use an object, where nextKey is going to be the attribute name
229 if (f.isUndefined(o[key]) || !f.isObject(o[key])) {
230 o[key] = {}; // define (or override) as object, to set nested properties
231 }
232 }
233 }
234  
235 // Recursively set the inner object
236 tail = keys.slice(1);
237 f.deepSet(o[key], tail, value, opts);
238 }
239 },
240  
241 // Fill the formAsArray object with values for the unchecked checkbox inputs,
242 // using the same format as the jquery.serializeArray function.
243 // The value of the unchecked values is determined from the opts.checkboxUncheckedValue
244 // and/or the data-unchecked-value attribute of the inputs.
245 readCheckboxUncheckedValues: function (formAsArray, $form, opts) {
246 var selector, $uncheckedCheckboxes, $el, dataUncheckedValue, f;
247 if (opts == null) { opts = {}; }
248 f = $.serializeJSON;
249  
250 selector = 'input[type=checkbox][name]:not(:checked):not([disabled])';
251 $uncheckedCheckboxes = $form.find(selector).add($form.filter(selector));
252 $uncheckedCheckboxes.each(function (i, el) {
253 $el = $(el);
254 dataUncheckedValue = $el.attr('data-unchecked-value');
255 if(dataUncheckedValue) { // data-unchecked-value has precedence over option opts.checkboxUncheckedValue
256 formAsArray.push({name: el.name, value: dataUncheckedValue});
257 } else {
258 if (!f.isUndefined(opts.checkboxUncheckedValue)) {
259 formAsArray.push({name: el.name, value: opts.checkboxUncheckedValue});
260 }
261 }
262 });
263 }
264  
265 };
266  
267 }(window.jQuery || window.Zepto || window.$));