/dashboard/src/app/account/details/account-update-password.directive.ts

https://github.com/codenvy/codenvy · TypeScript · 65 lines · 39 code · 8 blank · 18 comment · 10 complexity · 59197e676ba3da32e885c3e9acb9ebe7 MD5 · raw file

  1. /*
  2. * Copyright (c) [2015] - [2017] Red Hat, Inc.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors:
  9. * Red Hat, Inc. - initial API and implementation
  10. */
  11. 'use strict';
  12. interface IAccountUpdateScope extends ng.IScope {
  13. newPassword: string;
  14. passStrength: number;
  15. confirmPassword: string;
  16. onPasswordChange: Function;
  17. isPasswordMatch: Function;
  18. }
  19. /**
  20. * Defines a directive for displaying update password widget.
  21. * @author Oleksii Orel
  22. */
  23. export class AccountUpdatePassword {
  24. restrict = 'E';
  25. templateUrl = 'app/account/details/account-update-password.html';
  26. // scope values
  27. scope = {
  28. newPassword: '=cdvyPassword',
  29. changePasswordForm: '=cdvyForm'
  30. };
  31. /**
  32. * Keep reference to the model controller
  33. */
  34. link($scope: IAccountUpdateScope) {
  35. const reTests = [/[a-z]/, /[A-Z]/, /\d/, /[^a-zA-Z\d]/];
  36. const checkPassStrength = (pass: string): void => {
  37. if (!pass || !reTests || reTests.length === 0) {
  38. $scope.passStrength = 0;
  39. return;
  40. }
  41. let passStrength = pass && pass.length > 16 ? 1 : 0;
  42. reTests.forEach((reTest: RegExp) => {
  43. if (reTest.test(pass)) {
  44. passStrength++;
  45. }
  46. });
  47. $scope.passStrength = passStrength * 100 / reTests.length;
  48. };
  49. $scope.onPasswordChange = (value: string) => {
  50. $scope.confirmPassword = '';
  51. checkPassStrength(value);
  52. };
  53. $scope.isPasswordMatch = (value: string) => {
  54. return !value || value === $scope.newPassword;
  55. };
  56. }
  57. }