list.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import { Cookies } from 'meteor/ostrio:cookies';
  2. const cookies = new Cookies();
  3. const { calculateIndex, enableClickOnTouch } = 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. let swimlaneId = '';
  72. if (
  73. Utils.boardView() === 'board-view-swimlanes' ||
  74. currentBoard.isTemplatesBoard()
  75. )
  76. swimlaneId = Blaze.getData(ui.item.parents('.swimlane').get(0))._id;
  77. else if (
  78. Utils.boardView() === 'board-view-lists' ||
  79. Utils.boardView() === 'board-view-cal' ||
  80. !Utils.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. this.autorun(() => {
  111. let showDesktopDragHandles = false;
  112. currentUser = Meteor.user();
  113. if (currentUser) {
  114. showDesktopDragHandles = (currentUser.profile || {})
  115. .showDesktopDragHandles;
  116. } else if (cookies.has('showDesktopDragHandles')) {
  117. showDesktopDragHandles = true;
  118. } else {
  119. showDesktopDragHandles = false;
  120. }
  121. if (Utils.isMiniScreen() || showDesktopDragHandles) {
  122. $cards.sortable({
  123. handle: '.handle',
  124. });
  125. } else if (!Utils.isMiniScreen() && !showDesktopDragHandles) {
  126. $cards.sortable({
  127. handle: '.minicard',
  128. });
  129. }
  130. if ($cards.data('uiSortable') || $cards.data('sortable')) {
  131. $cards.sortable(
  132. 'option',
  133. 'disabled',
  134. // Disable drag-dropping when user is not member
  135. !userIsMember(),
  136. // Not disable drag-dropping while in multi-selection mode
  137. // MultiSelection.isActive() || !userIsMember(),
  138. );
  139. }
  140. });
  141. // We want to re-run this function any time a card is added.
  142. this.autorun(() => {
  143. const currentBoardId = Tracker.nonreactive(() => {
  144. return Session.get('currentBoard');
  145. });
  146. Cards.find({ boardId: currentBoardId }).fetch();
  147. Tracker.afterFlush(() => {
  148. $cards.find(itemsSelector).droppable({
  149. hoverClass: 'draggable-hover-card',
  150. accept: '.js-member,.js-label',
  151. drop(event, ui) {
  152. const cardId = Blaze.getData(this)._id;
  153. const card = Cards.findOne(cardId);
  154. if (ui.draggable.hasClass('js-member')) {
  155. const memberId = Blaze.getData(ui.draggable.get(0)).userId;
  156. card.assignMember(memberId);
  157. } else {
  158. const labelId = Blaze.getData(ui.draggable.get(0))._id;
  159. card.addLabel(labelId);
  160. }
  161. },
  162. });
  163. });
  164. });
  165. },
  166. }).register('list');
  167. Template.list.helpers({
  168. showDesktopDragHandles() {
  169. currentUser = Meteor.user();
  170. if (currentUser) {
  171. return (currentUser.profile || {}).showDesktopDragHandles;
  172. } else if (cookies.has('showDesktopDragHandles')) {
  173. return true;
  174. } else {
  175. return false;
  176. }
  177. },
  178. });
  179. Template.miniList.events({
  180. 'click .js-select-list'() {
  181. const listId = this._id;
  182. Session.set('currentList', listId);
  183. },
  184. });