activities.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 = Session.get('currentCard');
  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. BlazeComponent.extendComponent({
  53. checkItem() {
  54. const checkItemId = this.currentData().activity.checklistItemId;
  55. const checkItem = ChecklistItems.findOne({ _id: checkItemId });
  56. return checkItem && checkItem.title;
  57. },
  58. boardLabelLink() {
  59. const data = this.currentData();
  60. if (data.mode !== 'board') {
  61. return createBoardLink(data.activity.board(), data.activity.listName);
  62. }
  63. return TAPi18n.__('this-board');
  64. },
  65. cardLabelLink() {
  66. const data = this.currentData();
  67. if (data.mode !== 'card') {
  68. return createCardLink(data.activity.card());
  69. }
  70. return TAPi18n.__('this-card');
  71. },
  72. cardLink() {
  73. return createCardLink(this.currentData().activity.card());
  74. },
  75. receivedDate() {
  76. const receivedDate = this.currentData().activity.card();
  77. if (!receivedDate) return null;
  78. return receivedDate.receivedAt;
  79. },
  80. startDate() {
  81. const startDate = this.currentData().activity.card();
  82. if (!startDate) return null;
  83. return startDate.startAt;
  84. },
  85. dueDate() {
  86. const dueDate = this.currentData().activity.card();
  87. if (!dueDate) return null;
  88. return dueDate.dueAt;
  89. },
  90. endDate() {
  91. const endDate = this.currentData().activity.card();
  92. if (!endDate) return null;
  93. return endDate.endAt;
  94. },
  95. lastLabel() {
  96. const lastLabelId = this.currentData().activity.labelId;
  97. if (!lastLabelId) return null;
  98. const lastLabel = Boards.findOne(
  99. this.currentData().activity.boardId,
  100. ).getLabelById(lastLabelId);
  101. if (lastLabel && (lastLabel.name === undefined || lastLabel.name === '')) {
  102. return lastLabel.color;
  103. } else {
  104. return lastLabel.name;
  105. }
  106. },
  107. lastCustomField() {
  108. const lastCustomField = CustomFields.findOne(
  109. this.currentData().activity.customFieldId,
  110. );
  111. if (!lastCustomField) return null;
  112. return lastCustomField.name;
  113. },
  114. lastCustomFieldValue() {
  115. const lastCustomField = CustomFields.findOne(
  116. this.currentData().activity.customFieldId,
  117. );
  118. if (!lastCustomField) return null;
  119. const value = this.currentData().activity.value;
  120. if (
  121. lastCustomField.settings.dropdownItems &&
  122. lastCustomField.settings.dropdownItems.length > 0
  123. ) {
  124. const dropDownValue = _.find(
  125. lastCustomField.settings.dropdownItems,
  126. item => {
  127. return item._id === value;
  128. },
  129. );
  130. if (dropDownValue) return dropDownValue.name;
  131. }
  132. return value;
  133. },
  134. listLabel() {
  135. const activity = this.currentData().activity;
  136. const list = activity.list();
  137. return (list && list.title) || activity.title;
  138. },
  139. sourceLink() {
  140. const source = this.currentData().activity.source;
  141. if (source) {
  142. if (source.url) {
  143. return Blaze.toHTML(
  144. HTML.A(
  145. {
  146. href: source.url,
  147. },
  148. DOMPurify.sanitize(source.system, {
  149. ALLOW_UNKNOWN_PROTOCOLS: true,
  150. }),
  151. ),
  152. );
  153. } else {
  154. return DOMPurify.sanitize(source.system, {
  155. ALLOW_UNKNOWN_PROTOCOLS: true,
  156. });
  157. }
  158. }
  159. return null;
  160. },
  161. memberLink() {
  162. return Blaze.toHTMLWithData(Template.memberName, {
  163. user: this.currentData().activity.member(),
  164. });
  165. },
  166. attachmentLink() {
  167. const attachment = this.currentData().activity.attachment();
  168. // trying to display url before file is stored generates js errors
  169. return (
  170. (attachment &&
  171. attachment.url({ download: true }) &&
  172. Blaze.toHTML(
  173. HTML.A(
  174. {
  175. href: attachment.url({ download: true }),
  176. target: '_blank',
  177. },
  178. DOMPurify.sanitize(attachment.name()),
  179. ),
  180. )) ||
  181. DOMPurify.sanitize(this.currentData().activity.attachmentName)
  182. );
  183. },
  184. customField() {
  185. const customField = this.currentData().activity.customField();
  186. if (!customField) return null;
  187. return customField.name;
  188. },
  189. events() {
  190. return [
  191. {
  192. // XXX We should use Popup.afterConfirmation here
  193. 'click .js-delete-comment'() {
  194. const commentId = this.currentData().activity.commentId;
  195. CardComments.remove(commentId);
  196. },
  197. 'submit .js-edit-comment'(evt) {
  198. evt.preventDefault();
  199. const commentText = this.currentComponent()
  200. .getValue()
  201. .trim();
  202. const commentId = Template.parentData().activity.commentId;
  203. if (commentText) {
  204. CardComments.update(commentId, {
  205. $set: {
  206. text: commentText,
  207. },
  208. });
  209. }
  210. },
  211. },
  212. ];
  213. },
  214. }).register('activity');
  215. Template.activity.helpers({
  216. sanitize(value) {
  217. return DOMPurify.sanitize(value, { ALLOW_UNKNOWN_PROTOCOLS: true });
  218. },
  219. });
  220. function createCardLink(card) {
  221. if (!card) return '';
  222. return (
  223. card &&
  224. Blaze.toHTML(
  225. HTML.A(
  226. {
  227. href: card.originRelativeUrl(),
  228. class: 'action-card',
  229. },
  230. DOMPurify.sanitize(card.title, { ALLOW_UNKNOWN_PROTOCOLS: true }),
  231. ),
  232. )
  233. );
  234. }
  235. function createBoardLink(board, list) {
  236. let text = board.title;
  237. if (list) text += `: ${list}`;
  238. return (
  239. board &&
  240. Blaze.toHTML(
  241. HTML.A(
  242. {
  243. href: board.originRelativeUrl(),
  244. class: 'action-board',
  245. },
  246. DOMPurify.sanitize(text, { ALLOW_UNKNOWN_PROTOCOLS: true }),
  247. ),
  248. )
  249. );
  250. }