define(["../session", "dijit/_WidgetBase", "dijit/_TemplatedMixin", "dijit/_WidgetsInTemplateMixin", "dijit/registry", "dojo/_base/declare", "dojo/_base/event", "dojo/_base/lang", "dojo/on", "dojo/when", "dojo/text!./templates/LoginDialogWrapper.html"], function(session, _WidgetBase, _TemplatedMixin, _WidgetsInTemplateMixin, registry, declare, event, lang, on, when, template) { return declare([_WidgetBase, _TemplatedMixin, _WidgetsInTemplateMixin], { templateString: template, _fx: null, startup: function() { if (this._started) { return; } this.inherited(arguments); this.own(on(session, 'change', lang.hitch(this, 'onUserChange'))); this.own(this.loginForm.on('change', lang.hitch(this, '_handleFormChange'))); this.onUserChange(session.get()); }, _handleFormChange: function() { this.infoNode.innerHTML = ""; }, onLogin: function(evt) { this.infoNode.innerHTML = ""; if (this.loginForm.validate()) { var value = this.loginForm.get('value'); session.login(value.username, value.password) .then(lang.hitch(this,function() { this._fx = when(this._fx).then(lang.hitch(this.loginDialog,'hide')); }), lang.hitch(this,function() { this.infoNode.innerHTML = "Login failed."; })); } if (evt) { event.stop(evt); } return false; }, onUserChange: function(user) { this.infoNode.innerHTML = ""; this._fx = when(this._fx).then(lang.hitch(this.loginDialog,function() { return user ? this.hide() : this.show(); })); } }); });