]>
git.wh0rd.org - tt-rss.git/blob - lib/dojo/dom-form.js.uncompressed.js
1 define("dojo/dom-form", ["./_base/lang", "./dom", "./io-query", "./json"], function(lang
, dom
, ioq
, json
){
5 // This module defines form-processing functions.
8 dojo.fieldToObject = function(inputNode){
10 // Serialize a form field to a JavaScript object.
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
22 dojo.formToObject = function(formNode){
24 // Serialize a form node to a JavaScript object.
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
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>
46 // yields this object structure as the result of a call to
60 dojo.formToQuery = function(formNode){
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
70 dojo.formToJson = function(formNode, prettyPrint){
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?
80 function setValue(/*Object*/obj
, /*String*/name
, /*String*/value
){
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
86 // Skip it if there is no value
92 if(typeof val
== "string"){ // inline'd type check
93 obj
[name
] = [val
, value
];
94 }else if(lang
.isArray(val
)){
101 var exclude
= "file|submit|image|reset|button";
104 fieldToObject
: function fieldToObject(/*DOMNode|String*/ inputNode
){
106 inputNode
= dom
.byId(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
;
114 }else if(inputNode
.multiple
){
116 var nodes
= [inputNode
.firstChild
];
118 for(var node
= nodes
.pop(); node
; node
= node
.nextSibling
){
119 if(node
.nodeType
== 1 && node
.tagName
.toLowerCase() == "option"){
121 ret
.push(node
.value
);
124 if(node
.nextSibling
){
125 nodes
.push(node
.nextSibling
);
128 nodes
.push(node
.firstChild
);
135 ret
= inputNode
.value
;
139 return ret
; // Object
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
));
149 ret
[_in
+ ".x"] = ret
[_in
+ ".y"] = ret
[_in
].x
= ret
[_in
].y
= 0;
153 return ret
; // Object
156 toQuery
: function formToQuery(/*DOMNode|String*/ formNode
){
157 return ioq
.objectToQuery(form
.toObject(formNode
)); // String
160 toJson
: function formToJson(/*DOMNode|String*/ formNode
, /*Boolean?*/prettyPrint
){
161 return json
.stringify(form
.toObject(formNode
), null, prettyPrint
? 4 : 0); // String