2
0

list.js 7.8 KB

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