summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/projects/compare/components/revision_dropdown_legacy.vue
blob: f57a8942a77aeadc7e0ab48e27b7c70ba35df8c8 (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
<script>
import { GlDropdown, GlDropdownItem, GlSearchBoxByType, GlDropdownSectionHeader } from '@gitlab/ui';
import createFlash from '~/flash';
import axios from '~/lib/utils/axios_utils';
import { s__ } from '~/locale';

export default {
  components: {
    GlDropdown,
    GlDropdownItem,
    GlDropdownSectionHeader,
    GlSearchBoxByType,
  },
  props: {
    refsProjectPath: {
      type: String,
      required: true,
    },
    revisionText: {
      type: String,
      required: true,
    },
    paramsName: {
      type: String,
      required: true,
    },
    paramsBranch: {
      type: String,
      required: false,
      default: null,
    },
  },
  data() {
    return {
      branches: [],
      tags: [],
      loading: true,
      searchTerm: '',
      selectedRevision: this.getDefaultBranch(),
    };
  },
  computed: {
    filteredBranches() {
      return this.branches.filter((branch) =>
        branch.toLowerCase().includes(this.searchTerm.toLowerCase()),
      );
    },
    hasFilteredBranches() {
      return this.filteredBranches.length;
    },
    filteredTags() {
      return this.tags.filter((tag) => tag.toLowerCase().includes(this.searchTerm.toLowerCase()));
    },
    hasFilteredTags() {
      return this.filteredTags.length;
    },
  },
  watch: {
    paramsBranch(newBranch) {
      this.setSelectedRevision(newBranch);
    },
  },
  mounted() {
    this.fetchBranchesAndTags();
  },
  methods: {
    fetchBranchesAndTags() {
      const endpoint = this.refsProjectPath;

      return axios
        .get(endpoint)
        .then(({ data }) => {
          this.branches = data.Branches || [];
          this.tags = data.Tags || [];
        })
        .catch(() => {
          createFlash({
            message: `${s__(
              'CompareRevisions|There was an error while updating the branch/tag list. Please try again.',
            )}`,
          });
        })
        .finally(() => {
          this.loading = false;
        });
    },
    getDefaultBranch() {
      return this.paramsBranch || s__('CompareRevisions|Select branch/tag');
    },
    onClick(revision) {
      this.setSelectedRevision(revision);
    },
    onSearchEnter() {
      this.setSelectedRevision(this.searchTerm);
    },
    setSelectedRevision(revision) {
      this.selectedRevision = revision || s__('CompareRevisions|Select branch/tag');
      this.$emit('selectRevision', { direction: this.paramsName, revision });
    },
  },
};
</script>

<template>
  <div class="form-group compare-form-group" :class="`js-compare-${paramsName}-dropdown`">
    <div class="input-group inline-input-group">
      <span class="input-group-prepend">
        <div class="input-group-text">
          {{ revisionText }}
        </div>
      </span>
      <input type="hidden" :name="paramsName" :value="selectedRevision" />
      <gl-dropdown
        class="gl-flex-grow-1 gl-flex-basis-0 gl-min-w-0 gl-font-monospace"
        toggle-class="form-control compare-dropdown-toggle js-compare-dropdown gl-min-w-0 gl-rounded-top-left-none! gl-rounded-bottom-left-none!"
        :text="selectedRevision"
        header-text="Select Git revision"
        :loading="loading"
      >
        <template #header>
          <gl-search-box-by-type
            v-model.trim="searchTerm"
            :placeholder="s__('CompareRevisions|Filter by Git revision')"
            @keyup.enter="onSearchEnter"
          />
        </template>
        <gl-dropdown-section-header v-if="hasFilteredBranches">
          {{ s__('CompareRevisions|Branches') }}
        </gl-dropdown-section-header>
        <gl-dropdown-item
          v-for="(branch, index) in filteredBranches"
          :key="`branch${index}`"
          is-check-item
          :is-checked="selectedRevision === branch"
          @click="onClick(branch)"
        >
          {{ branch }}
        </gl-dropdown-item>
        <gl-dropdown-section-header v-if="hasFilteredTags">
          {{ s__('CompareRevisions|Tags') }}
        </gl-dropdown-section-header>
        <gl-dropdown-item
          v-for="(tag, index) in filteredTags"
          :key="`tag${index}`"
          is-check-item
          :is-checked="selectedRevision === tag"
          @click="onClick(tag)"
        >
          {{ tag }}
        </gl-dropdown-item>
      </gl-dropdown>
    </div>
  </div>
</template>