class LoginViewController extends TimeCardsViewController { viewWillAppear() { this.accountKeyField.value = localStorage.getItem("last_account_key"); this.usernameField.value = localStorage.getItem("last_username"); this.passwordField.value = ""; //Show or hide the "Log in with a different user" button depending on whether there is a session or not. if (localStorage.getItem("session")) { this.differentUserButtonContainer.style.display = ""; this.accountKeyField.disabled = true; this.usernameField.disabled = true; } else { this.differentUserButtonContainer.style.display = "none"; this.accountKeyField.disabled = false; this.usernameField.disabled = false; } Navigation.displayForLogin(); //Hide the loading view. //This is done because there have been cases where the loading view blocked a pending request login view. var loadingViewController = UIKit.getViewControllerById("loading-view-controller"); if (loadingViewController) { loadingViewController.dismissModally(); } } viewWillDisappear() { Navigation.endDisplayForLogin(); } onLoginButtonPressed(event) { this.startLogin(); } onDifferentUserButtonPressed(event) { Authentication.logOut(); } onKeyUp(event) { if (event.keyCode == 13) { this.onLoginButtonPressed(event); } } startLogin() { this.loginErrorBox.style.display = "none"; //Save the entered account key in the local storage. localStorage.setItem("last_account_key", this.accountKeyField.value); localStorage.setItem("last_username", this.usernameField.value); var request = new Request(TimeCards.REQUEST_URI + "Login", Request.POST, this.onLoginSuccess.bind(this), this.onLoginError.bind(this)); request.send({ //Use the stored values if the fields are disabled. account_key: this.accountKeyField.disabled ? localStorage.getItem("last_account_key") : this.accountKeyField.value, username: this.usernameField.disabled ? localStorage.getItem("last_username") : this.usernameField.value, password: this.passwordField.value }, true); } onLoginSuccess(response) { //Clear the password field for security reasons. this.passwordField.value = ""; if (response === true) { UIKit.onHashChanged(); } } onLoginError(request, status, error) { this.loginErrorBox.style.display = ""; } } UIKit.registerViewControllerType(LoginViewController);