~stephen-stewart/yui/smallest-yui

« back to all changes in this revision

Viewing changes to build/io-form/io-form-debug.js

  • Committer: Stephen Stewart
  • Date: 2014-09-16 16:20:59 UTC
  • Revision ID: stephen.stewart@canonical.com-20140916162059-sq8xqpsxzmrwr3l3
remove debug js

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
YUI.add('io-form', function (Y, NAME) {
2
 
 
3
 
/**
4
 
* Extends IO to enable HTML form data serialization, when specified
5
 
* in the transaction's configuration object.
6
 
* @module io
7
 
* @submodule io-form
8
 
* @for IO
9
 
*/
10
 
 
11
 
var eUC = encodeURIComponent;
12
 
 
13
 
/**
14
 
 * Enumerate through an HTML form's elements collection
15
 
 * and return a string comprised of key-value pairs.
16
 
 *
17
 
 * @method stringify
18
 
 * @static
19
 
 * @param {Node|String} form YUI form node or HTML form id
20
 
 * @param {Object} [options] Configuration options.
21
 
 * @param {Boolean} [options.useDisabled=false] Whether to include disabled fields.
22
 
 * @param {Object|String} [options.extra] Extra values to include. May be a query string or an object with key/value pairs.
23
 
 * @return {String}
24
 
 */
25
 
Y.IO.stringify = function(form, options) {
26
 
    options = options || {};
27
 
 
28
 
    var s = Y.IO.prototype._serialize({
29
 
        id: form,
30
 
        useDisabled: options.useDisabled
31
 
    },
32
 
    options.extra && typeof options.extra === 'object' ? Y.QueryString.stringify(options.extra) : options.extra);
33
 
 
34
 
    return s;
35
 
};
36
 
 
37
 
Y.mix(Y.IO.prototype, {
38
 
   /**
39
 
    * Enumerate through an HTML form's elements collection
40
 
    * and return a string comprised of key-value pairs.
41
 
    *
42
 
    * @method _serialize
43
 
    * @private
44
 
    * @param {Object} c
45
 
    * @param {String|Element} c.id YUI form node or HTML form id
46
 
    * @param {Boolean} c.useDisabled `true` to include disabled fields
47
 
    * @param {String} s Key-value data defined in the configuration object.
48
 
    * @return {String}
49
 
    */
50
 
    _serialize: function(c, s) {
51
 
        var data = [],
52
 
            df = c.useDisabled || false,
53
 
            item = 0,
54
 
            id = (typeof c.id === 'string') ? c.id : c.id.getAttribute('id'),
55
 
            e, f, n, v, d, i, il, j, jl, o;
56
 
 
57
 
        if (!id) {
58
 
            id = Y.guid('io:');
59
 
            c.id.setAttribute('id', id);
60
 
        }
61
 
 
62
 
        f = Y.config.doc.getElementById(id);
63
 
 
64
 
        if (!f || !f.elements) {
65
 
            return s || '';
66
 
        }
67
 
 
68
 
        // Iterate over the form elements collection to construct the
69
 
        // label-value pairs.
70
 
        for (i = 0, il = f.elements.length; i < il; ++i) {
71
 
            e = f.elements[i];
72
 
            d = e.disabled;
73
 
            n = e.name;
74
 
 
75
 
            if (df ? n : n && !d) {
76
 
                n = eUC(n) + '=';
77
 
                v = eUC(e.value);
78
 
 
79
 
                switch (e.type) {
80
 
                    // Safari, Opera, FF all default options.value from .text if
81
 
                    // value attribute not specified in markup
82
 
                    case 'select-one':
83
 
                        if (e.selectedIndex > -1) {
84
 
                            o = e.options[e.selectedIndex];
85
 
                            data[item++] = n + eUC(o.attributes.value && o.attributes.value.specified ? o.value : o.text);
86
 
                        }
87
 
                        break;
88
 
                    case 'select-multiple':
89
 
                        if (e.selectedIndex > -1) {
90
 
                            for (j = e.selectedIndex, jl = e.options.length; j < jl; ++j) {
91
 
                                o = e.options[j];
92
 
                                if (o.selected) {
93
 
                                  data[item++] = n + eUC(o.attributes.value && o.attributes.value.specified ? o.value : o.text);
94
 
                                }
95
 
                            }
96
 
                        }
97
 
                        break;
98
 
                    case 'radio':
99
 
                    case 'checkbox':
100
 
                        if (e.checked) {
101
 
                            data[item++] = n + v;
102
 
                        }
103
 
                        break;
104
 
                    case 'file':
105
 
                        // stub case as XMLHttpRequest will only send the file path as a string.
106
 
                    case undefined:
107
 
                        // stub case for fieldset element which returns undefined.
108
 
                    case 'reset':
109
 
                        // stub case for input type reset button.
110
 
                    case 'button':
111
 
                        // stub case for input type button elements.
112
 
                        break;
113
 
                    case 'submit':
114
 
                    default:
115
 
                        data[item++] = n + v;
116
 
                }
117
 
            }
118
 
        }
119
 
 
120
 
        if (s) {
121
 
            data[item++] = s;
122
 
        }
123
 
 
124
 
        Y.log('HTML form serialized. The value is: ' + data.join('&'), 'info', 'io');
125
 
        return data.join('&');
126
 
    }
127
 
}, true);
128
 
 
129
 
 
130
 
}, '@VERSION@', {"requires": ["io-base", "node-base"]});