activities.js 9.2 KB

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