userHeader.js 4.9 KB

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