]> git.pond.sub.org Git - eow/blob - static/dojo-release-1.1.1/dijit/form/ValidationTextBox.js
Comment class stub
[eow] / static / dojo-release-1.1.1 / dijit / form / ValidationTextBox.js
1 if(!dojo._hasResource["dijit.form.ValidationTextBox"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dijit.form.ValidationTextBox"] = true;
3 dojo.provide("dijit.form.ValidationTextBox");
4
5 dojo.require("dojo.i18n");
6
7 dojo.require("dijit.form.TextBox");
8 dojo.require("dijit.Tooltip");
9
10 dojo.requireLocalization("dijit.form", "validate", null, "zh,pt,da,tr,ru,de,sv,ja,he,fi,nb,el,ar,pt-pt,ROOT,cs,fr,es,ko,nl,zh-tw,pl,it,hu");
11
12 /*=====
13         dijit.form.ValidationTextBox.__Constraints = function(){
14                 // locale: String
15                 //              locale used for validation, picks up value from this widget's lang attribute
16                 // _flags_: anything
17                 //              various flags passed to regExpGen function
18                 this.locale = "";
19                 this._flags_ = "";
20         }
21 =====*/
22
23 dojo.declare(
24         "dijit.form.ValidationTextBox",
25         dijit.form.TextBox,
26         {
27                 // summary:
28                 //              A TextBox subclass with the ability to validate content of various types and provide user feedback.
29
30                 templateString:"<div class=\"dijit dijitReset dijitInlineTable dijitLeft\"\n\tid=\"widget_${id}\"\n\tdojoAttachEvent=\"onmouseenter:_onMouse,onmouseleave:_onMouse,onmousedown:_onMouse\" waiRole=\"presentation\"\n\t><div style=\"overflow:hidden;\"\n\t\t><div class=\"dijitReset dijitValidationIcon\"><br></div\n\t\t><div class=\"dijitReset dijitValidationIconText\">&Chi;</div\n\t\t><div class=\"dijitReset dijitInputField\"\n\t\t\t><input class=\"dijitReset\" dojoAttachPoint='textbox,focusNode' dojoAttachEvent='onfocus:_update,onkeyup:_onkeyup,onblur:_onMouse,onkeypress:_onKeyPress' autocomplete=\"off\"\n\t\t\ttype='${type}' name='${name}'\n\t\t/></div\n\t></div\n></div>\n",
31                 baseClass: "dijitTextBox",
32
33                 // default values for new subclass properties
34                 // required: Boolean
35                 //              Can be true or false, default is false.
36                 required: false,
37
38                 // promptMessage: String
39                 //              Hint string
40                 promptMessage: "",
41
42                 // invalidMessage: String
43                 //              The message to display if value is invalid.
44                 invalidMessage: "$_unset_$", // read from the message file if not overridden
45
46                 // constraints: dijit.form.ValidationTextBox.__Constraints
47                 //              user-defined object needed to pass parameters to the validator functions
48                 constraints: {},
49
50                 // regExp: String
51                 //              regular expression string used to validate the input
52                 //              Do not specify both regExp and regExpGen
53                 regExp: ".*",
54
55                 // regExpGen: Function
56                 //              user replaceable function used to generate regExp when dependent on constraints
57                 //              Do not specify both regExp and regExpGen
58                 regExpGen: function(/*dijit.form.ValidationTextBox.__Constraints*/constraints){ return this.regExp; },
59
60                 // state: String
61                 //              Shows current state (ie, validation result) of input (Normal, Warning, or Error)
62                 state: "",
63
64                 //      tooltipPosition: String[]
65                 //              See description of dijit.Tooltip.defaultPosition for details on this parameter.
66                 tooltipPosition: [],
67
68                 setValue: function(){
69                         this.inherited(arguments);
70                         this.validate(this._focused);
71                 },
72
73                 validator: function(/*anything*/value, /*dijit.form.ValidationTextBox.__Constraints*/constraints){
74                         // summary: user replaceable function used to validate the text input against the regular expression.
75                         return (new RegExp("^(" + this.regExpGen(constraints) + ")"+(this.required?"":"?")+"$")).test(value) &&
76                                 (!this.required || !this._isEmpty(value)) &&
77                                 (this._isEmpty(value) || this.parse(value, constraints) !== undefined); // Boolean
78                 },
79
80                 isValid: function(/*Boolean*/ isFocused){
81                         // summary: Need to over-ride with your own validation code in subclasses
82                         return this.validator(this.textbox.value, this.constraints);
83                 },
84
85                 _isEmpty: function(value){
86                         // summary: Checks for whitespace
87                         return /^\s*$/.test(value); // Boolean
88                 },
89
90                 getErrorMessage: function(/*Boolean*/ isFocused){
91                         // summary: return an error message to show if appropriate
92                         return this.invalidMessage; // String
93                 },
94
95                 getPromptMessage: function(/*Boolean*/ isFocused){
96                         // summary: return a hint to show if appropriate
97                         return this.promptMessage; // String
98                 },
99
100                 validate: function(/*Boolean*/ isFocused){
101                         // summary:
102                         //              Called by oninit, onblur, and onkeypress.
103                         // description:
104                         //              Show missing or invalid messages if appropriate, and highlight textbox field.
105                         var message = "";
106                         var isValid = this.isValid(isFocused);
107                         var isEmpty = this._isEmpty(this.textbox.value);
108                         this.state = (isValid || (!this._hasBeenBlurred && isEmpty)) ? "" : "Error";
109                         this._setStateClass();
110                         dijit.setWaiState(this.focusNode, "invalid", isValid ? "false" : "true");
111                         if(isFocused){
112                                 if(isEmpty){
113                                         message = this.getPromptMessage(true);
114                                 }
115                                 if(!message && this.state == "Error"){
116                                         message = this.getErrorMessage(true);
117                                 }
118                         }
119                         this.displayMessage(message);
120                         return isValid;
121                 },
122
123                 // currently displayed message
124                 _message: "",
125
126                 displayMessage: function(/*String*/ message){
127                         // summary:
128                         //              User overridable method to display validation errors/hints.
129                         //              By default uses a tooltip.
130                         if(this._message == message){ return; }
131                         this._message = message;
132                         dijit.hideTooltip(this.domNode);
133                         if(message){
134                                 dijit.showTooltip(message, this.domNode, this.tooltipPosition);
135                         }
136                 },
137
138                 _refreshState: function(){
139                         this.validate(this._focused);
140                 },
141
142                 _update: function(/*Event*/e){
143                         this._refreshState();
144                         this._onMouse(e);       // update CSS classes
145                 },
146
147                 _onkeyup: function(/*Event*/e){
148                         this._update(e);
149                         this.onkeyup(e);
150                 },
151
152                 //////////// INITIALIZATION METHODS ///////////////////////////////////////
153
154                 constructor: function(){
155                         this.constraints = {};
156                 },
157
158                 postMixInProperties: function(){
159                         this.inherited(arguments);
160                         this.constraints.locale = this.lang;
161                         this.messages = dojo.i18n.getLocalization("dijit.form", "validate", this.lang);
162                         if(this.invalidMessage == "$_unset_$"){ this.invalidMessage = this.messages.invalidMessage; }
163                         var p = this.regExpGen(this.constraints);
164                         this.regExp = p;
165                 }
166         }
167 );
168
169 dojo.declare(
170         "dijit.form.MappedTextBox",
171         dijit.form.ValidationTextBox,
172         {
173                 // summary:
174                 //              A dijit.form.ValidationTextBox subclass which provides a visible formatted display and a serializable
175                 //              value in a hidden input field which is actually sent to the server.  The visible display may
176                 //              be locale-dependent and interactive.  The value sent to the server is stored in a hidden
177                 //              input field which uses the `name` attribute declared by the original widget.  That value sent
178                 //              to the serveris defined by the dijit.form.MappedTextBox.serialize method and is typically
179                 //              locale-neutral.
180
181                 serialize: function(/*anything*/val, /*Object?*/options){
182                         // summary: user replaceable function used to convert the getValue() result to a String
183                         return val.toString ? val.toString() : ""; // String
184                 },
185
186                 toString: function(){
187                         // summary: display the widget as a printable string using the widget's value
188                         var val = this.filter(this.getValue());
189                         return val != null ? (typeof val == "string" ? val : this.serialize(val, this.constraints)) : ""; // String
190                 },
191
192                 validate: function(){
193                         this.valueNode.value = this.toString();
194                         return this.inherited(arguments);
195                 },
196
197                 setAttribute: function(/*String*/ attr, /*anything*/ value){
198                         this.inherited(arguments);
199                         switch(attr){
200                                 case "disabled":
201                                         if(this.valueNode){
202                                                 this.valueNode.disabled = this.disabled;
203                                         }
204                         }
205                 },
206
207                 postCreate: function(){
208                         var textbox = this.textbox;
209                         var valueNode = (this.valueNode = dojo.doc.createElement("input"));
210                         valueNode.setAttribute("type", textbox.type);
211                         valueNode.setAttribute("value", this.toString());
212                         dojo.style(valueNode, "display", "none");
213                         valueNode.name = this.textbox.name;
214                         valueNode.disabled = this.textbox.disabled;
215                         this.textbox.name = this.textbox.name + "_displayed_";
216                         this.textbox.removeAttribute("name");
217                         dojo.place(valueNode, textbox, "after");
218
219                         this.inherited(arguments);
220                 }
221         }
222 );
223
224 /*=====
225         dijit.form.RangeBoundTextBox.__Constraints = function(){
226                 // min: Number
227                 //              Minimum signed value.  Default is -Infinity
228                 // max: Number
229                 //              Maximum signed value.  Default is +Infinity
230                 this.min = min;
231                 this.max = max;
232         }
233 =====*/
234
235 dojo.declare(
236         "dijit.form.RangeBoundTextBox",
237         dijit.form.MappedTextBox,
238         {
239                 // summary:
240                 //              A dijit.form.MappedTextBox subclass which defines a range of valid values
241                 //
242                 // constraints: dijit.form.RangeBoundTextBox.__Constraints
243                 //
244                 // rangeMessage: String
245                 //              The message to display if value is out-of-range
246
247                 /*=====
248                 constraints: {},
249                 ======*/
250                 rangeMessage: "",
251
252                 compare: function(/*anything*/val1, /*anything*/val2){
253                         // summary: compare 2 values
254                         return val1 - val2; // anything
255                 },
256
257                 rangeCheck: function(/*Number*/ primitive, /*dijit.form.RangeBoundTextBox.__Constraints*/ constraints){
258                         // summary: user replaceable function used to validate the range of the numeric input value
259                         var isMin = "min" in constraints;
260                         var isMax = "max" in constraints;
261                         if(isMin || isMax){
262                                 return (!isMin || this.compare(primitive,constraints.min) >= 0) &&
263                                         (!isMax || this.compare(primitive,constraints.max) <= 0);
264                         }
265                         return true; // Boolean
266                 },
267
268                 isInRange: function(/*Boolean*/ isFocused){
269                         // summary: Need to over-ride with your own validation code in subclasses
270                         return this.rangeCheck(this.getValue(), this.constraints);
271                 },
272
273                 isValid: function(/*Boolean*/ isFocused){
274                         return this.inherited(arguments) &&
275                                 ((this._isEmpty(this.textbox.value) && !this.required) || this.isInRange(isFocused)); // Boolean
276                 },
277
278                 getErrorMessage: function(/*Boolean*/ isFocused){
279                         if(dijit.form.RangeBoundTextBox.superclass.isValid.call(this, false) && !this.isInRange(isFocused)){ return this.rangeMessage; } // String
280                         return this.inherited(arguments);
281                 },
282
283                 postMixInProperties: function(){
284                         this.inherited(arguments);
285                         if(!this.rangeMessage){
286                                 this.messages = dojo.i18n.getLocalization("dijit.form", "validate", this.lang);
287                                 this.rangeMessage = this.messages.rangeMessage;
288                         }
289                 },
290
291                 postCreate: function(){
292                         this.inherited(arguments);
293                         if(this.constraints.min !== undefined){
294                                 dijit.setWaiState(this.focusNode, "valuemin", this.constraints.min);
295                         }
296                         if(this.constraints.max !== undefined){
297                                 dijit.setWaiState(this.focusNode, "valuemax", this.constraints.max);
298                         }
299                 },
300                 
301                 setValue: function(/*Number*/ value, /*Boolean?*/ priorityChange){
302                         dijit.setWaiState(this.focusNode, "valuenow", value);
303                         this.inherited('setValue', arguments);
304                 }
305         }
306 );
307
308 }