activities.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. import sanitizeXss from 'xss';
  2. const activitiesPerPage = 20;
  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. sanitizeXss(source.system),
  149. ),
  150. );
  151. } else {
  152. return sanitizeXss(source.system);
  153. }
  154. }
  155. return null;
  156. },
  157. memberLink() {
  158. return Blaze.toHTMLWithData(Template.memberName, {
  159. user: this.currentData().activity.member(),
  160. });
  161. },
  162. attachmentLink() {
  163. const attachment = this.currentData().activity.attachment();
  164. // trying to display url before file is stored generates js errors
  165. return (
  166. (attachment &&
  167. attachment.url({ download: true }) &&
  168. Blaze.toHTML(
  169. HTML.A(
  170. {
  171. href: attachment.url({ download: true }),
  172. target: '_blank',
  173. },
  174. sanitizeXss(attachment.name()),
  175. ),
  176. )) ||
  177. sanitizeXss(this.currentData().activity.attachmentName)
  178. );
  179. },
  180. customField() {
  181. const customField = this.currentData().activity.customField();
  182. if (!customField) return null;
  183. return customField.name;
  184. },
  185. events() {
  186. return [
  187. {
  188. // XXX We should use Popup.afterConfirmation here
  189. 'click .js-delete-comment'() {
  190. const commentId = this.currentData().activity.commentId;
  191. CardComments.remove(commentId);
  192. },
  193. 'submit .js-edit-comment'(evt) {
  194. evt.preventDefault();
  195. const commentText = this.currentComponent()
  196. .getValue()
  197. .trim();
  198. const commentId = Template.parentData().activity.commentId;
  199. if (commentText) {
  200. CardComments.update(commentId, {
  201. $set: {
  202. text: commentText,
  203. },
  204. });
  205. }
  206. },
  207. },
  208. ];
  209. },
  210. }).register('activity');
  211. Template.activity.helpers({
  212. sanitize(value) {
  213. return sanitizeXss(value);
  214. },
  215. });
  216. function createCardLink(card) {
  217. if (!card) return '';
  218. return (
  219. card &&
  220. Blaze.toHTML(
  221. HTML.A(
  222. {
  223. href: card.absoluteUrl(),
  224. class: 'action-card',
  225. },
  226. sanitizeXss(card.title),
  227. ),
  228. )
  229. );
  230. }
  231. function createBoardLink(board, list) {
  232. let text = board.title;
  233. if (list) text += `: ${list}`;
  234. return (
  235. board &&
  236. Blaze.toHTML(
  237. HTML.A(
  238. {
  239. href: board.absoluteUrl(),
  240. class: 'action-board',
  241. },
  242. sanitizeXss(text),
  243. ),
  244. )
  245. );
  246. }