userAvatar.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. 'click .js-member': Popup.open('cardMember'),
  33. });
  34. Template.userAvatarInitials.helpers({
  35. initials() {
  36. const user = Users.findOne(this.userId);
  37. return user && user.getInitials();
  38. },
  39. viewPortWidth() {
  40. const user = Users.findOne(this.userId);
  41. return ((user && user.getInitials().length) || 1) * 12;
  42. },
  43. });
  44. BlazeComponent.extendComponent({
  45. onCreated() {
  46. this.error = new ReactiveVar('');
  47. Meteor.subscribe('my-avatars');
  48. },
  49. avatarUrlOptions() {
  50. return {
  51. auth: false,
  52. brokenIsFine: true,
  53. };
  54. },
  55. uploadedAvatars() {
  56. return Avatars.find({ userId: Meteor.userId() });
  57. },
  58. isSelected() {
  59. const userProfile = Meteor.user().profile;
  60. const avatarUrl = userProfile && userProfile.avatarUrl;
  61. const currentAvatarUrl = this.currentData().url(this.avatarUrlOptions());
  62. return avatarUrl === currentAvatarUrl;
  63. },
  64. noAvatarUrl() {
  65. const userProfile = Meteor.user().profile;
  66. const avatarUrl = userProfile && userProfile.avatarUrl;
  67. return !avatarUrl;
  68. },
  69. setAvatar(avatarUrl) {
  70. Meteor.user().setAvatarUrl(avatarUrl);
  71. },
  72. setError(error) {
  73. this.error.set(error);
  74. },
  75. events() {
  76. return [
  77. {
  78. 'click .js-upload-avatar'() {
  79. this.$('.js-upload-avatar-input').click();
  80. },
  81. 'change .js-upload-avatar-input'(event) {
  82. let file, fileUrl;
  83. FS.Utility.eachFile(event, f => {
  84. try {
  85. file = Avatars.insert(new FS.File(f));
  86. fileUrl = file.url(this.avatarUrlOptions());
  87. } catch (e) {
  88. this.setError('avatar-too-big');
  89. }
  90. });
  91. if (fileUrl) {
  92. this.setError('');
  93. const fetchAvatarInterval = window.setInterval(() => {
  94. $.ajax({
  95. url: fileUrl,
  96. success: () => {
  97. this.setAvatar(file.url(this.avatarUrlOptions()));
  98. window.clearInterval(fetchAvatarInterval);
  99. },
  100. });
  101. }, 100);
  102. }
  103. },
  104. 'click .js-select-avatar'() {
  105. const avatarUrl = this.currentData().url(this.avatarUrlOptions());
  106. this.setAvatar(avatarUrl);
  107. },
  108. 'click .js-select-initials'() {
  109. this.setAvatar('');
  110. },
  111. 'click .js-delete-avatar'() {
  112. Avatars.remove(this.currentData()._id);
  113. },
  114. },
  115. ];
  116. },
  117. }).register('changeAvatarPopup');
  118. Template.cardMembersPopup.helpers({
  119. isCardMember() {
  120. const card = Template.parentData();
  121. const cardMembers = card.getMembers();
  122. return _.contains(cardMembers, this.userId);
  123. },
  124. user() {
  125. return Users.findOne(this.userId);
  126. },
  127. });
  128. Template.cardMembersPopup.events({
  129. 'click .js-select-member'(event) {
  130. const card = Cards.findOne(Session.get('currentCard'));
  131. const memberId = this.userId;
  132. card.toggleMember(memberId);
  133. event.preventDefault();
  134. },
  135. });
  136. Template.cardMemberPopup.helpers({
  137. user() {
  138. return Users.findOne(this.userId);
  139. },
  140. });
  141. Template.cardMemberPopup.events({
  142. 'click .js-remove-member'() {
  143. Cards.findOne(this.cardId).unassignMember(this.userId);
  144. Popup.close();
  145. },
  146. 'click .js-edit-profile': Popup.open('editProfile'),
  147. });