body.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. BlazeComponent.extendComponent({
  2. template: function() {
  3. return 'listBody';
  4. },
  5. isSelected: function() {
  6. return Session.equals('currentCard', this.currentData()._id);
  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. // Clear the form in-memory cache
  32. // var inputCacheKey = "addCard-" + this.listId;
  33. // InputsCache.set(inputCacheKey, '');
  34. // title trim if not empty then
  35. if ($.trim(title)) {
  36. Cards.insert({
  37. title: title,
  38. listId: this.data()._id,
  39. boardId: this.data().board()._id,
  40. sort: sortIndex
  41. }, function(err, _id) {
  42. // In case the filter is active we need to add the newly
  43. // inserted card in the list of exceptions -- cards that are
  44. // not filtered. Otherwise the card will disappear instantly.
  45. // See https://github.com/libreboard/libreboard/issues/80
  46. Filter.addException(_id);
  47. });
  48. // We keep the form opened, empty it, and scroll to it.
  49. textarea.val('').focus();
  50. Utils.Scroll(this.find('.js-minicards')).top(1000, true);
  51. }
  52. },
  53. events: function() {
  54. return [{
  55. submit: this.addCard
  56. }];
  57. }
  58. }).register('listBody');
  59. BlazeComponent.extendComponent({
  60. template: function() {
  61. return 'addCardForm';
  62. },
  63. pressKey: function(evt) {
  64. // Pressing Enter should submit the card
  65. if (evt.keyCode === 13) {
  66. evt.preventDefault();
  67. $(evt.currentTarget).parents('form:first').submit();
  68. // Pressing Tab should open the form of the next column, and Maj+Tab go
  69. // in the reverse order
  70. } else if (evt.keyCode === 9) {
  71. evt.preventDefault();
  72. var isReverse = evt.shiftKey;
  73. var list = $('#js-list-' + this.data().listId);
  74. var listSelector = '.js-list:not(.js-add-list)';
  75. var nextList = list[isReverse ? 'prev' : 'next'](listSelector).get(0);
  76. // If there isn't no next list, loop back to the beginning.
  77. if (! nextList) {
  78. nextList = $(listSelector + (isReverse ? ':last' : ':first')).get(0);
  79. }
  80. BlazeComponent.getComponentForElement(nextList).openForm({
  81. position:this.data().position
  82. });
  83. }
  84. },
  85. events: function() {
  86. return [{
  87. keydown: this.pressKey
  88. }];
  89. }
  90. }).register('addCardForm');