userHeader.js 4.9 KB

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