boardsList.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. $boards.sortable('option', 'disabled', !userIsAllowedToMove());
  66. });
  67. },
  68. boards() {
  69. const query = {
  70. archived: false,
  71. type: 'board',
  72. };
  73. if (FlowRouter.getRouteName() === 'home')
  74. query['members.userId'] = Meteor.userId();
  75. else query.permission = 'public';
  76. return Boards.find(query, {
  77. sort: { sort: 1 /* boards default sorting */ },
  78. });
  79. },
  80. isStarred() {
  81. const user = Meteor.user();
  82. return user && user.hasStarred(this.currentData()._id);
  83. },
  84. isAdministrable() {
  85. const user = Meteor.user();
  86. return user && user.isBoardAdmin(this.currentData()._id);
  87. },
  88. hasOvertimeCards() {
  89. subManager.subscribe('board', this.currentData()._id, false);
  90. return this.currentData().hasOvertimeCards();
  91. },
  92. hasSpentTimeCards() {
  93. subManager.subscribe('board', this.currentData()._id, false);
  94. return this.currentData().hasSpentTimeCards();
  95. },
  96. isInvited() {
  97. const user = Meteor.user();
  98. return user && user.isInvitedTo(this.currentData()._id);
  99. },
  100. events() {
  101. return [
  102. {
  103. 'click .js-add-board': Popup.open('createBoard'),
  104. 'click .js-star-board'(evt) {
  105. const boardId = this.currentData()._id;
  106. Meteor.user().toggleBoardStar(boardId);
  107. evt.preventDefault();
  108. },
  109. 'click .js-clone-board'(evt) {
  110. Meteor.call(
  111. 'cloneBoard',
  112. this.currentData()._id,
  113. Session.get('fromBoard'),
  114. (err, res) => {
  115. if (err) {
  116. this.setError(err.error);
  117. } else {
  118. Session.set('fromBoard', null);
  119. Utils.goBoardId(res);
  120. }
  121. },
  122. );
  123. evt.preventDefault();
  124. },
  125. 'click .js-archive-board'(evt) {
  126. const boardId = this.currentData()._id;
  127. Meteor.call('archiveBoard', boardId);
  128. evt.preventDefault();
  129. },
  130. 'click .js-accept-invite'() {
  131. const boardId = this.currentData()._id;
  132. Meteor.call('acceptInvite', boardId);
  133. },
  134. 'click .js-decline-invite'() {
  135. const boardId = this.currentData()._id;
  136. Meteor.call('quitBoard', boardId, (err, ret) => {
  137. if (!err && ret) {
  138. Meteor.call('acceptInvite', boardId);
  139. FlowRouter.go('home');
  140. }
  141. });
  142. },
  143. },
  144. ];
  145. },
  146. }).register('boardList');