summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/ci_variable_list/components/ci_environments_dropdown.vue
blob: d22fef279648ace3e58bc2474c5e3e8954c1c4cb (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
<script>
import {
  GlDeprecatedDropdown,
  GlDeprecatedDropdownItem,
  GlDeprecatedDropdownDivider,
  GlSearchBoxByType,
  GlIcon,
} from '@gitlab/ui';
import { mapGetters } from 'vuex';
import { __, sprintf } from '~/locale';

export default {
  name: 'CiEnvironmentsDropdown',
  components: {
    GlDeprecatedDropdown,
    GlDeprecatedDropdownItem,
    GlDeprecatedDropdownDivider,
    GlSearchBoxByType,
    GlIcon,
  },
  props: {
    value: {
      type: String,
      required: false,
      default: '',
    },
  },
  data() {
    return {
      searchTerm: this.value || '',
    };
  },
  computed: {
    ...mapGetters(['joinedEnvironments']),
    composedCreateButtonLabel() {
      return sprintf(__('Create wildcard: %{searchTerm}'), { searchTerm: this.searchTerm });
    },
    shouldRenderCreateButton() {
      return this.searchTerm && !this.joinedEnvironments.includes(this.searchTerm);
    },
    filteredResults() {
      const lowerCasedSearchTerm = this.searchTerm.toLowerCase();
      return this.joinedEnvironments.filter(resultString =>
        resultString.toLowerCase().includes(lowerCasedSearchTerm),
      );
    },
  },
  watch: {
    value(newVal) {
      this.searchTerm = newVal;
    },
  },
  methods: {
    selectEnvironment(selected) {
      this.$emit('selectEnvironment', selected);
      this.searchTerm = '';
    },
    createClicked() {
      this.$emit('createClicked', this.searchTerm);
      this.searchTerm = '';
    },
    isSelected(env) {
      return this.value === env;
    },
  },
};
</script>
<template>
  <gl-deprecated-dropdown :text="value">
    <gl-search-box-by-type v-model.trim="searchTerm" class="m-2" />
    <gl-deprecated-dropdown-item
      v-for="environment in filteredResults"
      :key="environment"
      @click="selectEnvironment(environment)"
    >
      <gl-icon
        :class="{ invisible: !isSelected(environment) }"
        name="mobile-issue-close"
        class="vertical-align-middle"
      />
      {{ environment }}
    </gl-deprecated-dropdown-item>
    <gl-deprecated-dropdown-item v-if="!filteredResults.length" ref="noMatchingResults">{{
      __('No matching results')
    }}</gl-deprecated-dropdown-item>
    <template v-if="shouldRenderCreateButton">
      <gl-deprecated-dropdown-divider />
      <gl-deprecated-dropdown-item @click="createClicked">
        {{ composedCreateButtonLabel }}
      </gl-deprecated-dropdown-item>
    </template>
  </gl-deprecated-dropdown>
</template>