list.js 6.1 KB

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