summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/sidebar/components/labels/labels_select_widget/dropdown_contents_labels_view.vue
blob: e664d6b4bd6fd8744f9cceb6bc116c44a76a81a7 (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
<script>
import { GlDropdownForm, GlDropdownItem, GlLoadingIcon, GlIntersectionObserver } from '@gitlab/ui';
import fuzzaldrinPlus from 'fuzzaldrin-plus';
import { createAlert } from '~/alert';
import { getIdFromGraphQLId } from '~/graphql_shared/utils';
import { __ } from '~/locale';
import { workspaceLabelsQueries } from '../../../constants';
import LabelItem from './label_item.vue';

export default {
  components: {
    GlDropdownForm,
    GlDropdownItem,
    GlLoadingIcon,
    GlIntersectionObserver,
    LabelItem,
  },
  model: {
    prop: 'localSelectedLabels',
  },
  props: {
    allowMultiselect: {
      type: Boolean,
      required: true,
    },
    localSelectedLabels: {
      type: Array,
      required: true,
    },
    fullPath: {
      type: String,
      required: true,
    },
    searchKey: {
      type: String,
      required: true,
    },
    workspaceType: {
      type: String,
      required: true,
    },
  },
  data() {
    return {
      labels: [],
      isVisible: false,
    };
  },
  apollo: {
    labels: {
      query() {
        return workspaceLabelsQueries[this.workspaceType].query;
      },
      variables() {
        return {
          fullPath: this.fullPath,
          searchTerm: this.searchKey,
        };
      },
      skip() {
        return this.searchKey.length === 1 || !this.isVisible;
      },
      update: (data) => data.workspace?.labels?.nodes || [],
      error() {
        createAlert({ message: __('Error fetching labels.') });
      },
    },
  },
  computed: {
    labelsFetchInProgress() {
      return this.$apollo.queries.labels.loading;
    },
    localSelectedLabelsIds() {
      return this.localSelectedLabels.map((label) => getIdFromGraphQLId(label.id));
    },
    visibleLabels() {
      if (this.searchKey) {
        return fuzzaldrinPlus.filter(this.labels, this.searchKey, {
          key: ['title'],
        });
      }
      return this.labels;
    },
    showNoMatchingResultsMessage() {
      return Boolean(this.searchKey) && this.visibleLabels.length === 0;
    },
    shouldHighlightFirstItem() {
      return this.searchKey !== '' && this.visibleLabels.length > 0;
    },
  },
  methods: {
    isLabelSelected(label) {
      return this.localSelectedLabelsIds.includes(getIdFromGraphQLId(label.id));
    },
    /**
     * This method scrolls item from dropdown into
     * the view if it is off the viewable area of the
     * container.
     */
    scrollIntoViewIfNeeded() {
      const highlightedLabel = this.$refs.labelsListContainer.querySelector('.is-focused');

      if (highlightedLabel) {
        const container = this.$refs.labelsListContainer.getBoundingClientRect();
        const label = highlightedLabel.getBoundingClientRect();

        if (label.bottom > container.bottom) {
          this.$refs.labelsListContainer.scrollTop += label.bottom - container.bottom;
        } else if (label.top < container.top) {
          this.$refs.labelsListContainer.scrollTop -= container.top - label.top;
        }
      }
    },
    updateSelectedLabels(label) {
      let labels;
      if (this.isLabelSelected(label)) {
        labels = this.localSelectedLabels.filter(
          ({ id }) => id !== getIdFromGraphQLId(label.id) && id !== label.id,
        );
      } else {
        labels = [...this.localSelectedLabels, label];
      }
      this.$emit('input', labels);
    },
    handleLabelClick(label) {
      this.updateSelectedLabels(label);
      if (!this.allowMultiselect) {
        this.$emit('closeDropdown', this.localSelectedLabels);
      }
    },
    onDropdownAppear() {
      this.isVisible = true;
    },
    selectFirstItem() {
      if (this.shouldHighlightFirstItem) {
        this.handleLabelClick(this.visibleLabels[0]);
      }
    },
  },
};
</script>

<template>
  <gl-intersection-observer @appear="onDropdownAppear">
    <gl-dropdown-form class="labels-select-contents-list js-labels-list">
      <div ref="labelsListContainer" data-testid="dropdown-content">
        <gl-loading-icon
          v-if="labelsFetchInProgress"
          class="labels-fetch-loading gl-align-items-center gl-w-full gl-h-full gl-mb-3"
          size="lg"
        />
        <template v-else>
          <gl-dropdown-item
            v-for="(label, index) in visibleLabels"
            :key="label.id"
            :is-checked="isLabelSelected(label)"
            is-check-centered
            is-check-item
            :active="shouldHighlightFirstItem && index === 0"
            active-class="is-focused"
            data-testid="labels-list"
            @click.native.capture.stop="handleLabelClick(label)"
          >
            <label-item :label="label" />
          </gl-dropdown-item>
          <gl-dropdown-item
            v-show="showNoMatchingResultsMessage"
            class="gl-p-3 gl-text-center"
            data-testid="no-results"
          >
            {{ __('No matching results') }}
          </gl-dropdown-item>
        </template>
      </div>
    </gl-dropdown-form>
  </gl-intersection-observer>
</template>