debian-mirror-gitlab/app/assets/javascripts/projects/compare/components/revision_dropdown.vue

149 lines
3.8 KiB
Vue
Raw Normal View History

2021-03-11 19:13:27 +05:30
<script>
import { GlDropdown, GlDropdownItem, GlSearchBoxByType, GlDropdownSectionHeader } from '@gitlab/ui';
import createFlash from '~/flash';
import axios from '~/lib/utils/axios_utils';
import { s__ } from '~/locale';
2021-04-17 20:07:23 +05:30
const emptyDropdownText = s__('CompareRevisions|Select branch/tag');
2021-03-11 19:13:27 +05:30
export default {
components: {
GlDropdown,
GlDropdownItem,
GlDropdownSectionHeader,
GlSearchBoxByType,
},
props: {
refsProjectPath: {
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;
},
},
2021-04-17 20:07:23 +05:30
watch: {
refsProjectPath(newRefsProjectPath, oldRefsProjectPath) {
if (newRefsProjectPath !== oldRefsProjectPath) {
this.fetchBranchesAndTags(true);
}
},
},
2021-03-11 19:13:27 +05:30
mounted() {
this.fetchBranchesAndTags();
},
methods: {
2021-04-17 20:07:23 +05:30
fetchBranchesAndTags(reset = false) {
2021-03-11 19:13:27 +05:30
const endpoint = this.refsProjectPath;
2021-04-17 20:07:23 +05:30
this.loading = true;
if (reset) {
this.selectedRevision = this.getDefaultBranch();
}
2021-03-11 19:13:27 +05:30
return axios
.get(endpoint)
.then(({ data }) => {
this.branches = data.Branches || [];
this.tags = data.Tags || [];
})
.catch(() => {
createFlash({
2021-04-17 20:07:23 +05:30
message: s__(
'CompareRevisions|There was an error while loading the branch/tag list. Please try again.',
),
2021-03-11 19:13:27 +05:30
});
})
.finally(() => {
this.loading = false;
});
},
getDefaultBranch() {
2021-04-17 20:07:23 +05:30
return this.paramsBranch || emptyDropdownText;
2021-03-11 19:13:27 +05:30
},
onClick(revision) {
this.selectedRevision = revision;
},
onSearchEnter() {
this.selectedRevision = this.searchTerm;
},
},
};
</script>
<template>
2021-04-17 20:07:23 +05:30
<div :class="`js-compare-${paramsName}-dropdown`">
<input type="hidden" :name="paramsName" :value="selectedRevision" />
<gl-dropdown
class="gl-w-full gl-font-monospace"
toggle-class="form-control compare-dropdown-toggle js-compare-dropdown gl-min-w-0"
:text="selectedRevision"
:header-text="s__('CompareRevisions|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)"
2021-03-11 19:13:27 +05:30
>
2021-04-17 20:07:23 +05:30
{{ tag }}
</gl-dropdown-item>
</gl-dropdown>
2021-03-11 19:13:27 +05:30
</div>
</template>