activities.js 10 KB

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