| 1 |
define([ |
| 2 |
"dojo/_base/declare", |
| 3 |
"dojo/_base/kernel", |
| 4 |
"dojo/_base/lang", |
| 5 |
"dojo/i18n", |
| 6 |
"./TextBox", |
| 7 |
"../Tooltip", |
| 8 |
"dojo/text!./templates/ValidationTextBox.html", |
| 9 |
"dojo/i18n!./nls/validate" |
| 10 |
], function(declare, kernel, lang, i18n, TextBox, Tooltip, template){ |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
var ValidationTextBox = declare("dijit.form.ValidationTextBox", TextBox, { |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
templateString: template, |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
required: false, |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
promptMessage: "", |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
invalidMessage: "$_unset_$", |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
missingMessage: "$_unset_$", |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
message: "", |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
constraints:{}, |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
pattern: ".*", |
| 64 |
|
| 65 |
|
| 66 |
regExp: "", |
| 67 |
|
| 68 |
regExpGen: function( ){ |
| 69 |
|
| 70 |
|
| 71 |
}, |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
state: "", |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
tooltipPosition: [], |
| 80 |
|
| 81 |
_deprecateRegExp: function(attr, value){ |
| 82 |
if(value != ValidationTextBox.prototype[attr]){ |
| 83 |
kernel.deprecated("ValidationTextBox id="+this.id+", set('" + attr + "', ...) is deprecated. Use set('pattern', ...) instead.", "", "2.0"); |
| 84 |
this.set('pattern', value); |
| 85 |
} |
| 86 |
}, |
| 87 |
_setRegExpGenAttr: function( newFcn){ |
| 88 |
this._deprecateRegExp("regExpGen", newFcn); |
| 89 |
this._set("regExpGen", this._computeRegexp); |
| 90 |
}, |
| 91 |
_setRegExpAttr: function( value){ |
| 92 |
this._deprecateRegExp("regExp", value); |
| 93 |
}, |
| 94 |
|
| 95 |
_setValueAttr: function(){ |
| 96 |
|
| 97 |
|
| 98 |
this.inherited(arguments); |
| 99 |
this._refreshState(); |
| 100 |
}, |
| 101 |
|
| 102 |
validator: function( value, constraints){ |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
return (new RegExp("^(?:" + this._computeRegexp(constraints) + ")"+(this.required?"":"?")+"$")).test(value) && |
| 108 |
(!this.required || !this._isEmpty(value)) && |
| 109 |
(this._isEmpty(value) || this.parse(value, constraints) !== undefined); |
| 110 |
}, |
| 111 |
|
| 112 |
_isValidSubset: function(){ |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
return this.textbox.value.search(this._partialre) == 0; |
| 117 |
}, |
| 118 |
|
| 119 |
isValid: function( ){ |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
return this.validator(this.textbox.value, this.get('constraints')); |
| 126 |
}, |
| 127 |
|
| 128 |
_isEmpty: function(value){ |
| 129 |
|
| 130 |
|
| 131 |
return (this.trim ? /^\s*$/ : /^$/).test(value); |
| 132 |
}, |
| 133 |
|
| 134 |
getErrorMessage: function( ){ |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
var invalid = this.invalidMessage == "$_unset_$" ? this.messages.invalidMessage : |
| 140 |
!this.invalidMessage ? this.promptMessage : this.invalidMessage; |
| 141 |
var missing = this.missingMessage == "$_unset_$" ? this.messages.missingMessage : |
| 142 |
!this.missingMessage ? invalid : this.missingMessage; |
| 143 |
return (this.required && this._isEmpty(this.textbox.value)) ? missing : invalid; |
| 144 |
}, |
| 145 |
|
| 146 |
getPromptMessage: function( ){ |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
return this.promptMessage; |
| 152 |
}, |
| 153 |
|
| 154 |
_maskValidSubsetError: true, |
| 155 |
validate: function( isFocused){ |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
var message = ""; |
| 163 |
var isValid = this.disabled || this.isValid(isFocused); |
| 164 |
if(isValid){ this._maskValidSubsetError = true; } |
| 165 |
var isEmpty = this._isEmpty(this.textbox.value); |
| 166 |
var isValidSubset = !isValid && isFocused && this._isValidSubset(); |
| 167 |
this._set("state", isValid ? "" : (((((!this._hasBeenBlurred || isFocused) && isEmpty) || isValidSubset) && (this._maskValidSubsetError || (isValidSubset && !this._hasBeenBlurred && isFocused))) ? "Incomplete" : "Error")); |
| 168 |
this.focusNode.setAttribute("aria-invalid", this.state == "Error" ? "true" : "false"); |
| 169 |
|
| 170 |
if(this.state == "Error"){ |
| 171 |
this._maskValidSubsetError = isFocused && isValidSubset; |
| 172 |
message = this.getErrorMessage(isFocused); |
| 173 |
}else if(this.state == "Incomplete"){ |
| 174 |
message = this.getPromptMessage(isFocused); |
| 175 |
this._maskValidSubsetError = !this._hasBeenBlurred || isFocused; |
| 176 |
}else if(isEmpty){ |
| 177 |
message = this.getPromptMessage(isFocused); |
| 178 |
} |
| 179 |
this.set("message", message); |
| 180 |
|
| 181 |
return isValid; |
| 182 |
}, |
| 183 |
|
| 184 |
displayMessage: function( message){ |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
if(message && this.focused){ |
| 191 |
Tooltip.show(message, this.domNode, this.tooltipPosition, !this.isLeftToRight()); |
| 192 |
}else{ |
| 193 |
Tooltip.hide(this.domNode); |
| 194 |
} |
| 195 |
}, |
| 196 |
|
| 197 |
_refreshState: function(){ |
| 198 |
|
| 199 |
if(this._created){ |
| 200 |
this.validate(this.focused); |
| 201 |
} |
| 202 |
this.inherited(arguments); |
| 203 |
}, |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
constructor: function(params ){ |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
this.constraints = lang.clone(this.constraints); |
| 218 |
this.baseClass += ' dijitValidationTextBox'; |
| 219 |
}, |
| 220 |
|
| 221 |
startup: function(){ |
| 222 |
this.inherited(arguments); |
| 223 |
this._refreshState(); |
| 224 |
}, |
| 225 |
|
| 226 |
_setConstraintsAttr: function( constraints){ |
| 227 |
if(!constraints.locale && this.lang){ |
| 228 |
constraints.locale = this.lang; |
| 229 |
} |
| 230 |
this._set("constraints", constraints); |
| 231 |
this._refreshState(); |
| 232 |
}, |
| 233 |
|
| 234 |
_setPatternAttr: function( pattern){ |
| 235 |
this._set("pattern", pattern); |
| 236 |
this._refreshState(); |
| 237 |
}, |
| 238 |
|
| 239 |
_computeRegexp: function( constraints){ |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
var p = this.pattern; |
| 244 |
if(typeof p == "function"){ |
| 245 |
p = p.call(this, constraints); |
| 246 |
} |
| 247 |
if(p != this._lastRegExp){ |
| 248 |
var partialre = ""; |
| 249 |
this._lastRegExp = p; |
| 250 |
|
| 251 |
|
| 252 |
if(p != ".*"){ |
| 253 |
p.replace(/\\.|\[\]|\[.*?[^\\]{1}\]|\{.*?\}|\(\?[=:!]|./g, |
| 254 |
function(re){ |
| 255 |
switch(re.charAt(0)){ |
| 256 |
case '{': |
| 257 |
case '+': |
| 258 |
case '?': |
| 259 |
case '*': |
| 260 |
case '^': |
| 261 |
case '$': |
| 262 |
case '|': |
| 263 |
case '(': |
| 264 |
partialre += re; |
| 265 |
break; |
| 266 |
case ")": |
| 267 |
partialre += "|$)"; |
| 268 |
break; |
| 269 |
default: |
| 270 |
partialre += "(?:"+re+"|$)"; |
| 271 |
break; |
| 272 |
} |
| 273 |
}); |
| 274 |
} |
| 275 |
try{ |
| 276 |
"".search(partialre); |
| 277 |
}catch(e){ |
| 278 |
partialre = this.pattern; |
| 279 |
console.warn('RegExp error in ' + this.declaredClass + ': ' + this.pattern); |
| 280 |
} |
| 281 |
this._partialre = "^(?:" + partialre + ")$"; |
| 282 |
} |
| 283 |
return p; |
| 284 |
}, |
| 285 |
|
| 286 |
postMixInProperties: function(){ |
| 287 |
this.inherited(arguments); |
| 288 |
this.messages = i18n.getLocalization("dijit.form", "validate", this.lang); |
| 289 |
this._setConstraintsAttr(this.constraints); |
| 290 |
}, |
| 291 |
|
| 292 |
_setDisabledAttr: function( value){ |
| 293 |
this.inherited(arguments); |
| 294 |
this._refreshState(); |
| 295 |
}, |
| 296 |
|
| 297 |
_setRequiredAttr: function( value){ |
| 298 |
this._set("required", value); |
| 299 |
this.focusNode.setAttribute("aria-required", value); |
| 300 |
this._refreshState(); |
| 301 |
}, |
| 302 |
|
| 303 |
_setMessageAttr: function( message){ |
| 304 |
this._set("message", message); |
| 305 |
this.displayMessage(message); |
| 306 |
}, |
| 307 |
|
| 308 |
reset:function(){ |
| 309 |
|
| 310 |
|
| 311 |
this._maskValidSubsetError = true; |
| 312 |
this.inherited(arguments); |
| 313 |
}, |
| 314 |
|
| 315 |
_onBlur: function(){ |
| 316 |
|
| 317 |
|
| 318 |
this.displayMessage(''); |
| 319 |
|
| 320 |
this.inherited(arguments); |
| 321 |
}, |
| 322 |
|
| 323 |
destroy: function(){ |
| 324 |
Tooltip.hide(this.domNode); |
| 325 |
this.inherited(arguments); |
| 326 |
} |
| 327 |
}); |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
return ValidationTextBox; |
| 339 |
}); |