list.js 6.8 KB

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