body.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. if (position === 'top') {
  27. sortIndex = Utils.getSortIndex(null, this.find('.js-minicard:first'));
  28. } else if (position === 'bottom') {
  29. sortIndex = Utils.getSortIndex(this.find('.js-minicard:last'), null);
  30. }
  31. if ($.trim(title)) {
  32. var _id = Cards.insert({
  33. title: title,
  34. listId: this.data()._id,
  35. boardId: this.data().board()._id,
  36. sort: sortIndex
  37. });
  38. // In case the filter is active we need to add the newly inserted card in
  39. // the list of exceptions -- cards that are not filtered. Otherwise the
  40. // card will disappear instantly.
  41. // See https://github.com/libreboard/libreboard/issues/80
  42. Filter.addException(_id);
  43. // We keep the form opened, empty it, and scroll to it.
  44. textarea.val('').focus();
  45. if (position === 'bottom') {
  46. this.scrollToBottom();
  47. }
  48. }
  49. },
  50. scrollToBottom: function() {
  51. var container = this.firstNode();
  52. $(container).animate({
  53. scrollTop: container.scrollHeight
  54. });
  55. },
  56. events: function() {
  57. return [{
  58. submit: this.addCard,
  59. 'click .open-minicard-composer': this.scrollToBottom
  60. }];
  61. }
  62. }).register('listBody');
  63. BlazeComponent.extendComponent({
  64. template: function() {
  65. return 'addCardForm';
  66. },
  67. pressKey: function(evt) {
  68. // Pressing Enter should submit the card
  69. if (evt.keyCode === 13) {
  70. evt.preventDefault();
  71. var $form = $(evt.currentTarget).parents('form:first');
  72. // XXX For some reason $form.submit() does not work (it's probably a bug
  73. // of blaze-component related to the fact that the submit event is non-
  74. // bubbling). This is why we click on the submit button instead -- which
  75. // work.
  76. $form.find('button[type=submit]').click();
  77. // Pressing Tab should open the form of the next column, and Maj+Tab go
  78. // in the reverse order
  79. } else if (evt.keyCode === 9) {
  80. evt.preventDefault();
  81. var isReverse = evt.shiftKey;
  82. var list = $('#js-list-' + this.data().listId);
  83. var listSelector = '.js-list:not(.js-list-composer)';
  84. var nextList = list[isReverse ? 'prev' : 'next'](listSelector).get(0);
  85. // If there isn't no next list, loop back to the beginning.
  86. if (! nextList) {
  87. nextList = $(listSelector + (isReverse ? ':last' : ':first')).get(0);
  88. }
  89. BlazeComponent.getComponentForElement(nextList).openForm({
  90. position:this.data().position
  91. });
  92. }
  93. },
  94. events: function() {
  95. return [{
  96. keydown: this.pressKey
  97. }];
  98. }
  99. }).register('addCardForm');