list.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. const { calculateIndex } = Utils;
  2. BlazeComponent.extendComponent({
  3. template() {
  4. return 'list';
  5. },
  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. if (!Meteor.user() || !Meteor.user().isBoardMember())
  22. return;
  23. const boardComponent = this.parentComponent();
  24. const itemsSelector = '.js-minicard:not(.placeholder, .js-card-composer)';
  25. const $cards = this.$('.js-minicards');
  26. $cards.sortable({
  27. connectWith: '.js-minicards',
  28. tolerance: 'pointer',
  29. appendTo: 'body',
  30. helper(evt, item) {
  31. const helper = item.clone();
  32. if (MultiSelection.isActive()) {
  33. const andNOthers = $cards.find('.js-minicard.is-checked').length - 1;
  34. if (andNOthers > 0) {
  35. helper.append($(Blaze.toHTML(HTML.DIV(
  36. { 'class': 'and-n-other' },
  37. TAPi18n.__('and-n-other-card', { count: andNOthers })
  38. ))));
  39. }
  40. }
  41. return helper;
  42. },
  43. distance: 7,
  44. items: itemsSelector,
  45. scroll: false,
  46. placeholder: 'minicard-wrapper placeholder',
  47. start(evt, ui) {
  48. ui.placeholder.height(ui.helper.height());
  49. EscapeActions.executeUpTo('popup');
  50. boardComponent.setIsDragging(true);
  51. },
  52. stop(evt, ui) {
  53. // To attribute the new index number, we need to get the DOM element
  54. // of the previous and the following card -- if any.
  55. const prevCardDom = ui.item.prev('.js-minicard').get(0);
  56. const nextCardDom = ui.item.next('.js-minicard').get(0);
  57. const nCards = MultiSelection.isActive() ? MultiSelection.count() : 1;
  58. const sortIndex = calculateIndex(prevCardDom, nextCardDom, nCards);
  59. const listId = Blaze.getData(ui.item.parents('.list').get(0))._id;
  60. // Normally the jquery-ui sortable library moves the dragged DOM element
  61. // to its new position, which disrupts Blaze reactive updates mechanism
  62. // (especially when we move the last card of a list, or when multiple
  63. // users move some cards at the same time). To prevent these UX glitches
  64. // we ask sortable to gracefully cancel the move, and to put back the
  65. // DOM in its initial state. The card move is then handled reactively by
  66. // Blaze with the below query.
  67. $cards.sortable('cancel');
  68. if (MultiSelection.isActive()) {
  69. Cards.find(MultiSelection.getMongoSelector()).forEach((card, i) => {
  70. card.move(listId, sortIndex.base + i * sortIndex.increment);
  71. });
  72. } else {
  73. const cardDomElement = ui.item.get(0);
  74. const card = Blaze.getData(cardDomElement);
  75. card.move(listId, sortIndex.base);
  76. }
  77. boardComponent.setIsDragging(false);
  78. },
  79. });
  80. // We want to re-run this function any time a card is added.
  81. this.autorun(() => {
  82. const currentBoardId = Tracker.nonreactive(() => {
  83. return Session.get('currentBoard');
  84. });
  85. Cards.find({ boardId: currentBoardId }).fetch();
  86. Tracker.afterFlush(() => {
  87. $cards.find(itemsSelector).droppable({
  88. hoverClass: 'draggable-hover-card',
  89. accept: '.js-member,.js-label',
  90. drop(event, ui) {
  91. const cardId = Blaze.getData(this)._id;
  92. const card = Cards.findOne(cardId);
  93. if (ui.draggable.hasClass('js-member')) {
  94. const memberId = Blaze.getData(ui.draggable.get(0)).userId;
  95. card.assignMember(memberId);
  96. } else {
  97. const labelId = Blaze.getData(ui.draggable.get(0))._id;
  98. card.addLabel(labelId);
  99. }
  100. },
  101. });
  102. });
  103. });
  104. },
  105. }).register('list');