list.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. // XXX Super bad class name
  37. {'class': 'and-n-other'},
  38. // XXX Need to translate
  39. `and ${andNOthers} other cards.`
  40. ))));
  41. }
  42. }
  43. return helper;
  44. },
  45. distance: 7,
  46. items: itemsSelector,
  47. scroll: false,
  48. placeholder: 'minicard-wrapper placeholder',
  49. start(evt, ui) {
  50. ui.placeholder.height(ui.helper.height());
  51. EscapeActions.executeUpTo('popup');
  52. boardComponent.setIsDragging(true);
  53. },
  54. stop(evt, ui) {
  55. // To attribute the new index number, we need to get the DOM element
  56. // of the previous and the following card -- if any.
  57. const prevCardDom = ui.item.prev('.js-minicard').get(0);
  58. const nextCardDom = ui.item.next('.js-minicard').get(0);
  59. const nCards = MultiSelection.isActive() ? MultiSelection.count() : 1;
  60. const sortIndex = calculateIndex(prevCardDom, nextCardDom, nCards);
  61. const listId = Blaze.getData(ui.item.parents('.list').get(0))._id;
  62. // Normally the jquery-ui sortable library moves the dragged DOM element
  63. // to its new position, which disrupts Blaze reactive updates mechanism
  64. // (especially when we move the last card of a list, or when multiple
  65. // users move some cards at the same time). To prevent these UX glitches
  66. // we ask sortable to gracefully cancel the move, and to put back the
  67. // DOM in its initial state. The card move is then handled reactively by
  68. // Blaze with the below query.
  69. $cards.sortable('cancel');
  70. if (MultiSelection.isActive()) {
  71. Cards.find(MultiSelection.getMongoSelector()).forEach((c, i) => {
  72. Cards.update(c._id, {
  73. $set: {
  74. listId,
  75. sort: sortIndex.base + i * sortIndex.increment,
  76. },
  77. });
  78. });
  79. } else {
  80. const cardDomElement = ui.item.get(0);
  81. const cardId = Blaze.getData(cardDomElement)._id;
  82. Cards.update(cardId, {
  83. $set: {
  84. listId,
  85. sort: sortIndex.base,
  86. },
  87. });
  88. }
  89. boardComponent.setIsDragging(false);
  90. },
  91. });
  92. // We want to re-run this function any time a card is added.
  93. this.autorun(() => {
  94. const currentBoardId = Tracker.nonreactive(() => {
  95. return Session.get('currentBoard');
  96. });
  97. Cards.find({ boardId: currentBoardId }).fetch();
  98. Tracker.afterFlush(() => {
  99. $cards.find(itemsSelector).droppable({
  100. hoverClass: 'draggable-hover-card',
  101. accept: '.js-member,.js-label',
  102. drop(event, ui) {
  103. const cardId = Blaze.getData(this)._id;
  104. let addToSet;
  105. if (ui.draggable.hasClass('js-member')) {
  106. const memberId = Blaze.getData(ui.draggable.get(0)).userId;
  107. addToSet = { members: memberId };
  108. } else {
  109. const labelId = Blaze.getData(ui.draggable.get(0))._id;
  110. addToSet = { labelIds: labelId };
  111. }
  112. Cards.update(cardId, { $addToSet: addToSet });
  113. },
  114. });
  115. });
  116. });
  117. },
  118. }).register('list');