boardsList.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. const subManager = new SubsManager();
  2. const { calculateIndex, enableClickOnTouch } = Utils;
  3. Template.boardListHeaderBar.events({
  4. 'click .js-open-archived-board'() {
  5. Modal.open('archivedBoards');
  6. },
  7. });
  8. Template.boardListHeaderBar.helpers({
  9. title() {
  10. return FlowRouter.getRouteName() === 'home' ? 'my-boards' : 'public';
  11. },
  12. templatesBoardId() {
  13. return Meteor.user() && Meteor.user().getTemplatesBoardId();
  14. },
  15. templatesBoardSlug() {
  16. return Meteor.user() && Meteor.user().getTemplatesBoardSlug();
  17. },
  18. });
  19. BlazeComponent.extendComponent({
  20. onCreated() {
  21. Meteor.subscribe('setting');
  22. },
  23. onRendered() {
  24. function userIsAllowedToMove() {
  25. return Meteor.user();
  26. }
  27. const itemsSelector = '.js-board:not(.placeholder)';
  28. const $boards = this.$('.js-boards');
  29. $boards.sortable({
  30. connectWith: '.js-boards',
  31. tolerance: 'pointer',
  32. appendTo: '.board-list',
  33. helper: 'clone',
  34. distance: 7,
  35. items: itemsSelector,
  36. placeholder: 'board-wrapper placeholder',
  37. start(evt, ui) {
  38. ui.helper.css('z-index', 1000);
  39. ui.placeholder.height(ui.helper.height());
  40. EscapeActions.executeUpTo('popup-close');
  41. },
  42. stop(evt, ui) {
  43. // To attribute the new index number, we need to get the DOM element
  44. // of the previous and the following card -- if any.
  45. const prevBoardDom = ui.item.prev('.js-board').get(0);
  46. const nextBoardBom = ui.item.next('.js-board').get(0);
  47. const sortIndex = calculateIndex(prevBoardDom, nextBoardBom, 1);
  48. const boardDomElement = ui.item.get(0);
  49. const board = Blaze.getData(boardDomElement);
  50. // Normally the jquery-ui sortable library moves the dragged DOM element
  51. // to its new position, which disrupts Blaze reactive updates mechanism
  52. // (especially when we move the last card of a list, or when multiple
  53. // users move some cards at the same time). To prevent these UX glitches
  54. // we ask sortable to gracefully cancel the move, and to put back the
  55. // DOM in its initial state. The card move is then handled reactively by
  56. // Blaze with the below query.
  57. $boards.sortable('cancel');
  58. board.move(sortIndex.base);
  59. },
  60. });
  61. // ugly touch event hotfix
  62. enableClickOnTouch(itemsSelector);
  63. // Disable drag-dropping if the current user is not a board member or is comment only
  64. this.autorun(() => {
  65. if (Utils.isMiniScreen()) {
  66. $boards.sortable({
  67. handle: '.board-handle',
  68. });
  69. }
  70. $boards.sortable('option', 'disabled', !userIsAllowedToMove());
  71. });
  72. },
  73. boards() {
  74. const query = {
  75. archived: false,
  76. type: 'board',
  77. };
  78. if (FlowRouter.getRouteName() === 'home')
  79. query['members.userId'] = Meteor.userId();
  80. else query.permission = 'public';
  81. return Boards.find(query, {
  82. sort: { sort: 1 /* boards default sorting */ },
  83. });
  84. },
  85. isStarred() {
  86. const user = Meteor.user();
  87. return user && user.hasStarred(this.currentData()._id);
  88. },
  89. isAdministrable() {
  90. const user = Meteor.user();
  91. return user && user.isBoardAdmin(this.currentData()._id);
  92. },
  93. hasOvertimeCards() {
  94. subManager.subscribe('board', this.currentData()._id, false);
  95. return this.currentData().hasOvertimeCards();
  96. },
  97. hasSpentTimeCards() {
  98. subManager.subscribe('board', this.currentData()._id, false);
  99. return this.currentData().hasSpentTimeCards();
  100. },
  101. isInvited() {
  102. const user = Meteor.user();
  103. return user && user.isInvitedTo(this.currentData()._id);
  104. },
  105. events() {
  106. return [
  107. {
  108. 'click .js-add-board': Popup.open('createBoard'),
  109. 'click .js-star-board'(evt) {
  110. const boardId = this.currentData()._id;
  111. Meteor.user().toggleBoardStar(boardId);
  112. evt.preventDefault();
  113. },
  114. 'click .js-clone-board'(evt) {
  115. Meteor.call(
  116. 'cloneBoard',
  117. this.currentData()._id,
  118. Session.get('fromBoard'),
  119. (err, res) => {
  120. if (err) {
  121. this.setError(err.error);
  122. } else {
  123. Session.set('fromBoard', null);
  124. Utils.goBoardId(res);
  125. }
  126. },
  127. );
  128. evt.preventDefault();
  129. },
  130. 'click .js-archive-board'(evt) {
  131. const boardId = this.currentData()._id;
  132. Meteor.call('archiveBoard', boardId);
  133. evt.preventDefault();
  134. },
  135. 'click .js-accept-invite'() {
  136. const boardId = this.currentData()._id;
  137. Meteor.call('acceptInvite', boardId);
  138. },
  139. 'click .js-decline-invite'() {
  140. const boardId = this.currentData()._id;
  141. Meteor.call('quitBoard', boardId, (err, ret) => {
  142. if (!err && ret) {
  143. Meteor.call('acceptInvite', boardId);
  144. FlowRouter.go('home');
  145. }
  146. });
  147. },
  148. },
  149. ];
  150. },
  151. }).register('boardList');