list.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. const { calculateIndex, enableClickOnTouch } = 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().parentComponent();
  19. function userIsMember() {
  20. return Meteor.user() && Meteor.user().isBoardMember() && !Meteor.user().isCommentOnly();
  21. }
  22. const itemsSelector = '.js-minicard:not(.placeholder, .js-card-composer)';
  23. const $cards = this.$('.js-minicards');
  24. $cards.sortable({
  25. connectWith: '.js-minicards:not(.js-list-full)',
  26. tolerance: 'pointer',
  27. appendTo: '.board-canvas',
  28. helper(evt, item) {
  29. const helper = item.clone();
  30. if (MultiSelection.isActive()) {
  31. const andNOthers = $cards.find('.js-minicard.is-checked').length - 1;
  32. if (andNOthers > 0) {
  33. helper.append($(Blaze.toHTML(HTML.DIV(
  34. { 'class': 'and-n-other' },
  35. TAPi18n.__('and-n-other-card', { count: andNOthers })
  36. ))));
  37. }
  38. }
  39. return helper;
  40. },
  41. distance: 7,
  42. items: itemsSelector,
  43. placeholder: 'minicard-wrapper placeholder',
  44. start(evt, ui) {
  45. ui.helper.css('z-index', 1000);
  46. ui.placeholder.height(ui.helper.height());
  47. EscapeActions.executeUpTo('popup-close');
  48. boardComponent.setIsDragging(true);
  49. },
  50. stop(evt, ui) {
  51. // To attribute the new index number, we need to get the DOM element
  52. // of the previous and the following card -- if any.
  53. const prevCardDom = ui.item.prev('.js-minicard').get(0);
  54. const nextCardDom = ui.item.next('.js-minicard').get(0);
  55. const nCards = MultiSelection.isActive() ? MultiSelection.count() : 1;
  56. const sortIndex = calculateIndex(prevCardDom, nextCardDom, nCards);
  57. const listId = Blaze.getData(ui.item.parents('.list').get(0))._id;
  58. const swimlaneId = Blaze.getData(ui.item.parents('.swimlane').get(0))._id;
  59. // Normally the jquery-ui sortable library moves the dragged DOM element
  60. // to its new position, which disrupts Blaze reactive updates mechanism
  61. // (especially when we move the last card of a list, or when multiple
  62. // users move some cards at the same time). To prevent these UX glitches
  63. // we ask sortable to gracefully cancel the move, and to put back the
  64. // DOM in its initial state. The card move is then handled reactively by
  65. // Blaze with the below query.
  66. $cards.sortable('cancel');
  67. if (MultiSelection.isActive()) {
  68. Cards.find(MultiSelection.getMongoSelector()).forEach((card, i) => {
  69. card.move(swimlaneId, listId, sortIndex.base + i * sortIndex.increment);
  70. });
  71. } else {
  72. const cardDomElement = ui.item.get(0);
  73. const card = Blaze.getData(cardDomElement);
  74. card.move(swimlaneId, listId, sortIndex.base);
  75. }
  76. boardComponent.setIsDragging(false);
  77. },
  78. });
  79. // ugly touch event hotfix
  80. enableClickOnTouch(itemsSelector);
  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. });