activities.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. lastLabel() {
  76. const lastLabelId = this.currentData().activity.labelId;
  77. if (!lastLabelId) return null;
  78. const lastLabel = Boards.findOne(
  79. this.currentData().activity.boardId,
  80. ).getLabelById(lastLabelId);
  81. if (lastLabel && (lastLabel.name === undefined || lastLabel.name === '')) {
  82. return lastLabel.color;
  83. } else {
  84. return lastLabel.name;
  85. }
  86. },
  87. lastCustomField() {
  88. const lastCustomField = CustomFields.findOne(
  89. this.currentData().activity.customFieldId,
  90. );
  91. if (!lastCustomField) return null;
  92. return lastCustomField.name;
  93. },
  94. lastCustomFieldValue() {
  95. const lastCustomField = CustomFields.findOne(
  96. this.currentData().activity.customFieldId,
  97. );
  98. if (!lastCustomField) return null;
  99. const value = this.currentData().activity.value;
  100. if (
  101. lastCustomField.settings.dropdownItems &&
  102. lastCustomField.settings.dropdownItems.length > 0
  103. ) {
  104. const dropDownValue = _.find(
  105. lastCustomField.settings.dropdownItems,
  106. item => {
  107. return item._id === value;
  108. },
  109. );
  110. if (dropDownValue) return dropDownValue.name;
  111. }
  112. return value;
  113. },
  114. listLabel() {
  115. const activity = this.currentData().activity;
  116. const list = activity.list();
  117. return (list && list.title) || activity.title;
  118. },
  119. sourceLink() {
  120. const source = this.currentData().activity.source;
  121. if (source) {
  122. if (source.url) {
  123. return Blaze.toHTML(
  124. HTML.A(
  125. {
  126. href: source.url,
  127. },
  128. sanitizeXss(source.system),
  129. ),
  130. );
  131. } else {
  132. return sanitizeXss(source.system);
  133. }
  134. }
  135. return null;
  136. },
  137. memberLink() {
  138. return Blaze.toHTMLWithData(Template.memberName, {
  139. user: this.currentData().activity.member(),
  140. });
  141. },
  142. attachmentLink() {
  143. const attachment = this.currentData().activity.attachment();
  144. // trying to display url before file is stored generates js errors
  145. return (
  146. (attachment &&
  147. attachment.url({ download: true }) &&
  148. Blaze.toHTML(
  149. HTML.A(
  150. {
  151. href: attachment.url({ download: true }),
  152. target: '_blank',
  153. },
  154. sanitizeXss(attachment.name()),
  155. ),
  156. )) ||
  157. sanitizeXss(this.currentData().activity.attachmentName)
  158. );
  159. },
  160. customField() {
  161. const customField = this.currentData().activity.customField();
  162. if (!customField) return null;
  163. return customField.name;
  164. },
  165. events() {
  166. return [
  167. {
  168. // XXX We should use Popup.afterConfirmation here
  169. 'click .js-delete-comment'() {
  170. const commentId = this.currentData().activity.commentId;
  171. CardComments.remove(commentId);
  172. },
  173. 'submit .js-edit-comment'(evt) {
  174. evt.preventDefault();
  175. const commentText = this.currentComponent()
  176. .getValue()
  177. .trim();
  178. const commentId = Template.parentData().activity.commentId;
  179. if (commentText) {
  180. CardComments.update(commentId, {
  181. $set: {
  182. text: commentText,
  183. },
  184. });
  185. }
  186. },
  187. },
  188. ];
  189. },
  190. }).register('activity');
  191. Template.activity.helpers({
  192. sanitize(value) {
  193. return sanitizeXss(value);
  194. },
  195. });
  196. function createCardLink(card) {
  197. if (!card) return '';
  198. return (
  199. card &&
  200. Blaze.toHTML(
  201. HTML.A(
  202. {
  203. href: card.absoluteUrl(),
  204. class: 'action-card',
  205. },
  206. sanitizeXss(card.title),
  207. ),
  208. )
  209. );
  210. }
  211. function createBoardLink(board, list) {
  212. let text = board.title;
  213. if (list) text += `: ${list}`;
  214. return (
  215. board &&
  216. Blaze.toHTML(
  217. HTML.A(
  218. {
  219. href: board.absoluteUrl(),
  220. class: 'action-board',
  221. },
  222. sanitizeXss(text),
  223. ),
  224. )
  225. );
  226. }