body.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. showNewCardForm: function(value) {
  54. this.newCardFormIsVisible.set(value);
  55. },
  56. onCreated: function() {
  57. this.newCardFormIsVisible = new ReactiveVar(true);
  58. },
  59. events: function() {
  60. return [{
  61. submit: this.addCard
  62. }];
  63. }
  64. }).register('listBody');
  65. BlazeComponent.extendComponent({
  66. template: function() {
  67. return 'addCardForm';
  68. },
  69. pressKey: function(evt) {
  70. // Pressing Enter should submit the card
  71. if (evt.keyCode === 13) {
  72. evt.preventDefault();
  73. $(evt.currentTarget).parents('form:first').submit();
  74. // Pressing Tab should open the form of the next column, and Maj+Tab go
  75. // in the reverse order
  76. } else if (evt.keyCode === 9) {
  77. evt.preventDefault();
  78. var isReverse = evt.shiftKey;
  79. var list = $('#js-list-' + this.data().listId);
  80. var listSelector = '.js-list:not(.js-add-list)';
  81. var nextList = list[isReverse ? 'prev' : 'next'](listSelector).get(0);
  82. // If there isn't no next list, loop back to the beginning.
  83. if (! nextList) {
  84. nextList = $(listSelector + (isReverse ? ':last' : ':first')).get(0);
  85. }
  86. BlazeComponent.getComponentForElement(nextList).openForm({
  87. position:this.data().position
  88. });
  89. }
  90. },
  91. events: function() {
  92. return [{
  93. keydown: this.pressKey
  94. }];
  95. }
  96. }).register('addCardForm');