myCards.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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'() {
  11. // eslint-disable-next-line no-console
  12. // console.log('open sort');
  13. // Popup.open('myCardsSortChange');
  14. Utils.myCardsSortToggle();
  15. },
  16. },
  17. ];
  18. },
  19. }).register('myCardsHeaderBar');
  20. Template.myCards.helpers({
  21. userId() {
  22. return Meteor.userId();
  23. },
  24. });
  25. BlazeComponent.extendComponent({
  26. events() {
  27. return [
  28. {
  29. 'click .js-my-cards-sort-board'() {
  30. Utils.setMyCardsSort('board');
  31. Popup.close();
  32. },
  33. 'click .js-my-cards-sort-dueat'() {
  34. Utils.setMyCardsSort('dueAt');
  35. Popup.close();
  36. },
  37. },
  38. ];
  39. },
  40. }).register('myCardsSortChangePopup');
  41. BlazeComponent.extendComponent({
  42. onCreated() {
  43. Meteor.subscribe('setting');
  44. Meteor.subscribe('myCards');
  45. Meteor.subscribe('mySwimlanes');
  46. Meteor.subscribe('myLists');
  47. },
  48. myCardsSort() {
  49. // eslint-disable-next-line no-console
  50. console.log('sort:', Utils.myCardsSort());
  51. return Utils.myCardsSort();
  52. },
  53. sortByBoard() {
  54. return this.myCardsSort() === 'board';
  55. },
  56. myBoards() {
  57. const userId = Meteor.userId();
  58. const boards = [];
  59. let board = null;
  60. let swimlane = null;
  61. let list = null;
  62. const cursor = Cards.find(
  63. {
  64. $or: [{ members: userId }, { assignees: userId }],
  65. archived: false,
  66. },
  67. {
  68. sort: {
  69. boardId: 1,
  70. swimlaneId: 1,
  71. listId: 1,
  72. sort: 1,
  73. },
  74. },
  75. );
  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. myCardsList() {
  162. const userId = Meteor.userId();
  163. const cursor = Cards.find(
  164. {
  165. $or: [{ members: userId }, { assignees: userId }],
  166. archived: false,
  167. },
  168. {
  169. sort: {
  170. dueAt: -1,
  171. boardId: 1,
  172. swimlaneId: 1,
  173. listId: 1,
  174. sort: 1,
  175. },
  176. },
  177. );
  178. // eslint-disable-next-line no-console
  179. // console.log('cursor:', cursor);
  180. const cards = [];
  181. cursor.forEach(card => {
  182. if (
  183. !card.getBoard().archived &&
  184. !card.getSwimlane().archived &&
  185. !card.getList().archived
  186. ) {
  187. cards.push(card);
  188. }
  189. });
  190. cards.sort((a, b) => {
  191. const x = a.dueAt === null ? Date('2100-12-31') : a.dueAt;
  192. const y = b.dueAt === null ? Date('2100-12-31') : b.dueAt;
  193. if (x > y) return 1;
  194. else if (x < y) return -1;
  195. return 0;
  196. });
  197. // eslint-disable-next-line no-console
  198. // console.log('cursor:', cards);
  199. return cards;
  200. },
  201. events() {
  202. return [
  203. {
  204. // 'click .js-my-card'(evt) {
  205. // const card = this.currentData().card;
  206. // // eslint-disable-next-line no-console
  207. // console.log('currentData():', this.currentData());
  208. // // eslint-disable-next-line no-console
  209. // console.log('card:', card);
  210. // if (card) {
  211. // Utils.goCardId(card._id);
  212. // }
  213. // evt.preventDefault();
  214. // },
  215. },
  216. ];
  217. },
  218. }).register('myCards');