]>
git.wh0rd.org - tt-rss.git/blob - lib/dojo/dom-form.js.uncompressed.js
1015730659ef81d470c9b27874e2d33d8db0ba1c
1 define("dojo/dom-form", ["./_base/lang", "./dom", "./io-query", "./json"], function(lang
, dom
, ioq
, json
){
5 function setValue(/*Object*/ obj
, /*String*/ name
, /*String*/ value
){
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
11 // Skip it if there is no value
17 if(typeof val
== "string"){ // inline'd type check
18 obj
[name
] = [val
, value
];
19 }else if(lang
.isArray(val
)){
26 var exclude
= "file|submit|image|reset|button";
30 // This module defines form-processing functions.
32 fieldToObject
: function fieldToObject(/*DOMNode|String*/ inputNode
){
34 // Serialize a form field to a JavaScript object.
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
44 inputNode
= dom
.byId(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
;
52 }else if(inputNode
.multiple
){
54 var nodes
= [inputNode
.firstChild
];
56 for(var node
= nodes
.pop(); node
; node
= node
.nextSibling
){
57 if(node
.nodeType
== 1 && node
.tagName
.toLowerCase() == "option"){
63 nodes
.push(node
.nextSibling
);
66 nodes
.push(node
.firstChild
);
73 ret
= inputNode
.value
;
80 toObject
: function formToObject(/*DOMNode|String*/ formNode
){
82 // Serialize a form node to a JavaScript object.
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
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>
102 // yields this object structure as the result of a call to
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
));
119 ret
[_in
+ ".x"] = ret
[_in
+ ".y"] = ret
[_in
].x
= ret
[_in
].y
= 0;
123 return ret
; // Object
126 toQuery
: function formToQuery(/*DOMNode|String*/ formNode
){
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
133 return ioq
.objectToQuery(form
.toObject(formNode
)); // String
136 toJson
: function formToJson(/*DOMNode|String*/ formNode
, /*Boolean?*/ prettyPrint
){
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?
144 return json
.stringify(form
.toObject(formNode
), null, prettyPrint
? 4 : 0); // String