summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/boards/components/issue_card_labels.js
blob: 2c20c16fd4a421b1f6a61b2f646d46ce640f6895 (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
import eventHub from '../eventhub';

const Store = gl.issueBoards.BoardsStore;

window.gl = window.gl || {};
window.gl.issueBoards = window.gl.issueBoards || {};

export default {
  name: 'IssueCardLabels',
  props: {
    labels: { type: Array, required: true },
    list: { type: Object, required: false },
    updateFilters: { type: Boolean, required: false, default: false },
  },
  methods: {
    showLabel(label) {
      if (!this.list) return true;

      return !this.list.label || label.id !== this.list.label.id;
    },
    filterByLabel(label, e) {
      if (!this.updateFilters) return;

      const filterPath = gl.issueBoards.BoardsStore.filter.path.split('&');
      const labelTitle = encodeURIComponent(label.title);
      const param = `label_name[]=${labelTitle}`;
      const labelIndex = filterPath.indexOf(param);
      $(e.currentTarget).tooltip('hide');

      if (labelIndex === -1) {
        filterPath.push(param);
      } else {
        filterPath.splice(labelIndex, 1);
      }

      gl.issueBoards.BoardsStore.filter.path = filterPath.join('&');

      Store.updateFiltersUrl();

      eventHub.$emit('updateTokens');
    },
    labelStyle(label) {
      // TODO: What happens if label.color and/or label.textColor is not defined?

      return {
        backgroundColor: label.color,
        color: label.textColor,
      };
    },
  },
  template: `
    <div class="card-footer">
      <button
        class="label color-label has-tooltip"
        v-for="label in labels"
        type="button"
        v-if="showLabel(label)"
        @click="filterByLabel(label, $event)"
        :style="labelStyle(label)"
        :title="label.description"
        data-container="body">
        {{ label.title }}
      </button>
    </div>
  `,
};