main.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. // XXX The jQuery UI sortable plugin is far from ideal here. First we include
  13. // all jQuery components but only use one. Second, it modifies the DOM itself,
  14. // resulting in Blaze abandoning reactive update of the nodes that have been
  15. // moved which result in bugs if multiple users use the board in real time. I
  16. // tried sortable:sortable but that was not better. And dragula is not
  17. // powerful enough for our use casesShould we “simply” write the drag&drop
  18. // code ourselves?
  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-composer)';
  25. var $cards = self.$('.js-minicards');
  26. $cards.sortable({
  27. connectWith: '.js-minicards',
  28. tolerance: 'pointer',
  29. appendTo: '.js-lists',
  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. items: itemsSelector,
  46. placeholder: 'minicard-wrapper placeholder',
  47. start: function(evt, ui) {
  48. ui.placeholder.height(ui.helper.height());
  49. EscapeActions.executeUpTo('popup');
  50. boardComponent.setIsDragging(true);
  51. },
  52. stop: function(evt, ui) {
  53. // To attribute the new index number, we need to get the dom element
  54. // of the previous and the following card -- if any.
  55. var cardDomElement = ui.item.get(0);
  56. var prevCardDomElement = ui.item.prev('.js-minicard').get(0);
  57. var nextCardDomElement = ui.item.next('.js-minicard').get(0);
  58. var sort = Utils.getSortIndex(prevCardDomElement, nextCardDomElement);
  59. var listId = Blaze.getData(ui.item.parents('.list').get(0))._id;
  60. if (MultiSelection.isActive()) {
  61. Cards.find(MultiSelection.getMongoSelector()).forEach(function(c) {
  62. Cards.update(c._id, {
  63. $set: {
  64. listId: listId,
  65. sort: sort
  66. }
  67. });
  68. });
  69. } else {
  70. var cardId = Blaze.getData(cardDomElement)._id;
  71. Cards.update(cardId, {
  72. $set: {
  73. listId: listId,
  74. // XXX Using the same sort index for multiple cards is
  75. // unacceptable. Keep that only until we figure out if we want to
  76. // refactor the whole sorting mecanism or do something more basic.
  77. sort: sort
  78. }
  79. });
  80. }
  81. boardComponent.setIsDragging(false);
  82. }
  83. });
  84. // We want to re-run this function any time a card is added.
  85. self.autorun(function() {
  86. var currentBoardId = Tracker.nonreactive(function() {
  87. return Session.get('currentBoard');
  88. });
  89. Cards.find({ boardId: currentBoardId }).fetch();
  90. Tracker.afterFlush(function() {
  91. $cards.find(itemsSelector).droppable({
  92. hoverClass: 'draggable-hover-card',
  93. accept: '.js-member,.js-label',
  94. drop: function(event, ui) {
  95. var cardId = Blaze.getData(this)._id;
  96. var addToSet;
  97. if (ui.draggable.hasClass('js-member')) {
  98. var memberId = Blaze.getData(ui.draggable.get(0)).userId;
  99. addToSet = { members: memberId };
  100. } else {
  101. var labelId = Blaze.getData(ui.draggable.get(0))._id;
  102. addToSet = { labelIds: labelId };
  103. }
  104. Cards.update(cardId, { $addToSet: addToSet });
  105. }
  106. });
  107. });
  108. });
  109. }
  110. }).register('list');