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

package_search.vue « list « components « package_registry « packages_and_registries « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: df50f5a52b411aa225b8ea2746838fe11e358e7a (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
<script>
import { GlFilteredSearchToken } from '@gitlab/ui';
import { sortableFields } from '~/packages_and_registries/package_registry/utils';
import {
  FILTERED_SEARCH_TERM,
  OPERATORS_IS,
  TOKEN_TITLE_TYPE,
  TOKEN_TYPE_TYPE,
  TOKEN_TITLE_VERSION,
  TOKEN_TYPE_VERSION,
} from '~/vue_shared/components/filtered_search_bar/constants';
import PersistedSearch from '~/packages_and_registries/shared/components/persisted_search.vue';
import { LIST_KEY_CREATED_AT } from '~/packages_and_registries/package_registry/constants';
import LocalStorageSync from '~/vue_shared/components/local_storage_sync.vue';
import PackageTypeToken from './tokens/package_type_token.vue';

export default {
  tokens: [
    {
      type: TOKEN_TYPE_TYPE,
      icon: 'package',
      title: TOKEN_TITLE_TYPE,
      unique: true,
      token: PackageTypeToken,
      operators: OPERATORS_IS,
    },
    {
      type: TOKEN_TYPE_VERSION,
      icon: 'doc-versions',
      title: TOKEN_TITLE_VERSION,
      unique: true,
      token: GlFilteredSearchToken,
      operators: OPERATORS_IS,
    },
  ],
  components: {
    LocalStorageSync,
    PersistedSearch,
  },
  inject: ['isGroupPage'],
  data() {
    return {
      filters: [],
      sorting: {
        orderBy: LIST_KEY_CREATED_AT,
        sort: 'desc',
      },
      mountRegistrySearch: false,
    };
  },
  computed: {
    sortableFields() {
      return sortableFields(this.isGroupPage);
    },
  },
  mounted() {
    // local-storage-sync does not emit `input`
    // event when key is not found, so set the
    // flag if it hasn't been updated
    this.$nextTick(() => {
      if (!this.mountRegistrySearch) {
        this.mountRegistrySearch = true;
      }
    });
  },
  methods: {
    formatFilters(filters) {
      const parsed = {
        packageName: '',
        packageType: undefined,
        packageVersion: '',
      };

      return filters.reduce((acc, filter) => {
        if (filter.type === TOKEN_TYPE_TYPE && filter.value?.data) {
          return {
            ...acc,
            packageType: filter.value.data.toUpperCase(),
          };
        }

        if (filter.type === TOKEN_TYPE_VERSION && filter.value?.data) {
          return {
            ...acc,
            packageVersion: filter.value.data.trim(),
          };
        }

        if (filter.type === FILTERED_SEARCH_TERM) {
          return {
            ...acc,
            packageName: `${acc.packageName} ${filter.value.data}`.trim(),
          };
        }

        return acc;
      }, parsed);
    },
    updateSorting(newValue) {
      this.sorting = { ...this.sorting, ...newValue };
    },
    updateSortingFromLocalStorage(newValue) {
      this.updateSorting(newValue);
      this.mountRegistrySearch = true;
    },
    emitUpdate(values) {
      const { filters, sorting } = values;
      this.updateSorting(sorting);
      this.$emit('update', { ...values, filters: this.formatFilters(filters) });
    },
  },
};
</script>

<template>
  <local-storage-sync
    storage-key="package_registry_list_sorting"
    :value="sorting"
    @input="updateSortingFromLocalStorage"
  >
    <persisted-search
      v-if="mountRegistrySearch"
      :sortable-fields="sortableFields"
      :default-order="sorting.orderBy"
      :default-sort="sorting.sort"
      :tokens="$options.tokens"
      @update="emitUpdate"
    />
  </local-storage-sync>
</template>