activities.js 6.3 KB

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