list.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. const { calculateIndex } = Utils;
  2. BlazeComponent.extendComponent({
  3. template() {
  4. return 'list';
  5. },
  6. // Proxy
  7. openForm(options) {
  8. this.componentChildren('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.componentParent();
  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((c, i) => {
  70. Cards.update(c._id, {
  71. $set: {
  72. listId,
  73. sort: sortIndex.base + i * sortIndex.increment,
  74. },
  75. });
  76. });
  77. } else {
  78. const cardDomElement = ui.item.get(0);
  79. const cardId = Blaze.getData(cardDomElement)._id;
  80. Cards.update(cardId, {
  81. $set: {
  82. listId,
  83. sort: sortIndex.base,
  84. },
  85. });
  86. }
  87. boardComponent.setIsDragging(false);
  88. },
  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. let addToSet;
  103. if (ui.draggable.hasClass('js-member')) {
  104. const memberId = Blaze.getData(ui.draggable.get(0)).userId;
  105. addToSet = { members: memberId };
  106. } else {
  107. const labelId = Blaze.getData(ui.draggable.get(0))._id;
  108. addToSet = { labelIds: labelId };
  109. }
  110. Cards.update(cardId, { $addToSet: addToSet });
  111. },
  112. });
  113. });
  114. });
  115. },
  116. }).register('list');