boardsList.js 4.8 KB

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