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