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. 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. // Same code in /client/components/main/layouts.js
  110. // TODO : Make code reusable
  111. const tag = code;
  112. let name = lang.name;
  113. if (lang.name === 'br') {
  114. name = 'Brezhoneg';
  115. } else if (lang.name === 'ig') {
  116. name = 'Igbo';
  117. }
  118. return { tag, name };
  119. }).sort(function (a, b) {
  120. if (a.name === b.name) {
  121. return 0;
  122. } else {
  123. return a.name > b.name ? 1 : -1;
  124. }
  125. });
  126. },
  127. isCurrentLanguage() {
  128. return this.tag === TAPi18n.getLanguage();
  129. },
  130. });
  131. Template.changeLanguagePopup.events({
  132. 'click .js-set-language'(evt) {
  133. Users.update(Meteor.userId(), {
  134. $set: {
  135. 'profile.language': this.tag,
  136. },
  137. });
  138. evt.preventDefault();
  139. },
  140. });
  141. Template.changeSettingsPopup.helpers({
  142. hiddenSystemMessages() {
  143. return Meteor.user().hasHiddenSystemMessages();
  144. },
  145. showCardsCountAt() {
  146. return Meteor.user().getLimitToShowCardsCount();
  147. },
  148. });
  149. Template.changeSettingsPopup.events({
  150. 'click .js-toggle-system-messages'() {
  151. Meteor.call('toggleSystemMessages');
  152. },
  153. 'click .js-apply-show-cards-at'(evt, tpl) {
  154. evt.preventDefault();
  155. const minLimit = parseInt(tpl.$('#show-cards-count-at').val(), 10);
  156. if (!isNaN(minLimit)) {
  157. Meteor.call('changeLimitToShowCardsCount', minLimit);
  158. Popup.back();
  159. }
  160. },
  161. });