userAvatar.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. fixAvatarUrl(avatarUrl) {
  17. // Remove suburl from beginning of avatar file path,
  18. // so that avatar images don't get broken when root-url changes to different sub-url.
  19. avatarUrl = `/${ avatarUrl.substring(avatarUrl.indexOf('/cfs/files/avatars/')+1)}`;
  20. return avatarUrl;
  21. },
  22. memberType() {
  23. const user = Users.findOne(this.userId);
  24. return user && user.isBoardAdmin() ? 'admin' : 'normal';
  25. },
  26. presenceStatusClassName() {
  27. const user = Users.findOne(this.userId);
  28. const userPresence = presences.findOne({ userId: this.userId });
  29. if (user && user.isInvitedTo(Session.get('currentBoard')))
  30. return 'pending';
  31. else if (!userPresence)
  32. return 'disconnected';
  33. else if (Session.equals('currentBoard', userPresence.state.currentBoardId))
  34. return 'active';
  35. else
  36. return 'idle';
  37. },
  38. });
  39. Template.userAvatar.events({
  40. 'click .js-change-avatar': Popup.open('changeAvatar'),
  41. });
  42. Template.userAvatarInitials.helpers({
  43. initials() {
  44. const user = Users.findOne(this.userId);
  45. return user && user.getInitials();
  46. },
  47. viewPortWidth() {
  48. const user = Users.findOne(this.userId);
  49. return (user && user.getInitials().length || 1) * 12;
  50. },
  51. });
  52. BlazeComponent.extendComponent({
  53. onCreated() {
  54. this.error = new ReactiveVar('');
  55. Meteor.subscribe('my-avatars');
  56. },
  57. avatarUrlOptions() {
  58. return {
  59. auth: false,
  60. brokenIsFine: true,
  61. };
  62. },
  63. uploadedAvatars() {
  64. return Avatars.find({userId: Meteor.userId()});
  65. },
  66. isSelected() {
  67. const userProfile = Meteor.user().profile;
  68. const avatarUrl = userProfile && userProfile.avatarUrl;
  69. const currentAvatarUrl = this.currentData().url(this.avatarUrlOptions());
  70. return avatarUrl === currentAvatarUrl;
  71. },
  72. noAvatarUrl() {
  73. const userProfile = Meteor.user().profile;
  74. const avatarUrl = userProfile && userProfile.avatarUrl;
  75. return !avatarUrl;
  76. },
  77. setAvatar(avatarUrl) {
  78. Meteor.user().setAvatarUrl(avatarUrl);
  79. },
  80. setError(error) {
  81. this.error.set(error);
  82. },
  83. events() {
  84. return [{
  85. 'click .js-upload-avatar'() {
  86. this.$('.js-upload-avatar-input').click();
  87. },
  88. 'change .js-upload-avatar-input'(evt) {
  89. let file, fileUrl;
  90. FS.Utility.eachFile(evt, (f) => {
  91. try {
  92. file = Avatars.insert(new FS.File(f));
  93. fileUrl = file.url(this.avatarUrlOptions());
  94. } catch (e) {
  95. this.setError('avatar-too-big');
  96. }
  97. });
  98. if (fileUrl) {
  99. this.setError('');
  100. const fetchAvatarInterval = window.setInterval(() => {
  101. $.ajax({
  102. url: fileUrl,
  103. success: () => {
  104. this.setAvatar(file.url(this.avatarUrlOptions()));
  105. window.clearInterval(fetchAvatarInterval);
  106. },
  107. });
  108. }, 100);
  109. }
  110. },
  111. 'click .js-select-avatar'() {
  112. const avatarUrl = this.currentData().url(this.avatarUrlOptions());
  113. this.setAvatar(avatarUrl);
  114. },
  115. 'click .js-select-initials'() {
  116. this.setAvatar('');
  117. },
  118. 'click .js-delete-avatar'() {
  119. Avatars.remove(this.currentData()._id);
  120. },
  121. }];
  122. },
  123. }).register('changeAvatarPopup');
  124. Template.cardMembersPopup.helpers({
  125. isCardMember() {
  126. const card = Template.parentData();
  127. const cardMembers = card.getMembers();
  128. return _.contains(cardMembers, this.userId);
  129. },
  130. user() {
  131. return Users.findOne(this.userId);
  132. },
  133. });
  134. Template.cardMembersPopup.events({
  135. 'click .js-select-member'(evt) {
  136. const card = Cards.findOne(Session.get('currentCard'));
  137. const memberId = this.userId;
  138. card.toggleMember(memberId);
  139. evt.preventDefault();
  140. },
  141. });
  142. Template.cardMemberPopup.helpers({
  143. user() {
  144. return Users.findOne(this.userId);
  145. },
  146. });
  147. Template.cardMemberPopup.events({
  148. 'click .js-remove-member'() {
  149. Cards.findOne(this.cardId).unassignMember(this.userId);
  150. Popup.close();
  151. },
  152. 'click .js-edit-profile': Popup.open('editProfile'),
  153. });