userAvatar.js 7.0 KB

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