activities.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. import { ReactiveCache } from '/imports/reactiveCache';
  2. import DOMPurify from 'dompurify';
  3. import { TAPi18n } from '/imports/i18n';
  4. const activitiesPerPage = 500;
  5. BlazeComponent.extendComponent({
  6. onCreated() {
  7. // XXX Should we use ReactiveNumber?
  8. this.page = new ReactiveVar(1);
  9. this.loadNextPageLocked = false;
  10. // TODO is sidebar always available? E.g. on small screens/mobile devices
  11. const sidebar = Sidebar;
  12. sidebar && sidebar.callFirstWith(null, 'resetNextPeak');
  13. this.autorun(() => {
  14. let mode = this.data().mode;
  15. const capitalizedMode = Utils.capitalize(mode);
  16. let searchId;
  17. if (mode === 'linkedcard' || mode === 'linkedboard') {
  18. searchId = Utils.getCurrentCard().linkedId;
  19. mode = mode.replace('linked', '');
  20. } else if (mode === 'card') {
  21. searchId = Utils.getCurrentCardId();
  22. } else {
  23. searchId = Session.get(`current${capitalizedMode}`);
  24. }
  25. const limit = this.page.get() * activitiesPerPage;
  26. const user = Meteor.user();
  27. const hideSystem = user ? user.hasHiddenSystemMessages() : false;
  28. if (searchId === null) return;
  29. this.subscribe('activities', mode, searchId, limit, hideSystem, () => {
  30. this.loadNextPageLocked = false;
  31. // TODO the guard can be removed as soon as the TODO above is resolved
  32. if (!sidebar) return;
  33. // If the sibear peak hasn't increased, that mean that there are no more
  34. // activities, and we can stop calling new subscriptions.
  35. // XXX This is hacky! We need to know excatly and reactively how many
  36. // activities there are, we probably want to denormalize this number
  37. // dirrectly into card and board documents.
  38. const nextPeakBefore = sidebar.callFirstWith(null, 'getNextPeak');
  39. sidebar.calculateNextPeak();
  40. const nextPeakAfter = sidebar.callFirstWith(null, 'getNextPeak');
  41. if (nextPeakBefore === nextPeakAfter) {
  42. sidebar.callFirstWith(null, 'resetNextPeak');
  43. }
  44. });
  45. });
  46. },
  47. loadNextPage() {
  48. if (this.loadNextPageLocked === false) {
  49. this.page.set(this.page.get() + 1);
  50. this.loadNextPageLocked = true;
  51. }
  52. },
  53. }).register('activities');
  54. Template.activities.helpers({
  55. activities() {
  56. const ret = this.card.activities();
  57. return ret;
  58. },
  59. });
  60. BlazeComponent.extendComponent({
  61. checkItem() {
  62. const checkItemId = this.currentData().activity.checklistItemId;
  63. const checkItem = ChecklistItems.findOne({ _id: checkItemId });
  64. return checkItem && checkItem.title;
  65. },
  66. boardLabelLink() {
  67. const data = this.currentData();
  68. const currentBoardId = Session.get('currentBoard');
  69. if (data.mode !== 'board') {
  70. // data.mode: card, linkedcard, linkedboard
  71. return createBoardLink(data.activity.board(), data.activity.listName ? data.activity.listName : null);
  72. }
  73. else if (currentBoardId != data.activity.boardId) {
  74. // data.mode: board
  75. // current activitie is linked
  76. return createBoardLink(data.activity.board(), data.activity.listName ? data.activity.listName : null);
  77. }
  78. return TAPi18n.__('this-board');
  79. },
  80. cardLabelLink() {
  81. const data = this.currentData();
  82. const currentBoardId = Session.get('currentBoard');
  83. if (data.mode == 'card') {
  84. // data.mode: card
  85. return TAPi18n.__('this-card');
  86. }
  87. else if (data.mode !== 'board') {
  88. // data.mode: linkedcard, linkedboard
  89. return createCardLink(data.activity.card(), null);
  90. }
  91. else if (currentBoardId != data.activity.boardId) {
  92. // data.mode: board
  93. // current activitie is linked
  94. return createCardLink(data.activity.card(), data.activity.board().title);
  95. }
  96. return createCardLink(this.currentData().activity.card(), null);
  97. },
  98. cardLink() {
  99. const data = this.currentData();
  100. const currentBoardId = Session.get('currentBoard');
  101. if (data.mode !== 'board') {
  102. // data.mode: card, linkedcard, linkedboard
  103. return createCardLink(data.activity.card(), null);
  104. }
  105. else if (currentBoardId != data.activity.boardId) {
  106. // data.mode: board
  107. // current activitie is linked
  108. return createCardLink(data.activity.card(), data.activity.board().title);
  109. }
  110. return createCardLink(this.currentData().activity.card(), null);
  111. },
  112. receivedDate() {
  113. const receivedDate = this.currentData().activity.card();
  114. if (!receivedDate) return null;
  115. return receivedDate.receivedAt;
  116. },
  117. startDate() {
  118. const startDate = this.currentData().activity.card();
  119. if (!startDate) return null;
  120. return startDate.startAt;
  121. },
  122. dueDate() {
  123. const dueDate = this.currentData().activity.card();
  124. if (!dueDate) return null;
  125. return dueDate.dueAt;
  126. },
  127. endDate() {
  128. const endDate = this.currentData().activity.card();
  129. if (!endDate) return null;
  130. return endDate.endAt;
  131. },
  132. lastLabel() {
  133. const lastLabelId = this.currentData().activity.labelId;
  134. if (!lastLabelId) return null;
  135. const lastLabel = ReactiveCache.getBoard(
  136. this.currentData().activity.boardId,
  137. ).getLabelById(lastLabelId);
  138. if (lastLabel && (lastLabel.name === undefined || lastLabel.name === '')) {
  139. return lastLabel.color;
  140. } else if (lastLabel.name !== undefined && lastLabel.name !== '') {
  141. return lastLabel.name;
  142. } else {
  143. return null;
  144. }
  145. },
  146. lastCustomField() {
  147. const lastCustomField = CustomFields.findOne(
  148. this.currentData().activity.customFieldId,
  149. );
  150. if (!lastCustomField) return null;
  151. return lastCustomField.name;
  152. },
  153. lastCustomFieldValue() {
  154. const lastCustomField = CustomFields.findOne(
  155. this.currentData().activity.customFieldId,
  156. );
  157. if (!lastCustomField) return null;
  158. const value = this.currentData().activity.value;
  159. if (
  160. lastCustomField.settings.dropdownItems &&
  161. lastCustomField.settings.dropdownItems.length > 0
  162. ) {
  163. const dropDownValue = _.find(
  164. lastCustomField.settings.dropdownItems,
  165. item => {
  166. return item._id === value;
  167. },
  168. );
  169. if (dropDownValue) return dropDownValue.name;
  170. }
  171. return value;
  172. },
  173. listLabel() {
  174. const activity = this.currentData().activity;
  175. const list = activity.list();
  176. return (list && list.title) || activity.title;
  177. },
  178. sourceLink() {
  179. const source = this.currentData().activity.source;
  180. if (source) {
  181. if (source.url) {
  182. return Blaze.toHTML(
  183. HTML.A(
  184. {
  185. href: source.url,
  186. },
  187. DOMPurify.sanitize(source.system, {
  188. ALLOW_UNKNOWN_PROTOCOLS: true,
  189. }),
  190. ),
  191. );
  192. } else {
  193. return DOMPurify.sanitize(source.system, {
  194. ALLOW_UNKNOWN_PROTOCOLS: true,
  195. });
  196. }
  197. }
  198. return null;
  199. },
  200. memberLink() {
  201. return Blaze.toHTMLWithData(Template.memberName, {
  202. user: this.currentData().activity.member(),
  203. });
  204. },
  205. attachmentLink() {
  206. const attachment = this.currentData().activity.attachment();
  207. // trying to display url before file is stored generates js errors
  208. return (
  209. (attachment &&
  210. attachment.path &&
  211. Blaze.toHTML(
  212. HTML.A(
  213. {
  214. href: `${attachment.link()}?download=true`,
  215. target: '_blank',
  216. },
  217. DOMPurify.sanitize(attachment.name),
  218. ),
  219. )) ||
  220. DOMPurify.sanitize(this.currentData().activity.attachmentName)
  221. );
  222. },
  223. customField() {
  224. const customField = this.currentData().activity.customField();
  225. if (!customField) return null;
  226. return customField.name;
  227. },
  228. events() {
  229. return [
  230. {
  231. // XXX We should use Popup.afterConfirmation here
  232. 'click .js-delete-comment': Popup.afterConfirm('deleteComment', () => {
  233. const commentId = this.data().activity.commentId;
  234. CardComments.remove(commentId);
  235. Popup.back();
  236. }),
  237. 'submit .js-edit-comment'(evt) {
  238. evt.preventDefault();
  239. const commentText = this.currentComponent()
  240. .getValue()
  241. .trim();
  242. const commentId = Template.parentData().activity.commentId;
  243. if (commentText) {
  244. CardComments.update(commentId, {
  245. $set: {
  246. text: commentText,
  247. },
  248. });
  249. }
  250. },
  251. },
  252. ];
  253. },
  254. }).register('activity');
  255. Template.activity.helpers({
  256. sanitize(value) {
  257. return DOMPurify.sanitize(value, { ALLOW_UNKNOWN_PROTOCOLS: true });
  258. },
  259. });
  260. Template.commentReactions.events({
  261. 'click .reaction'(event) {
  262. if (Meteor.user().isBoardMember()) {
  263. const codepoint = event.currentTarget.dataset['codepoint'];
  264. const commentId = Template.instance().data.commentId;
  265. const cardComment = CardComments.findOne({_id: commentId});
  266. cardComment.toggleReaction(codepoint);
  267. }
  268. },
  269. 'click .open-comment-reaction-popup': Popup.open('addReaction'),
  270. })
  271. Template.addReactionPopup.events({
  272. 'click .add-comment-reaction'(event) {
  273. if (Meteor.user().isBoardMember()) {
  274. const codepoint = event.currentTarget.dataset['codepoint'];
  275. const commentId = Template.instance().data.commentId;
  276. const cardComment = CardComments.findOne({_id: commentId});
  277. cardComment.toggleReaction(codepoint);
  278. }
  279. Popup.back();
  280. },
  281. })
  282. Template.addReactionPopup.helpers({
  283. codepoints() {
  284. // Starting set of unicode codepoints as comment reactions
  285. return [
  286. '👍',
  287. '👎',
  288. '👀',
  289. '✅',
  290. '❌',
  291. '🙏',
  292. '👏',
  293. '🎉',
  294. '🚀',
  295. '😊',
  296. '🤔',
  297. '😔'];
  298. }
  299. })
  300. Template.commentReactions.helpers({
  301. isSelected(userIds) {
  302. return userIds.includes(Meteor.user()._id);
  303. },
  304. userNames(userIds) {
  305. return Users.find({_id: {$in: userIds}})
  306. .map(user => user.profile.fullname)
  307. .join(', ');
  308. }
  309. })
  310. function createCardLink(card, board) {
  311. if (!card) return '';
  312. let text = card.title;
  313. if (board) text = `${board} > ` + text;
  314. return (
  315. card &&
  316. Blaze.toHTML(
  317. HTML.A(
  318. {
  319. href: card.originRelativeUrl(),
  320. class: 'action-card',
  321. },
  322. DOMPurify.sanitize(text, { ALLOW_UNKNOWN_PROTOCOLS: true }),
  323. ),
  324. )
  325. );
  326. }
  327. function createBoardLink(board, list) {
  328. let text = board.title;
  329. if (list) text += `: ${list}`;
  330. return (
  331. board &&
  332. Blaze.toHTML(
  333. HTML.A(
  334. {
  335. href: board.originRelativeUrl(),
  336. class: 'action-board',
  337. },
  338. DOMPurify.sanitize(text, { ALLOW_UNKNOWN_PROTOCOLS: true }),
  339. ),
  340. )
  341. );
  342. }