Merge branch 'MDL-32509' of git://github.com/danpoltawski/moodle
[moodle.git] / lib / yui / 3.5.0 / build / io-form / io-form.js
blob04d2e00fc8ce6361ccac85d527caae95f7777559
1 /*
2 YUI 3.5.0 (build 5089)
3 Copyright 2012 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
6 */
7 YUI.add('io-form', function(Y) {
9 /**
10 * Extends IO to enable HTML form data serialization, when specified
11 * in the transaction's configuration object.
12 * @module io
13 * @submodule io-form
14 * @for IO
17 var eUC = encodeURIComponent;
19 Y.mix(Y.IO.prototype, {
20    /**
21     * 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 - Key-value data defined in the configuration object.
29     * @return {String}
30     */
31     _serialize: function(c, s) {
32         var data = [],
33             df = 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;
38             if (!id) {
39                 id = Y.guid('io:');
40                 c.id.setAttribute('id', id);
41             }
43             f = Y.config.doc.getElementById(id);
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;
52             if (df ? n : n && !d) {
53                 n = eUC(n) + '=';
54                 v = eUC(e.value);
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);
101 }, '3.5.0' ,{requires:['io-base','node-base']});