list.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. const { calculateIndex, enableClickOnTouch } = Utils;
  2. BlazeComponent.extendComponent({
  3. // Proxy
  4. openForm(options) {
  5. this.childComponents('listBody')[0].openForm(options);
  6. },
  7. onCreated() {
  8. this.newCardFormIsVisible = new ReactiveVar(true);
  9. },
  10. // The jquery UI sortable library is the best solution I've found so far. I
  11. // tried sortable and dragula but they were not powerful enough four our use
  12. // case. I also considered writing/forking a drag-and-drop + sortable library
  13. // but it's probably too much work.
  14. // By calling asking the sortable library to cancel its move on the `stop`
  15. // callback, we basically solve all issues related to reactive updates. A
  16. // comment below provides further details.
  17. onRendered() {
  18. const boardComponent = this.parentComponent().parentComponent();
  19. function userIsMember() {
  20. return Meteor.user() && Meteor.user().isBoardMember() && !Meteor.user().isCommentOnly();
  21. }
  22. const itemsSelector = '.js-minicard:not(.placeholder, .js-card-composer)';
  23. const $cards = this.$('.js-minicards');
  24. if(window.matchMedia('(max-width: 1199px)').matches) {
  25. $( '.js-minicards' ).sortable({
  26. handle: '.handle',
  27. });
  28. }
  29. $cards.sortable({
  30. connectWith: '.js-minicards:not(.js-list-full)',
  31. tolerance: 'pointer',
  32. appendTo: '.board-canvas',
  33. helper(evt, item) {
  34. const helper = item.clone();
  35. if (MultiSelection.isActive()) {
  36. const andNOthers = $cards.find('.js-minicard.is-checked').length - 1;
  37. if (andNOthers > 0) {
  38. helper.append($(Blaze.toHTML(HTML.DIV(
  39. { 'class': 'and-n-other' },
  40. TAPi18n.__('and-n-other-card', { count: andNOthers })
  41. ))));
  42. }
  43. }
  44. return helper;
  45. },
  46. distance: 7,
  47. items: itemsSelector,
  48. placeholder: 'minicard-wrapper placeholder',
  49. start(evt, ui) {
  50. ui.helper.css('z-index', 1000);
  51. ui.placeholder.height(ui.helper.height());
  52. EscapeActions.executeUpTo('popup-close');
  53. boardComponent.setIsDragging(true);
  54. },
  55. stop(evt, ui) {
  56. // To attribute the new index number, we need to get the DOM element
  57. // of the previous and the following card -- if any.
  58. const prevCardDom = ui.item.prev('.js-minicard').get(0);
  59. const nextCardDom = ui.item.next('.js-minicard').get(0);
  60. const nCards = MultiSelection.isActive() ? MultiSelection.count() : 1;
  61. const sortIndex = calculateIndex(prevCardDom, nextCardDom, nCards);
  62. const listId = Blaze.getData(ui.item.parents('.list').get(0))._id;
  63. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  64. let swimlaneId = '';
  65. const boardView = Meteor.user().profile.boardView;
  66. if (boardView === 'board-view-swimlanes')
  67. swimlaneId = Blaze.getData(ui.item.parents('.swimlane').get(0))._id;
  68. else if ((boardView === 'board-view-lists') || (boardView === 'board-view-cal'))
  69. swimlaneId = currentBoard.getDefaultSwimline()._id;
  70. // Normally the jquery-ui sortable library moves the dragged DOM element
  71. // to its new position, which disrupts Blaze reactive updates mechanism
  72. // (especially when we move the last card of a list, or when multiple
  73. // users move some cards at the same time). To prevent these UX glitches
  74. // we ask sortable to gracefully cancel the move, and to put back the
  75. // DOM in its initial state. The card move is then handled reactively by
  76. // Blaze with the below query.
  77. $cards.sortable('cancel');
  78. if (MultiSelection.isActive()) {
  79. Cards.find(MultiSelection.getMongoSelector()).forEach((card, i) => {
  80. card.move(swimlaneId, listId, sortIndex.base + i * sortIndex.increment);
  81. });
  82. } else {
  83. const cardDomElement = ui.item.get(0);
  84. const card = Blaze.getData(cardDomElement);
  85. card.move(swimlaneId, listId, sortIndex.base);
  86. }
  87. boardComponent.setIsDragging(false);
  88. },
  89. });
  90. // ugly touch event hotfix
  91. enableClickOnTouch(itemsSelector);
  92. // Disable drag-dropping if the current user is not a board member or is comment only
  93. this.autorun(() => {
  94. $cards.sortable('option', 'disabled', !userIsMember());
  95. });
  96. // We want to re-run this function any time a card is added.
  97. this.autorun(() => {
  98. const currentBoardId = Tracker.nonreactive(() => {
  99. return Session.get('currentBoard');
  100. });
  101. Cards.find({ boardId: currentBoardId }).fetch();
  102. Tracker.afterFlush(() => {
  103. $cards.find(itemsSelector).droppable({
  104. hoverClass: 'draggable-hover-card',
  105. accept: '.js-member,.js-label',
  106. drop(event, ui) {
  107. const cardId = Blaze.getData(this)._id;
  108. const card = Cards.findOne(cardId);
  109. if (ui.draggable.hasClass('js-member')) {
  110. const memberId = Blaze.getData(ui.draggable.get(0)).userId;
  111. card.assignMember(memberId);
  112. } else {
  113. const labelId = Blaze.getData(ui.draggable.get(0))._id;
  114. card.addLabel(labelId);
  115. }
  116. },
  117. });
  118. });
  119. });
  120. },
  121. }).register('list');
  122. Template.miniList.events({
  123. 'click .js-select-list'() {
  124. const listId = this._id;
  125. Session.set('currentList', listId);
  126. },
  127. });