|
1 /* |
|
2 Copyright (c) 2009, Yahoo! Inc. All rights reserved. |
|
3 Code licensed under the BSD License: |
|
4 http://developer.yahoo.net/yui/license.txt |
|
5 version: 3.0.0 |
|
6 build: 1549 |
|
7 */ |
|
8 YUI.add('io-form', function(Y) { |
|
9 |
|
10 /** |
|
11 * Extends the IO base class to enable HTML form data serialization, when specified |
|
12 * in the transaction's configuration object. |
|
13 * @module io |
|
14 * @submodule io-form |
|
15 */ |
|
16 |
|
17 Y.mix(Y.io, { |
|
18 /** |
|
19 * @description Method to enumerate through an HTML form's elements collection |
|
20 * and return a string comprised of key-value pairs. |
|
21 * |
|
22 * @method _serialize |
|
23 * @private |
|
24 * @static |
|
25 * @param {object} c - YUI form node or HTML form id. |
|
26 * @param {string} s - Transaction data defined in the configuration. |
|
27 * @return string |
|
28 */ |
|
29 _serialize: function(c, s) { |
|
30 var eUC = encodeURIComponent, |
|
31 data = [], |
|
32 useDf = c.useDisabled || false, |
|
33 item = 0, |
|
34 id = (typeof c.id === 'string') ? c.id : c.id.getAttribute('id'), |
|
35 e, f, n, v, d, i, il, j, jl, o; |
|
36 |
|
37 if (!id) { |
|
38 id = Y.guid('io:'); |
|
39 c.id.setAttribute('id', id); |
|
40 } |
|
41 |
|
42 f = Y.config.doc.getElementById(id); |
|
43 |
|
44 // Iterate over the form elements collection to construct the |
|
45 // label-value pairs. |
|
46 for (i = 0, il = f.elements.length; i < il; ++i) { |
|
47 e = f.elements[i]; |
|
48 d = e.disabled; |
|
49 n = e.name; |
|
50 |
|
51 if ((useDf) ? n : (n && !d)) { |
|
52 n = encodeURIComponent(n) + '='; |
|
53 v = encodeURIComponent(e.value); |
|
54 |
|
55 switch (e.type) { |
|
56 // Safari, Opera, FF all default options.value from .text if |
|
57 // value attribute not specified in markup |
|
58 case 'select-one': |
|
59 if (e.selectedIndex > -1) { |
|
60 o = e.options[e.selectedIndex]; |
|
61 data[item++] = n + eUC((o.attributes.value && o.attributes.value.specified) ? o.value : o.text); |
|
62 } |
|
63 break; |
|
64 case 'select-multiple': |
|
65 if (e.selectedIndex > -1) { |
|
66 for (j = e.selectedIndex, jl = e.options.length; j < jl; ++j) { |
|
67 o = e.options[j]; |
|
68 if (o.selected) { |
|
69 data[item++] = n + eUC((o.attributes.value && o.attributes.value.specified) ? o.value : o.text); |
|
70 } |
|
71 } |
|
72 } |
|
73 break; |
|
74 case 'radio': |
|
75 case 'checkbox': |
|
76 if(e.checked){ |
|
77 data[item++] = n + v; |
|
78 } |
|
79 break; |
|
80 case 'file': |
|
81 // stub case as XMLHttpRequest will only send the file path as a string. |
|
82 case undefined: |
|
83 // stub case for fieldset element which returns undefined. |
|
84 case 'reset': |
|
85 // stub case for input type reset button. |
|
86 case 'button': |
|
87 // stub case for input type button elements. |
|
88 break; |
|
89 case 'submit': |
|
90 default: |
|
91 data[item++] = n + v; |
|
92 } |
|
93 } |
|
94 } |
|
95 Y.log('HTML form serialized. The value is: ' + data.join('&'), 'info', 'io'); |
|
96 return s ? data.join('&') + "&" + s : data.join('&'); |
|
97 } |
|
98 }, true); |
|
99 |
|
100 |
|
101 |
|
102 }, '3.0.0' ,{requires:['io-base','node-base','node-style']}); |