boardsList.js 4.8 KB

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