dom-form.js 5.0 KB

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