userAvatar.js 4.2 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')))
  24. return 'pending';
  25. else if (!userPresence)
  26. return 'disconnected';
  27. else if (Session.equals('currentBoard', userPresence.state.currentBoardId))
  28. return 'active';
  29. else
  30. return 'idle';
  31. },
  32. });
  33. Template.userAvatar.events({
  34. 'click .js-change-avatar': Popup.open('changeAvatar'),
  35. });
  36. Template.userAvatarInitials.helpers({
  37. initials() {
  38. const user = Users.findOne(this.userId);
  39. return user && user.getInitials();
  40. },
  41. viewPortWidth() {
  42. const user = Users.findOne(this.userId);
  43. return (user && user.getInitials().length || 1) * 12;
  44. },
  45. });
  46. BlazeComponent.extendComponent({
  47. onCreated() {
  48. this.error = new ReactiveVar('');
  49. Meteor.subscribe('my-avatars');
  50. },
  51. avatarUrlOptions() {
  52. return {
  53. auth: false,
  54. brokenIsFine: true,
  55. };
  56. },
  57. uploadedAvatars() {
  58. return Avatars.find({userId: Meteor.userId()});
  59. },
  60. isSelected() {
  61. const userProfile = Meteor.user().profile;
  62. const avatarUrl = userProfile && userProfile.avatarUrl;
  63. const currentAvatarUrl = this.currentData().url(this.avatarUrlOptions());
  64. return avatarUrl === currentAvatarUrl;
  65. },
  66. noAvatarUrl() {
  67. const userProfile = Meteor.user().profile;
  68. const avatarUrl = userProfile && userProfile.avatarUrl;
  69. return !avatarUrl;
  70. },
  71. setAvatar(avatarUrl) {
  72. Meteor.user().setAvatarUrl(avatarUrl);
  73. },
  74. setError(error) {
  75. this.error.set(error);
  76. },
  77. events() {
  78. return [{
  79. 'click .js-upload-avatar'() {
  80. this.$('.js-upload-avatar-input').click();
  81. },
  82. 'change .js-upload-avatar-input'(evt) {
  83. let file, fileUrl;
  84. FS.Utility.eachFile(evt, (f) => {
  85. try {
  86. file = Avatars.insert(new FS.File(f));
  87. fileUrl = file.url(this.avatarUrlOptions());
  88. } catch (e) {
  89. this.setError('avatar-too-big');
  90. }
  91. });
  92. if (fileUrl) {
  93. this.setError('');
  94. const fetchAvatarInterval = window.setInterval(() => {
  95. $.ajax({
  96. url: fileUrl,
  97. success: () => {
  98. this.setAvatar(file.url(this.avatarUrlOptions()));
  99. window.clearInterval(fetchAvatarInterval);
  100. },
  101. });
  102. }, 100);
  103. }
  104. },
  105. 'click .js-select-avatar'() {
  106. const avatarUrl = this.currentData().url(this.avatarUrlOptions());
  107. this.setAvatar(avatarUrl);
  108. },
  109. 'click .js-select-initials'() {
  110. this.setAvatar('');
  111. },
  112. 'click .js-delete-avatar'() {
  113. Avatars.remove(this.currentData()._id);
  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'(evt) {
  130. const card = Cards.findOne(Session.get('currentCard'));
  131. const memberId = this.userId;
  132. card.toggleMember(memberId);
  133. evt.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. });