activities.js 10 KB

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