activities.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. const activitiesPerPage = 20;
  2. BlazeComponent.extendComponent({
  3. onCreated() {
  4. // XXX Should we use ReactiveNumber?
  5. this.page = new ReactiveVar(1);
  6. this.loadNextPageLocked = false;
  7. const sidebar = this.parentComponent(); // XXX for some reason not working
  8. sidebar.callFirstWith(null, 'resetNextPeak');
  9. this.autorun(() => {
  10. let mode = this.data().mode;
  11. const capitalizedMode = Utils.capitalize(mode);
  12. let thisId, searchId;
  13. if (mode === 'linkedcard' || mode === 'linkedboard') {
  14. thisId = Session.get('currentCard');
  15. searchId = Cards.findOne({ _id: thisId }).linkedId;
  16. mode = mode.replace('linked', '');
  17. } else {
  18. thisId = Session.get(`current${capitalizedMode}`);
  19. searchId = thisId;
  20. }
  21. const limit = this.page.get() * activitiesPerPage;
  22. const user = Meteor.user();
  23. const hideSystem = user ? user.hasHiddenSystemMessages() : false;
  24. if (searchId === null) return;
  25. this.subscribe('activities', mode, searchId, limit, hideSystem, () => {
  26. this.loadNextPageLocked = false;
  27. // If the sibear peak hasn't increased, that mean that there are no more
  28. // activities, and we can stop calling new subscriptions.
  29. // XXX This is hacky! We need to know excatly and reactively how many
  30. // activities there are, we probably want to denormalize this number
  31. // dirrectly into card and board documents.
  32. const nextPeakBefore = sidebar.callFirstWith(null, 'getNextPeak');
  33. sidebar.calculateNextPeak();
  34. const nextPeakAfter = sidebar.callFirstWith(null, 'getNextPeak');
  35. if (nextPeakBefore === nextPeakAfter) {
  36. sidebar.callFirstWith(null, 'resetNextPeak');
  37. }
  38. });
  39. });
  40. },
  41. }).register('activities');
  42. BlazeComponent.extendComponent({
  43. loadNextPage() {
  44. if (this.loadNextPageLocked === false) {
  45. this.page.set(this.page.get() + 1);
  46. this.loadNextPageLocked = true;
  47. }
  48. },
  49. checkItem() {
  50. const checkItemId = this.currentData().activity.checklistItemId;
  51. const checkItem = ChecklistItems.findOne({ _id: checkItemId });
  52. return checkItem && checkItem.title;
  53. },
  54. boardLabel() {
  55. const data = this.currentData();
  56. if (data.mode !== 'board') {
  57. return createBoardLink(data.activity.board(), data.activity.listName);
  58. }
  59. return TAPi18n.__('this-board');
  60. },
  61. cardLabel() {
  62. const data = this.currentData();
  63. if (data.mode !== 'card') {
  64. return createCardLink(this.currentData().activity.card());
  65. }
  66. return TAPi18n.__('this-card');
  67. },
  68. cardLink() {
  69. return createCardLink(this.currentData().activity.card());
  70. },
  71. lastLabel() {
  72. const lastLabelId = this.currentData().activity.labelId;
  73. if (!lastLabelId) return null;
  74. const lastLabel = Boards.findOne(
  75. this.currentData().activity.boardId,
  76. ).getLabelById(lastLabelId);
  77. if (lastLabel && (lastLabel.name === undefined || lastLabel.name === '')) {
  78. return lastLabel.color;
  79. } else {
  80. return lastLabel.name;
  81. }
  82. },
  83. lastCustomField() {
  84. const lastCustomField = CustomFields.findOne(
  85. this.currentData().activity.customFieldId,
  86. );
  87. if (!lastCustomField) return null;
  88. return lastCustomField.name;
  89. },
  90. lastCustomFieldValue() {
  91. const lastCustomField = CustomFields.findOne(
  92. this.currentData().activity.customFieldId,
  93. );
  94. if (!lastCustomField) return null;
  95. const value = this.currentData().activity.value;
  96. if (
  97. lastCustomField.settings.dropdownItems &&
  98. lastCustomField.settings.dropdownItems.length > 0
  99. ) {
  100. const dropDownValue = _.find(
  101. lastCustomField.settings.dropdownItems,
  102. item => {
  103. return item._id === value;
  104. },
  105. );
  106. if (dropDownValue) return dropDownValue.name;
  107. }
  108. return value;
  109. },
  110. listLabel() {
  111. const activity = this.currentData().activity;
  112. const list = activity.list();
  113. return (list && list.title) || activity.title;
  114. },
  115. sourceLink() {
  116. const source = this.currentData().activity.source;
  117. if (source) {
  118. if (source.url) {
  119. return Blaze.toHTML(
  120. HTML.A(
  121. {
  122. href: source.url,
  123. },
  124. source.system,
  125. ),
  126. );
  127. } else {
  128. return source.system;
  129. }
  130. }
  131. return null;
  132. },
  133. memberLink() {
  134. return Blaze.toHTMLWithData(Template.memberName, {
  135. user: this.currentData().activity.member(),
  136. });
  137. },
  138. attachmentLink() {
  139. const activity = this.currentData().activity;
  140. const attachment = activity.attachment();
  141. const link = attachment ? attachment.link('original', '/') : null;
  142. // trying to display url before file is stored generates js errors
  143. return (
  144. (attachment &&
  145. link &&
  146. Blaze.toHTML(
  147. HTML.A(
  148. {
  149. href: link,
  150. target: '_blank',
  151. },
  152. attachment.name,
  153. ),
  154. )) ||
  155. activity.attachmentName
  156. );
  157. },
  158. customField() {
  159. const customField = this.currentData().activity.customField();
  160. if (!customField) return null;
  161. return customField.name;
  162. },
  163. events() {
  164. return [
  165. {
  166. // XXX We should use Popup.afterConfirmation here
  167. 'click .js-delete-comment'() {
  168. const commentId = this.currentData().activity.commentId;
  169. CardComments.remove(commentId);
  170. },
  171. 'submit .js-edit-comment'(evt) {
  172. evt.preventDefault();
  173. const commentText = this.currentComponent()
  174. .getValue()
  175. .trim();
  176. const commentId = Template.parentData().activity.commentId;
  177. if (commentText) {
  178. CardComments.update(commentId, {
  179. $set: {
  180. text: commentText,
  181. },
  182. });
  183. }
  184. },
  185. },
  186. ];
  187. },
  188. }).register('activity');
  189. function createCardLink(card) {
  190. return (
  191. card &&
  192. Blaze.toHTML(
  193. HTML.A(
  194. {
  195. href: card.absoluteUrl(),
  196. class: 'action-card',
  197. },
  198. card.title,
  199. ),
  200. )
  201. );
  202. }
  203. function createBoardLink(board, list) {
  204. let text = board.title;
  205. if (list) text += `: ${list}`;
  206. return (
  207. board &&
  208. Blaze.toHTML(
  209. HTML.A(
  210. {
  211. href: board.absoluteUrl(),
  212. class: 'action-board',
  213. },
  214. text,
  215. ),
  216. )
  217. );
  218. }