body.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. BlazeComponent.extendComponent({
  2. template: function() {
  3. return 'listBody';
  4. },
  5. isSelected: function() {
  6. return Session.equals('currentCard', this.currentData()._id);
  7. },
  8. addCard: function(evt) {
  9. evt.preventDefault();
  10. var textarea = $(evt.currentTarget).find('textarea');
  11. var title = textarea.val();
  12. var position = this.currentData().position;
  13. var sortIndex;
  14. if (position === 'top') {
  15. sortIndex = Utils.getSortIndex(null, this.find('.js-minicard:first'));
  16. } else if (position === 'bottom') {
  17. sortIndex = Utils.getSortIndex(this.find('.js-minicard:last'), null);
  18. }
  19. // Clear the form in-memory cache
  20. // var inputCacheKey = "addCard-" + this.listId;
  21. // InputsCache.set(inputCacheKey, '');
  22. // title trim if not empty then
  23. if ($.trim(title)) {
  24. Cards.insert({
  25. title: title,
  26. listId: this.data()._id,
  27. boardId: this.data().board()._id,
  28. sort: sortIndex
  29. }, function(err, _id) {
  30. // In case the filter is active we need to add the newly
  31. // inserted card in the list of exceptions -- cards that are
  32. // not filtered. Otherwise the card will disappear instantly.
  33. // See https://github.com/libreboard/libreboard/issues/80
  34. Filter.addException(_id);
  35. });
  36. // We keep the form opened, empty it, and scroll to it.
  37. textarea.val('').focus();
  38. Utils.Scroll(this.find('.js-minicards')).top(1000, true);
  39. }
  40. },
  41. events: function() {
  42. return [{
  43. submit: this.addCard,
  44. 'keydown form textarea': function(evt) {
  45. // Pressing Enter should submit the card
  46. if (evt.keyCode === 13) {
  47. evt.preventDefault();
  48. $(evt.currentTarget).parents('form:first').submit();
  49. // Pressing Tab should open the form of the next column, and Maj+Tab go
  50. // in the reverse order
  51. } else if (evt.keyCode === 9) {
  52. evt.preventDefault();
  53. var isReverse = evt.shiftKey;
  54. var list = $('#js-list-' + this.data()._id);
  55. var nextList = list[isReverse ? 'prev' : 'next']('.js-list').get(0) ||
  56. $('.js-list:' + (isReverse ? 'last' : 'first')).get(0);
  57. var nextListComponent = BlazeComponent.getComponentForElement(nextList);
  58. // XXX Get the real position
  59. var position = 'bottom';
  60. nextListComponent.openForm({position: position});
  61. }
  62. }
  63. }];
  64. }
  65. }).register('listBody');