activities.js 7.2 KB

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