summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/ci_variable_list/components/ci_environments_dropdown.vue
blob: c9002edc1abd9ed2728c1fa5a541c6799d241b00 (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
<script>
import { GlDropdown, GlDropdownItem, GlDropdownDivider, GlSearchBoxByType } from '@gitlab/ui';
import { __, sprintf } from '~/locale';
import { convertEnvironmentScope } from '../utils';

export default {
  name: 'CiEnvironmentsDropdown',
  components: {
    GlDropdown,
    GlDropdownItem,
    GlDropdownDivider,
    GlSearchBoxByType,
  },
  props: {
    environments: {
      type: Array,
      required: true,
    },
    selectedEnvironmentScope: {
      type: String,
      required: false,
      default: '',
    },
  },
  data() {
    return {
      searchTerm: '',
    };
  },
  computed: {
    composedCreateButtonLabel() {
      return sprintf(__('Create wildcard: %{searchTerm}'), { searchTerm: this.searchTerm });
    },
    filteredEnvironments() {
      const lowerCasedSearchTerm = this.searchTerm.toLowerCase();
      return this.environments.filter((environment) => {
        return environment.toLowerCase().includes(lowerCasedSearchTerm);
      });
    },
    shouldRenderCreateButton() {
      return this.searchTerm && !this.environments.includes(this.searchTerm);
    },
    environmentScopeLabel() {
      return convertEnvironmentScope(this.selectedEnvironmentScope);
    },
  },
  methods: {
    selectEnvironment(selected) {
      this.$emit('select-environment', selected);
      this.clearSearch();
    },
    convertEnvironmentScopeValue(scope) {
      return convertEnvironmentScope(scope);
    },
    createEnvironmentScope() {
      this.$emit('create-environment-scope', this.searchTerm);
      this.selectEnvironment(this.searchTerm);
    },
    isSelected(env) {
      return this.selectedEnvironmentScope === env;
    },
    clearSearch() {
      this.searchTerm = '';
    },
  },
};
</script>
<template>
  <gl-dropdown :text="environmentScopeLabel" @show="clearSearch">
    <gl-search-box-by-type v-model.trim="searchTerm" data-testid="ci-environment-search" />
    <gl-dropdown-item
      v-for="environment in filteredEnvironments"
      :key="environment"
      :is-checked="isSelected(environment)"
      is-check-item
      @click="selectEnvironment(environment)"
    >
      {{ convertEnvironmentScopeValue(environment) }}
    </gl-dropdown-item>
    <gl-dropdown-item v-if="!filteredEnvironments.length" ref="noMatchingResults">{{
      __('No matching results')
    }}</gl-dropdown-item>
    <template v-if="shouldRenderCreateButton">
      <gl-dropdown-divider />
      <gl-dropdown-item data-testid="create-wildcard-button" @click="createEnvironmentScope">
        {{ composedCreateButtonLabel }}
      </gl-dropdown-item>
    </template>
  </gl-dropdown>
</template>