userHeader.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. Template.headerUserBar.events({
  2. 'click .js-open-header-member-menu': Popup.open('memberMenu'),
  3. 'click .js-change-avatar': Popup.open('changeAvatar'),
  4. });
  5. Template.memberMenuPopup.helpers({
  6. templatesBoardId() {
  7. return Meteor.user().getTemplatesBoardId();
  8. },
  9. templatesBoardSlug() {
  10. return Meteor.user().getTemplatesBoardSlug();
  11. },
  12. });
  13. Template.memberMenuPopup.events({
  14. 'click .js-edit-profile': Popup.open('editProfile'),
  15. 'click .js-change-settings': Popup.open('changeSettings'),
  16. 'click .js-change-avatar': Popup.open('changeAvatar'),
  17. 'click .js-change-password': Popup.open('changePassword'),
  18. 'click .js-change-language': Popup.open('changeLanguage'),
  19. 'click .js-logout'(event) {
  20. event.preventDefault();
  21. AccountsTemplates.logout();
  22. },
  23. 'click .js-go-setting'() {
  24. Popup.close();
  25. },
  26. });
  27. Template.editProfilePopup.helpers({
  28. allowEmailChange() {
  29. return AccountSettings.findOne('accounts-allowEmailChange').booleanValue;
  30. },
  31. allowUserNameChange() {
  32. return AccountSettings.findOne('accounts-allowUserNameChange').booleanValue;
  33. },
  34. });
  35. Template.editProfilePopup.events({
  36. submit(event, templateInstance) {
  37. event.preventDefault();
  38. const fullname = templateInstance.find('.js-profile-fullname').value.trim();
  39. const username = templateInstance.find('.js-profile-username').value.trim();
  40. const initials = templateInstance.find('.js-profile-initials').value.trim();
  41. const email = templateInstance.find('.js-profile-email').value.trim();
  42. let isChangeUserName = false;
  43. let isChangeEmail = false;
  44. Users.update(Meteor.userId(), {
  45. $set: {
  46. 'profile.fullname': fullname,
  47. 'profile.initials': initials,
  48. },
  49. });
  50. isChangeUserName = username !== Meteor.user().username;
  51. isChangeEmail =
  52. email.toLowerCase() !== Meteor.user().emails[0].address.toLowerCase();
  53. if (isChangeUserName && isChangeEmail) {
  54. Meteor.call(
  55. 'setUsernameAndEmail',
  56. username,
  57. email.toLowerCase(),
  58. Meteor.userId(),
  59. function(error) {
  60. const usernameMessageElement = templateInstance.$('.username-taken');
  61. const emailMessageElement = templateInstance.$('.email-taken');
  62. if (error) {
  63. const errorElement = error.error;
  64. if (errorElement === 'username-already-taken') {
  65. usernameMessageElement.show();
  66. emailMessageElement.hide();
  67. } else if (errorElement === 'email-already-taken') {
  68. usernameMessageElement.hide();
  69. emailMessageElement.show();
  70. }
  71. } else {
  72. usernameMessageElement.hide();
  73. emailMessageElement.hide();
  74. Popup.back();
  75. }
  76. },
  77. );
  78. } else if (isChangeUserName) {
  79. Meteor.call('setUsername', username, Meteor.userId(), function(error) {
  80. const messageElement = templateInstance.$('.username-taken');
  81. if (error) {
  82. messageElement.show();
  83. } else {
  84. messageElement.hide();
  85. Popup.back();
  86. }
  87. });
  88. } else if (isChangeEmail) {
  89. Meteor.call('setEmail', email.toLowerCase(), Meteor.userId(), function(
  90. error,
  91. ) {
  92. const messageElement = templateInstance.$('.email-taken');
  93. if (error) {
  94. messageElement.show();
  95. } else {
  96. messageElement.hide();
  97. Popup.back();
  98. }
  99. });
  100. } else Popup.back();
  101. },
  102. 'click #deleteButton'() {
  103. Users.remove(Meteor.userId());
  104. Popup.close();
  105. AccountsTemplates.logout();
  106. },
  107. });
  108. // XXX For some reason the useraccounts autofocus isnt working in this case.
  109. // See https://github.com/meteor-useraccounts/core/issues/384
  110. Template.changePasswordPopup.onRendered(function() {
  111. this.find('#at-field-current_password').focus();
  112. });
  113. Template.changeLanguagePopup.helpers({
  114. languages() {
  115. return _.map(TAPi18n.getLanguages(), (lang, code) => {
  116. // Same code in /client/components/main/layouts.js
  117. // TODO : Make code reusable
  118. const tag = code;
  119. let name = lang.name;
  120. if (lang.name === 'br') {
  121. name = 'Brezhoneg';
  122. } else if (lang.name === 'ig') {
  123. name = 'Igbo';
  124. } else if (lang.name === 'oc') {
  125. name = 'Occitan';
  126. }
  127. return { tag, name };
  128. }).sort(function(a, b) {
  129. if (a.name === b.name) {
  130. return 0;
  131. } else {
  132. return a.name > b.name ? 1 : -1;
  133. }
  134. });
  135. },
  136. isCurrentLanguage() {
  137. return this.tag === TAPi18n.getLanguage();
  138. },
  139. });
  140. Template.changeLanguagePopup.events({
  141. 'click .js-set-language'(event) {
  142. Users.update(Meteor.userId(), {
  143. $set: {
  144. 'profile.language': this.tag,
  145. },
  146. });
  147. event.preventDefault();
  148. },
  149. });
  150. Template.changeSettingsPopup.helpers({
  151. hiddenSystemMessages() {
  152. return Meteor.user().hasHiddenSystemMessages();
  153. },
  154. showCardsCountAt() {
  155. return Meteor.user().getLimitToShowCardsCount();
  156. },
  157. });
  158. Template.changeSettingsPopup.events({
  159. 'click .js-toggle-system-messages'() {
  160. Meteor.call('toggleSystemMessages');
  161. },
  162. 'click .js-apply-show-cards-at'(event, templateInstance) {
  163. event.preventDefault();
  164. const minLimit = parseInt(
  165. templateInstance.$('#show-cards-count-at').val(),
  166. 10,
  167. );
  168. if (!isNaN(minLimit)) {
  169. Meteor.call('changeLimitToShowCardsCount', minLimit);
  170. Popup.back();
  171. }
  172. },
  173. });