userHeader.js 5.2 KB

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