summaryrefslogtreecommitdiffstats
path: root/client/components/sidebar/sidebar.js
blob: 0161283562455b46df98f8e998ba3933f555e2b3 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
Sidebar = null;

var defaultView = 'home';

var viewTitles = {
  filter: 'filter-cards',
  multiselection: 'multi-selection',
  archives: 'archives'
};

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

  mixins: function() {
    return [Mixins.InfiniteScrolling, Mixins.PerfectScrollbar];
  },

  onCreated: function() {
    this._isOpen = new ReactiveVar(! Session.get('currentCard'));
    this._view = new ReactiveVar(defaultView);
    Sidebar = this;
  },

  onDestroyed: function() {
    Sidebar = null;
  },

  isOpen: function() {
    return this._isOpen.get();
  },

  open: function() {
    if (! this._isOpen.get()) {
      this._isOpen.set(true);
      EscapeActions.executeUpTo('detailsPane');
    }
  },

  hide: function() {
    if (this._isOpen.get()) {
      this._isOpen.set(false);
    }
  },

  toogle: function() {
    this._isOpen.set(! this._isOpen.get());
  },

  calculateNextPeak: function() {
    var altitude = this.find('.js-board-sidebar-content').scrollHeight;
    this.callFirstWith(this, 'setNextPeak', altitude);
  },

  reachNextPeak: function() {
    var activitiesComponent = this.componentChildren('activities')[0];
    activitiesComponent.loadNextPage();
  },

  isTongueHidden: function() {
    return this.isOpen() && this.getView() !== defaultView;
  },

  getView: function() {
    return this._view.get();
  },

  setView: function(view) {
    view = _.isString(view) ? view : defaultView;
    if (this._view.get() !== view) {
      this._view.set(view);
      EscapeActions.executeUpTo('detailsPane');
    }
    this.open();
  },

  isDefaultView: function() {
    return this.getView() === defaultView;
  },

  getViewTemplate: function() {
    return this.getView() + 'Sidebar';
  },

  getViewTitle: function() {
    return TAPi18n.__(viewTitles[this.getView()]);
  },

  // Board members can assign people or labels by drag-dropping elements from
  // the sidebar to the cards on the board. In order to re-initialize the
  // jquery-ui plugin any time a draggable member or label is modified or
  // removed we use a autorun function and register a dependency on the both
  // members and labels fields of the current board document.
  onRendered: function() {
    var self = this;
    if (! Meteor.userId() || ! Meteor.user().isBoardMember())
      return;

    self.autorun(function() {
      var currentBoardId = Tracker.nonreactive(function() {
        return Session.get('currentBoard');
      });
      Boards.findOne(currentBoardId, {
        fields: {
          members: 1,
          labels: 1
        }
      });
      Tracker.afterFlush(function() {
        self.$('.js-member,.js-label').draggable({
          appendTo: 'body',
          helper: 'clone',
          revert: 'invalid',
          revertDuration: 150,
          snap: false,
          snapMode: 'both',
          start: function() {
            EscapeActions.executeUpTo('popup');
          }
        });
      });
    });
  },

  events: function() {
    // XXX Hacky, we need some kind of `super`
    var mixinEvents = this.getMixin(Mixins.InfiniteScrolling).events();
    return mixinEvents.concat([{
      'click .js-toogle-sidebar': this.toogle,
      'click .js-back-home': this.setView
    }]);
  }
}).register('sidebar');

EscapeActions.register('sidebarView',
  function() { Sidebar.setView(defaultView); },
  function() { return Sidebar && Sidebar.getView() !== defaultView; }
);

var getMemberIndex = function(board, searchId) {
  for (var i = 0; i < board.members.length; i++) {
    if (board.members[i].userId === searchId)
      return i;
  }
  throw new Meteor.Error('Member not found');
};

Template.memberPopup.helpers({
  user: function() {
    return Users.findOne(this.userId);
  },
  memberType: function() {
    var type = Users.findOne(this.userId).isBoardAdmin() ? 'admin' : 'normal';
    return TAPi18n.__(type).toLowerCase();
  }
});

Template.memberPopup.events({
  'click .js-filter-member': function() {
    Filter.members.toogle(this.userId);
    Popup.close();
  },
  'click .js-change-role': Popup.open('changePermissions'),
  'click .js-remove-member': Popup.afterConfirm('removeMember', function() {
    var currentBoard = Boards.findOne(Session.get('currentBoard'));
    var memberIndex = getMemberIndex(currentBoard, this.userId);
    var setQuery = {};
    setQuery[['members', memberIndex, 'isActive'].join('.')] = false;
    Boards.update(currentBoard._id, { $set: setQuery });
    Popup.close();
  }),
  'click .js-leave-member': function() {
    // XXX Not implemented
    Popup.close();
  }
});

Template.membersWidget.events({
  'click .js-member': Popup.open('member'),
  'click .js-manage-board-members': Popup.open('addMember')
});

Template.labelsWidget.events({
  'click .js-label': Popup.open('editLabel'),
  'click .js-add-label': Popup.open('createLabel')
});

Template.addMemberPopup.helpers({
  isBoardMember: function() {
    var user = Users.findOne(this._id);
    return user && user.isBoardMember();
  }
});

Template.addMemberPopup.events({
  'click .pop-over-member-list li:not(.disabled)': function() {
    var userId = this._id;
    var currentBoard = Boards.findOne(Session.get('currentBoard'));
    var currentMembersIds = _.pluck(currentBoard.members, 'userId');
    if (currentMembersIds.indexOf(userId) === -1) {
      Boards.update(currentBoard._id, {
        $push: {
          members: {
            userId: userId,
            isAdmin: false,
            isActive: true
          }
        }
      });
    } else {
      var memberIndex = getMemberIndex(currentBoard, userId);
      var setQuery = {};
      setQuery[['members', memberIndex, 'isActive'].join('.')] = true;
      Boards.update(currentBoard._id, { $set: setQuery });
    }
    Popup.close();
  }
});

Template.addMemberPopup.onRendered(function() {
  this.find('.js-search-member input').focus();
});

Template.changePermissionsPopup.events({
  'click .js-set-admin, click .js-set-normal': function(event) {
    var currentBoard = Boards.findOne(Session.get('currentBoard'));
    var memberIndex = getMemberIndex(currentBoard, this.user._id);
    var isAdmin = $(event.currentTarget).hasClass('js-set-admin');
    var setQuery = {};
    setQuery[['members', memberIndex, 'isAdmin'].join('.')] = isAdmin;
    Boards.update(currentBoard._id, {
      $set: setQuery
    });
    Popup.back(1);
  }
});

Template.changePermissionsPopup.helpers({
  isAdmin: function() {
    return this.user.isBoardAdmin();
  },
  isLastAdmin: function() {
    if (! this.user.isBoardAdmin())
      return false;
    var currentBoard = Boards.findOne(Session.get('currentBoard'));
    var nbAdmins = _.where(currentBoard.members, { isAdmin: true }).length;
    return nbAdmins === 1;
  }
});