myCards.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import { CardSearchPagedComponent } from '../../lib/cardSearch';
  2. const subManager = new SubsManager();
  3. BlazeComponent.extendComponent({
  4. myCardsSort() {
  5. // eslint-disable-next-line no-console
  6. // console.log('sort:', Utils.myCardsSort());
  7. return Utils.myCardsSort();
  8. },
  9. events() {
  10. return [
  11. {
  12. 'click .js-toggle-my-cards-choose-sort': Popup.open(
  13. 'myCardsSortChange',
  14. ),
  15. },
  16. ];
  17. },
  18. }).register('myCardsHeaderBar');
  19. Template.myCards.helpers({
  20. userId() {
  21. return Meteor.userId();
  22. },
  23. });
  24. BlazeComponent.extendComponent({
  25. events() {
  26. return [
  27. {
  28. 'click .js-my-cards-sort-board'() {
  29. Utils.setMyCardsSort('board');
  30. Popup.close();
  31. },
  32. 'click .js-my-cards-sort-dueat'() {
  33. Utils.setMyCardsSort('dueAt');
  34. Popup.close();
  35. },
  36. },
  37. ];
  38. },
  39. }).register('myCardsSortChangePopup');
  40. class MyCardsComponent extends CardSearchPagedComponent {
  41. onCreated() {
  42. super.onCreated();
  43. const queryParams = {
  44. users: [Meteor.user().username],
  45. sort: { name: 'dueAt', order: 'des' },
  46. };
  47. this.runGlobalSearch(queryParams);
  48. Meteor.subscribe('setting');
  49. }
  50. myCardsSort() {
  51. // eslint-disable-next-line no-console
  52. //console.log('sort:', Utils.myCardsSort());
  53. return Utils.myCardsSort();
  54. }
  55. sortByBoard() {
  56. return this.myCardsSort() === 'board';
  57. }
  58. myCardsList() {
  59. const boards = [];
  60. let board = null;
  61. let swimlane = null;
  62. let list = null;
  63. const cursor = this.getResults();
  64. if (cursor) {
  65. let newBoard = false;
  66. let newSwimlane = false;
  67. let newList = false;
  68. cursor.forEach(card => {
  69. // eslint-disable-next-line no-console
  70. // console.log('card:', card.title);
  71. if (list === null || card.listId !== list._id) {
  72. // eslint-disable-next-line no-console
  73. // console.log('new list');
  74. list = card.getList();
  75. if (list.archived) {
  76. list = null;
  77. return;
  78. }
  79. list.myCards = [card];
  80. newList = true;
  81. }
  82. if (swimlane === null || card.swimlaneId !== swimlane._id) {
  83. // eslint-disable-next-line no-console
  84. // console.log('new swimlane');
  85. swimlane = card.getSwimlane();
  86. if (swimlane.archived) {
  87. swimlane = null;
  88. return;
  89. }
  90. swimlane.myLists = [list];
  91. newSwimlane = true;
  92. }
  93. if (board === null || card.boardId !== board._id) {
  94. // eslint-disable-next-line no-console
  95. // console.log('new board');
  96. board = card.getBoard();
  97. if (board.archived) {
  98. board = null;
  99. return;
  100. }
  101. // eslint-disable-next-line no-console
  102. // console.log('board:', b, b._id, b.title);
  103. board.mySwimlanes = [swimlane];
  104. newBoard = true;
  105. }
  106. if (newBoard) {
  107. boards.push(board);
  108. } else if (newSwimlane) {
  109. board.mySwimlanes.push(swimlane);
  110. } else if (newList) {
  111. swimlane.myLists.push(list);
  112. } else {
  113. list.myCards.push(card);
  114. }
  115. newBoard = false;
  116. newSwimlane = false;
  117. newList = false;
  118. });
  119. // sort the data structure
  120. boards.forEach(board => {
  121. board.mySwimlanes.forEach(swimlane => {
  122. swimlane.myLists.forEach(list => {
  123. list.myCards.sort((a, b) => {
  124. return a.sort - b.sort;
  125. });
  126. });
  127. swimlane.myLists.sort((a, b) => {
  128. return a.sort - b.sort;
  129. });
  130. });
  131. board.mySwimlanes.sort((a, b) => {
  132. return a.sort - b.sort;
  133. });
  134. });
  135. boards.sort((a, b) => {
  136. let x = a.sort;
  137. let y = b.sort;
  138. // show the template board last
  139. if (a.type === 'template-container') {
  140. x = 99999999;
  141. } else if (b.type === 'template-container') {
  142. y = 99999999;
  143. }
  144. return x - y;
  145. });
  146. // eslint-disable-next-line no-console
  147. // console.log('boards:', boards);
  148. return boards;
  149. }
  150. return [];
  151. }
  152. myDueCardsList() {
  153. const cursor = this.getResults();
  154. const cards = [];
  155. cursor.forEach(card => {
  156. cards.push(card);
  157. });
  158. cards.sort((a, b) => {
  159. const x = a.dueAt === null ? Date('2100-12-31') : a.dueAt;
  160. const y = b.dueAt === null ? Date('2100-12-31') : b.dueAt;
  161. if (x > y) return 1;
  162. else if (x < y) return -1;
  163. return 0;
  164. });
  165. // eslint-disable-next-line no-console
  166. // console.log('cursor:', cards);
  167. return cards;
  168. }
  169. }
  170. MyCardsComponent.register('myCards');