dom-form.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. define(["./_base/lang", "./dom", "./io-query", "./json"], function(lang, dom, ioq, json){
  2. // module:
  3. // dojo/dom-form
  4. function setValue(/*Object*/ obj, /*String*/ name, /*String*/ value){
  5. // summary:
  6. // For the named property in object, set the value. If a value
  7. // already exists and it is a string, convert the value to be an
  8. // array of values.
  9. // Skip it if there is no value
  10. if(value === null){
  11. return;
  12. }
  13. var val = obj[name];
  14. if(typeof val == "string"){ // inline'd type check
  15. obj[name] = [val, value];
  16. }else if(lang.isArray(val)){
  17. val.push(value);
  18. }else{
  19. obj[name] = value;
  20. }
  21. }
  22. var exclude = "file|submit|image|reset|button";
  23. var form = {
  24. // summary:
  25. // This module defines form-processing functions.
  26. fieldToObject: function fieldToObject(/*DOMNode|String*/ inputNode){
  27. // summary:
  28. // Serialize a form field to a JavaScript object.
  29. // description:
  30. // Returns the value encoded in a form field as
  31. // as a string or an array of strings. Disabled form elements
  32. // and unchecked radio and checkboxes are skipped. Multi-select
  33. // elements are returned as an array of string values.
  34. // inputNode: DOMNode|String
  35. // returns: Object
  36. var ret = null;
  37. inputNode = dom.byId(inputNode);
  38. if(inputNode){
  39. var _in = inputNode.name, type = (inputNode.type || "").toLowerCase();
  40. if(_in && type && !inputNode.disabled){
  41. if(type == "radio" || type == "checkbox"){
  42. if(inputNode.checked){
  43. ret = inputNode.value;
  44. }
  45. }else if(inputNode.multiple){
  46. ret = [];
  47. var nodes = [inputNode.firstChild];
  48. while(nodes.length){
  49. for(var node = nodes.pop(); node; node = node.nextSibling){
  50. if(node.nodeType == 1 && node.tagName.toLowerCase() == "option"){
  51. if(node.selected){
  52. ret.push(node.value);
  53. }
  54. }else{
  55. if(node.nextSibling){
  56. nodes.push(node.nextSibling);
  57. }
  58. if(node.firstChild){
  59. nodes.push(node.firstChild);
  60. }
  61. break;
  62. }
  63. }
  64. }
  65. }else{
  66. ret = inputNode.value;
  67. }
  68. }
  69. }
  70. return ret; // Object
  71. },
  72. toObject: function formToObject(/*DOMNode|String*/ formNode){
  73. // summary:
  74. // Serialize a form node to a JavaScript object.
  75. // description:
  76. // Returns the values encoded in an HTML form as
  77. // string properties in an object which it then returns. Disabled form
  78. // elements, buttons, and other non-value form elements are skipped.
  79. // Multi-select elements are returned as an array of string values.
  80. // formNode: DOMNode|String
  81. // example:
  82. // This form:
  83. // | <form id="test_form">
  84. // | <input type="text" name="blah" value="blah">
  85. // | <input type="text" name="no_value" value="blah" disabled>
  86. // | <input type="button" name="no_value2" value="blah">
  87. // | <select type="select" multiple name="multi" size="5">
  88. // | <option value="blah">blah</option>
  89. // | <option value="thud" selected>thud</option>
  90. // | <option value="thonk" selected>thonk</option>
  91. // | </select>
  92. // | </form>
  93. //
  94. // yields this object structure as the result of a call to
  95. // formToObject():
  96. //
  97. // | {
  98. // | blah: "blah",
  99. // | multi: [
  100. // | "thud",
  101. // | "thonk"
  102. // | ]
  103. // | };
  104. var ret = {}, elems = dom.byId(formNode).elements;
  105. for(var i = 0, l = elems.length; i < l; ++i){
  106. var item = elems[i], _in = item.name, type = (item.type || "").toLowerCase();
  107. if(_in && type && exclude.indexOf(type) < 0 && !item.disabled){
  108. setValue(ret, _in, form.fieldToObject(item));
  109. if(type == "image"){
  110. ret[_in + ".x"] = ret[_in + ".y"] = ret[_in].x = ret[_in].y = 0;
  111. }
  112. }
  113. }
  114. return ret; // Object
  115. },
  116. toQuery: function formToQuery(/*DOMNode|String*/ formNode){
  117. // summary:
  118. // Returns a URL-encoded string representing the form passed as either a
  119. // node or string ID identifying the form to serialize
  120. // formNode: DOMNode|String
  121. // returns: String
  122. return ioq.objectToQuery(form.toObject(formNode)); // String
  123. },
  124. toJson: function formToJson(/*DOMNode|String*/ formNode, /*Boolean?*/ prettyPrint){
  125. // summary:
  126. // Create a serialized JSON string from a form node or string
  127. // ID identifying the form to serialize
  128. // formNode: DOMNode|String
  129. // prettyPrint: Boolean?
  130. // returns: String
  131. return json.stringify(form.toObject(formNode), null, prettyPrint ? 4 : 0); // String
  132. }
  133. };
  134. return form;
  135. });