myCards.js 5.0 KB

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