body.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. BlazeComponent.extendComponent({
  2. template: function() {
  3. return 'listBody';
  4. },
  5. mixins: function() {
  6. return [Mixins.PerfectScrollbar];
  7. },
  8. openForm: function(options) {
  9. options = options || {};
  10. options.position = options.position || 'top';
  11. var forms = this.componentChildren('inlinedForm');
  12. var form = _.find(forms, function(component) {
  13. return component.data().position === options.position;
  14. });
  15. if (! form && forms.length > 0) {
  16. form = forms[0];
  17. }
  18. form.open();
  19. },
  20. addCard: function(evt) {
  21. evt.preventDefault();
  22. var textarea = $(evt.currentTarget).find('textarea');
  23. var title = textarea.val();
  24. var position = Blaze.getData(evt.currentTarget).position;
  25. var sortIndex;
  26. var firstCard = this.find('.js-minicard:first');
  27. var lastCard = this.find('.js-minicard:last');
  28. if (position === 'top') {
  29. sortIndex = Utils.calculateIndex(null, firstCard).base;
  30. } else if (position === 'bottom') {
  31. sortIndex = Utils.calculateIndex(lastCard, null).base;
  32. }
  33. if ($.trim(title)) {
  34. var _id = Cards.insert({
  35. title: title,
  36. listId: this.data()._id,
  37. boardId: this.data().board()._id,
  38. sort: sortIndex
  39. });
  40. // In case the filter is active we need to add the newly inserted card in
  41. // the list of exceptions -- cards that are not filtered. Otherwise the
  42. // card will disappear instantly.
  43. // See https://github.com/libreboard/libreboard/issues/80
  44. Filter.addException(_id);
  45. // We keep the form opened, empty it, and scroll to it.
  46. textarea.val('').focus();
  47. if (position === 'bottom') {
  48. this.scrollToBottom();
  49. }
  50. }
  51. },
  52. scrollToBottom: function() {
  53. var container = this.firstNode();
  54. $(container).animate({
  55. scrollTop: container.scrollHeight
  56. });
  57. },
  58. clickOnMiniCard: function(evt) {
  59. if (MultiSelection.isActive() || evt.shiftKey) {
  60. evt.stopImmediatePropagation();
  61. evt.preventDefault();
  62. var methodName = evt.shiftKey ? 'toogleRange' : 'toogle';
  63. MultiSelection[methodName](this.currentData()._id);
  64. // If the card is already selected, we want to de-select it.
  65. // XXX We should probably modify the minicard href attribute instead of
  66. // overwriting the event in case the card is already selected.
  67. } else if (Session.equals('currentCard', this.currentData()._id)) {
  68. evt.stopImmediatePropagation();
  69. evt.preventDefault();
  70. Utils.goBoardId(Session.get('currentBoard'));
  71. }
  72. },
  73. cardIsSelected: function() {
  74. return Session.equals('currentCard', this.currentData()._id);
  75. },
  76. toggleMultiSelection: function(evt) {
  77. evt.stopPropagation();
  78. evt.preventDefault();
  79. MultiSelection.toogle(this.currentData()._id);
  80. },
  81. events: function() {
  82. return [{
  83. 'click .js-minicard': this.clickOnMiniCard,
  84. 'click .js-toggle-multi-selection': this.toggleMultiSelection,
  85. 'click .open-minicard-composer': this.scrollToBottom,
  86. submit: this.addCard
  87. }];
  88. }
  89. }).register('listBody');
  90. BlazeComponent.extendComponent({
  91. template: function() {
  92. return 'addCardForm';
  93. },
  94. pressKey: function(evt) {
  95. // Pressing Enter should submit the card
  96. if (evt.keyCode === 13) {
  97. evt.preventDefault();
  98. var $form = $(evt.currentTarget).closest('form');
  99. // XXX For some reason $form.submit() does not work (it's probably a bug
  100. // of blaze-component related to the fact that the submit event is non-
  101. // bubbling). This is why we click on the submit button instead -- which
  102. // work.
  103. $form.find('button[type=submit]').click();
  104. // Pressing Tab should open the form of the next column, and Maj+Tab go
  105. // in the reverse order
  106. } else if (evt.keyCode === 9) {
  107. evt.preventDefault();
  108. var isReverse = evt.shiftKey;
  109. var list = $('#js-list-' + this.data().listId);
  110. var listSelector = '.js-list:not(.js-list-composer)';
  111. var nextList = list[isReverse ? 'prev' : 'next'](listSelector).get(0);
  112. // If there is no next list, loop back to the beginning.
  113. if (! nextList) {
  114. nextList = $(listSelector + (isReverse ? ':last' : ':first')).get(0);
  115. }
  116. BlazeComponent.getComponentForElement(nextList).openForm({
  117. position:this.data().position
  118. });
  119. }
  120. },
  121. events: function() {
  122. return [{
  123. keydown: this.pressKey
  124. }];
  125. }
  126. }).register('addCardForm');