Welcome to mirror list, hosted at ThFree Co, Russian Federation.

index.vue « list_selector « components « vue_shared « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 237369f59006d3c265c98b1745ac43e9f28b7d81 (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
<script>
import { GlCard, GlIcon, GlCollapsibleListbox, GlSearchBoxByType } from '@gitlab/ui';
import usersAutocompleteQuery from '~/graphql_shared/queries/users_autocomplete.query.graphql';
import User from './user.vue';
import { CONFIG } from './constants';

export default {
  name: 'ListSelector',
  components: {
    GlCard,
    GlIcon,
    GlSearchBoxByType,
    GlCollapsibleListbox,
    User,
  },
  props: {
    title: {
      type: String,
      required: true,
    },
    type: {
      type: String,
      required: true,
    },
    selectedItems: {
      type: Array,
      required: false,
      default: () => [],
    },
    projectPath: {
      type: String,
      required: false,
      default: null,
    },
  },
  data() {
    return {
      searchValue: '',
      isProject: true, // TODO: implement a way to distinguish between project/group
      selected: [],
      items: [],
    };
  },
  computed: {
    config() {
      return CONFIG[this.type];
    },
    searchItems() {
      return (
        this.items?.map((item) => ({
          value: item.username,
          text: item.name,
          ...item,
        })) || []
      );
    },
    component() {
      // Note, we can extend this for the component to support other contexts
      // https://gitlab.com/gitlab-org/gitlab/-/issues/428865
      return User;
    },
  },
  methods: {
    async handleSearchInput(search) {
      this.$refs.results.open();
      this.items = await this.fetchUsersBySearchTerm(search);
    },
    fetchUsersBySearchTerm(search) {
      const namespace = this.isProject ? 'project' : 'group';
      return this.$apollo
        .query({
          query: usersAutocompleteQuery,
          variables: { fullPath: this.projectPath, search, isProject: this.isProject },
        })
        .then(({ data }) => data[namespace]?.autocompleteUsers);
    },
    getItemByKey(key) {
      return this.searchItems.find((item) => item[this.config.filterKey] === key);
    },
    handleSelectItem(key) {
      this.$emit('select', this.getItemByKey(key));
    },
    handleDeleteItem(key) {
      this.$emit('delete', key);
    },
  },
};
</script>

<template>
  <gl-card header-class="gl-new-card-header gl-border-none" body-class="gl-card-footer">
    <template #header
      ><strong
        >{{ title }}
        <span class="gl-text-gray-500"
          ><gl-icon :name="config.icon" /> {{ selectedItems.length }}</span
        ></strong
      ></template
    >

    <gl-collapsible-listbox
      ref="results"
      v-model="selected"
      class="list-selector gl-mb-4 gl-display-block"
      :items="searchItems"
      multiple
      @shown="$refs.search.focusInput()"
    >
      <template #toggle>
        <gl-search-box-by-type
          ref="search"
          v-model="searchValue"
          autofocus
          debounce="500"
          @input="handleSearchInput"
        />
      </template>

      <template #list-item="{ item }">
        <component :is="component" :data="item" @select="handleSelectItem" />
      </template>
    </gl-collapsible-listbox>

    <component
      :is="component"
      v-for="(item, index) of selectedItems"
      :key="index"
      :class="{ 'gl-border-t': index > 0 }"
      class="gl-p-3"
      :data="item"
      can-delete
      @delete="handleDeleteItem"
    />
  </gl-card>
</template>