userHeader.js 5.1 KB

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