summaryrefslogtreecommitdiffstats
path: root/client/components/activities/activities.js
blob: 5d95006ac9202b451754431a92bd55c6903ce94d (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
var activitiesPerPage = 20;

BlazeComponent.extendComponent({
  template: function() {
    return 'activities';
  },

  onCreated: function() {
    var self = this;
    // XXX Should we use ReactiveNumber?
    self.page = new ReactiveVar(1);
    self.loadNextPageLocked = false;
    var sidebar = self.componentParent(); // XXX for some reason not working
    sidebar.callFirstWith(null, 'resetNextPeak');
    self.autorun(function() {
      var mode = self.data().mode;
      var capitalizedMode = Utils.capitalize(mode);
      var id = Session.get('current' + capitalizedMode);
      var limit = self.page.get() * activitiesPerPage;
      if (id === null)
        return;

      self.subscribe('activities', mode, id, limit, function() {
        self.loadNextPageLocked = false;

        // If the sibear peak hasn't increased, that mean that there are no more
        // activities, and we can stop calling new subscriptions.
        // XXX This is hacky! We need to know excatly and reactively how many
        // activities there are, we probably want to denormalize this number
        // dirrectly into card and board documents.
        var a = sidebar.callFirstWith(null, 'getNextPeak');
        sidebar.calculateNextPeak();
        var b = sidebar.callFirstWith(null, 'getNextPeak');
        if (a === b) {
          sidebar.callFirstWith(null, 'resetNextPeak');
        }
      });
    });
  },

  loadNextPage: function() {
    if (this.loadNextPageLocked === false) {
      this.page.set(this.page.get() + 1);
      this.loadNextPageLocked = true;
    }
  },

  boardLabel: function() {
    return TAPi18n.__('this-board');
  },

  cardLink: function() {
    var card = this.currentData().card();
    return Blaze.toHTML(HTML.A({
      href: card.absoluteUrl(),
      'class': 'action-card'
    }, card.title));
  },

  memberLink: function() {
    return Blaze.toHTMLWithData(Template.memberName, {
      user: this.currentData().member()
    });
  },

  attachmentLink: function() {
    var attachment = this.currentData().attachment();
    return Blaze.toHTML(HTML.A({
      href: attachment.url(),
      'class': 'js-open-attachment-viewer'
    }, attachment.name()));
  }
}).register('activities');

BlazeComponent.extendComponent({
  template: function() {
    return 'cardActivities';
  },

  cardLabel: function() {
    return TAPi18n.__('this-card');
  },

  events: function() {
    return [{
      // XXX We should use Popup.afterConfirmation here
      'click .js-delete-comment': function() {
        var commentId = this.currentData().commentId;
        CardComments.remove(commentId);
      },
      'submit .js-edit-comment': function(evt) {
        evt.preventDefault();
        var commentText = this.currentComponent().getValue();
        var commentId = Template.parentData().commentId;
        if ($.trim(commentText)) {
          CardComments.update(commentId, {
            $set: {
              text: commentText
            }
          });
        }
      }
    }];
  }
}).register('cardActivities');