main.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. BlazeComponent.extendComponent({
  2. template: function() {
  3. return 'list';
  4. },
  5. // Proxies
  6. openForm: function(options) {
  7. this.componentChildren('listBody')[0].openForm(options);
  8. },
  9. onCreated: function() {
  10. this.newCardFormIsVisible = new ReactiveVar(true);
  11. },
  12. // The jquery UI sortable library is the best solution I've found so far. I
  13. // tried sortable and dragula but they were not powerful enough four our use
  14. // case. I also considered writing/forking a drag-and-drop + sortable library
  15. // but it's probably too much work.
  16. // By calling asking the sortable library to cancel its move on the `stop`
  17. // callback, we basically solve all issues related to reactive updates. A
  18. // comment below provides further details.
  19. onRendered: function() {
  20. var self = this;
  21. if (! Meteor.userId() || ! Meteor.user().isBoardMember())
  22. return;
  23. var boardComponent = self.componentParent();
  24. var itemsSelector = '.js-minicard:not(.placeholder, .js-card-composer)';
  25. var $cards = self.$('.js-minicards');
  26. $cards.sortable({
  27. connectWith: '.js-minicards',
  28. tolerance: 'pointer',
  29. appendTo: '#surface',
  30. helper: function(evt, item) {
  31. var helper = item.clone();
  32. if (MultiSelection.isActive()) {
  33. var 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: function(evt, ui) {
  50. ui.placeholder.height(ui.helper.height());
  51. EscapeActions.executeUpTo('popup');
  52. boardComponent.setIsDragging(true);
  53. },
  54. stop: function(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. var prevCardDom = ui.item.prev('.js-minicard').get(0);
  58. var nextCardDom = ui.item.next('.js-minicard').get(0);
  59. var nCards = MultiSelection.isActive() ? MultiSelection.count() : 1;
  60. var sortIndex = Utils.calculateIndex(prevCardDom, nextCardDom, nCards);
  61. var 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(function(c, i) {
  72. Cards.update(c._id, {
  73. $set: {
  74. listId: listId,
  75. sort: sortIndex.base + i * sortIndex.increment
  76. }
  77. });
  78. });
  79. } else {
  80. var cardDomElement = ui.item.get(0);
  81. var cardId = Blaze.getData(cardDomElement)._id;
  82. Cards.update(cardId, {
  83. $set: {
  84. listId: 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. self.autorun(function() {
  94. var currentBoardId = Tracker.nonreactive(function() {
  95. return Session.get('currentBoard');
  96. });
  97. Cards.find({ boardId: currentBoardId }).fetch();
  98. Tracker.afterFlush(function() {
  99. $cards.find(itemsSelector).droppable({
  100. hoverClass: 'draggable-hover-card',
  101. accept: '.js-member,.js-label',
  102. drop: function(event, ui) {
  103. var cardId = Blaze.getData(this)._id;
  104. var addToSet;
  105. if (ui.draggable.hasClass('js-member')) {
  106. var memberId = Blaze.getData(ui.draggable.get(0)).userId;
  107. addToSet = { members: memberId };
  108. } else {
  109. var 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');