list.js 5.9 KB

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