list.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. scrollSpeed: 10,
  57. start(evt, ui) {
  58. ui.helper.css('z-index', 1000);
  59. ui.placeholder.height(ui.helper.height());
  60. EscapeActions.executeUpTo('popup-close');
  61. boardComponent.setIsDragging(true);
  62. },
  63. stop(evt, ui) {
  64. // To attribute the new index number, we need to get the DOM element
  65. // of the previous and the following card -- if any.
  66. const prevCardDom = ui.item.prev('.js-minicard').get(0);
  67. const nextCardDom = ui.item.next('.js-minicard').get(0);
  68. const nCards = MultiSelection.isActive() ? MultiSelection.count() : 1;
  69. const sortIndex = calculateIndex(prevCardDom, nextCardDom, nCards);
  70. const listId = Blaze.getData(ui.item.parents('.list').get(0))._id;
  71. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  72. const defaultSwimlaneId = currentBoard.getDefaultSwimline()._id;
  73. let targetSwimlaneId = null;
  74. // only set a new swimelane ID if the swimlanes view is active
  75. if (
  76. Utils.boardView() === 'board-view-swimlanes' ||
  77. currentBoard.isTemplatesBoard()
  78. )
  79. targetSwimlaneId = Blaze.getData(ui.item.parents('.swimlane').get(0))
  80. ._id;
  81. // Normally the jquery-ui sortable library moves the dragged DOM element
  82. // to its new position, which disrupts Blaze reactive updates mechanism
  83. // (especially when we move the last card of a list, or when multiple
  84. // users move some cards at the same time). To prevent these UX glitches
  85. // we ask sortable to gracefully cancel the move, and to put back the
  86. // DOM in its initial state. The card move is then handled reactively by
  87. // Blaze with the below query.
  88. $cards.sortable('cancel');
  89. if (MultiSelection.isActive()) {
  90. Cards.find(MultiSelection.getMongoSelector(), { sort: ['sort'] }).forEach((card, i) => {
  91. const newSwimlaneId = targetSwimlaneId
  92. ? targetSwimlaneId
  93. : card.swimlaneId || defaultSwimlaneId;
  94. card.move(
  95. currentBoard._id,
  96. newSwimlaneId,
  97. listId,
  98. sortIndex.base + i * sortIndex.increment,
  99. );
  100. });
  101. } else {
  102. const cardDomElement = ui.item.get(0);
  103. const card = Blaze.getData(cardDomElement);
  104. const newSwimlaneId = targetSwimlaneId
  105. ? targetSwimlaneId
  106. : card.swimlaneId || defaultSwimlaneId;
  107. card.move(currentBoard._id, newSwimlaneId, listId, sortIndex.base);
  108. }
  109. boardComponent.setIsDragging(false);
  110. },
  111. sort(event, ui) {
  112. const $boardCanvas = $('.board-canvas');
  113. const boardCanvas = $boardCanvas[0];
  114. if (event.pageX < 10) { // 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. ) { // scroll to the right
  122. boardCanvas.scrollLeft += 15;
  123. }
  124. if (
  125. event.pageY > boardCanvas.offsetHeight - 10 &&
  126. event.pageY + boardCanvas.scrollTop < $boardCanvas.data('scrollTopMax') // don't scroll more than possible
  127. ) { // scroll to the bottom
  128. boardCanvas.scrollTop += 15;
  129. }
  130. if (event.pageY < 10) { // scroll to the top
  131. boardCanvas.scrollTop -= 15;
  132. }
  133. },
  134. activate(event, ui) {
  135. const $boardCanvas = $('.board-canvas');
  136. const boardCanvas = $boardCanvas[0];
  137. // scrollTopMax and scrollLeftMax only available at Firefox (https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollTopMax)
  138. // https://www.it-swarm.com.de/de/javascript/so-erhalten-sie-den-maximalen-dokument-scrolltop-wert/1069126844/
  139. $boardCanvas.data('scrollTopMax', boardCanvas.scrollHeight - boardCanvas.clientTop);
  140. // https://stackoverflow.com/questions/5138373/how-do-i-get-the-max-value-of-scrollleft/5704386#5704386
  141. $boardCanvas.data('scrollLeftMax', boardCanvas.scrollWidth - boardCanvas.clientWidth);
  142. },
  143. });
  144. this.autorun(() => {
  145. if (Utils.isTouchScreenOrShowDesktopDragHandles()) {
  146. $cards.sortable({
  147. handle: '.handle',
  148. });
  149. } else {
  150. $cards.sortable({
  151. handle: '.minicard',
  152. });
  153. }
  154. if ($cards.data('uiSortable') || $cards.data('sortable')) {
  155. $cards.sortable(
  156. 'option',
  157. 'disabled',
  158. // Disable drag-dropping when user is not member
  159. !userIsMember(),
  160. // Not disable drag-dropping while in multi-selection mode
  161. // MultiSelection.isActive() || !userIsMember(),
  162. );
  163. }
  164. });
  165. // We want to re-run this function any time a card is added.
  166. this.autorun(() => {
  167. const currentBoardId = Tracker.nonreactive(() => {
  168. return Session.get('currentBoard');
  169. });
  170. Cards.find({ boardId: currentBoardId }).fetch();
  171. Tracker.afterFlush(() => {
  172. $cards.find(itemsSelector).droppable({
  173. hoverClass: 'draggable-hover-card',
  174. accept: '.js-member,.js-label',
  175. drop(event, ui) {
  176. const cardId = Blaze.getData(this)._id;
  177. const card = Cards.findOne(cardId);
  178. if (ui.draggable.hasClass('js-member')) {
  179. const memberId = Blaze.getData(ui.draggable.get(0)).userId;
  180. card.assignMember(memberId);
  181. } else {
  182. const labelId = Blaze.getData(ui.draggable.get(0))._id;
  183. card.addLabel(labelId);
  184. }
  185. },
  186. });
  187. });
  188. });
  189. },
  190. }).register('list');
  191. Template.miniList.events({
  192. 'click .js-select-list'() {
  193. const listId = this._id;
  194. Session.set('currentList', listId);
  195. },
  196. });