class ChangePasswordViewController extends TimeCardsViewController { constructor(element) { super(element); this.successDialog = new Dialog("The password has been successfully updated.", this.onSuccessDialogDismissed.bind(this), Dialog.BUTTONS_OK_ONLY); this.onPasswordChangeErrorBound = this.onPasswordChangeError.bind(this); } viewWillAppear() { TimeCards.dataManager.addStoreErrorListener("user", this.onPasswordChangeErrorBound); this.reset(); } viewWillDisappear() { TimeCards.dataManager.removeStoreErrorListener("user", this.onPasswordChangeErrorBound); } reset() { if (!this.forcedMode) { this.oldPasswordField.value = ""; } this.newPasswordField.value = ""; this.saveButton.disabled = true; } onFieldChanged(event) { this.saveButton.disabled = !this.changePasswordForm.checkValidity(); } onFormSubmitted(event) { event.preventDefault(); TimeCards.dataManager.store("user", Authentication.currentUser.user_id, { old_password: this.oldPasswordField.value, password: this.newPasswordField.value }, this.onPasswordChanged.bind(this)); } onPasswordChanged(response) { this.dismiss(); this.successDialog.show(); } onPasswordChangeError(key, entity, error) { //Do nothing if key is not current user. if (key != Authentication.currentUser.user_id) { return; } //Reset disabled state of save button. this.onFieldChanged(); } onCancelButtonPressed(event) { this.dismiss(); } dismiss() { this.setForcedMode(false); this.dismissModally(); } /** * Prepares this ViewController for forced password reset mode (if forcedMode is true). * This is automatically reset when dismissing this ViewController via its own dismiss() function. * @param {*} forcedMode Pass true to prepare for forced password reset mode, false to revert back to normal mode. * @param {*} oldPassword Optional way to pass the old password. When passed, the old password field is auto-filled and hidden for user convenience. * @param {*} callback Optional callback to be called after the password was changed. */ setForcedMode(forcedMode, oldPassword = null, callback = null) { this.forcedMode = forcedMode; if (this.forcedMode) { this.forcedModeCallback = callback; } this.cancelButton.style.display = this.forcedMode ? "none" : ""; this.forcedResetLabel.style.display = this.forcedMode ? "" : "none"; if (this.forcedMode && oldPassword) { this.oldPasswordField.value = oldPassword; this.oldPasswordField.style.display = "none"; this.oldPasswordFieldLabel.style.display = "none"; } else { this.oldPasswordField.style.display = ""; this.oldPasswordFieldLabel.style.display = ""; } } /** * Called when the success dialog has been dismissed. * Calls the stored callback for forced mode if one is set. */ onSuccessDialogDismissed() { if (this.forcedModeCallback) { this.forcedModeCallback(); this.forcedModeCallback = null; } } } UIKit.registerViewControllerType(ChangePasswordViewController);