list.js 5.6 KB

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