list.js 4.4 KB

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