activities.js 10 KB

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