io-form-debug.js revision f675023bc7d7e00eff60080f3b554ce49dd9795f
c10c16dec587a0662068f6e2991c29ed3a9db943Richard LoweYUI.add('io-form', function(Y) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe /**
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * Extends the IO base class to enable HTML form data serialization, when specified
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * in the transaction's configuration object.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * @module io
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * @submodule io-form
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe Y.mix(Y.io, {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe /**
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * @description Method to enumerate through an HTML form's elements collection
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * and return a string comprised of key-value pairs.
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov *
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * @method _serialize
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * @private
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * @static
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * @param {object} o - YUI form node or HTML form id.
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * @return string
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe */
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov _serialize: function(o) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe var id = (typeof o.id === 'string') ? o.id : o.id.getAttribute('id'),
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe f = Y.config.doc.getElementById(id),
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe eUC = encodeURIComponent,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe data = [],
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe useDf = o.useDisabled || false,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe item = 0,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov e, n, v, d, i, ilen, j, jlen, o;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe // Iterate over the form elements collection to construct the
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe // label-value pairs.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe for (i = 0, ilen = f.elements.length; i < ilen; ++i) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe e = f.elements[i];
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe d = e.disabled;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe n = e.name;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if ((useDf) ? n : (n && !d)) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe n = encodeURIComponent(n) + '=';
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov v = encodeURIComponent(e.value);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov switch (e.type) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe // Safari, Opera, FF all default options.value from .text if
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov // value attribute not specified in markup
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov case 'select-one':
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (e.selectedIndex > -1) {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov o = e.options[e.selectedIndex];
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe data[item++] = n + eUC((o.attributes.value && o.attributes.value.specified) ? o.value : o.text);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe break;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe case 'select-multiple':
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (e.selectedIndex > -1) {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov for (j = e.selectedIndex, jlen = e.options.length; j < jlen; ++j) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe o = e.options[j];
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (o.selected) {
data[item++] = n + eUC((o.attributes.value && o.attributes.value.specified) ? o.value : o.text);
}
}
}
break;
case 'radio':
case 'checkbox':
if(e.checked){
data[item++] = n + v;
}
break;
case 'file':
// stub case as XMLHttpRequest will only send the file path as a string.
case undefined:
// stub case for fieldset element which returns undefined.
case 'reset':
// stub case for input type reset button.
case 'button':
// stub case for input type button elements.
break;
case 'submit':
default:
data[item++] = n + v;
}
}
}
Y.log('HTML form serialized. The value is: ' + data.join('&'), 'info', 'io');
return data.join('&');
}
}, true);
}, '@VERSION@' ,{requires:['io-base']});