summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/pages/admin/projects/components/namespace_select.vue
blob: c75c031b0b1e728fe20e850905fe44b42ca0a107 (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
<script>
import {
  GlDropdown,
  GlDropdownItem,
  GlDropdownDivider,
  GlSearchBoxByType,
  GlLoadingIcon,
} from '@gitlab/ui';
import Api from '~/api';
import { __ } from '~/locale';

export default {
  i18n: {
    dropdownHeader: __('Namespaces'),
    searchPlaceholder: __('Search for Namespace'),
    anyNamespace: __('Any namespace'),
  },
  components: {
    GlDropdown,
    GlDropdownItem,
    GlDropdownDivider,
    GlLoadingIcon,
    GlSearchBoxByType,
  },
  props: {
    showAny: {
      type: Boolean,
      required: false,
      default: false,
    },
    placeholder: {
      type: String,
      required: false,
      default: __('Namespace'),
    },
    fieldName: {
      type: String,
      required: false,
      default: null,
    },
  },
  data() {
    return {
      namespaceOptions: [],
      selectedNamespaceId: null,
      selectedNamespace: null,
      searchTerm: '',
      isLoading: false,
    };
  },
  computed: {
    selectedNamespaceName() {
      if (this.selectedNamespaceId === null) {
        return this.placeholder;
      }
      return this.selectedNamespace;
    },
  },
  watch: {
    searchTerm() {
      this.fetchNamespaces(this.searchTerm);
    },
  },
  mounted() {
    this.fetchNamespaces();
  },
  methods: {
    fetchNamespaces(filter) {
      this.isLoading = true;
      this.namespaceOptions = [];
      return Api.namespaces(filter, (namespaces) => {
        this.namespaceOptions = namespaces;
        this.isLoading = false;
      });
    },
    selectNamespace(key) {
      this.selectedNamespaceId = this.namespaceOptions[key].id;
      this.selectedNamespace = this.getNamespaceString(this.namespaceOptions[key]);
      this.$emit('setNamespace', this.selectedNamespaceId);
    },
    selectAnyNamespace() {
      this.selectedNamespaceId = null;
      this.selectedNamespace = null;
      this.$emit('setNamespace', null);
    },
    getNamespaceString(namespace) {
      return `${namespace.kind}: ${namespace.full_path}`;
    },
  },
};
</script>

<template>
  <div class="gl-display-flex">
    <input
      v-if="fieldName"
      :name="fieldName"
      :value="selectedNamespaceId"
      type="hidden"
      data-testid="hidden-input"
    />
    <gl-dropdown
      :text="selectedNamespaceName"
      :header-text="$options.i18n.dropdownHeader"
      toggle-class="dropdown-menu-toggle large"
      data-testid="namespace-dropdown"
      :right="true"
    >
      <template #header>
        <gl-search-box-by-type
          v-model.trim="searchTerm"
          class="namespace-search-box"
          debounce="250"
          :placeholder="$options.i18n.searchPlaceholder"
        />
      </template>

      <template v-if="showAny">
        <gl-dropdown-item @click="selectAnyNamespace">
          {{ $options.i18n.anyNamespace }}
        </gl-dropdown-item>
        <gl-dropdown-divider />
      </template>

      <gl-loading-icon v-if="isLoading" />

      <gl-dropdown-item
        v-for="(namespace, key) in namespaceOptions"
        :key="namespace.id"
        @click="selectNamespace(key)"
      >
        {{ getNamespaceString(namespace) }}
      </gl-dropdown-item>
    </gl-dropdown>
  </div>
</template>

<style scoped>
/* workaround position: relative imposed by .top-area .nav-controls */
.namespace-search-box >>> input {
  position: static;
}
</style>