summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/search/sidebar/components/language_filter.vue
blob: 26ce204cb5c42a5d46a6de2b523ca49a52115a69 (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
<script>
import { GlButton, GlAlert, GlForm } from '@gitlab/ui';
import { mapState, mapActions, mapGetters } from 'vuex';
import { __, s__, sprintf } from '~/locale';
import glFeatureFlagsMixin from '~/vue_shared/mixins/gl_feature_flags_mixin';
import { DEFAULT_ITEM_LENGTH, MAX_ITEM_LENGTH } from '../constants/language_filter_data';
import { HR_DEFAULT_CLASSES, ONLY_SHOW_MD } from '../constants';
import { convertFiltersData } from '../utils';
import CheckboxFilter from './checkbox_filter.vue';

export default {
  name: 'LanguageFilter',
  components: {
    CheckboxFilter,
    GlButton,
    GlAlert,
    GlForm,
  },
  mixins: [glFeatureFlagsMixin()],
  data() {
    return {
      showAll: false,
    };
  },
  i18n: {
    showMore: s__('GlobalSearch|Show more'),
    apply: __('Apply'),
    showingMax: sprintf(s__('GlobalSearch|Showing top %{maxItems}'), { maxItems: MAX_ITEM_LENGTH }),
    loadError: s__('GlobalSearch|Aggregations load error.'),
  },
  computed: {
    ...mapState(['aggregations', 'sidebarDirty']),
    ...mapGetters(['langugageAggregationBuckets']),
    hasBuckets() {
      return this.langugageAggregationBuckets.length > 0;
    },
    filtersData() {
      return convertFiltersData(this.shortenedLanguageFilters);
    },
    shortenedLanguageFilters() {
      if (!this.hasShowMore) {
        return this.langugageAggregationBuckets;
      }
      if (this.showAll) {
        return this.trimBuckets(MAX_ITEM_LENGTH);
      }
      return this.trimBuckets(DEFAULT_ITEM_LENGTH);
    },
    hasShowMore() {
      return this.langugageAggregationBuckets.length > DEFAULT_ITEM_LENGTH;
    },
    hasOverMax() {
      return this.langugageAggregationBuckets.length > MAX_ITEM_LENGTH;
    },
    dividerClasses() {
      return [...HR_DEFAULT_CLASSES, ...ONLY_SHOW_MD];
    },
  },
  async created() {
    await this.fetchLanguageAggregation();
  },
  methods: {
    ...mapActions(['applyQuery', 'fetchLanguageAggregation']),
    onShowMore() {
      this.showAll = true;
    },
    trimBuckets(length) {
      return this.langugageAggregationBuckets.slice(0, length);
    },
  },
  HR_DEFAULT_CLASSES,
};
</script>

<template>
  <gl-form
    v-if="hasBuckets"
    class="gl-pt-5 gl-md-pt-0 language-filter-checkbox"
    @submit.prevent="applyQuery"
  >
    <hr :class="dividerClasses" />
    <div
      v-if="!aggregations.error"
      class="gl-overflow-x-hidden gl-overflow-y-auto"
      :class="{ 'language-filter-max-height': showAll }"
    >
      <checkbox-filter class="gl-px-5" :filter-data="filtersData" />
      <span v-if="showAll && hasOverMax" data-testid="has-over-max-text">{{
        $options.i18n.showingMax
      }}</span>
    </div>
    <gl-alert v-else class="gl-mx-5" variant="danger" :dismissible="false">{{
      $options.i18n.loadError
    }}</gl-alert>
    <div v-if="hasShowMore && !showAll" class="gl-px-5 language-filter-show-all">
      <gl-button
        data-testid="show-more-button"
        category="tertiary"
        variant="link"
        size="small"
        button-text-classes="gl-font-sm"
        @click="onShowMore"
      >
        {{ $options.i18n.showMore }}
      </gl-button>
    </div>
    <div v-if="!aggregations.error">
      <hr :class="$options.HR_DEFAULT_CLASSES" />
      <div class="gl-display-flex gl-align-items-center gl-mt-4 gl-mx-5 gl-px-5">
        <gl-button
          category="primary"
          variant="confirm"
          type="submit"
          :disabled="!sidebarDirty"
          data-testid="apply-button"
        >
          {{ $options.i18n.apply }}
        </gl-button>
      </div>
    </div>
  </gl-form>
</template>