myCards.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. const subManager = new SubsManager();
  2. BlazeComponent.extendComponent({
  3. myCardsSort() {
  4. // eslint-disable-next-line no-console
  5. // console.log('sort:', Utils.myCardsSort());
  6. return Utils.myCardsSort();
  7. },
  8. events() {
  9. return [
  10. {
  11. 'click .js-toggle-my-cards-choose-sort': Popup.open(
  12. 'myCardsSortChange',
  13. ),
  14. },
  15. ];
  16. },
  17. }).register('myCardsHeaderBar');
  18. Template.myCards.helpers({
  19. userId() {
  20. return Meteor.userId();
  21. },
  22. });
  23. BlazeComponent.extendComponent({
  24. events() {
  25. return [
  26. {
  27. 'click .js-my-cards-sort-board'() {
  28. Utils.setMyCardsSort('board');
  29. Popup.close();
  30. },
  31. 'click .js-my-cards-sort-dueat'() {
  32. Utils.setMyCardsSort('dueAt');
  33. Popup.close();
  34. },
  35. },
  36. ];
  37. },
  38. }).register('myCardsSortChangePopup');
  39. BlazeComponent.extendComponent({
  40. onCreated() {
  41. this.isPageReady = new ReactiveVar(false);
  42. this.autorun(() => {
  43. const handle = subManager.subscribe('myCards');
  44. Tracker.nonreactive(() => {
  45. Tracker.autorun(() => {
  46. this.isPageReady.set(handle.ready());
  47. });
  48. });
  49. });
  50. Meteor.subscribe('setting');
  51. },
  52. myCardsSort() {
  53. // eslint-disable-next-line no-console
  54. //console.log('sort:', Utils.myCardsSort());
  55. return Utils.myCardsSort();
  56. },
  57. sortByBoard() {
  58. return this.myCardsSort() === 'board';
  59. },
  60. myCardsList() {
  61. const userId = Meteor.userId();
  62. const boards = [];
  63. let board = null;
  64. let swimlane = null;
  65. let list = null;
  66. const cursor = Cards.find(
  67. {
  68. $or: [{ members: userId }, { assignees: userId }],
  69. archived: false,
  70. },
  71. {
  72. sort: {
  73. boardId: 1,
  74. swimlaneId: 1,
  75. listId: 1,
  76. sort: 1,
  77. },
  78. },
  79. );
  80. let newBoard = false;
  81. let newSwimlane = false;
  82. let newList = false;
  83. cursor.forEach(card => {
  84. // eslint-disable-next-line no-console
  85. // console.log('card:', card.title);
  86. if (list === null || card.listId !== list._id) {
  87. // eslint-disable-next-line no-console
  88. // console.log('new list');
  89. list = card.getList();
  90. if (list.archived) {
  91. list = null;
  92. return;
  93. }
  94. list.myCards = [card];
  95. newList = true;
  96. }
  97. if (swimlane === null || card.swimlaneId !== swimlane._id) {
  98. // eslint-disable-next-line no-console
  99. // console.log('new swimlane');
  100. swimlane = card.getSwimlane();
  101. if (swimlane.archived) {
  102. swimlane = null;
  103. return;
  104. }
  105. swimlane.myLists = [list];
  106. newSwimlane = true;
  107. }
  108. if (board === null || card.boardId !== board._id) {
  109. // eslint-disable-next-line no-console
  110. // console.log('new board');
  111. board = card.getBoard();
  112. if (board.archived) {
  113. board = null;
  114. return;
  115. }
  116. // eslint-disable-next-line no-console
  117. // console.log('board:', b, b._id, b.title);
  118. board.mySwimlanes = [swimlane];
  119. newBoard = true;
  120. }
  121. if (newBoard) {
  122. boards.push(board);
  123. } else if (newSwimlane) {
  124. board.mySwimlanes.push(swimlane);
  125. } else if (newList) {
  126. swimlane.myLists.push(list);
  127. } else {
  128. list.myCards.push(card);
  129. }
  130. newBoard = false;
  131. newSwimlane = false;
  132. newList = false;
  133. });
  134. // sort the data structure
  135. boards.forEach(board => {
  136. board.mySwimlanes.forEach(swimlane => {
  137. swimlane.myLists.forEach(list => {
  138. list.myCards.sort((a, b) => {
  139. return a.sort - b.sort;
  140. });
  141. });
  142. swimlane.myLists.sort((a, b) => {
  143. return a.sort - b.sort;
  144. });
  145. });
  146. board.mySwimlanes.sort((a, b) => {
  147. return a.sort - b.sort;
  148. });
  149. });
  150. boards.sort((a, b) => {
  151. let x = a.sort;
  152. let y = b.sort;
  153. // show the template board last
  154. if (a.type === 'template-container') {
  155. x = 99999999;
  156. } else if (b.type === 'template-container') {
  157. y = 99999999;
  158. }
  159. return x - y;
  160. });
  161. // eslint-disable-next-line no-console
  162. // console.log('boards:', boards);
  163. return boards;
  164. },
  165. myDueCardsList() {
  166. const userId = Meteor.userId();
  167. const cursor = Cards.find(
  168. {
  169. $or: [{ members: userId }, { assignees: userId }],
  170. archived: false,
  171. },
  172. {
  173. sort: {
  174. dueAt: -1,
  175. boardId: 1,
  176. swimlaneId: 1,
  177. listId: 1,
  178. sort: 1,
  179. },
  180. },
  181. );
  182. // eslint-disable-next-line no-console
  183. // console.log('cursor:', cursor);
  184. const cards = [];
  185. cursor.forEach(card => {
  186. if (
  187. !card.getBoard().archived &&
  188. !card.getSwimlane().archived &&
  189. !card.getList().archived
  190. ) {
  191. cards.push(card);
  192. }
  193. });
  194. cards.sort((a, b) => {
  195. const x = a.dueAt === null ? Date('2100-12-31') : a.dueAt;
  196. const y = b.dueAt === null ? Date('2100-12-31') : b.dueAt;
  197. if (x > y) return 1;
  198. else if (x < y) return -1;
  199. return 0;
  200. });
  201. // eslint-disable-next-line no-console
  202. // console.log('cursor:', cards);
  203. return cards;
  204. },
  205. events() {
  206. return [
  207. {
  208. // 'click .js-my-card'(evt) {
  209. // const card = this.currentData().card;
  210. // // eslint-disable-next-line no-console
  211. // console.log('currentData():', this.currentData());
  212. // // eslint-disable-next-line no-console
  213. // console.log('card:', card);
  214. // if (card) {
  215. // Utils.goCardId(card._id);
  216. // }
  217. // evt.preventDefault();
  218. // },
  219. },
  220. ];
  221. },
  222. }).register('myCards');