summaryrefslogtreecommitdiffstats
path: root/client/components/lists/body.js
blob: 31e43160f588d0a868a249b5bed73d713a367f3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
BlazeComponent.extendComponent({
  template: function() {
    return 'listBody';
  },

  isSelected: function() {
    return Session.equals('currentCard', this.currentData()._id);
  },

  addCard: function(evt) {
    evt.preventDefault();
    var textarea = $(evt.currentTarget).find('textarea');
    var title = textarea.val();
    var position = this.currentData().position;
    var sortIndex;
    if (position === 'top') {
      sortIndex = Utils.getSortIndex(null, this.find('.js-minicard:first'));
    } else if (position === 'bottom') {
      sortIndex = Utils.getSortIndex(this.find('.js-minicard:last'), null);
    }

    // Clear the form in-memory cache
    // var inputCacheKey = "addCard-" + this.listId;
    // InputsCache.set(inputCacheKey, '');

    // title trim if not empty then
    if ($.trim(title)) {
      Cards.insert({
        title: title,
        listId: this.data()._id,
        boardId: this.data().board()._id,
        sort: sortIndex
      }, function(err, _id) {
        // In case the filter is active we need to add the newly
        // inserted card in the list of exceptions -- cards that are
        // not filtered. Otherwise the card will disappear instantly.
        // See https://github.com/libreboard/libreboard/issues/80
        Filter.addException(_id);
      });

      // We keep the form opened, empty it, and scroll to it.
      textarea.val('').focus();
      Utils.Scroll(this.find('.js-minicards')).top(1000, true);
    }
  },

  events: function() {
    return [{
      submit: this.addCard,
      'keydown form textarea': function(evt) {
        // Pressing Enter should submit the card
        if (evt.keyCode === 13) {
          evt.preventDefault();
          $(evt.currentTarget).parents('form:first').submit();

        // Pressing Tab should open the form of the next column, and Maj+Tab go
        // in the reverse order
        } else if (evt.keyCode === 9) {
          evt.preventDefault();
          var isReverse = evt.shiftKey;
          var list = $('#js-list-' + this.data()._id);
          var nextList = list[isReverse ? 'prev' : 'next']('.js-list').get(0) ||
            $('.js-list:' + (isReverse ? 'last' : 'first')).get(0);
          var nextListComponent =
                                BlazeComponent.getComponentForElement(nextList);

          // XXX Get the real position
          var position = 'bottom';
          nextListComponent.openForm({position: position});
        }
      }
    }];
  }
}).register('listBody');