]>
Commit | Line | Data |
---|---|---|
2f01fe57 | 1 | /* |
81bea17a | 2 | Copyright (c) 2004-2011, The Dojo Foundation All Rights Reserved. |
2f01fe57 AD |
3 | Available via Academic Free License >= 2.1 OR the modified BSD license. |
4 | see: http://dojotoolkit.org/license for details | |
5 | */ | |
6 | ||
7 | ||
81bea17a AD |
8 | if(!dojo._hasResource["dijit.form.Form"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code. |
9 | dojo._hasResource["dijit.form.Form"] = true; | |
2f01fe57 AD |
10 | dojo.provide("dijit.form.Form"); |
11 | dojo.require("dijit._Widget"); | |
12 | dojo.require("dijit._Templated"); | |
13 | dojo.require("dijit.form._FormMixin"); | |
81bea17a AD |
14 | dojo.require("dijit.layout._ContentPaneResizeMixin"); |
15 | ||
16 | ||
17 | dojo.declare( | |
18 | "dijit.form.Form", | |
19 | [dijit._Widget, dijit._Templated, dijit.form._FormMixin, dijit.layout._ContentPaneResizeMixin], | |
20 | { | |
21 | // summary: | |
22 | // Widget corresponding to HTML form tag, for validation and serialization | |
23 | // | |
24 | // example: | |
25 | // | <form dojoType="dijit.form.Form" id="myForm"> | |
26 | // | Name: <input type="text" name="name" /> | |
27 | // | </form> | |
28 | // | myObj = {name: "John Doe"}; | |
29 | // | dijit.byId('myForm').set('value', myObj); | |
30 | // | | |
31 | // | myObj=dijit.byId('myForm').get('value'); | |
32 | ||
33 | // HTML <FORM> attributes | |
34 | ||
35 | // name: String? | |
36 | // Name of form for scripting. | |
37 | name: "", | |
38 | ||
39 | // action: String? | |
40 | // Server-side form handler. | |
41 | action: "", | |
42 | ||
43 | // method: String? | |
44 | // HTTP method used to submit the form, either "GET" or "POST". | |
45 | method: "", | |
46 | ||
47 | // encType: String? | |
48 | // Encoding type for the form, ex: application/x-www-form-urlencoded. | |
49 | encType: "", | |
50 | ||
51 | // accept-charset: String? | |
52 | // List of supported charsets. | |
53 | "accept-charset": "", | |
54 | ||
55 | // accept: String? | |
56 | // List of MIME types for file upload. | |
57 | accept: "", | |
58 | ||
59 | // target: String? | |
60 | // Target frame for the document to be opened in. | |
61 | target: "", | |
62 | ||
63 | templateString: "<form dojoAttachPoint='containerNode' dojoAttachEvent='onreset:_onReset,onsubmit:_onSubmit' ${!nameAttrSetting}></form>", | |
64 | ||
65 | attributeMap: dojo.delegate(dijit._Widget.prototype.attributeMap, { | |
66 | action: "", | |
67 | method: "", | |
68 | encType: "", | |
69 | "accept-charset": "", | |
70 | accept: "", | |
71 | target: "" | |
72 | }), | |
73 | ||
74 | postMixInProperties: function(){ | |
75 | // Setup name=foo string to be referenced from the template (but only if a name has been specified) | |
76 | // Unfortunately we can't use attributeMap to set the name due to IE limitations, see #8660 | |
77 | this.nameAttrSetting = this.name ? ("name='" + this.name + "'") : ""; | |
78 | this.inherited(arguments); | |
79 | }, | |
80 | ||
81 | execute: function(/*Object*/ formContents){ | |
82 | // summary: | |
83 | // Deprecated: use submit() | |
84 | // tags: | |
85 | // deprecated | |
86 | }, | |
87 | ||
88 | onExecute: function(){ | |
89 | // summary: | |
90 | // Deprecated: use onSubmit() | |
91 | // tags: | |
92 | // deprecated | |
93 | }, | |
94 | ||
95 | _setEncTypeAttr: function(/*String*/ value){ | |
96 | this.encType = value; | |
97 | dojo.attr(this.domNode, "encType", value); | |
98 | if(dojo.isIE){ this.domNode.encoding = value; } | |
99 | }, | |
100 | ||
101 | postCreate: function(){ | |
102 | // IE tries to hide encType | |
103 | // TODO: remove in 2.0, no longer necessary with data-dojo-params | |
104 | if(dojo.isIE && this.srcNodeRef && this.srcNodeRef.attributes){ | |
105 | var item = this.srcNodeRef.attributes.getNamedItem('encType'); | |
106 | if(item && !item.specified && (typeof item.value == "string")){ | |
107 | this.set('encType', item.value); | |
108 | } | |
109 | } | |
110 | this.inherited(arguments); | |
111 | }, | |
112 | ||
113 | reset: function(/*Event?*/ e){ | |
114 | // summary: | |
115 | // restores all widget values back to their init values, | |
116 | // calls onReset() which can cancel the reset by returning false | |
117 | ||
118 | // create fake event so we can know if preventDefault() is called | |
119 | var faux = { | |
120 | returnValue: true, // the IE way | |
121 | preventDefault: function(){ // not IE | |
122 | this.returnValue = false; | |
123 | }, | |
124 | stopPropagation: function(){}, | |
125 | currentTarget: e ? e.target : this.domNode, | |
126 | target: e ? e.target : this.domNode | |
127 | }; | |
128 | // if return value is not exactly false, and haven't called preventDefault(), then reset | |
129 | if(!(this.onReset(faux) === false) && faux.returnValue){ | |
130 | this.inherited(arguments, []); | |
131 | } | |
132 | }, | |
133 | ||
134 | onReset: function(/*Event?*/ e){ | |
135 | // summary: | |
136 | // Callback when user resets the form. This method is intended | |
137 | // to be over-ridden. When the `reset` method is called | |
138 | // programmatically, the return value from `onReset` is used | |
139 | // to compute whether or not resetting should proceed | |
140 | // tags: | |
141 | // callback | |
142 | return true; // Boolean | |
143 | }, | |
144 | ||
145 | _onReset: function(e){ | |
146 | this.reset(e); | |
147 | dojo.stopEvent(e); | |
148 | return false; | |
149 | }, | |
150 | ||
151 | _onSubmit: function(e){ | |
152 | var fp = dijit.form.Form.prototype; | |
153 | // TODO: remove this if statement beginning with 2.0 | |
154 | if(this.execute != fp.execute || this.onExecute != fp.onExecute){ | |
155 | dojo.deprecated("dijit.form.Form:execute()/onExecute() are deprecated. Use onSubmit() instead.", "", "2.0"); | |
156 | this.onExecute(); | |
157 | this.execute(this.getValues()); | |
158 | } | |
159 | if(this.onSubmit(e) === false){ // only exactly false stops submit | |
160 | dojo.stopEvent(e); | |
161 | } | |
162 | }, | |
163 | ||
164 | onSubmit: function(/*Event?*/ e){ | |
165 | // summary: | |
166 | // Callback when user submits the form. | |
167 | // description: | |
168 | // This method is intended to be over-ridden, but by default it checks and | |
169 | // returns the validity of form elements. When the `submit` | |
170 | // method is called programmatically, the return value from | |
171 | // `onSubmit` is used to compute whether or not submission | |
172 | // should proceed | |
173 | // tags: | |
174 | // extension | |
175 | ||
176 | return this.isValid(); // Boolean | |
177 | }, | |
178 | ||
179 | submit: function(){ | |
180 | // summary: | |
181 | // programmatically submit form if and only if the `onSubmit` returns true | |
182 | if(!(this.onSubmit() === false)){ | |
183 | this.containerNode.submit(); | |
184 | } | |
185 | } | |
186 | } | |
187 | ); | |
188 | ||
2f01fe57 | 189 | } |