userAvatar.js 6.8 KB

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