activities.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 attachment = this.currentData().activity.attachment();
  140. // trying to display url before file is stored generates js errors
  141. return (
  142. (attachment &&
  143. attachment.url({ download: true }) &&
  144. Blaze.toHTML(
  145. HTML.A(
  146. {
  147. href: attachment.url({ download: true }),
  148. target: '_blank',
  149. },
  150. attachment.name(),
  151. ),
  152. )) ||
  153. this.currentData().activity.attachmentName
  154. );
  155. },
  156. customField() {
  157. const customField = this.currentData().activity.customField();
  158. if (!customField) return null;
  159. return customField.name;
  160. },
  161. events() {
  162. return [
  163. {
  164. // XXX We should use Popup.afterConfirmation here
  165. 'click .js-delete-comment'() {
  166. const commentId = this.currentData().commentId;
  167. CardComments.remove(commentId);
  168. },
  169. 'submit .js-edit-comment'(evt) {
  170. evt.preventDefault();
  171. const commentText = this.currentComponent()
  172. .getValue()
  173. .trim();
  174. const commentId = Template.parentData().commentId;
  175. if (commentText) {
  176. CardComments.update(commentId, {
  177. $set: {
  178. text: commentText,
  179. },
  180. });
  181. }
  182. },
  183. },
  184. ];
  185. },
  186. }).register('activity');
  187. function createCardLink(card) {
  188. return (
  189. card &&
  190. Blaze.toHTML(
  191. HTML.A(
  192. {
  193. href: card.absoluteUrl(),
  194. class: 'action-card',
  195. },
  196. card.title,
  197. ),
  198. )
  199. );
  200. }
  201. function createBoardLink(board, list) {
  202. let text = board.title;
  203. if (list) text += `: ${list}`;
  204. return (
  205. board &&
  206. Blaze.toHTML(
  207. HTML.A(
  208. {
  209. href: board.absoluteUrl(),
  210. class: 'action-board',
  211. },
  212. text,
  213. ),
  214. )
  215. );
  216. }