list.js 4.8 KB

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