list.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 Meteor.user() && Meteor.user().isBoardMember() && !Meteor.user().isCommentOnly();
  21. }
  22. const itemsSelector = '.js-minicard:not(.placeholder, .js-card-composer)';
  23. const $cards = this.$('.js-minicards');
  24. if(window.matchMedia('(max-width: 1199px)').matches) {
  25. $( '.js-minicards' ).sortable({
  26. handle: '.handle',
  27. });
  28. }
  29. $cards.sortable({
  30. connectWith: '.js-minicards:not(.js-list-full)',
  31. tolerance: 'pointer',
  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($(Blaze.toHTML(HTML.DIV(
  39. { 'class': 'and-n-other' },
  40. TAPi18n.__('and-n-other-card', { count: andNOthers })
  41. ))));
  42. }
  43. }
  44. return helper;
  45. },
  46. distance: 7,
  47. items: itemsSelector,
  48. placeholder: 'minicard-wrapper placeholder',
  49. start(evt, ui) {
  50. ui.helper.css('z-index', 1000);
  51. ui.placeholder.height(ui.helper.height());
  52. EscapeActions.executeUpTo('popup-close');
  53. boardComponent.setIsDragging(true);
  54. },
  55. stop(evt, ui) {
  56. // To attribute the new index number, we need to get the DOM element
  57. // of the previous and the following card -- if any.
  58. const prevCardDom = ui.item.prev('.js-minicard').get(0);
  59. const nextCardDom = ui.item.next('.js-minicard').get(0);
  60. const nCards = MultiSelection.isActive() ? MultiSelection.count() : 1;
  61. const sortIndex = calculateIndex(prevCardDom, nextCardDom, nCards);
  62. const listId = Blaze.getData(ui.item.parents('.list').get(0))._id;
  63. const swimlaneId = Blaze.getData(ui.item.parents('.swimlane').get(0))._id;
  64. // Normally the jquery-ui sortable library moves the dragged DOM element
  65. // to its new position, which disrupts Blaze reactive updates mechanism
  66. // (especially when we move the last card of a list, or when multiple
  67. // users move some cards at the same time). To prevent these UX glitches
  68. // we ask sortable to gracefully cancel the move, and to put back the
  69. // DOM in its initial state. The card move is then handled reactively by
  70. // Blaze with the below query.
  71. $cards.sortable('cancel');
  72. if (MultiSelection.isActive()) {
  73. Cards.find(MultiSelection.getMongoSelector()).forEach((card, i) => {
  74. card.move(swimlaneId, listId, sortIndex.base + i * sortIndex.increment);
  75. });
  76. } else {
  77. const cardDomElement = ui.item.get(0);
  78. const card = Blaze.getData(cardDomElement);
  79. card.move(swimlaneId, listId, sortIndex.base);
  80. }
  81. boardComponent.setIsDragging(false);
  82. },
  83. });
  84. // ugly touch event hotfix
  85. enableClickOnTouch(itemsSelector);
  86. // Disable drag-dropping if the current user is not a board member or is comment only
  87. this.autorun(() => {
  88. $cards.sortable('option', 'disabled', !userIsMember());
  89. });
  90. // We want to re-run this function any time a card is added.
  91. this.autorun(() => {
  92. const currentBoardId = Tracker.nonreactive(() => {
  93. return Session.get('currentBoard');
  94. });
  95. Cards.find({ boardId: currentBoardId }).fetch();
  96. Tracker.afterFlush(() => {
  97. $cards.find(itemsSelector).droppable({
  98. hoverClass: 'draggable-hover-card',
  99. accept: '.js-member,.js-label',
  100. drop(event, ui) {
  101. const cardId = Blaze.getData(this)._id;
  102. const card = Cards.findOne(cardId);
  103. if (ui.draggable.hasClass('js-member')) {
  104. const memberId = Blaze.getData(ui.draggable.get(0)).userId;
  105. card.assignMember(memberId);
  106. } else {
  107. const labelId = Blaze.getData(ui.draggable.get(0))._id;
  108. card.addLabel(labelId);
  109. }
  110. },
  111. });
  112. });
  113. });
  114. },
  115. }).register('list');
  116. Template.miniList.events({
  117. 'click .js-select-list'() {
  118. const listId = this._id;
  119. Session.set('currentList', listId);
  120. },
  121. });