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

overview_tabs.vue « components « groups « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 982dab45117d3916399eae5c8c7cfb102cd47fb5 (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<script>
import { GlTabs, GlTab, GlSearchBoxByType, GlSorting, GlSortingItem } from '@gitlab/ui';
import { isString, debounce } from 'lodash';
import { __ } from '~/locale';
import { DEBOUNCE_DELAY } from '~/vue_shared/components/filtered_search_bar/constants';
import GroupsStore from '../store/groups_store';
import GroupsService from '../service/groups_service';
import {
  ACTIVE_TAB_SUBGROUPS_AND_PROJECTS,
  ACTIVE_TAB_SHARED,
  ACTIVE_TAB_ARCHIVED,
  OVERVIEW_TABS_SORTING_ITEMS,
} from '../constants';
import eventHub from '../event_hub';
import GroupsApp from './app.vue';
import SubgroupsAndProjectsEmptyState from './empty_states/subgroups_and_projects_empty_state.vue';
import SharedProjectsEmptyState from './empty_states/shared_projects_empty_state.vue';
import ArchivedProjectsEmptyState from './empty_states/archived_projects_empty_state.vue';

const [SORTING_ITEM_NAME] = OVERVIEW_TABS_SORTING_ITEMS;
const MIN_SEARCH_LENGTH = 3;

export default {
  components: {
    GlTabs,
    GlTab,
    GroupsApp,
    GlSearchBoxByType,
    GlSorting,
    GlSortingItem,
    SubgroupsAndProjectsEmptyState,
    SharedProjectsEmptyState,
    ArchivedProjectsEmptyState,
  },
  inject: ['endpoints', 'initialSort'],
  data() {
    const tabs = [
      {
        title: this.$options.i18n[ACTIVE_TAB_SUBGROUPS_AND_PROJECTS],
        key: ACTIVE_TAB_SUBGROUPS_AND_PROJECTS,
        emptyStateComponent: SubgroupsAndProjectsEmptyState,
        lazy: this.$route.name !== ACTIVE_TAB_SUBGROUPS_AND_PROJECTS,
        service: new GroupsService(this.endpoints[ACTIVE_TAB_SUBGROUPS_AND_PROJECTS]),
        store: new GroupsStore({ showSchemaMarkup: true }),
      },
      {
        title: this.$options.i18n[ACTIVE_TAB_SHARED],
        key: ACTIVE_TAB_SHARED,
        emptyStateComponent: SharedProjectsEmptyState,
        lazy: this.$route.name !== ACTIVE_TAB_SHARED,
        service: new GroupsService(this.endpoints[ACTIVE_TAB_SHARED]),
        store: new GroupsStore(),
      },
      {
        title: this.$options.i18n[ACTIVE_TAB_ARCHIVED],
        key: ACTIVE_TAB_ARCHIVED,
        emptyStateComponent: ArchivedProjectsEmptyState,
        lazy: this.$route.name !== ACTIVE_TAB_ARCHIVED,
        service: new GroupsService(this.endpoints[ACTIVE_TAB_ARCHIVED]),
        store: new GroupsStore(),
      },
    ];
    return {
      tabs,
      activeTabIndex: tabs.findIndex((tab) => tab.key === this.$route.name),
      sort: SORTING_ITEM_NAME,
      isAscending: true,
      search: '',
    };
  },
  computed: {
    activeTab() {
      return this.tabs[this.activeTabIndex];
    },
    sortQueryStringValue() {
      return this.isAscending ? this.sort.asc : this.sort.desc;
    },
  },
  mounted() {
    this.search = this.$route.query?.filter || '';

    const sortQueryStringValue = this.$route.query?.sort || this.initialSort;
    const sort =
      OVERVIEW_TABS_SORTING_ITEMS.find((sortOption) =>
        [sortOption.asc, sortOption.desc].includes(sortQueryStringValue),
      ) || SORTING_ITEM_NAME;
    this.sort = sort;
    this.isAscending = sort.asc === sortQueryStringValue;
  },
  methods: {
    handleTabInput(tabIndex) {
      if (tabIndex === this.activeTabIndex) {
        return;
      }

      this.activeTabIndex = tabIndex;

      const tab = this.tabs[tabIndex];
      tab.lazy = false;

      // Vue router will convert `/` to `%2F` if you pass a string as a param
      // If you pass an array as a param it will concatenate them with a `/`
      // This makes sure we are always passing an array for the group param
      const groupParam = isString(this.$route.params.group)
        ? this.$route.params.group.split('/')
        : this.$route.params.group;

      this.$router.push({ name: tab.key, params: { group: groupParam }, query: this.$route.query });
    },
    handleSearchOrSortChange() {
      // Update query string
      const query = {};
      if (this.sortQueryStringValue !== this.initialSort) {
        query.sort = this.isAscending ? this.sort.asc : this.sort.desc;
      }
      if (this.search) {
        query.filter = this.search;
      }
      this.$router.push({ query });

      // Reset `lazy` prop so that groups/projects are fetched with updated `sort` and `filter` params when switching tabs
      this.tabs.forEach((tab, index) => {
        if (index === this.activeTabIndex) {
          return;
        }
        // eslint-disable-next-line no-param-reassign
        tab.lazy = true;
      });

      // Update data
      eventHub.$emit(`${this.activeTab.key}fetchFilteredAndSortedGroups`, {
        filterGroupsBy: this.search,
        sortBy: this.sortQueryStringValue,
      });
    },
    handleSortDirectionChange() {
      this.isAscending = !this.isAscending;

      this.handleSearchOrSortChange();
    },
    handleSortingItemClick(sortingItem) {
      if (sortingItem === this.sort) {
        return;
      }

      this.sort = sortingItem;

      this.handleSearchOrSortChange();
    },
    handleSearchInput(value) {
      this.search = value;

      if (!this.search || this.search.length >= MIN_SEARCH_LENGTH) {
        this.debouncedSearch();
      }
    },
    debouncedSearch: debounce(async function debouncedSearch() {
      this.handleSearchOrSortChange();
    }, DEBOUNCE_DELAY),
  },
  i18n: {
    [ACTIVE_TAB_SUBGROUPS_AND_PROJECTS]: __('Subgroups and projects'),
    [ACTIVE_TAB_SHARED]: __('Shared projects'),
    [ACTIVE_TAB_ARCHIVED]: __('Archived projects'),
    searchPlaceholder: __('Search'),
  },
  OVERVIEW_TABS_SORTING_ITEMS,
};
</script>

<template>
  <gl-tabs content-class="gl-pt-0" :value="activeTabIndex" @input="handleTabInput">
    <gl-tab
      v-for="{ key, title, emptyStateComponent, lazy, service, store } in tabs"
      :key="key"
      :title="title"
      :lazy="lazy"
    >
      <groups-app :action="key" :service="service" :store="store">
        <template v-if="emptyStateComponent" #empty-state>
          <component :is="emptyStateComponent" />
        </template>
      </groups-app>
    </gl-tab>
    <template #tabs-end>
      <li class="gl-flex-grow-1 gl-align-self-center gl-w-full gl-lg-w-auto gl-py-2">
        <div class="gl-lg-display-flex gl-justify-content-end gl-mx-n2 gl-my-n2">
          <div class="gl-p-2 gl-lg-form-input-md gl-w-full">
            <gl-search-box-by-type
              :value="search"
              :placeholder="$options.i18n.searchPlaceholder"
              data-qa-selector="groups_filter_field"
              @input="handleSearchInput"
            />
          </div>
          <div class="gl-p-2 gl-w-full gl-lg-w-auto">
            <gl-sorting
              class="gl-w-full"
              dropdown-class="gl-w-full"
              data-testid="group_sort_by_dropdown"
              :text="sort.label"
              :is-ascending="isAscending"
              @sortDirectionChange="handleSortDirectionChange"
            >
              <gl-sorting-item
                v-for="sortingItem in $options.OVERVIEW_TABS_SORTING_ITEMS"
                :key="sortingItem.label"
                :active="sortingItem === sort"
                @click="handleSortingItemClick(sortingItem)"
                >{{ sortingItem.label }}</gl-sorting-item
              >
            </gl-sorting>
          </div>
        </div>
      </li>
    </template>
  </gl-tabs>
</template>