Advertisement
Guest User

Untitled

a guest
Mar 29th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.44 KB | None | 0 0
  1. import View from "../View";
  2.  
  3. import ServiceLocator from "../../ServiceLocator";
  4. import FieldValidator from "../util/FieldValidator";
  5. import DataModel from "../../model/DataModel";
  6. import Path from "../../Path";
  7. import * as shajs from "sha.js";
  8. import { view_layer_id } from "../../services/ViewManager";
  9.  
  10. import InfoMessageView from "../common/popup/InfoMessageView";
  11. import ForgottenPasswordView from "./ForgottenPasswordView";
  12. import ForgottenUsernameView from "./ForgottenUsernameView";
  13. import Signal from "../../lib/signals/Signal";
  14. import TooltipManager, { ITooltipInfo } from "../TooltipManager";
  15. import UserRegistrationView from "./UserRegistrationView";
  16. import UsuariosEvent from "../../events/UsuariosEvent";
  17. import UsuariosVO from "../../vo/UsuarioVO";
  18. import { event_type } from "../../services/server_events/ServerEvent";
  19. import Config from "../../Config";
  20.  
  21.  
  22. export default class LoginView extends View
  23. {
  24. // #region Attributes //
  25.  
  26. // private:
  27.  
  28. private _isValidUsername: boolean = true;
  29. private _isValidPassword: boolean = true;
  30.  
  31. private _tooltipManager: TooltipManager = null;
  32. private _fieldValidator: FieldValidator = null;
  33.  
  34. // Signals.
  35. private _onLoginSuccess: Signal = new Signal();
  36.  
  37. // Constants.
  38. private static readonly _kResId: string = "login-login";
  39.  
  40. // #endregion //
  41.  
  42.  
  43. // #region Properties //
  44.  
  45. public static get kResId(): string { return this._kResId; }
  46.  
  47. // Signals.
  48. public get onLoginSuccess(): Signal { return this._onLoginSuccess; };
  49.  
  50. // #endregion //
  51.  
  52.  
  53. // #region Methods //
  54.  
  55. // public:
  56.  
  57. public constructor()
  58. {
  59. super( LoginView._kResId );
  60.  
  61. this._fieldValidator = new FieldValidator();
  62.  
  63. // Attach UI event handlers.
  64. this._jqRoot.find( "#username" ).on( "input", this.onUsername_Changed.bind( this ) );
  65. this._jqRoot.find( "#username" ).on( "keydown", this.onEnterKey_Pressed.bind( this ) );
  66. this._jqRoot.find( "#password" ).on( "input", this.onPassword_Changed.bind( this ) );
  67. this._jqRoot.find( "#password" ).on( "keydown", this.onEnterKey_Pressed.bind( this ) );
  68. this._jqRoot.find( "#loginBtn" ).on( "click", this.onLoginBtn_Click.bind( this ) );
  69. this._jqRoot.find( "#generalConditions" ).on( "click", this.onGeneralConditions_Click );
  70. this._jqRoot.find( "#register" ).on( "click", this.onRegister_Click.bind( this ) );
  71. this._jqRoot.find( "#recoverPassword" ).on( "click", this.onRecoverPassword_Click.bind( this ) );
  72. this._jqRoot.find( "#recoverUsername" ).on( "click", this.onRecoverUsername_Click.bind( this ) );
  73. }
  74.  
  75. public init(): void
  76. {
  77. // Signals.
  78. DataModel.usuario.isLoggedSignal.add( this.onLogin_Result, this );
  79. }
  80.  
  81. public postinit(): void
  82. {
  83. // Add tooltips.
  84. const kArrTooltipInfo: Array<ITooltipInfo> = [
  85. { selector: "#lgUsernameError", locale: "CAMPONOVALIDO" },
  86. { selector: "#passwordError", locale: "CAMPONOVALIDO" }
  87. ];
  88.  
  89. this._tooltipManager = new TooltipManager();
  90. this._tooltipManager.init( kArrTooltipInfo, this._jqRoot );
  91. }
  92.  
  93. public preend(): void
  94. {
  95. // Cleanup tooltips.
  96. this._tooltipManager.end();
  97. }
  98.  
  99. public end(): void
  100. {
  101. // Signals.
  102. DataModel.usuario.isLoggedSignal.remove( this.onLogin_Result, this );
  103.  
  104. super.end();
  105. }
  106.  
  107. public scale( scaleFactor: number, winWidth: number, winHeight: number ): void
  108. {
  109. this._jqRoot.find( "#huntersLogo" ).css( "transform", "scale(" + scaleFactor.toString() + ")" );
  110. this._jqRoot.find( "#loginPanel" ).css( "transform", "scale(" + scaleFactor.toString() + ")" );
  111. }
  112.  
  113. // TEST:
  114. public test_login( username: string, password: string ): void
  115. {
  116. // TODO: SHA256 should not be used for passwords because it is not safe ??? Look for information about this matter.
  117. let passHash: string = shajs( "sha256" ).update( password ).digest( "hex" );
  118.  
  119. // Disable login button.
  120. this._jqRoot.find( "#loginBtn" ).prop( "disabled", true );
  121.  
  122. // Login.
  123. let usuarioVO: UsuariosVO = new UsuariosVO();
  124. usuarioVO.IdPersonaje = username;
  125. usuarioVO.Contrasena = passHash;
  126. DataModel.usuario.passHash = passHash;
  127.  
  128. new UsuariosEvent( event_type.LOGIN, { usuarioVO: usuarioVO, atributo: jQuery.i18n( "LOCALE" ) } ).dispatch();
  129.  
  130. // Show loading animation.
  131. ServiceLocator.loadingAnimation.show();
  132. }
  133.  
  134. // #endregion //
  135.  
  136.  
  137. // #region Callbacks //
  138.  
  139. private onEnterKey_Pressed(event: KeyboardEvent):void{
  140. let keycode:number = (event.keyCode ? event.keyCode : event.which);
  141. if (keycode == 13) {
  142. this.onLoginBtn_Click();
  143. }
  144. }
  145.  
  146. private onLoginBtn_Click(): void
  147. {
  148. // Get input fields.
  149. let username: string = this._jqRoot.find( "#username" ).val() as string;
  150. username = username.trim();
  151. this._jqRoot.find( "#username" ).val( username );
  152. let password: string = this._jqRoot.find( "#password" ).val() as string;
  153.  
  154. // Validate input fields.
  155. this._isValidUsername = this._fieldValidator.validate( this._fieldValidator.kAlphanumeric, username, Config.minimoCaracteresUsuario, Config.maximoCaracteresUsuario );
  156. this._isValidPassword = password.length >= Config.minimoCaracteresContra ? true : false;
  157.  
  158. if ( this._isValidUsername && this._isValidPassword )
  159. {
  160. // TODO: SHA256 should not be used for passwords because it is not safe ??? Look for information about this matter.
  161. let passHash: string = shajs( "sha256" ).update( password ).digest( "hex" );
  162.  
  163. // Disable login button.
  164. this._jqRoot.find( "#loginBtn" ).prop( "disabled", true );
  165.  
  166. // Login.
  167. let usuarioVO: UsuariosVO = new UsuariosVO();
  168. usuarioVO.IdPersonaje = username;
  169. usuarioVO.Contrasena = passHash;
  170. DataModel.usuario.passHash = passHash;
  171.  
  172. new UsuariosEvent( event_type.LOGIN, { usuarioVO: usuarioVO, atributo: jQuery.i18n( "LOCALE" ) } ).dispatch();
  173.  
  174. // Show loading animation.
  175. ServiceLocator.loadingAnimation.show();
  176. }
  177. else
  178. {
  179. // Show error.
  180. if ( this._isValidUsername )
  181. this._jqRoot.find( "#lgUsernameError" ).css( "visibility", "hidden" );
  182. else
  183. this._jqRoot.find( "#lgUsernameError" ).css( "visibility", "visible" );
  184.  
  185. if ( this._isValidPassword )
  186. this._jqRoot.find( "#passwordError" ).css( "visibility", "hidden" );
  187. else
  188. this._jqRoot.find( "#passwordError" ).css( "visibility", "visible" );
  189. }
  190. }
  191.  
  192. private onRegister_Click(): void
  193. {
  194. let userRegistration = new UserRegistrationView();
  195. userRegistration.init();
  196. ServiceLocator.viewManager.fadeIn( userRegistration, view_layer_id.POPUP );
  197. }
  198.  
  199. private onRecoverPassword_Click(): void
  200. {
  201. let forgottenPassword = new ForgottenPasswordView();
  202. forgottenPassword.init();
  203. ServiceLocator.viewManager.fadeIn( forgottenPassword, view_layer_id.POPUP );
  204. }
  205.  
  206. private onRecoverUsername_Click(): void
  207. {
  208. let forgottenUsername = new ForgottenUsernameView();
  209. forgottenUsername.init();
  210. ServiceLocator.viewManager.fadeIn( forgottenUsername, view_layer_id.POPUP );
  211. }
  212.  
  213. private onGeneralConditions_Click(): void
  214. {
  215. window.open( Path.kGeneralConditions );
  216. }
  217.  
  218. private onLogin_Result( result: number ): void
  219. {
  220. // Hide loading animation.
  221. ServiceLocator.loadingAnimation.hide();
  222.  
  223. switch ( result )
  224. {
  225. case login_result.INVALID_USERNAME:
  226. {
  227. // Invalid username.
  228. let popInfo: InfoMessageView = new InfoMessageView();
  229. popInfo.message = jQuery.i18n( "USUINCORRECTO" );
  230. popInfo.init();
  231. ServiceLocator.viewManager.fadeIn( popInfo, view_layer_id.POPUP );
  232.  
  233. break;
  234. }
  235.  
  236. case login_result.ACCOUNT_NOT_ACTIVATED:
  237. {
  238. // User account not activated.
  239. let popInfo: InfoMessageView = new InfoMessageView();
  240. popInfo.message = jQuery.i18n( "PENDIENTECONFIRMACION" );
  241. popInfo.init();
  242. ServiceLocator.viewManager.fadeIn( popInfo, view_layer_id.POPUP );
  243.  
  244. break;
  245. }
  246.  
  247. case login_result.LOGIN_SUCCESS:
  248. {
  249. // Login success.
  250. //this._jqRoot.find( "#loginform" ).submit(); //Launch browser save password prompt
  251. this._onLoginSuccess.dispatch(); //Change view
  252.  
  253. break;
  254. }
  255.  
  256. case login_result.SERVER_UNAVAILABLE:
  257. {
  258. // Server unavailable.
  259. let popInfo: InfoMessageView = new InfoMessageView();
  260. popInfo.message = jQuery.i18n( "SERVERMANTENIMIENTO" );
  261. popInfo.init();
  262. ServiceLocator.viewManager.fadeIn( popInfo, view_layer_id.POPUP );
  263.  
  264. break;
  265. }
  266.  
  267. case login_result.ACCOUNT_BANNED:
  268. {
  269. // User account banned.
  270. let fechaArr: string[] = DataModel.usuario.usuarioVO.FechaIniBan.split( ' ' );
  271.  
  272. let popInfo: InfoMessageView = new InfoMessageView();
  273. popInfo.message = jQuery.i18n( "REGISTERBAN1" ) + " " + fechaArr[ 0 ] + " " + jQuery.i18n( "REGISTERBAN2" ) + " " +
  274. fechaArr[ 1 ] + ", " + jQuery.i18n( "REGISTERBAN3" ) + " " + DataModel.usuario.usuarioVO.DiasBan + " " + jQuery.i18n( "REGISTERBAN4" );
  275. popInfo.init();
  276. ServiceLocator.viewManager.fadeIn( popInfo, view_layer_id.POPUP );
  277.  
  278. break;
  279. }
  280. }
  281.  
  282. this._jqRoot.find( "#loginBtn" ).prop( "disabled", false );
  283. }
  284.  
  285. private onUsername_Changed(): void
  286. {
  287. if ( !this._isValidUsername )
  288. {
  289. let username: string = this._jqRoot.find( "#username" ).val() as string;
  290. username = username.trim();
  291. this._jqRoot.find( "#username" ).val( username );
  292.  
  293. if ( this._fieldValidator.validate( this._fieldValidator.kAlphanumeric, username, Config.minimoCaracteresUsuario, Config.maximoCaracteresUsuario ) )
  294. this._jqRoot.find( "#lgUsernameError" ).css( "visibility", "hidden" );
  295. else
  296. this._jqRoot.find( "#lgUsernameError" ).css( "visibility", "visible" );
  297. }
  298. }
  299.  
  300. private onPassword_Changed(): void
  301. {
  302. if ( !this._isValidPassword )
  303. {
  304. let password: string = this._jqRoot.find( "#password" ).val() as string;
  305.  
  306. if ( password.length >= Config.minimoCaracteresContra )
  307. this._jqRoot.find( "#passwordError" ).css( "visibility", "hidden" );
  308. else
  309. this._jqRoot.find( "#passwordError" ).css( "visibility", "visible" );
  310. }
  311. }
  312.  
  313. // #endregion //
  314. }
  315.  
  316. const enum login_result
  317. {
  318. INVALID_USERNAME = 1,
  319. ACCOUNT_NOT_ACTIVATED,
  320. LOGIN_SUCCESS,
  321. SERVER_UNAVAILABLE,
  322. ACCOUNT_BANNED
  323. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement