1318 |
alexandre_ |
1 |
/*
|
|
|
2 |
Copyright (c) 2004-2006, The Dojo Foundation
|
|
|
3 |
All Rights Reserved.
|
|
|
4 |
|
|
|
5 |
Licensed under the Academic Free License version 2.1 or above OR the
|
|
|
6 |
modified BSD license. For more information on Dojo licensing, see:
|
|
|
7 |
|
|
|
8 |
http://dojotoolkit.org/community/licensing.shtml
|
|
|
9 |
*/
|
|
|
10 |
|
1422 |
alexandre_ |
11 |
|
|
|
12 |
|
1318 |
alexandre_ |
13 |
dojo.provide("dojo.widget.ValidationTextbox");
|
|
|
14 |
dojo.require("dojo.widget.Textbox");
|
|
|
15 |
dojo.require("dojo.i18n.common");
|
|
|
16 |
dojo.widget.defineWidget("dojo.widget.ValidationTextbox", dojo.widget.Textbox, function () {
|
|
|
17 |
this.flags = {};
|
|
|
18 |
}, {required:false, rangeClass:"range", invalidClass:"invalid", missingClass:"missing", classPrefix:"dojoValidate", size:"", maxlength:"", promptMessage:"", invalidMessage:"", missingMessage:"", rangeMessage:"", listenOnKeyPress:true, htmlfloat:"none", lastCheckedValue:null, templateString:"<span style='float:${this.htmlfloat};'>\n\t<input dojoAttachPoint='textbox' type='${this.type}' dojoAttachEvent='onblur;onfocus;onkeyup'\n\t\tid='${this.widgetId}' name='${this.name}' size='${this.size}' maxlength='${this.maxlength}'\n\t\tclass='${this.className}' style=''>\n\t<span dojoAttachPoint='invalidSpan' class='${this.invalidClass}'>${this.messages.invalidMessage}</span>\n\t<span dojoAttachPoint='missingSpan' class='${this.missingClass}'>${this.messages.missingMessage}</span>\n\t<span dojoAttachPoint='rangeSpan' class='${this.rangeClass}'>${this.messages.rangeMessage}</span>\n</span>\n", templateCssString:".dojoValidateEmpty{\n\tbackground-color: #00FFFF;\n}\n.dojoValidateValid{\n\tbackground-color: #cfc;\n}\n.dojoValidateInvalid{\n\tbackground-color: #fcc;\n}\n.dojoValidateRange{\n\tbackground-color: #ccf;\n}\n", templateCssPath:dojo.uri.moduleUri("dojo.widget", "templates/Validate.css"), invalidSpan:null, missingSpan:null, rangeSpan:null, getValue:function () {
|
|
|
19 |
return this.textbox.value;
|
|
|
20 |
}, setValue:function (value) {
|
|
|
21 |
this.textbox.value = value;
|
|
|
22 |
this.update();
|
|
|
23 |
}, isValid:function () {
|
|
|
24 |
return true;
|
|
|
25 |
}, isInRange:function () {
|
|
|
26 |
return true;
|
|
|
27 |
}, isEmpty:function () {
|
|
|
28 |
return (/^\s*$/.test(this.textbox.value));
|
|
|
29 |
}, isMissing:function () {
|
|
|
30 |
return (this.required && this.isEmpty());
|
|
|
31 |
}, update:function () {
|
|
|
32 |
this.lastCheckedValue = this.textbox.value;
|
|
|
33 |
this.missingSpan.style.display = "none";
|
|
|
34 |
this.invalidSpan.style.display = "none";
|
|
|
35 |
this.rangeSpan.style.display = "none";
|
|
|
36 |
var empty = this.isEmpty();
|
|
|
37 |
var valid = true;
|
|
|
38 |
if (this.promptMessage != this.textbox.value) {
|
|
|
39 |
valid = this.isValid();
|
|
|
40 |
}
|
|
|
41 |
var missing = this.isMissing();
|
|
|
42 |
if (missing) {
|
|
|
43 |
this.missingSpan.style.display = "";
|
|
|
44 |
} else {
|
|
|
45 |
if (!empty && !valid) {
|
|
|
46 |
this.invalidSpan.style.display = "";
|
|
|
47 |
} else {
|
|
|
48 |
if (!empty && !this.isInRange()) {
|
|
|
49 |
this.rangeSpan.style.display = "";
|
|
|
50 |
}
|
|
|
51 |
}
|
|
|
52 |
}
|
|
|
53 |
this.highlight();
|
|
|
54 |
}, updateClass:function (className) {
|
|
|
55 |
var pre = this.classPrefix;
|
|
|
56 |
dojo.html.removeClass(this.textbox, pre + "Empty");
|
|
|
57 |
dojo.html.removeClass(this.textbox, pre + "Valid");
|
|
|
58 |
dojo.html.removeClass(this.textbox, pre + "Invalid");
|
|
|
59 |
dojo.html.addClass(this.textbox, pre + className);
|
|
|
60 |
}, highlight:function () {
|
|
|
61 |
if (this.isEmpty()) {
|
|
|
62 |
this.updateClass("Empty");
|
|
|
63 |
} else {
|
|
|
64 |
if (this.isValid() && this.isInRange()) {
|
|
|
65 |
this.updateClass("Valid");
|
|
|
66 |
} else {
|
|
|
67 |
if (this.textbox.value != this.promptMessage) {
|
|
|
68 |
this.updateClass("Invalid");
|
|
|
69 |
} else {
|
|
|
70 |
this.updateClass("Empty");
|
|
|
71 |
}
|
|
|
72 |
}
|
|
|
73 |
}
|
|
|
74 |
}, onfocus:function (evt) {
|
|
|
75 |
if (!this.listenOnKeyPress) {
|
|
|
76 |
this.updateClass("Empty");
|
|
|
77 |
}
|
|
|
78 |
}, onblur:function (evt) {
|
|
|
79 |
this.filter();
|
|
|
80 |
this.update();
|
|
|
81 |
}, onkeyup:function (evt) {
|
|
|
82 |
if (this.listenOnKeyPress) {
|
|
|
83 |
this.update();
|
|
|
84 |
} else {
|
|
|
85 |
if (this.textbox.value != this.lastCheckedValue) {
|
|
|
86 |
this.updateClass("Empty");
|
|
|
87 |
}
|
|
|
88 |
}
|
|
|
89 |
}, postMixInProperties:function (localProperties, frag) {
|
|
|
90 |
dojo.widget.ValidationTextbox.superclass.postMixInProperties.apply(this, arguments);
|
|
|
91 |
this.messages = dojo.i18n.getLocalization("dojo.widget", "validate", this.lang);
|
|
|
92 |
dojo.lang.forEach(["invalidMessage", "missingMessage", "rangeMessage"], function (prop) {
|
|
|
93 |
if (this[prop]) {
|
|
|
94 |
this.messages[prop] = this[prop];
|
|
|
95 |
}
|
|
|
96 |
}, this);
|
|
|
97 |
}, fillInTemplate:function () {
|
|
|
98 |
dojo.widget.ValidationTextbox.superclass.fillInTemplate.apply(this, arguments);
|
|
|
99 |
this.textbox.isValid = function () {
|
|
|
100 |
this.isValid.call(this);
|
|
|
101 |
};
|
|
|
102 |
this.textbox.isMissing = function () {
|
|
|
103 |
this.isMissing.call(this);
|
|
|
104 |
};
|
|
|
105 |
this.textbox.isInRange = function () {
|
|
|
106 |
this.isInRange.call(this);
|
|
|
107 |
};
|
|
|
108 |
dojo.html.setClass(this.invalidSpan, this.invalidClass);
|
|
|
109 |
this.update();
|
|
|
110 |
this.filter();
|
|
|
111 |
if (dojo.render.html.ie) {
|
|
|
112 |
dojo.html.addClass(this.domNode, "ie");
|
|
|
113 |
}
|
|
|
114 |
if (dojo.render.html.moz) {
|
|
|
115 |
dojo.html.addClass(this.domNode, "moz");
|
|
|
116 |
}
|
|
|
117 |
if (dojo.render.html.opera) {
|
|
|
118 |
dojo.html.addClass(this.domNode, "opera");
|
|
|
119 |
}
|
|
|
120 |
if (dojo.render.html.safari) {
|
|
|
121 |
dojo.html.addClass(this.domNode, "safari");
|
|
|
122 |
}
|
|
|
123 |
}});
|
|
|
124 |
|