2
0

body.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. clickOnMiniCard: function(evt) {
  57. if (MultiSelection.isActive() || evt.shiftKey) {
  58. evt.stopImmediatePropagation();
  59. evt.preventDefault();
  60. var methodName = evt.shiftKey ? 'toogleRange' : 'toogle';
  61. MultiSelection[methodName](this.currentData()._id);
  62. // If the card is already selected, we want to de-select it.
  63. // XXX We should probably modify the minicard href attribute instead of
  64. // overwriting the event in case the card is already selected.
  65. } else if (Session.equals('currentCard', this.currentData()._id)) {
  66. evt.stopImmediatePropagation();
  67. evt.preventDefault();
  68. Utils.goBoardId(Session.get('currentBoard'));
  69. }
  70. },
  71. cardIsSelected: function() {
  72. return Session.equals('currentCard', this.currentData()._id);
  73. },
  74. toggleMultiSelection: function(evt) {
  75. evt.stopPropagation();
  76. evt.preventDefault();
  77. MultiSelection.toogle(this.currentData()._id);
  78. },
  79. events: function() {
  80. return [{
  81. 'click .js-minicard': this.clickOnMiniCard,
  82. 'click .js-toggle-multi-selection': this.toggleMultiSelection,
  83. 'click .open-minicard-composer': this.scrollToBottom,
  84. submit: this.addCard
  85. }];
  86. }
  87. }).register('listBody');
  88. BlazeComponent.extendComponent({
  89. template: function() {
  90. return 'addCardForm';
  91. },
  92. pressKey: function(evt) {
  93. // Pressing Enter should submit the card
  94. if (evt.keyCode === 13) {
  95. evt.preventDefault();
  96. var $form = $(evt.currentTarget).closest('form');
  97. // XXX For some reason $form.submit() does not work (it's probably a bug
  98. // of blaze-component related to the fact that the submit event is non-
  99. // bubbling). This is why we click on the submit button instead -- which
  100. // work.
  101. $form.find('button[type=submit]').click();
  102. // Pressing Tab should open the form of the next column, and Maj+Tab go
  103. // in the reverse order
  104. } else if (evt.keyCode === 9) {
  105. evt.preventDefault();
  106. var isReverse = evt.shiftKey;
  107. var list = $('#js-list-' + this.data().listId);
  108. var listSelector = '.js-list:not(.js-list-composer)';
  109. var nextList = list[isReverse ? 'prev' : 'next'](listSelector).get(0);
  110. // If there is no next list, loop back to the beginning.
  111. if (! nextList) {
  112. nextList = $(listSelector + (isReverse ? ':last' : ':first')).get(0);
  113. }
  114. BlazeComponent.getComponentForElement(nextList).openForm({
  115. position:this.data().position
  116. });
  117. }
  118. },
  119. events: function() {
  120. return [{
  121. keydown: this.pressKey
  122. }];
  123. }
  124. }).register('addCardForm');