summaryrefslogtreecommitdiffstats
path: root/client/components/cards/events.js
blob: 2363f4deaac78511459fd0c71f986dda3e6a86d2 (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
Template.cardMemberPopup.events({
  'click .js-remove-member': function() {
    Cards.update(this.cardId, {$pull: {members: this.userId}});
    Popup.close();
  }
});

Template.WindowActivityModule.events({
  'click .js-new-comment:not(.focus)': function(evt) {
    var $this = $(evt.currentTarget);
    $this.addClass('focus');
  },
  'submit #CommentForm': function(evt, t) {
    var text = t.$('.js-new-comment-input');
    if ($.trim(text.val())) {
      CardComments.insert({
        boardId: this.card.boardId,
        cardId: this.card._id,
        text: text.val()
      });
      text.val('');
      $('.focus').removeClass('focus');
    }
    evt.preventDefault();
  }
});

Template.WindowSidebarModule.events({
  'click .js-change-card-members': Popup.open('cardMembers'),
  'click .js-edit-labels': Popup.open('cardLabels'),
  'click .js-archive-card': function(evt) {
    // Update
    Cards.update(this.card._id, {
      $set: {
        archived: true
      }
    });
    evt.preventDefault();
  },
  'click .js-unarchive-card': function(evt) {
    Cards.update(this.card._id, {
      $set: {
        archived: false
      }
    });
    evt.preventDefault();
  },
  'click .js-delete-card': Popup.afterConfirm('cardDelete', function() {
    Cards.remove(this.card._id);

    // redirect board
    Utils.goBoardId(this.card.board()._id);
    Popup.close();
  }),
  'click .js-more-menu': Popup.open('cardMore'),
  'click .js-attach': Popup.open('cardAttachments')
});

Template.WindowAttachmentsModule.events({
  'click .js-attach': Popup.open('cardAttachments'),
  'click .js-confirm-delete': Popup.afterConfirm('attachmentDelete',
    function() {
      Attachments.remove(this._id);
      Popup.close();
    }
  ),
  // If we let this event bubble, Iron-Router will handle it and empty the
  // page content, see #101.
  'click .js-open-viewer, click .js-download': function(event) {
    event.stopPropagation();
  },
  'click .js-add-cover': function() {
    Cards.update(this.cardId, { $set: { coverId: this._id } });
  },
  'click .js-remove-cover': function() {
    Cards.update(this.cardId, { $unset: { coverId: '' } });
  }
});

Template.cardMembersPopup.events({
  'click .js-select-member': function(evt) {
    var cardId = Template.parentData(2).data._id;
    var memberId = this.userId;
    var operation;
    if (Cards.find({ _id: cardId, members: memberId}).count() === 0)
      operation = '$addToSet';
    else
      operation = '$pull';

    var query = {};
    query[operation] = {
      members: memberId
    };
    Cards.update(cardId, query);
    evt.preventDefault();
  }
});

Template.cardLabelsPopup.events({
  'click .js-select-label': function(evt) {
    var cardId = Template.parentData(2).data._id;
    var labelId = this._id;
    var operation;
    if (Cards.find({ _id: cardId, labelIds: labelId}).count() === 0)
      operation = '$addToSet';
    else
      operation = '$pull';

    var query = {};
    query[operation] = {
      labelIds: labelId
    };
    Cards.update(cardId, query);
    evt.preventDefault();
  },
  'click .js-edit-label': Popup.open('editLabel'),
  'click .js-add-label': Popup.open('createLabel')
});

Template.formLabel.events({
  'click .js-palette-color': function(evt) {
    var $this = $(evt.currentTarget);

    // hide selected ll colors
    $('.js-palette-select').addClass('hide');

    // show select color
    $this.find('.js-palette-select').removeClass('hide');
  }
});

Template.createLabelPopup.events({
  // Create the new label
  'submit .create-label': function(evt, tpl) {
    var name = tpl.$('#labelName').val().trim();
    var boardId = Session.get('currentBoard');
    var selectLabelDom = tpl.$('.js-palette-select').get(0);
    var selectLabel = Blaze.getData(selectLabelDom);
    Boards.update(boardId, {
      $push: {
        labels: {
          _id: Random.id(6),
          name: name,
          color: selectLabel.color
        }
      }
    });
    Popup.back();
    evt.preventDefault();
  }
});

Template.editLabelPopup.events({
  'click .js-delete-label': Popup.afterConfirm('deleteLabel', function() {
    var boardId = Session.get('currentBoard');
    Boards.update(boardId, {
      $pull: {
        labels: {
          _id: this._id
        }
      }
    });
    Popup.back(2);
  }),
  'submit .edit-label': function(evt, tpl) {
    var name = tpl.$('#labelName').val().trim();
    var boardId = Session.get('currentBoard');
    var getLabel = Utils.getLabelIndex(boardId, this._id);
    var selectLabelDom = tpl.$('.js-palette-select').get(0);
    var selectLabel = Blaze.getData(selectLabelDom);
    var $set = {};

    // set label index
    $set[getLabel.key('name')] = name;

    // set color
    $set[getLabel.key('color')] = selectLabel.color;

    // update
    Boards.update(boardId, { $set: $set });

    // return to the previous popup view trigger
    Popup.back();

    evt.preventDefault();
  },
  'click .js-select-label': function() {
    Cards.remove(this.cardId);

    // redirect board
    Utils.goBoardId(this.boardId);
  }
});

Template.cardMorePopup.events({
  'click .js-delete': Popup.afterConfirm('cardDelete', function() {
    Cards.remove(this.card._id);

    // redirect board
    Utils.goBoardId(this.card.board()._id);
  })
});

Template.cardAttachmentsPopup.events({
  'change .js-attach-file': function(evt) {
    var card = this.card;
    FS.Utility.eachFile(evt, function(f) {
      var file = new FS.File(f);

      // set Ids
      file.boardId = card.boardId;
      file.cardId  = card._id;

      // upload file
      Attachments.insert(file);

      Popup.close();
    });
  },
  'click .js-computer-upload': function(evt, t) {
    t.find('.js-attach-file').click();
    evt.preventDefault();
  }
});