list.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import { Cookies } from 'meteor/ostrio:cookies';
  2. const cookies = new Cookies();
  3. const { calculateIndex } = Utils;
  4. BlazeComponent.extendComponent({
  5. // Proxy
  6. openForm(options) {
  7. this.childComponents('listBody')[0].openForm(options);
  8. },
  9. onCreated() {
  10. this.newCardFormIsVisible = new ReactiveVar(true);
  11. },
  12. // The jquery UI sortable library is the best solution I've found so far. I
  13. // tried sortable and dragula but they were not powerful enough four our use
  14. // case. I also considered writing/forking a drag-and-drop + sortable library
  15. // but it's probably too much work.
  16. // By calling asking the sortable library to cancel its move on the `stop`
  17. // callback, we basically solve all issues related to reactive updates. A
  18. // comment below provides further details.
  19. onRendered() {
  20. const boardComponent = this.parentComponent().parentComponent();
  21. function userIsMember() {
  22. return (
  23. Meteor.user() &&
  24. Meteor.user().isBoardMember() &&
  25. !Meteor.user().isCommentOnly()
  26. );
  27. }
  28. const itemsSelector = '.js-minicard:not(.placeholder, .js-card-composer)';
  29. const $cards = this.$('.js-minicards');
  30. $cards.sortable({
  31. connectWith: '.js-minicards:not(.js-list-full)',
  32. tolerance: 'pointer',
  33. appendTo: '.board-canvas',
  34. helper(evt, item) {
  35. const helper = item.clone();
  36. if (MultiSelection.isActive()) {
  37. const andNOthers = $cards.find('.js-minicard.is-checked').length - 1;
  38. if (andNOthers > 0) {
  39. helper.append(
  40. $(
  41. Blaze.toHTML(
  42. HTML.DIV(
  43. { class: 'and-n-other' },
  44. TAPi18n.__('and-n-other-card', { count: andNOthers }),
  45. ),
  46. ),
  47. ),
  48. );
  49. }
  50. }
  51. return helper;
  52. },
  53. distance: 7,
  54. items: itemsSelector,
  55. placeholder: 'minicard-wrapper placeholder',
  56. start(evt, ui) {
  57. ui.helper.css('z-index', 1000);
  58. ui.placeholder.height(ui.helper.height());
  59. EscapeActions.executeUpTo('popup-close');
  60. boardComponent.setIsDragging(true);
  61. },
  62. stop(evt, ui) {
  63. // To attribute the new index number, we need to get the DOM element
  64. // of the previous and the following card -- if any.
  65. const prevCardDom = ui.item.prev('.js-minicard').get(0);
  66. const nextCardDom = ui.item.next('.js-minicard').get(0);
  67. const nCards = MultiSelection.isActive() ? MultiSelection.count() : 1;
  68. const sortIndex = calculateIndex(prevCardDom, nextCardDom, nCards);
  69. const listId = Blaze.getData(ui.item.parents('.list').get(0))._id;
  70. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  71. const defaultSwimlaneId = currentBoard.getDefaultSwimline()._id;
  72. let targetSwimlaneId = null;
  73. // only set a new swimelane ID if the swimlanes view is active
  74. if (
  75. Utils.boardView() === 'board-view-swimlanes' ||
  76. currentBoard.isTemplatesBoard()
  77. )
  78. targetSwimlaneId = Blaze.getData(ui.item.parents('.swimlane').get(0))
  79. ._id;
  80. // Normally the jquery-ui sortable library moves the dragged DOM element
  81. // to its new position, which disrupts Blaze reactive updates mechanism
  82. // (especially when we move the last card of a list, or when multiple
  83. // users move some cards at the same time). To prevent these UX glitches
  84. // we ask sortable to gracefully cancel the move, and to put back the
  85. // DOM in its initial state. The card move is then handled reactively by
  86. // Blaze with the below query.
  87. $cards.sortable('cancel');
  88. if (MultiSelection.isActive()) {
  89. Cards.find(MultiSelection.getMongoSelector()).forEach((card, i) => {
  90. const newSwimlaneId = targetSwimlaneId
  91. ? targetSwimlaneId
  92. : card.swimlaneId || defaultSwimlaneId;
  93. card.move(
  94. currentBoard._id,
  95. newSwimlaneId,
  96. listId,
  97. sortIndex.base + i * sortIndex.increment,
  98. );
  99. });
  100. } else {
  101. const cardDomElement = ui.item.get(0);
  102. const card = Blaze.getData(cardDomElement);
  103. const newSwimlaneId = targetSwimlaneId
  104. ? targetSwimlaneId
  105. : card.swimlaneId || defaultSwimlaneId;
  106. card.move(currentBoard._id, newSwimlaneId, listId, sortIndex.base);
  107. }
  108. boardComponent.setIsDragging(false);
  109. },
  110. });
  111. this.autorun(() => {
  112. let showDesktopDragHandles = false;
  113. currentUser = Meteor.user();
  114. if (currentUser) {
  115. showDesktopDragHandles = (currentUser.profile || {})
  116. .showDesktopDragHandles;
  117. } else if (cookies.has('showDesktopDragHandles')) {
  118. showDesktopDragHandles = true;
  119. } else {
  120. showDesktopDragHandles = false;
  121. }
  122. if (Utils.isMiniScreen() || showDesktopDragHandles) {
  123. $cards.sortable({
  124. handle: '.handle',
  125. });
  126. } else if (!Utils.isMiniScreen() && !showDesktopDragHandles) {
  127. $cards.sortable({
  128. handle: '.minicard',
  129. });
  130. }
  131. if ($cards.data('uiSortable') || $cards.data('sortable')) {
  132. $cards.sortable(
  133. 'option',
  134. 'disabled',
  135. // Disable drag-dropping when user is not member
  136. !userIsMember(),
  137. // Not disable drag-dropping while in multi-selection mode
  138. // MultiSelection.isActive() || !userIsMember(),
  139. );
  140. }
  141. });
  142. // We want to re-run this function any time a card is added.
  143. this.autorun(() => {
  144. const currentBoardId = Tracker.nonreactive(() => {
  145. return Session.get('currentBoard');
  146. });
  147. Cards.find({ boardId: currentBoardId }).fetch();
  148. Tracker.afterFlush(() => {
  149. $cards.find(itemsSelector).droppable({
  150. hoverClass: 'draggable-hover-card',
  151. accept: '.js-member,.js-label',
  152. drop(event, ui) {
  153. const cardId = Blaze.getData(this)._id;
  154. const card = Cards.findOne(cardId);
  155. if (ui.draggable.hasClass('js-member')) {
  156. const memberId = Blaze.getData(ui.draggable.get(0)).userId;
  157. card.assignMember(memberId);
  158. } else {
  159. const labelId = Blaze.getData(ui.draggable.get(0))._id;
  160. card.addLabel(labelId);
  161. }
  162. },
  163. });
  164. });
  165. });
  166. },
  167. }).register('list');
  168. Template.list.helpers({
  169. showDesktopDragHandles() {
  170. currentUser = Meteor.user();
  171. if (currentUser) {
  172. return (currentUser.profile || {}).showDesktopDragHandles;
  173. } else if (cookies.has('showDesktopDragHandles')) {
  174. return true;
  175. } else {
  176. return false;
  177. }
  178. },
  179. });
  180. Template.miniList.events({
  181. 'click .js-select-list'() {
  182. const listId = this._id;
  183. Session.set('currentList', listId);
  184. },
  185. });