Version 3.18.1
Show:

File: io/js/io-form.js

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