list.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 (
  21. Meteor.user() &&
  22. Meteor.user().isBoardMember() &&
  23. !Meteor.user().isCommentOnly()
  24. );
  25. }
  26. const itemsSelector = '.js-minicard:not(.placeholder, .js-card-composer)';
  27. const $cards = this.$('.js-minicards');
  28. if (window.matchMedia('(max-width: 1199px)').matches) {
  29. $('.js-minicards').sortable({
  30. handle: '.handle',
  31. });
  32. }
  33. $cards.sortable({
  34. connectWith: '.js-minicards:not(.js-list-full)',
  35. tolerance: 'pointer',
  36. appendTo: '.board-canvas',
  37. helper(evt, item) {
  38. const helper = item.clone();
  39. if (MultiSelection.isActive()) {
  40. const andNOthers = $cards.find('.js-minicard.is-checked').length - 1;
  41. if (andNOthers > 0) {
  42. helper.append(
  43. $(
  44. Blaze.toHTML(
  45. HTML.DIV(
  46. { class: 'and-n-other' },
  47. TAPi18n.__('and-n-other-card', { count: andNOthers }),
  48. ),
  49. ),
  50. ),
  51. );
  52. }
  53. }
  54. return helper;
  55. },
  56. distance: 7,
  57. items: itemsSelector,
  58. placeholder: 'minicard-wrapper placeholder',
  59. start(evt, ui) {
  60. ui.helper.css('z-index', 1000);
  61. ui.placeholder.height(ui.helper.height());
  62. EscapeActions.executeUpTo('popup-close');
  63. boardComponent.setIsDragging(true);
  64. },
  65. stop(evt, ui) {
  66. // To attribute the new index number, we need to get the DOM element
  67. // of the previous and the following card -- if any.
  68. const prevCardDom = ui.item.prev('.js-minicard').get(0);
  69. const nextCardDom = ui.item.next('.js-minicard').get(0);
  70. const nCards = MultiSelection.isActive() ? MultiSelection.count() : 1;
  71. const sortIndex = calculateIndex(prevCardDom, nextCardDom, nCards);
  72. const listId = Blaze.getData(ui.item.parents('.list').get(0))._id;
  73. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  74. let swimlaneId = '';
  75. const boardView = (Meteor.user().profile || {}).boardView;
  76. if (
  77. boardView === 'board-view-swimlanes' ||
  78. currentBoard.isTemplatesBoard()
  79. )
  80. swimlaneId = Blaze.getData(ui.item.parents('.swimlane').get(0))._id;
  81. else if (
  82. boardView === 'board-view-lists' ||
  83. boardView === 'board-view-cal' ||
  84. !boardView
  85. )
  86. swimlaneId = currentBoard.getDefaultSwimline()._id;
  87. // Normally the jquery-ui sortable library moves the dragged DOM element
  88. // to its new position, which disrupts Blaze reactive updates mechanism
  89. // (especially when we move the last card of a list, or when multiple
  90. // users move some cards at the same time). To prevent these UX glitches
  91. // we ask sortable to gracefully cancel the move, and to put back the
  92. // DOM in its initial state. The card move is then handled reactively by
  93. // Blaze with the below query.
  94. $cards.sortable('cancel');
  95. if (MultiSelection.isActive()) {
  96. Cards.find(MultiSelection.getMongoSelector()).forEach((card, i) => {
  97. card.move(
  98. currentBoard._id,
  99. swimlaneId,
  100. listId,
  101. sortIndex.base + i * sortIndex.increment,
  102. );
  103. });
  104. } else {
  105. const cardDomElement = ui.item.get(0);
  106. const card = Blaze.getData(cardDomElement);
  107. card.move(currentBoard._id, swimlaneId, listId, sortIndex.base);
  108. }
  109. boardComponent.setIsDragging(false);
  110. },
  111. });
  112. // ugly touch event hotfix
  113. enableClickOnTouch(itemsSelector);
  114. // Disable drag-dropping if the current user is not a board member or is comment only
  115. this.autorun(() => {
  116. $cards.sortable('option', 'disabled', !userIsMember());
  117. });
  118. // We want to re-run this function any time a card is added.
  119. this.autorun(() => {
  120. const currentBoardId = Tracker.nonreactive(() => {
  121. return Session.get('currentBoard');
  122. });
  123. Cards.find({ boardId: currentBoardId }).fetch();
  124. Tracker.afterFlush(() => {
  125. $cards.find(itemsSelector).droppable({
  126. hoverClass: 'draggable-hover-card',
  127. accept: '.js-member,.js-label',
  128. drop(event, ui) {
  129. const cardId = Blaze.getData(this)._id;
  130. const card = Cards.findOne(cardId);
  131. if (ui.draggable.hasClass('js-member')) {
  132. const memberId = Blaze.getData(ui.draggable.get(0)).userId;
  133. card.assignMember(memberId);
  134. } else {
  135. const labelId = Blaze.getData(ui.draggable.get(0))._id;
  136. card.addLabel(labelId);
  137. }
  138. },
  139. });
  140. });
  141. });
  142. },
  143. }).register('list');
  144. Template.miniList.events({
  145. 'click .js-select-list'() {
  146. const listId = this._id;
  147. Session.set('currentList', listId);
  148. },
  149. });