Debian Codegraph @

dojox/form/manager/_FormMixin.js

raw symbols SCIP
1 define([
2 "dojo/_base/lang",
3 "dojo/_base/kernel",
4 "dojo/_base/event",
5 "dojo/window",
6 "./_Mixin",
7 "dojo/_base/declare"
8 ], function(lang, dojo, event, windowUtils, _Mixin, declare){
9 var fm = lang.getObject("dojox.form.manager", true),
10 aa = fm.actionAdapter;
11
12 return declare("dojox.form.manager._FormMixin", null, {
13 // summary:
14 // Form manager's mixin for form-specific functionality.
15 // description:
16 // This mixin adds automated "onreset", and "onsubmit" event processing
17 // if we are based on a form node, defines onReset(), onSubmit(),
18 // reset(), submit(), and isValid() methods like dijit.form.Form.
19 // It should be used together with dojox.form.manager.Mixin.
20
21 // HTML <FORM> attributes (if we are based on the form element)
22 name: "",
23 action: "",
24 method: "",
25 encType: "",
26 "accept-charset": "",
27 accept: "",
28 target: "",
29
30 startup: function(){
31 this.isForm = this.domNode.tagName.toLowerCase() == "form";
32 if(this.isForm){
33 this.connect(this.domNode, "onreset", "_onReset");
34 this.connect(this.domNode, "onsubmit", "_onSubmit");
35 }
36 this.inherited(arguments);
37 },
38
39 // form-specific functionality
40
41 _onReset: function(evt){
42 // NOTE: this function is taken from dijit.form.Form, it works only
43 // for form-based managers.
44
45 // create fake event so we can know if preventDefault() is called
46 var faux = {
47 returnValue: true, // the IE way
48 preventDefault: function(){ // not IE
49 this.returnValue = false;
50 },
51 stopPropagation: function(){}, currentTarget: evt.currentTarget, target: evt.target
52 };
53 // if return value is not exactly false, and haven't called preventDefault(), then reset
54 if(!(this.onReset(faux) === false) && faux.returnValue){
55 this.reset();
56 }
57 event.stop(evt);
58 return false;
59 },
60
61 onReset: function(){
62 // summary:
63 // Callback when user resets the form. This method is intended
64 // to be over-ridden. When the `reset` method is called
65 // programmatically, the return value from `onReset` is used
66 // to compute whether or not resetting should proceed
67 return true; // Boolean
68 },
69
70 reset: function(){
71 // summary:
72 // Resets form widget values.
73 this.inspectFormWidgets(aa(function(_, widget){
74 if(widget.reset){
75 widget.reset();
76 }
77 }));
78 if(this.isForm){
79 this.domNode.reset();
80 }
81 return this;
82 },
83
84 _onSubmit: function(evt){
85 // NOTE: this function is taken from dijit.formForm, it works only
86 // for form-based managers.
87
88 if(this.onSubmit(evt) === false){ // only exactly false stops submit
89 event.stop(evt);
90 }
91 },
92
93 onSubmit: function(){
94 // summary:
95 // Callback when user submits the form. This method is
96 // intended to be over-ridden, but by default it checks and
97 // returns the validity of form elements. When the `submit`
98 // method is called programmatically, the return value from
99 // `onSubmit` is used to compute whether or not submission
100 // should proceed
101
102 return this.isValid(); // Boolean
103 },
104
105 submit: function(){
106 // summary:
107 // programmatically submit form if and only if the `onSubmit` returns true
108 if(this.isForm){
109 if(!(this.onSubmit() === false)){
110 this.domNode.submit();
111 }
112 }
113 },
114
115 isValid: function(){
116 // summary:
117 // Make sure that every widget that has a validator function returns true.
118 for(var name in this.formWidgets){
119 var stop = false;
120 aa(function(_, widget){
121 if(!widget.get("disabled") && widget.isValid && !widget.isValid()){
122 stop = true;
123 }
124 }).call(this, null, this.formWidgets[name].widget);
125 if(stop){
126 return false;
127 }
128 }
129 return true;
130 },
131 validate: function(){
132 // summary:
133 // validate() returns if the form is valid - same as isValid -
134 // but provides a few additional (ui-specific) features: it
135 // will highlight any sub-widgets that are not valid it will
136 // call focus() on the first invalid sub-widget
137 var isValid = true,
138 formWidgets = this.formWidgets,
139 didFocus = false, name;
140
141 for(name in formWidgets){
142 aa(function(_, widget){
143 // Need to set this so that "required" widgets get their
144 // state set.
145 widget._hasBeenBlurred = true;
146 var valid = widget.disabled || !widget.validate || widget.validate();
147 if(!valid && !didFocus){
148 // Set focus of the first non-valid widget
149 windowUtils.scrollIntoView(widget.containerNode || widget.domNode);
150 widget.focus();
151 didFocus = true;
152 }
153 isValid = isValid && valid;
154 }).call(this, null, formWidgets[name].widget);
155 }
156
157 return isValid;
158 }
159 });
160 });