userAvatar.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. import Cards from '/models/cards';
  2. import Avatars from '/models/avatars';
  3. import Users from '/models/users';
  4. import Org from '/models/org';
  5. import Team from '/models/team';
  6. Template.userAvatar.helpers({
  7. userData() {
  8. // We need to handle a special case for the search results provided by the
  9. // `matteodem:easy-search` package. Since these results gets published in a
  10. // separate collection, and not in the standard Meteor.Users collection as
  11. // expected, we use a component parameter ("property") to distinguish the
  12. // two cases.
  13. const userCollection = this.esSearch ? ESSearchResults : Users;
  14. return userCollection.findOne(this.userId, {
  15. fields: {
  16. profile: 1,
  17. username: 1,
  18. },
  19. });
  20. },
  21. memberType() {
  22. const user = Users.findOne(this.userId);
  23. return user && user.isBoardAdmin() ? 'admin' : 'normal';
  24. },
  25. presenceStatusClassName() {
  26. const user = Users.findOne(this.userId);
  27. const userPresence = presences.findOne({ userId: this.userId });
  28. if (user && user.isInvitedTo(Session.get('currentBoard'))) return 'pending';
  29. else if (!userPresence) return 'disconnected';
  30. else if (Session.equals('currentBoard', userPresence.state.currentBoardId))
  31. return 'active';
  32. else return 'idle';
  33. },
  34. });
  35. Template.userAvatarInitials.helpers({
  36. initials() {
  37. const user = Users.findOne(this.userId);
  38. return user && user.getInitials();
  39. },
  40. viewPortWidth() {
  41. const user = Users.findOne(this.userId);
  42. return ((user && user.getInitials().length) || 1) * 12;
  43. },
  44. });
  45. BlazeComponent.extendComponent({
  46. onCreated() {
  47. this.error = new ReactiveVar('');
  48. this.loading = new ReactiveVar(false);
  49. this.findOrgsOptions = new ReactiveVar({});
  50. this.page = new ReactiveVar(1);
  51. this.autorun(() => {
  52. const limitOrgs = this.page.get() * Number.MAX_SAFE_INTEGER;
  53. this.subscribe('org', this.findOrgsOptions.get(), limitOrgs, () => {});
  54. });
  55. },
  56. onRendered() {
  57. this.setLoading(false);
  58. },
  59. setError(error) {
  60. this.error.set(error);
  61. },
  62. setLoading(w) {
  63. this.loading.set(w);
  64. },
  65. isLoading() {
  66. return this.loading.get();
  67. },
  68. events() {
  69. return [
  70. {
  71. 'keyup input'() {
  72. this.setError('');
  73. },
  74. 'click .js-manage-board-removeOrg': Popup.open('removeBoardOrg'),
  75. },
  76. ];
  77. },
  78. }).register('boardOrgRow');
  79. Template.boardOrgRow.helpers({
  80. orgData() {
  81. const orgCollection = this.esSearch ? ESSearchResults : Org;
  82. return orgCollection.findOne(this.orgId);
  83. },
  84. currentUser(){
  85. return Meteor.user();
  86. },
  87. });
  88. Template.boardOrgName.helpers({
  89. orgName() {
  90. const org = Org.findOne(this.orgId);
  91. return org && org.orgDisplayName;
  92. },
  93. orgViewPortWidth() {
  94. const org = Org.findOne(this.orgId);
  95. return ((org && org.orgDisplayName.length) || 1) * 12;
  96. },
  97. });
  98. BlazeComponent.extendComponent({
  99. onCreated() {
  100. this.error = new ReactiveVar('');
  101. this.loading = new ReactiveVar(false);
  102. this.findOrgsOptions = new ReactiveVar({});
  103. this.page = new ReactiveVar(1);
  104. this.autorun(() => {
  105. const limitTeams = this.page.get() * Number.MAX_SAFE_INTEGER;
  106. this.subscribe('team', this.findOrgsOptions.get(), limitTeams, () => {});
  107. });
  108. },
  109. onRendered() {
  110. this.setLoading(false);
  111. },
  112. setError(error) {
  113. this.error.set(error);
  114. },
  115. setLoading(w) {
  116. this.loading.set(w);
  117. },
  118. isLoading() {
  119. return this.loading.get();
  120. },
  121. events() {
  122. return [
  123. {
  124. 'keyup input'() {
  125. this.setError('');
  126. },
  127. 'click .js-manage-board-removeTeam': Popup.open('removeBoardTeam'),
  128. },
  129. ];
  130. },
  131. }).register('boardTeamRow');
  132. Template.boardTeamRow.helpers({
  133. teamData() {
  134. const teamCollection = this.esSearch ? ESSearchResults : Team;
  135. return teamCollection.findOne(this.teamId);
  136. },
  137. currentUser(){
  138. return Meteor.user();
  139. },
  140. });
  141. Template.boardTeamName.helpers({
  142. teamName() {
  143. const team = Team.findOne(this.teamId);
  144. return team && team.teamDisplayName;
  145. },
  146. teamViewPortWidth() {
  147. const team = Team.findOne(this.teamId);
  148. return ((team && team.teamDisplayName.length) || 1) * 12;
  149. },
  150. });
  151. BlazeComponent.extendComponent({
  152. onCreated() {
  153. this.error = new ReactiveVar('');
  154. Meteor.subscribe('my-avatars');
  155. },
  156. avatarUrlOptions() {
  157. return {
  158. auth: false,
  159. brokenIsFine: true,
  160. };
  161. },
  162. uploadedAvatars() {
  163. return Avatars.find({ userId: Meteor.userId() });
  164. },
  165. isSelected() {
  166. const userProfile = Meteor.user().profile;
  167. const avatarUrl = userProfile && userProfile.avatarUrl;
  168. const currentAvatarUrl = this.currentData().url(this.avatarUrlOptions());
  169. return avatarUrl === currentAvatarUrl;
  170. },
  171. noAvatarUrl() {
  172. const userProfile = Meteor.user().profile;
  173. const avatarUrl = userProfile && userProfile.avatarUrl;
  174. return !avatarUrl;
  175. },
  176. setAvatar(avatarUrl) {
  177. Meteor.user().setAvatarUrl(avatarUrl);
  178. },
  179. setError(error) {
  180. this.error.set(error);
  181. },
  182. events() {
  183. return [
  184. {
  185. 'click .js-upload-avatar'() {
  186. this.$('.js-upload-avatar-input').click();
  187. },
  188. 'change .js-upload-avatar-input'(event) {
  189. const self = this;
  190. if (event.currentTarget.files && event.currentTarget.files[0]) {
  191. const uploader = Avatars.insert(
  192. {
  193. file: event.currentTarget.files[0],
  194. chunkSize: 'dynamic',
  195. },
  196. false,
  197. );
  198. uploader.on('uploaded', (error, fileRef) => {
  199. if (!error) {
  200. self.setAvatar(fileRef.path);
  201. // self.setAvatar(this.currentData().url(this.avatarUrlOptions()));
  202. }
  203. });
  204. uploader.on('error', (error, fileData) => {
  205. self.setError(error.reason);
  206. });
  207. uploader.start();
  208. }
  209. },
  210. 'click .js-select-avatar'() {
  211. const avatarUrl = this.currentData().url(this.avatarUrlOptions());
  212. this.setAvatar(avatarUrl);
  213. },
  214. 'click .js-select-initials'() {
  215. this.setAvatar('');
  216. },
  217. 'click .js-delete-avatar'() {
  218. Avatars.remove(this.currentData()._id);
  219. },
  220. },
  221. ];
  222. },
  223. }).register('changeAvatarPopup');
  224. Template.cardMembersPopup.helpers({
  225. isCardMember() {
  226. const card = Template.parentData();
  227. const cardMembers = card.getMembers();
  228. return _.contains(cardMembers, this.userId);
  229. },
  230. user() {
  231. return Users.findOne(this.userId);
  232. },
  233. });
  234. Template.cardMembersPopup.events({
  235. 'click .js-select-member'(event) {
  236. const card = Utils.getCurrentCard();
  237. const memberId = this.userId;
  238. card.toggleMember(memberId);
  239. event.preventDefault();
  240. },
  241. });
  242. Template.cardMemberPopup.helpers({
  243. user() {
  244. return Users.findOne(this.userId);
  245. },
  246. });
  247. Template.cardMemberPopup.events({
  248. 'click .js-remove-member'() {
  249. Cards.findOne(this.cardId).unassignMember(this.userId);
  250. Popup.back();
  251. },
  252. 'click .js-edit-profile': Popup.open('editProfile'),
  253. });