📄 logincomponent.js
字号:
// This component creates a login form. The email and password can be remembered// for future use using cookies. The actual login is done using a web service provided// by the RegistrationService class// requires CookieManager// requires RegistrationServicefunction LoginComponent(oStringBundle, oRegService) { // call super() BiComponent.call(this); this.stringBundle = oStringBundle; this.regService = oRegService; // create the components this.messageLabel = new BiLabel; this.userLabel = new BiLabel; this.userField = new BiTextField; this.passwordLabel = new BiLabel; this.passwordField = new BiPasswordField; this.rememberCheck = new BiCheckBox; this.loginButton = new BiButton; this.cancelButton = new BiButton; this.loginProgress = new BiUndeterminedProgressBar; this.errorLabel = new BiLabel; this.updateStrings(); // this assosciates the label with the field this.userLabel.setLabelFor(this.userField); this.passwordLabel.setLabelFor(this.passwordField); // some other properties this.errorLabel.setWrap(true); this.errorLabel.setBorder( new BiBorder(1, "solid", "ThreeDDarkShadow") ); this.errorLabel.setBackColor("Window"); this.errorLabel.setForeColor("WindowText"); this.errorLabel.setPadding(5); this.errorLabel.setIcon( new BiImage( application.getPath() + "images/exclamation.16.png", 16, 16 ) ); // set the fixed size and position values // the dynamic position is done in layoutAllChildren* this.messageLabel.setLeft(10); this.messageLabel.setRight(10); this.userLabel.setLeft(10); this.userLabel.setWidth(105); this.passwordLabel.setLeft(10); this.passwordLabel.setWidth(105); this.userField.setLeft(115); this.passwordField.setLeft(115); this.rememberCheck.setLeft(115); this.userField.setRight(10); this.passwordField.setRight(10); this.rememberCheck.setRight(10); this.loginProgress.setLeft(10); this.loginProgress.setRight(10); this.loginProgress.setVisible(false); this.errorLabel.setLeft(10); this.errorLabel.setRight(10); this.errorLabel.setVisible(false); this.loginButton.setBottom(10); this.loginButton.setWidth(80); this.loginButton.setRight(100); this.cancelButton.setBottom(10); this.cancelButton.setWidth(80); this.cancelButton.setRight(10); // add all the components this.add(this.messageLabel); this.add(this.userLabel); this.add(this.userField); this.add(this.passwordLabel); this.add(this.passwordField); this.add(this.rememberCheck); this.add(this.loginButton); this.add(this.cancelButton); this.add(this.loginProgress); this.add(this.errorLabel); // check cookies var cookieEmail = CookieManager.getCookie("email"); if (cookieEmail) this.userField.setText(cookieEmail); var cookiePassword = CookieManager.getCookie("password"); if (cookiePassword) this.passwordField.setText(cookiePassword); // event hookup this.regService.addEventListener("login", this.onWsResult, this); this.regService.addEventListener("loginerror", this.onWsResult, this); this.loginButton.addEventListener("action", this.login, this); this.cancelButton.addEventListener("action", this.close, this); this.stringBundle.addEventListener("change", this.updateStrings, this);}// make LoginComponent extend BiComponentvar _p = LoginComponent.prototype = new BiComponent;_p._className = "LoginComponent";// override layoutAllChildrenY to calculate the position_p.layoutAllChildrenY = function () { var y = 10; this.messageLabel.setTop(y); y += this.messageLabel.getHeight() + 30; this.userLabel.setTop(y); this.userField.setTop( y - 2); y += this.userLabel.getHeight() + 15; this.passwordLabel.setTop(y); this.passwordField.setTop( y - 2 ); y += this.passwordLabel.getHeight() + 20; this.rememberCheck.setTop(y); y += this.rememberCheck.getHeight() + 10; this.loginProgress.setTop(y); this.errorLabel.setTop(y); // call super.layoutAllChildrenY() BiComponent.prototype.layoutAllChildrenY.call(this);};// if you override layoutAllChildrenY or layoutAllChildrenX you also need to// override layoutAllChildren. Otherwise your y (or x) changes will not be called// when both width and height are changed_p.layoutAllChildren = function () { this.layoutAllChildrenY(); this.layoutAllChildrenX();};// this calls RegistrationService login and updates the UI to show some progress// during the call_p.login = function () { var error = this.validate(); if (error != "") { // since we got an error we show the error label and update its text this.errorLabel.setVisible(true); this.errorLabel.setText(error); } else { var userName = this.userField.getText(); var password = this.passwordField.getText(); this.regService.login(userName, password); this.setFieldsEnabled(false); this.loginProgress.setVisible(true); this.errorLabel.setVisible(false); this.loginProgress.start(); }};_p.loginIfRemembered = function () { var cookieEmail = CookieManager.getCookie("email"); var cookiePassword = CookieManager.getCookie("password"); if (cookieEmail && cookiePassword) { // since we have both we can assume that the user has previously checked // the remember check this.rememberCheck.setValue(true); this.login(); }};_p.close = function () { application.getWindow().close();};// enables the text fields_p.setFieldsEnabled = function (b) { this.userField.setEnabled(b); this.passwordField.setEnabled(b); this.loginButton.setEnabled(b); this.rememberCheck.setEnabled(b);};// validates the text in the required fields_p.validate = function () { var password = this.passwordField.getText(); var email = this.userField.getText(); var error = ""; if ( !(/^\S+(\.S+)*@\S+(\.\S+)*$/.test(email)) ) { error = this.stringBundle.getString("invalidEmail"); } else if (/^\s*$/.test(password)) { error = this.stringBundle.getString("invalidPassword"); } return error;}// this is called when RegistrationService fires the login event_p.onWsResult = function (e) { this.loginProgress.stop(); var error = false; var userName = this.userField.getText(); var password = this.passwordField.getText(); var remember = this.rememberCheck.getValue(); this.setFieldsEnabled(true); this.loginProgress.setVisible(false); this.loginProgress.setValue(10); if (e.result.error) { this.errorLabel.setText(e.result.errorDetail.string); this.errorLabel.setVisible(true); error = true; remember = false; } else if (e.result.value.ErrorCode != 0) { this.errorLabel.setText(e.result.value.Message); this.errorLabel.setVisible(true); error = true; remember = false; } else { this.dispatchEvent(new BiEvent("login")); } if (remember) { CookieManager.setCookie("email", userName, 30); CookieManager.setCookie("password", password, 30); } else { CookieManager.removeCookie("email"); CookieManager.removeCookie("password"); } try { this.userField.setFocused(true); this.userField.selectAll(); } catch (ex) {}};_p.getSessionId = function () { return this.regService.getSessionId();};_p.getUserName = function () { return this.userField.getText();};_p.updateStrings = function () { this.messageLabel.setText( this.stringBundle.getString("loginInfoMessage") ); this.userLabel.setText( this.stringBundle.getString("emailLabel") ); this.passwordLabel.setText( this.stringBundle.getString("passwordLabel") ); this.rememberCheck.setText( this.stringBundle.getString("rememberCheckLabel") ); this.loginButton.setText( this.stringBundle.getString("loginButton") ); this.cancelButton.setText( this.stringBundle.getString("cancelButton") );};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -