activities.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. const sidebar = Sidebar;
  9. sidebar.callFirstWith(null, 'resetNextPeak');
  10. this.autorun(() => {
  11. let mode = this.data().mode;
  12. const capitalizedMode = Utils.capitalize(mode);
  13. let thisId, searchId;
  14. if (mode === 'linkedcard' || mode === 'linkedboard') {
  15. thisId = Session.get('currentCard');
  16. searchId = Cards.findOne({ _id: thisId }).linkedId;
  17. mode = mode.replace('linked', '');
  18. } else {
  19. thisId = Session.get(`current${capitalizedMode}`);
  20. searchId = thisId;
  21. }
  22. const limit = this.page.get() * activitiesPerPage;
  23. const user = Meteor.user();
  24. const hideSystem = user ? user.hasHiddenSystemMessages() : false;
  25. if (searchId === null) return;
  26. this.subscribe('activities', mode, searchId, limit, hideSystem, () => {
  27. this.loadNextPageLocked = false;
  28. // If the sibear peak hasn't increased, that mean that there are no more
  29. // activities, and we can stop calling new subscriptions.
  30. // XXX This is hacky! We need to know excatly and reactively how many
  31. // activities there are, we probably want to denormalize this number
  32. // dirrectly into card and board documents.
  33. const nextPeakBefore = sidebar.callFirstWith(null, 'getNextPeak');
  34. sidebar.calculateNextPeak();
  35. const nextPeakAfter = sidebar.callFirstWith(null, 'getNextPeak');
  36. if (nextPeakBefore === nextPeakAfter) {
  37. sidebar.callFirstWith(null, 'resetNextPeak');
  38. }
  39. });
  40. });
  41. },
  42. loadNextPage() {
  43. if (this.loadNextPageLocked === false) {
  44. this.page.set(this.page.get() + 1);
  45. this.loadNextPageLocked = true;
  46. }
  47. },
  48. }).register('activities');
  49. BlazeComponent.extendComponent({
  50. checkItem() {
  51. const checkItemId = this.currentData().activity.checklistItemId;
  52. const checkItem = ChecklistItems.findOne({ _id: checkItemId });
  53. return checkItem && checkItem.title;
  54. },
  55. boardLabelLink() {
  56. const data = this.currentData();
  57. if (data.mode !== 'board') {
  58. return createBoardLink(data.activity.board(), data.activity.listName);
  59. }
  60. return TAPi18n.__('this-board');
  61. },
  62. cardLabelLink() {
  63. const data = this.currentData();
  64. if (data.mode !== 'card') {
  65. return createCardLink(data.activity.card());
  66. }
  67. return TAPi18n.__('this-card');
  68. },
  69. cardLink() {
  70. return createCardLink(this.currentData().activity.card());
  71. },
  72. lastLabel() {
  73. const lastLabelId = this.currentData().activity.labelId;
  74. if (!lastLabelId) return null;
  75. const lastLabel = Boards.findOne(
  76. this.currentData().activity.boardId,
  77. ).getLabelById(lastLabelId);
  78. if (lastLabel && (lastLabel.name === undefined || lastLabel.name === '')) {
  79. return lastLabel.color;
  80. } else {
  81. return lastLabel.name;
  82. }
  83. },
  84. lastCustomField() {
  85. const lastCustomField = CustomFields.findOne(
  86. this.currentData().activity.customFieldId,
  87. );
  88. if (!lastCustomField) return null;
  89. return lastCustomField.name;
  90. },
  91. lastCustomFieldValue() {
  92. const lastCustomField = CustomFields.findOne(
  93. this.currentData().activity.customFieldId,
  94. );
  95. if (!lastCustomField) return null;
  96. const value = this.currentData().activity.value;
  97. if (
  98. lastCustomField.settings.dropdownItems &&
  99. lastCustomField.settings.dropdownItems.length > 0
  100. ) {
  101. const dropDownValue = _.find(
  102. lastCustomField.settings.dropdownItems,
  103. item => {
  104. return item._id === value;
  105. },
  106. );
  107. if (dropDownValue) return dropDownValue.name;
  108. }
  109. return value;
  110. },
  111. listLabel() {
  112. const activity = this.currentData().activity;
  113. const list = activity.list();
  114. return (list && list.title) || activity.title;
  115. },
  116. sourceLink() {
  117. const source = this.currentData().activity.source;
  118. if (source) {
  119. if (source.url) {
  120. return Blaze.toHTML(
  121. HTML.A(
  122. {
  123. href: source.url,
  124. },
  125. sanitizeXss(source.system),
  126. ),
  127. );
  128. } else {
  129. return sanitizeXss(source.system);
  130. }
  131. }
  132. return null;
  133. },
  134. memberLink() {
  135. return Blaze.toHTMLWithData(Template.memberName, {
  136. user: this.currentData().activity.member(),
  137. });
  138. },
  139. attachmentLink() {
  140. const attachment = this.currentData().activity.attachment();
  141. // trying to display url before file is stored generates js errors
  142. return (
  143. (attachment &&
  144. attachment.url({ download: true }) &&
  145. Blaze.toHTML(
  146. HTML.A(
  147. {
  148. href: attachment.url({ download: true }),
  149. target: '_blank',
  150. },
  151. sanitizeXss(attachment.name()),
  152. ),
  153. )) ||
  154. sanitizeXss(this.currentData().activity.attachmentName)
  155. );
  156. },
  157. customField() {
  158. const customField = this.currentData().activity.customField();
  159. if (!customField) return null;
  160. return customField.name;
  161. },
  162. events() {
  163. return [
  164. {
  165. // XXX We should use Popup.afterConfirmation here
  166. 'click .js-delete-comment'() {
  167. const commentId = this.currentData().activity.commentId;
  168. CardComments.remove(commentId);
  169. },
  170. 'submit .js-edit-comment'(evt) {
  171. evt.preventDefault();
  172. const commentText = this.currentComponent()
  173. .getValue()
  174. .trim();
  175. const commentId = Template.parentData().activity.commentId;
  176. if (commentText) {
  177. CardComments.update(commentId, {
  178. $set: {
  179. text: commentText,
  180. },
  181. });
  182. }
  183. },
  184. },
  185. ];
  186. },
  187. }).register('activity');
  188. Template.activity.helpers({
  189. sanitize(value) {
  190. return sanitizeXss(value);
  191. },
  192. });
  193. function createCardLink(card) {
  194. if (!card) return '';
  195. return (
  196. card &&
  197. Blaze.toHTML(
  198. HTML.A(
  199. {
  200. href: card.absoluteUrl(),
  201. class: 'action-card',
  202. },
  203. sanitizeXss(card.title),
  204. ),
  205. )
  206. );
  207. }
  208. function createBoardLink(board, list) {
  209. let text = board.title;
  210. if (list) text += `: ${list}`;
  211. return (
  212. board &&
  213. Blaze.toHTML(
  214. HTML.A(
  215. {
  216. href: board.absoluteUrl(),
  217. class: 'action-board',
  218. },
  219. sanitizeXss(text),
  220. ),
  221. )
  222. );
  223. }