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