summaryrefslogtreecommitdiffstats
path: root/client/components/cards/labels.js
blob: 2da3b80b9f1da195c0db2f310813334a1d281305 (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
let labelColors;
Meteor.startup(() => {
  labelColors = Boards.simpleSchema()._schema['labels.$.color'].allowedValues;
});

BlazeComponent.extendComponent({
  template() {
    return 'formLabel';
  },

  onCreated() {
    this.currentColor = new ReactiveVar(this.data().color);
  },

  labels() {
    return _.map(labelColors, (color) => {
      return { color, name: '' };
    });
  },

  isSelected(color) {
    return this.currentColor.get() === color;
  },

  events() {
    return [{
      'click .js-palette-color'() {
        this.currentColor.set(this.currentData().color);
      },
    }];
  },
}).register('formLabel');

Template.createLabelPopup.helpers({
  // This is the default color for a new label. We search the first color that
  // is not already used in the board (although it's not a problem if two
  // labels have the same color).
  defaultColor() {
    const labels = Boards.findOne(Session.get('currentBoard')).labels;
    const usedColors = _.pluck(labels, 'color');
    const availableColors = _.difference(labelColors, usedColors);
    return availableColors.length > 1 ? availableColors[0] : labelColors[0];
  },
});

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

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

Template.formLabel.events({
  'click .js-palette-color'(evt) {
    const $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'(evt, tpl) {
    const name = tpl.$('#labelName').val().trim();
    const boardId = Session.get('currentBoard');
    const color = Blaze.getData(tpl.find('.fa-check')).color;

    Boards.update(boardId, {
      $push: {
        labels: {
          name,
          color,
          _id: Random.id(6),
        },
      },
    });

    Popup.back();
    evt.preventDefault();
  },
});

Template.editLabelPopup.events({
  'click .js-delete-label': Popup.afterConfirm('deleteLabel', function() {
    const boardId = Session.get('currentBoard');
    Boards.update(boardId, {
      $pull: {
        labels: {
          _id: this._id,
        },
      },
    });

    Popup.back(2);
  }),
  'submit .edit-label'(evt, tpl) {
    evt.preventDefault();
    const name = tpl.$('#labelName').val().trim();
    const boardId = Session.get('currentBoard');
    const getLabel = Utils.getLabelIndex(boardId, this._id);
    const color = Blaze.getData(tpl.find('.fa-check')).color;

    Boards.update(boardId, {
      $set: {
        [getLabel.key('name')]: name,
        [getLabel.key('color')]: color,
      },
    });

    Popup.back();
  },
});

Template.cardLabelsPopup.helpers({
  isLabelSelected(cardId) {
    return _.contains(Cards.findOne(cardId).labelIds, this._id);
  },
});