| 1 |
define([ |
| 2 |
"dojo/_base/array", |
| 3 |
"dojo/_base/lang", |
| 4 |
"dojo/dom-attr", |
| 5 |
"dojo/i18n", |
| 6 |
"dojo/query", |
| 7 |
"dojo/keys", |
| 8 |
"dijit/form/_FormValueWidget", |
| 9 |
"dijit/form/ValidationTextBox", |
| 10 |
"dojo/text!./resources/PasswordValidator.html", |
| 11 |
"dojo/i18n!./nls/PasswordValidator", |
| 12 |
"dojo/_base/declare" |
| 13 |
], function(array, lang, domAttr, i18n, query, keys, FormValueWidget, ValidationTextBox, template, formNlsPasswordValidator, declare){ |
| 14 |
|
| 15 |
var _ChildTextBox = declare("dojox.form._ChildTextBox", ValidationTextBox, { |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
containerWidget: null, |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
type: "password", |
| 27 |
|
| 28 |
reset: function(){ |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
ValidationTextBox.prototype._setValueAttr.call(this, "", true); |
| 34 |
this._hasBeenBlurred = false; |
| 35 |
}, |
| 36 |
|
| 37 |
postCreate: function(){ |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
this.inherited(arguments); |
| 43 |
if(!this.name){ |
| 44 |
domAttr.remove(this.focusNode, "name"); |
| 45 |
} |
| 46 |
this.connect(this.focusNode, "onkeypress", "_onChildKeyPress"); |
| 47 |
}, |
| 48 |
|
| 49 |
_onChildKeyPress: function(e){ |
| 50 |
|
| 51 |
|
| 52 |
if(e && e.keyCode == keys.ENTER){ |
| 53 |
this._setBlurValue(); |
| 54 |
} |
| 55 |
} |
| 56 |
}); |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
var _OldPWBox = declare("dojox.form._OldPWBox", _ChildTextBox, { |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
_isPWValid: false, |
| 67 |
|
| 68 |
_setValueAttr: function( newVal, priority){ |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
if(newVal === ""){ |
| 73 |
newVal = _OldPWBox.superclass.attr.call(this, "value"); |
| 74 |
} |
| 75 |
if(priority !== null){ |
| 76 |
|
| 77 |
|
| 78 |
this._isPWValid = this.containerWidget.pwCheck(newVal); |
| 79 |
} |
| 80 |
this.inherited(arguments); |
| 81 |
|
| 82 |
this.containerWidget._childValueAttr(this.containerWidget._inputWidgets[1].get("value")); |
| 83 |
}, |
| 84 |
|
| 85 |
isValid: function( isFocused){ |
| 86 |
|
| 87 |
return this.inherited("isValid", arguments) && this._isPWValid; |
| 88 |
}, |
| 89 |
|
| 90 |
_update: function( e){ |
| 91 |
|
| 92 |
|
| 93 |
if(this._hasBeenBlurred){ this.validate(true); } |
| 94 |
this._onMouse(e); |
| 95 |
}, |
| 96 |
|
| 97 |
_getValueAttr: function(){ |
| 98 |
if(this.containerWidget._started && this.containerWidget.isValid()){ |
| 99 |
return this.inherited(arguments); |
| 100 |
} |
| 101 |
return ""; |
| 102 |
}, |
| 103 |
|
| 104 |
_setBlurValue: function(){ |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
var value = ValidationTextBox.prototype._getValueAttr.call(this); |
| 111 |
this._setValueAttr(value, (this.isValid ? this.isValid() : true)); |
| 112 |
} |
| 113 |
}); |
| 114 |
|
| 115 |
|
| 116 |
var _NewPWBox = declare("dojox.form._NewPWBox", _ChildTextBox, { |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
required: true, |
| 123 |
|
| 124 |
onChange: function(){ |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
this.containerWidget._inputWidgets[2].validate(false); |
| 129 |
this.inherited(arguments); |
| 130 |
} |
| 131 |
}); |
| 132 |
|
| 133 |
var _VerifyPWBox = declare("dojox.form._VerifyPWBox", _ChildTextBox, { |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
isValid: function(isFocused){ |
| 138 |
|
| 139 |
|
| 140 |
return this.inherited("isValid", arguments) && |
| 141 |
(this.get("value") == this.containerWidget._inputWidgets[1].get("value")); |
| 142 |
} |
| 143 |
}); |
| 144 |
|
| 145 |
return declare("dojox.form.PasswordValidator", FormValueWidget, { |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
required: true, |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
_inputWidgets: null, |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
oldName: "", |
| 162 |
|
| 163 |
templateString: template, |
| 164 |
|
| 165 |
_hasBeenBlurred: false, |
| 166 |
|
| 167 |
isValid: function( isFocused){ |
| 168 |
|
| 169 |
|
| 170 |
return array.every(this._inputWidgets, function(i){ |
| 171 |
if(i && i._setStateClass){ i._setStateClass(); } |
| 172 |
return (!i || i.isValid()); |
| 173 |
}); |
| 174 |
}, |
| 175 |
|
| 176 |
validate: function( isFocused){ |
| 177 |
|
| 178 |
|
| 179 |
return array.every(array.map(this._inputWidgets, function(i){ |
| 180 |
if(i && i.validate){ |
| 181 |
i._hasBeenBlurred = (i._hasBeenBlurred || this._hasBeenBlurred); |
| 182 |
return i.validate(); |
| 183 |
} |
| 184 |
return true; |
| 185 |
}, this), function(item){ return item; }); |
| 186 |
}, |
| 187 |
|
| 188 |
reset: function(){ |
| 189 |
|
| 190 |
|
| 191 |
this._hasBeenBlurred = false; |
| 192 |
array.forEach(this._inputWidgets, function(i){ |
| 193 |
if(i && i.reset){ i.reset(); } |
| 194 |
}, this); |
| 195 |
}, |
| 196 |
|
| 197 |
_createSubWidgets: function(){ |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
var widgets = this._inputWidgets, |
| 202 |
msg = i18n.getLocalization("dojox.form", "PasswordValidator", this.lang); |
| 203 |
array.forEach(widgets, function(i, idx){ |
| 204 |
if(i){ |
| 205 |
var p = {containerWidget: this}, c; |
| 206 |
if(idx === 0){ |
| 207 |
p.name = this.oldName; |
| 208 |
p.invalidMessage = msg.badPasswordMessage; |
| 209 |
c = _OldPWBox; |
| 210 |
}else if(idx === 1){ |
| 211 |
p.required = this.required; |
| 212 |
c = _NewPWBox; |
| 213 |
}else if(idx === 2){ |
| 214 |
p.invalidMessage = msg.nomatchMessage; |
| 215 |
c = _VerifyPWBox; |
| 216 |
} |
| 217 |
widgets[idx] = new c(p, i); |
| 218 |
} |
| 219 |
}, this); |
| 220 |
}, |
| 221 |
|
| 222 |
pwCheck: function( password){ |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
return false; |
| 246 |
}, |
| 247 |
|
| 248 |
postCreate: function(){ |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
this.inherited(arguments); |
| 258 |
|
| 259 |
|
| 260 |
var widgets = this._inputWidgets = []; |
| 261 |
array.forEach(["old","new","verify"], function(i){ |
| 262 |
widgets.push(query("input[pwType=" + i + "]", this.containerNode)[0]); |
| 263 |
}, this); |
| 264 |
if(!widgets[1] || !widgets[2]){ |
| 265 |
throw new Error("Need at least pwType=\"new\" and pwType=\"verify\""); |
| 266 |
} |
| 267 |
if(this.oldName && !widgets[0]){ |
| 268 |
throw new Error("Need to specify pwType=\"old\" if using oldName"); |
| 269 |
} |
| 270 |
this.containerNode = this.domNode; |
| 271 |
this._createSubWidgets(); |
| 272 |
this.connect(this._inputWidgets[1], "_setValueAttr", "_childValueAttr"); |
| 273 |
this.connect(this._inputWidgets[2], "_setValueAttr", "_childValueAttr"); |
| 274 |
}, |
| 275 |
|
| 276 |
_childValueAttr: function(v){ |
| 277 |
this.set("value", this.isValid() ? v : ""); |
| 278 |
}, |
| 279 |
|
| 280 |
_setDisabledAttr: function(value){ |
| 281 |
this.inherited(arguments); |
| 282 |
array.forEach(this._inputWidgets, function(i){ |
| 283 |
if(i && i.set){ i.set("disabled", value);} |
| 284 |
}); |
| 285 |
}, |
| 286 |
|
| 287 |
_setRequiredAttr: function(value){ |
| 288 |
this.required = value; |
| 289 |
domAttr.set(this.focusNode, "required", value); |
| 290 |
this.focusNode.setAttribute("aria-required", value); |
| 291 |
array.forEach(this._inputWidgets, function(i){ |
| 292 |
if(i && i.set){ i.set("required", value);} |
| 293 |
}); |
| 294 |
}, |
| 295 |
|
| 296 |
_setValueAttr: function(v){ |
| 297 |
this.inherited(arguments); |
| 298 |
domAttr.set(this.focusNode, "value", v); |
| 299 |
}, |
| 300 |
|
| 301 |
_getValueAttr: function(){ |
| 302 |
|
| 303 |
return this.value||""; |
| 304 |
}, |
| 305 |
|
| 306 |
focus: function(){ |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
var f = false; |
| 311 |
array.forEach(this._inputWidgets, function(i){ |
| 312 |
if(i && !i.isValid() && !f){ |
| 313 |
i.focus(); |
| 314 |
f = true; |
| 315 |
} |
| 316 |
}); |
| 317 |
if(!f){ this._inputWidgets[1].focus(); } |
| 318 |
} |
| 319 |
}); |
| 320 |
}); |