2
0

userAvatar.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. Meteor.subscribe('my-avatars');
  2. Template.userAvatar.helpers({
  3. userData() {
  4. // We need to handle a special case for the search results provided by the
  5. // `matteodem:easy-search` package. Since these results gets published in a
  6. // separate collection, and not in the standard Meteor.Users collection as
  7. // expected, we use a component parameter ("property") to distinguish the
  8. // two cases.
  9. const userCollection = this.esSearch ? ESSearchResults : Users;
  10. return userCollection.findOne(this.userId, {
  11. fields: {
  12. profile: 1,
  13. username: 1,
  14. },
  15. });
  16. },
  17. memberType() {
  18. const user = Users.findOne(this.userId);
  19. return user && user.isBoardAdmin() ? 'admin' : 'normal';
  20. },
  21. presenceStatusClassName() {
  22. const user = Users.findOne(this.userId);
  23. const userPresence = presences.findOne({ userId: this.userId });
  24. if (user && user.isInvitedTo(Session.get('currentBoard')))
  25. return 'pending';
  26. else if (!userPresence)
  27. return 'disconnected';
  28. else if (Session.equals('currentBoard', userPresence.state.currentBoardId))
  29. return 'active';
  30. else
  31. return 'idle';
  32. },
  33. });
  34. Template.userAvatar.events({
  35. 'click .js-change-avatar': Popup.open('changeAvatar'),
  36. });
  37. Template.userAvatarInitials.helpers({
  38. initials() {
  39. const user = Users.findOne(this.userId);
  40. return user && user.getInitials();
  41. },
  42. viewPortWidth() {
  43. const user = Users.findOne(this.userId);
  44. return (user && user.getInitials().length || 1) * 12;
  45. },
  46. });
  47. BlazeComponent.extendComponent({
  48. onCreated() {
  49. this.error = new ReactiveVar('');
  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 cardId = Template.parentData()._id;
  121. const cardMembers = Cards.findOne(cardId).members || [];
  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. });