myCards.js 5.5 KB

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