userAvatar.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. let file, fileUrl;
  190. FS.Utility.eachFile(event, f => {
  191. try {
  192. file = Avatars.insert(new FS.File(f));
  193. fileUrl = file.url(this.avatarUrlOptions());
  194. } catch (e) {
  195. this.setError('avatar-too-big');
  196. }
  197. });
  198. if (fileUrl) {
  199. this.setError('');
  200. const fetchAvatarInterval = window.setInterval(() => {
  201. $.ajax({
  202. url: fileUrl,
  203. success: () => {
  204. this.setAvatar(file.url(this.avatarUrlOptions()));
  205. window.clearInterval(fetchAvatarInterval);
  206. },
  207. });
  208. }, 100);
  209. }
  210. },
  211. 'click .js-select-avatar'() {
  212. const avatarUrl = this.currentData().url(this.avatarUrlOptions());
  213. this.setAvatar(avatarUrl);
  214. },
  215. 'click .js-select-initials'() {
  216. this.setAvatar('');
  217. },
  218. 'click .js-delete-avatar'() {
  219. Avatars.remove(this.currentData()._id);
  220. },
  221. },
  222. ];
  223. },
  224. }).register('changeAvatarPopup');
  225. Template.cardMembersPopup.helpers({
  226. isCardMember() {
  227. const card = Template.parentData();
  228. const cardMembers = card.getMembers();
  229. return _.contains(cardMembers, this.userId);
  230. },
  231. user() {
  232. return Users.findOne(this.userId);
  233. },
  234. });
  235. Template.cardMembersPopup.events({
  236. 'click .js-select-member'(event) {
  237. const card = Utils.getCurrentCard();
  238. const memberId = this.userId;
  239. card.toggleMember(memberId);
  240. event.preventDefault();
  241. },
  242. });
  243. Template.cardMemberPopup.helpers({
  244. user() {
  245. return Users.findOne(this.userId);
  246. },
  247. });
  248. Template.cardMemberPopup.events({
  249. 'click .js-remove-member'() {
  250. Cards.findOne(this.cardId).unassignMember(this.userId);
  251. Popup.back();
  252. },
  253. 'click .js-edit-profile': Popup.open('editProfile'),
  254. });