xonecas.io
286
Sf

simple-flask-coffee-sass-boilerplate / files

simple-flask-coffee-sass-boilerplate / bower_components / jquery / src / serialize.js

3.1 KB
1define([
2	"./core",
3	"./manipulation/var/rcheckableType",
4	"./core/init",
5	"./traversing", // filter
6	"./attributes/prop"
7], function( jQuery, rcheckableType ) {
8
9var r20 = /%20/g,
10	rbracket = /\[\]$/,
11	rCRLF = /\r?\n/g,
12	rsubmitterTypes = /^(?:submit|button|image|reset|file)$/i,
13	rsubmittable = /^(?:input|select|textarea|keygen)/i;
14
15function buildParams( prefix, obj, traditional, add ) {
16	var name;
17
18	if ( jQuery.isArray( obj ) ) {
19		// Serialize array item.
20		jQuery.each( obj, function( i, v ) {
21			if ( traditional || rbracket.test( prefix ) ) {
22				// Treat each array item as a scalar.
23				add( prefix, v );
24
25			} else {
26				// Item is non-scalar (array or object), encode its numeric index.
27				buildParams( prefix + "[" + ( typeof v === "object" ? i : "" ) + "]", v, traditional, add );
28			}
29		});
30
31	} else if ( !traditional && jQuery.type( obj ) === "object" ) {
32		// Serialize object item.
33		for ( name in obj ) {
34			buildParams( prefix + "[" + name + "]", obj[ name ], traditional, add );
35		}
36
37	} else {
38		// Serialize scalar item.
39		add( prefix, obj );
40	}
41}
42
43// Serialize an array of form elements or a set of
44// key/values into a query string
45jQuery.param = function( a, traditional ) {
46	var prefix,
47		s = [],
48		add = function( key, value ) {
49			// If value is a function, invoke it and return its value
50			value = jQuery.isFunction( value ) ? value() : ( value == null ? "" : value );
51			s[ s.length ] = encodeURIComponent( key ) + "=" + encodeURIComponent( value );
52		};
53
54	// Set traditional to true for jQuery <= 1.3.2 behavior.
55	if ( traditional === undefined ) {
56		traditional = jQuery.ajaxSettings && jQuery.ajaxSettings.traditional;
57	}
58
59	// If an array was passed in, assume that it is an array of form elements.
60	if ( jQuery.isArray( a ) || ( a.jquery && !jQuery.isPlainObject( a ) ) ) {
61		// Serialize the form elements
62		jQuery.each( a, function() {
63			add( this.name, this.value );
64		});
65
66	} else {
67		// If traditional, encode the "old" way (the way 1.3.2 or older
68		// did it), otherwise encode params recursively.
69		for ( prefix in a ) {
70			buildParams( prefix, a[ prefix ], traditional, add );
71		}
72	}
73
74	// Return the resulting serialization
75	return s.join( "&" ).replace( r20, "+" );
76};
77
78jQuery.fn.extend({
79	serialize: function() {
80		return jQuery.param( this.serializeArray() );
81	},
82	serializeArray: function() {
83		return this.map(function() {
84			// Can add propHook for "elements" to filter or add form elements
85			var elements = jQuery.prop( this, "elements" );
86			return elements ? jQuery.makeArray( elements ) : this;
87		})
88		.filter(function() {
89			var type = this.type;
90
91			// Use .is( ":disabled" ) so that fieldset[disabled] works
92			return this.name && !jQuery( this ).is( ":disabled" ) &&
93				rsubmittable.test( this.nodeName ) && !rsubmitterTypes.test( type ) &&
94				( this.checked || !rcheckableType.test( type ) );
95		})
96		.map(function( i, elem ) {
97			var val = jQuery( this ).val();
98
99			return val == null ?
100				null :
101				jQuery.isArray( val ) ?
102					jQuery.map( val, function( val ) {
103						return { name: elem.name, value: val.replace( rCRLF, "\r\n" ) };
104					}) :
105					{ name: elem.name, value: val.replace( rCRLF, "\r\n" ) };
106		}).get();
107	}
108});
109
110return jQuery;
111});