userAvatar.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. Template.userAvatar.helpers({
  2. userData() {
  3. // We need to handle a special case for the search results provided by the
  4. // `matteodem:easy-search` package. Since these results gets published in a
  5. // separate collection, and not in the standard Meteor.Users collection as
  6. // expected, we use a component parameter ("property") to distinguish the
  7. // two cases.
  8. const userCollection = this.esSearch ? ESSearchResults : Users;
  9. return userCollection.findOne(this.userId, {
  10. fields: {
  11. profile: 1,
  12. username: 1,
  13. },
  14. });
  15. },
  16. memberType() {
  17. const user = Users.findOne(this.userId);
  18. return user && user.isBoardAdmin() ? 'admin' : 'normal';
  19. },
  20. presenceStatusClassName() {
  21. const user = Users.findOne(this.userId);
  22. const userPresence = presences.findOne({ userId: this.userId });
  23. if (user && user.isInvitedTo(Session.get('currentBoard'))) return 'pending';
  24. else if (!userPresence) return 'disconnected';
  25. else if (Session.equals('currentBoard', userPresence.state.currentBoardId))
  26. return 'active';
  27. else return 'idle';
  28. },
  29. });
  30. Template.userAvatar.events({
  31. 'click .js-change-avatar': Popup.open('changeAvatar'),
  32. });
  33. Template.userAvatarInitials.helpers({
  34. initials() {
  35. const user = Users.findOne(this.userId);
  36. return user && user.getInitials();
  37. },
  38. viewPortWidth() {
  39. const user = Users.findOne(this.userId);
  40. return ((user && user.getInitials().length) || 1) * 12;
  41. },
  42. });
  43. BlazeComponent.extendComponent({
  44. onCreated() {
  45. this.error = new ReactiveVar('');
  46. Meteor.subscribe('my-avatars');
  47. },
  48. avatarUrlOptions() {
  49. return {
  50. auth: false,
  51. brokenIsFine: true,
  52. };
  53. },
  54. uploadedAvatars() {
  55. return Avatars.find({ userId: Meteor.userId() });
  56. },
  57. isSelected() {
  58. const userProfile = Meteor.user().profile;
  59. const avatarUrl = userProfile && userProfile.avatarUrl;
  60. const currentAvatarUrl = this.currentData().url(this.avatarUrlOptions());
  61. return avatarUrl === currentAvatarUrl;
  62. },
  63. noAvatarUrl() {
  64. const userProfile = Meteor.user().profile;
  65. const avatarUrl = userProfile && userProfile.avatarUrl;
  66. return !avatarUrl;
  67. },
  68. setAvatar(avatarUrl) {
  69. Meteor.user().setAvatarUrl(avatarUrl);
  70. },
  71. setError(error) {
  72. this.error.set(error);
  73. },
  74. events() {
  75. return [
  76. {
  77. 'click .js-upload-avatar'() {
  78. this.$('.js-upload-avatar-input').click();
  79. },
  80. 'change .js-upload-avatar-input'(event) {
  81. let file, fileUrl;
  82. FS.Utility.eachFile(event, f => {
  83. try {
  84. file = Avatars.insert(new FS.File(f));
  85. fileUrl = file.url(this.avatarUrlOptions());
  86. } catch (e) {
  87. this.setError('avatar-too-big');
  88. }
  89. });
  90. if (fileUrl) {
  91. this.setError('');
  92. const fetchAvatarInterval = window.setInterval(() => {
  93. $.ajax({
  94. url: fileUrl,
  95. success: () => {
  96. this.setAvatar(file.url(this.avatarUrlOptions()));
  97. window.clearInterval(fetchAvatarInterval);
  98. },
  99. });
  100. }, 100);
  101. }
  102. },
  103. 'click .js-select-avatar'() {
  104. const avatarUrl = this.currentData().url(this.avatarUrlOptions());
  105. this.setAvatar(avatarUrl);
  106. },
  107. 'click .js-select-initials'() {
  108. this.setAvatar('');
  109. },
  110. 'click .js-delete-avatar'() {
  111. Avatars.remove(this.currentData()._id);
  112. },
  113. },
  114. ];
  115. },
  116. }).register('changeAvatarPopup');
  117. Template.cardMembersPopup.helpers({
  118. isCardMember() {
  119. const card = Template.parentData();
  120. const cardMembers = card.getMembers();
  121. return _.contains(cardMembers, this.userId);
  122. },
  123. user() {
  124. return Users.findOne(this.userId);
  125. },
  126. });
  127. Template.cardMembersPopup.events({
  128. 'click .js-select-member'(event) {
  129. const card = Cards.findOne(Session.get('currentCard'));
  130. const memberId = this.userId;
  131. card.toggleMember(memberId);
  132. event.preventDefault();
  133. },
  134. });
  135. Template.cardMemberPopup.helpers({
  136. user() {
  137. return Users.findOne(this.userId);
  138. },
  139. });
  140. Template.cardMemberPopup.events({
  141. 'click .js-remove-member'() {
  142. Cards.findOne(this.cardId).unassignMember(this.userId);
  143. Popup.close();
  144. },
  145. 'click .js-edit-profile': Popup.open('editProfile'),
  146. });