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