list.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import { TAPi18n } from '/imports/i18n';
  2. require('/client/lib/jquery-ui.js')
  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(), {sort: ['sort']}).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. sort(event, ui) {
  111. const $boardCanvas = $('.board-canvas');
  112. const boardCanvas = $boardCanvas[0];
  113. if (event.pageX < 10)
  114. { // scroll to the left
  115. boardCanvas.scrollLeft -= 15;
  116. ui.helper[0].offsetLeft -= 15;
  117. }
  118. if (
  119. event.pageX > boardCanvas.offsetWidth - 10 &&
  120. boardCanvas.scrollLeft < $boardCanvas.data('scrollLeftMax') // don't scroll more than possible
  121. )
  122. { // scroll to the right
  123. boardCanvas.scrollLeft += 15;
  124. }
  125. if (
  126. event.pageY > boardCanvas.offsetHeight - 10 &&
  127. event.pageY + boardCanvas.scrollTop < $boardCanvas.data('scrollTopMax') // don't scroll more than possible
  128. )
  129. { // scroll to the bottom
  130. boardCanvas.scrollTop += 15;
  131. }
  132. if (event.pageY < 10)
  133. { // scroll to the top
  134. boardCanvas.scrollTop -= 15;
  135. }
  136. },
  137. activate(event, ui) {
  138. const $boardCanvas = $('.board-canvas');
  139. const boardCanvas = $boardCanvas[0];
  140. // scrollTopMax and scrollLeftMax only available at Firefox (https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollTopMax)
  141. // https://www.it-swarm.com.de/de/javascript/so-erhalten-sie-den-maximalen-dokument-scrolltop-wert/1069126844/
  142. $boardCanvas.data('scrollTopMax', boardCanvas.scrollHeight - boardCanvas.clientTop);
  143. // https://stackoverflow.com/questions/5138373/how-do-i-get-the-max-value-of-scrollleft/5704386#5704386
  144. $boardCanvas.data('scrollLeftMax', boardCanvas.scrollWidth - boardCanvas.clientWidth);
  145. },
  146. });
  147. this.autorun(() => {
  148. if (Utils.isMiniScreenOrShowDesktopDragHandles()) {
  149. $cards.sortable({
  150. handle: '.handle',
  151. });
  152. } else {
  153. $cards.sortable({
  154. handle: '.minicard',
  155. });
  156. }
  157. if ($cards.data('uiSortable') || $cards.data('sortable')) {
  158. $cards.sortable(
  159. 'option',
  160. 'disabled',
  161. // Disable drag-dropping when user is not member
  162. !userIsMember(),
  163. // Not disable drag-dropping while in multi-selection mode
  164. // MultiSelection.isActive() || !userIsMember(),
  165. );
  166. }
  167. });
  168. // We want to re-run this function any time a card is added.
  169. this.autorun(() => {
  170. const currentBoardId = Tracker.nonreactive(() => {
  171. return Session.get('currentBoard');
  172. });
  173. Cards.find({ boardId: currentBoardId }).fetch();
  174. Tracker.afterFlush(() => {
  175. $cards.find(itemsSelector).droppable({
  176. hoverClass: 'draggable-hover-card',
  177. accept: '.js-member,.js-label',
  178. drop(event, ui) {
  179. const cardId = Blaze.getData(this)._id;
  180. const card = Cards.findOne(cardId);
  181. if (ui.draggable.hasClass('js-member')) {
  182. const memberId = Blaze.getData(ui.draggable.get(0)).userId;
  183. card.assignMember(memberId);
  184. } else {
  185. const labelId = Blaze.getData(ui.draggable.get(0))._id;
  186. card.addLabel(labelId);
  187. }
  188. },
  189. });
  190. });
  191. });
  192. },
  193. }).register('list');
  194. Template.miniList.events({
  195. 'click .js-select-list'() {
  196. const listId = this._id;
  197. Session.set('currentList', listId);
  198. },
  199. });