main.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. showNewCardForm: function(value) {
  10. this.componentChildren('listBody')[0].showNewCardForm(value);
  11. },
  12. onCreated: function() {
  13. this.newCardFormIsVisible = new ReactiveVar(true);
  14. },
  15. // XXX The jQuery UI sortable plugin is far from ideal here. First we include
  16. // all jQuery components but only use one. Second, it modifies the DOM itself,
  17. // resulting in Blaze abandoning reactive update of the nodes that have been
  18. // moved which result in bugs if multiple users use the board in real time. I
  19. // tried sortable:sortable but that was not better. And dragula is not
  20. // powerful enough for our use casesShould we “simply” write the drag&drop
  21. // code ourselves?
  22. onRendered: function() {
  23. if (Meteor.user().isBoardMember()) {
  24. var boardComponent = this.componentParent();
  25. var itemsSelector = '.js-minicard:not(.placeholder, .hide, .js-composer)';
  26. var $cards = this.$('.js-minicards');
  27. $cards.sortable({
  28. connectWith: '.js-minicards',
  29. tolerance: 'pointer',
  30. appendTo: '.js-lists',
  31. helper: 'clone',
  32. items: itemsSelector,
  33. placeholder: 'minicard placeholder',
  34. start: function(event, ui) {
  35. $('.minicard.placeholder').height(ui.item.height());
  36. Popup.close();
  37. boardComponent.showNewCardForms(false);
  38. },
  39. stop: function(event, ui) {
  40. // To attribute the new index number, we need to get the dom element
  41. // of the previous and the following card -- if any.
  42. var cardDomElement = ui.item.get(0);
  43. var prevCardDomElement = ui.item.prev('.js-minicard').get(0);
  44. var nextCardDomElement = ui.item.next('.js-minicard').get(0);
  45. var sort = Utils.getSortIndex(prevCardDomElement, nextCardDomElement);
  46. var cardId = Blaze.getData(cardDomElement)._id;
  47. var listId = Blaze.getData(ui.item.parents('.list').get(0))._id;
  48. Cards.update(cardId, {
  49. $set: {
  50. listId: listId,
  51. sort: sort
  52. }
  53. });
  54. boardComponent.showNewCardForms(true);
  55. }
  56. }).disableSelection();
  57. $(document).on('mouseover', function() {
  58. $cards.find(itemsSelector).droppable({
  59. hoverClass: 'draggable-hover-card',
  60. accept: '.js-member,.js-label',
  61. drop: function(event, ui) {
  62. var cardId = Blaze.getData(this)._id;
  63. if (ui.draggable.hasClass('js-member')) {
  64. var memberId = Blaze.getData(ui.draggable.get(0)).userId;
  65. Cards.update(cardId, {$addToSet: {members: memberId}});
  66. } else {
  67. var labelId = Blaze.getData(ui.draggable.get(0))._id;
  68. Cards.update(cardId, {$addToSet: {labelIds: labelId}});
  69. }
  70. }
  71. });
  72. });
  73. }
  74. }
  75. }).register('list');