/www/src/LoginDialog.js
enyo.kind({
    name: "LoginDialog",
    kind: "onyx.Popup",
    classes: "login-dialog",
    centered: true,
    floating: true,
    modal: true,
    scrim: true,
    scrimWhenModal: true,
    events: {
        onTryLogin: ""
    },
    components: [
        {tag: "h2", content: "Login"},
        {kind: "onyx.Groupbox", components: [
            {kind: "onyx.InputDecorator", components: [
                {name: "username", kind: "onyx.Input", placeholder: "Username", onkeypress: "keypress"}
            ]},
            {kind: "onyx.InputDecorator", components: [
                {name: "password", kind: "onyx.Input", placeholder: "Password", type: "password", onkeypress: "keypress"}
            ]}
        ]},
        {name: "loginError", tag: "p", showing: false, classes: "error", content: "I couldn't log you in."},
        {name: "loginButton", kind: "onyx.Button", content: "Login", onclick: "loginClick", classes: "onyx-affirmative"},
        {name: "cancelButton", kind: "onyx.Button", content: "Cancel", onclick: "cancelClick"},
        {kind: "Signals", onLoginSuccessful: "success", onLoginFailed: "failure"}
    ],
    getData: function() {
        return {
            username: this.$.username.getValue(),
            password: this.$.password.getValue()
        };
    },
    loginClick: function() {
        this.$.loginError.hide();
        this.$.loginButton.setDisabled(true);
        this.doTryLogin(this.getData());
    },
    reset: function() {
        this.$.username.setValue('');
        this.$.password.setValue('');
    },
    failure: function(inSender, inEvent) {
        this.$.loginButton.setDisabled(false);
        this.$.loginError.show();
    },
    success: function(inSender, inEvent) {
        this.reset();
        this.$.loginButton.setDisabled(false);
        this.hide();
    },
    cancelClick: function(inSender, inEvent) {
        // Eh, it's kind of like success.
        this.success();
    },
    keypress: function(inSender, inEvent) {
        if (inEvent.keyCode == 13) {
            this.loginClick(this);
        }
    }
});