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