userAvatar.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 userPresence = presences.findOne({ userId: this.userId });
  23. if (!userPresence)
  24. return 'disconnected';
  25. else if (Session.equals('currentBoard', userPresence.state.currentBoardId))
  26. return 'active';
  27. else
  28. return 'idle';
  29. },
  30. });
  31. Template.userAvatar.events({
  32. 'click .js-change-avatar': Popup.open('changeAvatar'),
  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. template() {
  46. return 'changeAvatarPopup';
  47. },
  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.users.update(Meteor.userId(), {
  73. $set: {
  74. 'profile.avatarUrl': avatarUrl,
  75. },
  76. });
  77. },
  78. setError(error) {
  79. this.error.set(error);
  80. },
  81. events() {
  82. return [{
  83. 'click .js-upload-avatar'() {
  84. this.$('.js-upload-avatar-input').click();
  85. },
  86. 'change .js-upload-avatar-input'(evt) {
  87. let file, fileUrl;
  88. FS.Utility.eachFile(evt, (f) => {
  89. try {
  90. file = Avatars.insert(new FS.File(f));
  91. fileUrl = file.url(this.avatarUrlOptions());
  92. } catch (e) {
  93. this.setError('avatar-too-big');
  94. }
  95. });
  96. if (fileUrl) {
  97. this.setError('');
  98. const fetchAvatarInterval = window.setInterval(() => {
  99. $.ajax({
  100. url: fileUrl,
  101. success: () => {
  102. this.setAvatar(file.url(this.avatarUrlOptions()));
  103. window.clearInterval(fetchAvatarInterval);
  104. },
  105. });
  106. }, 100);
  107. }
  108. },
  109. 'click .js-select-avatar'() {
  110. const avatarUrl = this.currentData().url(this.avatarUrlOptions());
  111. this.setAvatar(avatarUrl);
  112. },
  113. 'click .js-select-initials'() {
  114. this.setAvatar('');
  115. },
  116. 'click .js-delete-avatar'() {
  117. Avatars.remove(this.currentData()._id);
  118. },
  119. }];
  120. },
  121. }).register('changeAvatarPopup');
  122. Template.cardMembersPopup.helpers({
  123. isCardMember() {
  124. const cardId = Template.parentData()._id;
  125. const cardMembers = Cards.findOne(cardId).members || [];
  126. return _.contains(cardMembers, this.userId);
  127. },
  128. user() {
  129. return Users.findOne(this.userId);
  130. },
  131. });
  132. Template.cardMembersPopup.events({
  133. 'click .js-select-member'(evt) {
  134. const cardId = Template.parentData(2).data._id;
  135. const memberId = this.userId;
  136. let operation;
  137. if (Cards.find({ _id: cardId, members: memberId}).count() === 0)
  138. operation = '$addToSet';
  139. else
  140. operation = '$pull';
  141. Cards.update(cardId, {
  142. [operation]: {
  143. members: memberId,
  144. },
  145. });
  146. evt.preventDefault();
  147. },
  148. });
  149. Template.cardMemberPopup.helpers({
  150. user() {
  151. return Users.findOne(this.userId);
  152. },
  153. });
  154. Template.cardMemberPopup.events({
  155. 'click .js-remove-member'() {
  156. Cards.update(this.cardId, {$pull: {members: this.userId}});
  157. Popup.close();
  158. },
  159. 'click .js-edit-profile': Popup.open('editProfile'),
  160. });