userAvatar.js 4.1 KB

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