activities.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. import DOMPurify from 'dompurify';
  2. const activitiesPerPage = 500;
  3. BlazeComponent.extendComponent({
  4. onCreated() {
  5. // XXX Should we use ReactiveNumber?
  6. this.page = new ReactiveVar(1);
  7. this.loadNextPageLocked = false;
  8. // TODO is sidebar always available? E.g. on small screens/mobile devices
  9. const sidebar = Sidebar;
  10. sidebar && sidebar.callFirstWith(null, 'resetNextPeak');
  11. this.autorun(() => {
  12. let mode = this.data().mode;
  13. const capitalizedMode = Utils.capitalize(mode);
  14. let thisId, searchId;
  15. if (mode === 'linkedcard' || mode === 'linkedboard') {
  16. thisId = Utils.getCurrentCardId();
  17. searchId = Cards.findOne({ _id: thisId }).linkedId;
  18. mode = mode.replace('linked', '');
  19. } else {
  20. thisId = Session.get(`current${capitalizedMode}`);
  21. searchId = thisId;
  22. }
  23. const limit = this.page.get() * activitiesPerPage;
  24. const user = Meteor.user();
  25. const hideSystem = user ? user.hasHiddenSystemMessages() : false;
  26. if (searchId === null) return;
  27. this.subscribe('activities', mode, searchId, limit, hideSystem, () => {
  28. this.loadNextPageLocked = false;
  29. // TODO the guard can be removed as soon as the TODO above is resolved
  30. if (!sidebar) return;
  31. // If the sibear peak hasn't increased, that mean that there are no more
  32. // activities, and we can stop calling new subscriptions.
  33. // XXX This is hacky! We need to know excatly and reactively how many
  34. // activities there are, we probably want to denormalize this number
  35. // dirrectly into card and board documents.
  36. const nextPeakBefore = sidebar.callFirstWith(null, 'getNextPeak');
  37. sidebar.calculateNextPeak();
  38. const nextPeakAfter = sidebar.callFirstWith(null, 'getNextPeak');
  39. if (nextPeakBefore === nextPeakAfter) {
  40. sidebar.callFirstWith(null, 'resetNextPeak');
  41. }
  42. });
  43. });
  44. },
  45. loadNextPage() {
  46. if (this.loadNextPageLocked === false) {
  47. this.page.set(this.page.get() + 1);
  48. this.loadNextPageLocked = true;
  49. }
  50. },
  51. }).register('activities');
  52. Template.activities.helpers({
  53. activities() {
  54. const ret = this.card.activities();
  55. return ret;
  56. },
  57. });
  58. BlazeComponent.extendComponent({
  59. checkItem() {
  60. const checkItemId = this.currentData().activity.checklistItemId;
  61. const checkItem = ChecklistItems.findOne({ _id: checkItemId });
  62. return checkItem && checkItem.title;
  63. },
  64. boardLabelLink() {
  65. const data = this.currentData();
  66. if (data.mode !== 'board') {
  67. return createBoardLink(data.activity.board(), data.activity.listName);
  68. }
  69. return TAPi18n.__('this-board');
  70. },
  71. cardLabelLink() {
  72. const data = this.currentData();
  73. if (data.mode !== 'card') {
  74. return createCardLink(data.activity.card());
  75. }
  76. return TAPi18n.__('this-card');
  77. },
  78. cardLink() {
  79. return createCardLink(this.currentData().activity.card());
  80. },
  81. receivedDate() {
  82. const receivedDate = this.currentData().activity.card();
  83. if (!receivedDate) return null;
  84. return receivedDate.receivedAt;
  85. },
  86. startDate() {
  87. const startDate = this.currentData().activity.card();
  88. if (!startDate) return null;
  89. return startDate.startAt;
  90. },
  91. dueDate() {
  92. const dueDate = this.currentData().activity.card();
  93. if (!dueDate) return null;
  94. return dueDate.dueAt;
  95. },
  96. endDate() {
  97. const endDate = this.currentData().activity.card();
  98. if (!endDate) return null;
  99. return endDate.endAt;
  100. },
  101. lastLabel() {
  102. const lastLabelId = this.currentData().activity.labelId;
  103. if (!lastLabelId) return null;
  104. const lastLabel = Boards.findOne(
  105. this.currentData().activity.boardId,
  106. ).getLabelById(lastLabelId);
  107. if (lastLabel && (lastLabel.name === undefined || lastLabel.name === '')) {
  108. return lastLabel.color;
  109. } else if (lastLabel.name !== undefined && lastLabel.name !== '') {
  110. return lastLabel.name;
  111. } else {
  112. return null;
  113. }
  114. },
  115. lastCustomField() {
  116. const lastCustomField = CustomFields.findOne(
  117. this.currentData().activity.customFieldId,
  118. );
  119. if (!lastCustomField) return null;
  120. return lastCustomField.name;
  121. },
  122. lastCustomFieldValue() {
  123. const lastCustomField = CustomFields.findOne(
  124. this.currentData().activity.customFieldId,
  125. );
  126. if (!lastCustomField) return null;
  127. const value = this.currentData().activity.value;
  128. if (
  129. lastCustomField.settings.dropdownItems &&
  130. lastCustomField.settings.dropdownItems.length > 0
  131. ) {
  132. const dropDownValue = _.find(
  133. lastCustomField.settings.dropdownItems,
  134. item => {
  135. return item._id === value;
  136. },
  137. );
  138. if (dropDownValue) return dropDownValue.name;
  139. }
  140. return value;
  141. },
  142. listLabel() {
  143. const activity = this.currentData().activity;
  144. const list = activity.list();
  145. return (list && list.title) || activity.title;
  146. },
  147. sourceLink() {
  148. const source = this.currentData().activity.source;
  149. if (source) {
  150. if (source.url) {
  151. return Blaze.toHTML(
  152. HTML.A(
  153. {
  154. href: source.url,
  155. },
  156. DOMPurify.sanitize(source.system, {
  157. ALLOW_UNKNOWN_PROTOCOLS: true,
  158. }),
  159. ),
  160. );
  161. } else {
  162. return DOMPurify.sanitize(source.system, {
  163. ALLOW_UNKNOWN_PROTOCOLS: true,
  164. });
  165. }
  166. }
  167. return null;
  168. },
  169. memberLink() {
  170. return Blaze.toHTMLWithData(Template.memberName, {
  171. user: this.currentData().activity.member(),
  172. });
  173. },
  174. attachmentLink() {
  175. const attachment = this.currentData().activity.attachment();
  176. // trying to display url before file is stored generates js errors
  177. return (
  178. (attachment &&
  179. attachment.url({ download: true }) &&
  180. Blaze.toHTML(
  181. HTML.A(
  182. {
  183. href: attachment.url({ download: true }),
  184. target: '_blank',
  185. },
  186. DOMPurify.sanitize(attachment.name()),
  187. ),
  188. )) ||
  189. DOMPurify.sanitize(this.currentData().activity.attachmentName)
  190. );
  191. },
  192. customField() {
  193. const customField = this.currentData().activity.customField();
  194. if (!customField) return null;
  195. return customField.name;
  196. },
  197. events() {
  198. return [
  199. {
  200. // XXX We should use Popup.afterConfirmation here
  201. 'click .js-delete-comment': Popup.afterConfirm('deleteComment', () => {
  202. const commentId = this.data().activity.commentId;
  203. CardComments.remove(commentId);
  204. Popup.back();
  205. }),
  206. 'submit .js-edit-comment'(evt) {
  207. evt.preventDefault();
  208. const commentText = this.currentComponent()
  209. .getValue()
  210. .trim();
  211. const commentId = Template.parentData().activity.commentId;
  212. if (commentText) {
  213. CardComments.update(commentId, {
  214. $set: {
  215. text: commentText,
  216. },
  217. });
  218. }
  219. },
  220. },
  221. ];
  222. },
  223. }).register('activity');
  224. Template.activity.helpers({
  225. sanitize(value) {
  226. return DOMPurify.sanitize(value, { ALLOW_UNKNOWN_PROTOCOLS: true });
  227. },
  228. });
  229. Template.commentReactions.events({
  230. 'click .reaction'(event) {
  231. if (Meteor.user().isBoardMember()) {
  232. const codepoint = event.currentTarget.dataset['codepoint'];
  233. const commentId = Template.instance().data.commentId;
  234. const cardComment = CardComments.findOne({_id: commentId});
  235. cardComment.toggleReaction(codepoint);
  236. }
  237. },
  238. 'click .open-comment-reaction-popup': Popup.open('addReaction'),
  239. })
  240. Template.addReactionPopup.events({
  241. 'click .add-comment-reaction'(event) {
  242. if (Meteor.user().isBoardMember()) {
  243. const codepoint = event.currentTarget.dataset['codepoint'];
  244. const commentId = Template.instance().data.commentId;
  245. const cardComment = CardComments.findOne({_id: commentId});
  246. cardComment.toggleReaction(codepoint);
  247. }
  248. Popup.back();
  249. },
  250. })
  251. Template.addReactionPopup.helpers({
  252. codepoints() {
  253. // Starting set of unicode codepoints as comment reactions
  254. return [
  255. '👍',
  256. '👎',
  257. '👀',
  258. '✅',
  259. '❌',
  260. '🙏',
  261. '👏',
  262. '🎉',
  263. '🚀',
  264. '😊',
  265. '🤔',
  266. '😔'];
  267. }
  268. })
  269. Template.commentReactions.helpers({
  270. isSelected(userIds) {
  271. return userIds.includes(Meteor.user()._id);
  272. },
  273. userNames(userIds) {
  274. return Users.find({_id: {$in: userIds}})
  275. .map(user => user.profile.fullname)
  276. .join(', ');
  277. }
  278. })
  279. function createCardLink(card) {
  280. if (!card) return '';
  281. return (
  282. card &&
  283. Blaze.toHTML(
  284. HTML.A(
  285. {
  286. href: card.originRelativeUrl(),
  287. class: 'action-card',
  288. },
  289. DOMPurify.sanitize(card.title, { ALLOW_UNKNOWN_PROTOCOLS: true }),
  290. ),
  291. )
  292. );
  293. }
  294. function createBoardLink(board, list) {
  295. let text = board.title;
  296. if (list) text += `: ${list}`;
  297. return (
  298. board &&
  299. Blaze.toHTML(
  300. HTML.A(
  301. {
  302. href: board.originRelativeUrl(),
  303. class: 'action-board',
  304. },
  305. DOMPurify.sanitize(text, { ALLOW_UNKNOWN_PROTOCOLS: true }),
  306. ),
  307. )
  308. );
  309. }