list.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. $cards.sortable({
  29. connectWith: '.js-minicards:not(.js-list-full)',
  30. tolerance: 'pointer',
  31. handle: 'list-header',
  32. appendTo: '.board-canvas',
  33. helper(evt, item) {
  34. const helper = item.clone();
  35. if (MultiSelection.isActive()) {
  36. const andNOthers = $cards.find('.js-minicard.is-checked').length - 1;
  37. if (andNOthers > 0) {
  38. helper.append(
  39. $(
  40. Blaze.toHTML(
  41. HTML.DIV(
  42. { class: 'and-n-other' },
  43. TAPi18n.__('and-n-other-card', { count: andNOthers }),
  44. ),
  45. ),
  46. ),
  47. );
  48. }
  49. }
  50. return helper;
  51. },
  52. distance: 7,
  53. items: itemsSelector,
  54. placeholder: 'minicard-wrapper placeholder',
  55. start(evt, ui) {
  56. ui.helper.css('z-index', 1000);
  57. ui.placeholder.height(ui.helper.height());
  58. EscapeActions.executeUpTo('popup-close');
  59. boardComponent.setIsDragging(true);
  60. },
  61. stop(evt, ui) {
  62. // To attribute the new index number, we need to get the DOM element
  63. // of the previous and the following card -- if any.
  64. const prevCardDom = ui.item.prev('.js-minicard').get(0);
  65. const nextCardDom = ui.item.next('.js-minicard').get(0);
  66. const nCards = MultiSelection.isActive() ? MultiSelection.count() : 1;
  67. const sortIndex = calculateIndex(prevCardDom, nextCardDom, nCards);
  68. const listId = Blaze.getData(ui.item.parents('.list').get(0))._id;
  69. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  70. let swimlaneId = '';
  71. const boardView = (Meteor.user().profile || {}).boardView;
  72. if (
  73. boardView === 'board-view-swimlanes' ||
  74. currentBoard.isTemplatesBoard()
  75. )
  76. swimlaneId = Blaze.getData(ui.item.parents('.swimlane').get(0))._id;
  77. else if (
  78. boardView === 'board-view-lists' ||
  79. boardView === 'board-view-cal' ||
  80. !boardView
  81. )
  82. swimlaneId = currentBoard.getDefaultSwimline()._id;
  83. // Normally the jquery-ui sortable library moves the dragged DOM element
  84. // to its new position, which disrupts Blaze reactive updates mechanism
  85. // (especially when we move the last card of a list, or when multiple
  86. // users move some cards at the same time). To prevent these UX glitches
  87. // we ask sortable to gracefully cancel the move, and to put back the
  88. // DOM in its initial state. The card move is then handled reactively by
  89. // Blaze with the below query.
  90. $cards.sortable('cancel');
  91. if (MultiSelection.isActive()) {
  92. Cards.find(MultiSelection.getMongoSelector()).forEach((card, i) => {
  93. card.move(
  94. currentBoard._id,
  95. swimlaneId,
  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. card.move(currentBoard._id, swimlaneId, listId, sortIndex.base);
  104. }
  105. boardComponent.setIsDragging(false);
  106. },
  107. });
  108. // ugly touch event hotfix
  109. enableClickOnTouch(itemsSelector);
  110. // Disable drag-dropping if the current user is not a board member or is comment only
  111. this.autorun(() => {
  112. $cards.sortable('option', 'disabled', !userIsMember());
  113. if (Utils.isMiniScreen()) {
  114. this.$('.js-minicards').sortable({
  115. handle: '.handle',
  116. });
  117. } else {
  118. if (Meteor.user().hasShowDesktopDragHandles()) {
  119. this.$('.js-minicards').sortable({
  120. handle: '.handle',
  121. });
  122. } else {
  123. this.$('.js-minicards').sortable({
  124. handle: '.minicard-title',
  125. });
  126. }
  127. }
  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. });
  166. Template.miniList.helpers({
  167. showDesktopDragHandles() {
  168. return Meteor.user().hasShowDesktopDragHandles();
  169. },
  170. });