change-email-password.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. define([
  6. 'jquery',
  7. 'jquery/ui'
  8. ], function ($) {
  9. 'use strict';
  10. $.widget('mage.changeEmailPassword', {
  11. options: {
  12. changeEmailSelector: '[data-role=change-email]',
  13. changePasswordSelector: '[data-role=change-password]',
  14. mainContainerSelector: '[data-container=change-email-password]',
  15. titleSelector: '[data-title=change-email-password]',
  16. emailContainerSelector: '[data-container=change-email]',
  17. newPasswordContainerSelector: '[data-container=new-password]',
  18. confirmPasswordContainerSelector: '[data-container=confirm-password]',
  19. currentPasswordSelector: '[data-input=current-password]',
  20. emailSelector: '[data-input=change-email]',
  21. newPasswordSelector: '[data-input=new-password]',
  22. confirmPasswordSelector: '[data-input=confirm-password]'
  23. },
  24. /**
  25. * Create widget
  26. * @private
  27. */
  28. _create: function () {
  29. this.element.on('change', $.proxy(function () {
  30. this._checkChoice();
  31. }, this));
  32. this._checkChoice();
  33. this._bind();
  34. },
  35. /**
  36. * Event binding, will monitor change, keyup and paste events.
  37. * @private
  38. */
  39. _bind: function () {
  40. this._on($(this.options.emailSelector), {
  41. 'change': this._updatePasswordFieldWithEmailValue,
  42. 'keyup': this._updatePasswordFieldWithEmailValue,
  43. 'paste': this._updatePasswordFieldWithEmailValue
  44. });
  45. },
  46. /**
  47. * Check choice
  48. * @private
  49. */
  50. _checkChoice: function () {
  51. if ($(this.options.changeEmailSelector).is(':checked') &&
  52. $(this.options.changePasswordSelector).is(':checked')) {
  53. this._showAll();
  54. } else if ($(this.options.changeEmailSelector).is(':checked')) {
  55. this._showEmail();
  56. } else if ($(this.options.changePasswordSelector).is(':checked')) {
  57. this._showPassword();
  58. } else {
  59. this._hideAll();
  60. }
  61. },
  62. /**
  63. * Show email and password input fields
  64. * @private
  65. */
  66. _showAll: function () {
  67. $(this.options.titleSelector).html(this.options.titleChangeEmailAndPassword);
  68. $(this.options.mainContainerSelector).show();
  69. $(this.options.emailContainerSelector).show();
  70. $(this.options.newPasswordContainerSelector).show();
  71. $(this.options.confirmPasswordContainerSelector).show();
  72. $(this.options.currentPasswordSelector).attr('data-validate', '{required:true}').prop('disabled', false);
  73. $(this.options.emailSelector).attr('data-validate', '{required:true}').prop('disabled', false);
  74. this._updatePasswordFieldWithEmailValue();
  75. $(this.options.confirmPasswordSelector).attr(
  76. 'data-validate',
  77. '{required:true, equalTo:"' + this.options.newPasswordSelector + '"}'
  78. ).prop('disabled', false);
  79. },
  80. /**
  81. * Hide email and password input fields
  82. * @private
  83. */
  84. _hideAll: function () {
  85. $(this.options.mainContainerSelector).hide();
  86. $(this.options.emailContainerSelector).hide();
  87. $(this.options.newPasswordContainerSelector).hide();
  88. $(this.options.confirmPasswordContainerSelector).hide();
  89. $(this.options.currentPasswordSelector).removeAttr('data-validate').prop('disabled', true);
  90. $(this.options.emailSelector).removeAttr('data-validate').prop('disabled', true);
  91. $(this.options.newPasswordSelector).removeAttr('data-validate').prop('disabled', true);
  92. $(this.options.confirmPasswordSelector).removeAttr('data-validate').prop('disabled', true);
  93. },
  94. /**
  95. * Show email input fields
  96. * @private
  97. */
  98. _showEmail: function () {
  99. this._showAll();
  100. $(this.options.titleSelector).html(this.options.titleChangeEmail);
  101. $(this.options.newPasswordContainerSelector).hide();
  102. $(this.options.confirmPasswordContainerSelector).hide();
  103. $(this.options.newPasswordSelector).removeAttr('data-validate').prop('disabled', true);
  104. $(this.options.confirmPasswordSelector).removeAttr('data-validate').prop('disabled', true);
  105. },
  106. /**
  107. * Show password input fields
  108. * @private
  109. */
  110. _showPassword: function () {
  111. this._showAll();
  112. $(this.options.titleSelector).html(this.options.titleChangePassword);
  113. $(this.options.emailContainerSelector).hide();
  114. $(this.options.emailSelector).removeAttr('data-validate').prop('disabled', true);
  115. },
  116. /**
  117. * Update password validation rules with email input field value
  118. * @private
  119. */
  120. _updatePasswordFieldWithEmailValue: function () {
  121. $(this.options.newPasswordSelector).attr(
  122. 'data-validate',
  123. '{required:true, ' +
  124. '\'validate-customer-password\':true, ' +
  125. '\'password-not-equal-to-user-name\':\'' + $(this.options.emailSelector).val() + '\'}'
  126. ).prop('disabled', false);
  127. }
  128. });
  129. return $.mage.changeEmailPassword;
  130. });