2020-07-14 05:09:02 -04:00
|
|
|
<script>
|
|
|
|
import {
|
2020-09-14 08:09:34 -04:00
|
|
|
GlDropdown,
|
2020-09-14 14:09:48 -04:00
|
|
|
GlDropdownDivider,
|
2020-07-14 05:09:02 -04:00
|
|
|
GlSearchBoxByType,
|
|
|
|
GlSprintf,
|
|
|
|
GlLoadingIcon,
|
|
|
|
} from '@gitlab/ui';
|
2021-03-03 13:11:16 -05:00
|
|
|
import { debounce, isArray } from 'lodash';
|
2021-02-14 13:09:20 -05:00
|
|
|
import { mapActions, mapGetters, mapState } from 'vuex';
|
2021-03-03 13:11:16 -05:00
|
|
|
import {
|
|
|
|
ALL_REF_TYPES,
|
|
|
|
SEARCH_DEBOUNCE_MS,
|
|
|
|
DEFAULT_I18N,
|
|
|
|
REF_TYPE_BRANCHES,
|
|
|
|
REF_TYPE_TAGS,
|
|
|
|
REF_TYPE_COMMITS,
|
|
|
|
} from '../constants';
|
2021-02-14 13:09:20 -05:00
|
|
|
import createStore from '../stores';
|
2020-07-14 05:09:02 -04:00
|
|
|
import RefResultsSection from './ref_results_section.vue';
|
|
|
|
|
|
|
|
export default {
|
|
|
|
name: 'RefSelector',
|
|
|
|
components: {
|
2020-09-14 08:09:34 -04:00
|
|
|
GlDropdown,
|
2020-09-14 14:09:48 -04:00
|
|
|
GlDropdownDivider,
|
2020-07-14 05:09:02 -04:00
|
|
|
GlSearchBoxByType,
|
|
|
|
GlSprintf,
|
|
|
|
GlLoadingIcon,
|
|
|
|
RefResultsSection,
|
|
|
|
},
|
|
|
|
props: {
|
2021-03-03 13:11:16 -05:00
|
|
|
enabledRefTypes: {
|
|
|
|
type: Array,
|
|
|
|
required: false,
|
|
|
|
default: () => ALL_REF_TYPES,
|
|
|
|
validator: (val) =>
|
|
|
|
// It has to be an arrray
|
|
|
|
isArray(val) &&
|
|
|
|
// with at least one item
|
|
|
|
val.length > 0 &&
|
|
|
|
// and only "REF_TYPE_BRANCHES", "REF_TYPE_TAGS", and "REF_TYPE_COMMITS" are allowed
|
|
|
|
val.every((item) => ALL_REF_TYPES.includes(item)) &&
|
|
|
|
// and no duplicates are allowed
|
|
|
|
val.length === new Set(val).size,
|
|
|
|
},
|
2020-07-14 05:09:02 -04:00
|
|
|
value: {
|
|
|
|
type: String,
|
|
|
|
required: false,
|
|
|
|
default: '',
|
|
|
|
},
|
|
|
|
projectId: {
|
|
|
|
type: String,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
translations: {
|
|
|
|
type: Object,
|
|
|
|
required: false,
|
|
|
|
default: () => ({}),
|
|
|
|
},
|
2021-03-05 10:09:12 -05:00
|
|
|
|
|
|
|
/** The validation state of this component. */
|
|
|
|
state: {
|
|
|
|
type: Boolean,
|
|
|
|
required: false,
|
|
|
|
default: true,
|
|
|
|
},
|
2020-07-14 05:09:02 -04:00
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
query: '',
|
|
|
|
};
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
...mapState({
|
2020-12-23 19:10:25 -05:00
|
|
|
matches: (state) => state.matches,
|
|
|
|
lastQuery: (state) => state.query,
|
|
|
|
selectedRef: (state) => state.selectedRef,
|
2020-07-14 05:09:02 -04:00
|
|
|
}),
|
|
|
|
...mapGetters(['isLoading', 'isQueryPossiblyASha']),
|
|
|
|
i18n() {
|
|
|
|
return {
|
|
|
|
...DEFAULT_I18N,
|
|
|
|
...this.translations,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
showBranchesSection() {
|
2021-03-03 13:11:16 -05:00
|
|
|
return (
|
|
|
|
this.enabledRefTypes.includes(REF_TYPE_BRANCHES) &&
|
|
|
|
Boolean(this.matches.branches.totalCount > 0 || this.matches.branches.error)
|
|
|
|
);
|
2020-07-14 05:09:02 -04:00
|
|
|
},
|
|
|
|
showTagsSection() {
|
2021-03-03 13:11:16 -05:00
|
|
|
return (
|
|
|
|
this.enabledRefTypes.includes(REF_TYPE_TAGS) &&
|
|
|
|
Boolean(this.matches.tags.totalCount > 0 || this.matches.tags.error)
|
|
|
|
);
|
2020-07-14 05:09:02 -04:00
|
|
|
},
|
|
|
|
showCommitsSection() {
|
2021-03-03 13:11:16 -05:00
|
|
|
return (
|
|
|
|
this.enabledRefTypes.includes(REF_TYPE_COMMITS) &&
|
|
|
|
Boolean(this.matches.commits.totalCount > 0 || this.matches.commits.error)
|
|
|
|
);
|
2020-07-14 05:09:02 -04:00
|
|
|
},
|
|
|
|
showNoResults() {
|
|
|
|
return !this.showBranchesSection && !this.showTagsSection && !this.showCommitsSection;
|
|
|
|
},
|
2021-03-03 13:11:16 -05:00
|
|
|
showSectionHeaders() {
|
|
|
|
return this.enabledRefTypes.length > 1;
|
|
|
|
},
|
2021-03-05 10:09:12 -05:00
|
|
|
toggleButtonClass() {
|
2021-03-10 07:09:14 -05:00
|
|
|
return {
|
|
|
|
'gl-inset-border-1-red-500!': !this.state,
|
|
|
|
'gl-font-monospace': Boolean(this.selectedRef),
|
|
|
|
};
|
2021-03-05 10:09:12 -05:00
|
|
|
},
|
|
|
|
footerSlotProps() {
|
|
|
|
return {
|
|
|
|
isLoading: this.isLoading,
|
|
|
|
matches: this.matches,
|
|
|
|
query: this.lastQuery,
|
|
|
|
};
|
|
|
|
},
|
2021-03-10 07:09:14 -05:00
|
|
|
buttonText() {
|
|
|
|
return this.selectedRef || this.i18n.noRefSelected;
|
|
|
|
},
|
2020-07-14 05:09:02 -04:00
|
|
|
},
|
2020-07-20 20:09:37 -04:00
|
|
|
watch: {
|
|
|
|
// Keep the Vuex store synchronized if the parent
|
|
|
|
// component updates the selected ref through v-model
|
|
|
|
value: {
|
|
|
|
immediate: true,
|
|
|
|
handler() {
|
|
|
|
if (this.value !== this.selectedRef) {
|
|
|
|
this.setSelectedRef(this.value);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-03-05 10:09:12 -05:00
|
|
|
beforeCreate() {
|
|
|
|
// Setting the store here instead of using
|
|
|
|
// the built in `store` component option because
|
|
|
|
// we need each new `RefSelector` instance to
|
|
|
|
// create a new Vuex store instance.
|
|
|
|
// See https://github.com/vuejs/vuex/issues/414#issue-184491718.
|
|
|
|
this.$store = createStore();
|
|
|
|
},
|
2020-07-14 05:09:02 -04:00
|
|
|
created() {
|
2020-09-01 05:10:28 -04:00
|
|
|
// This method is defined here instead of in `methods`
|
|
|
|
// because we need to access the .cancel() method
|
|
|
|
// lodash attaches to the function, which is
|
|
|
|
// made inaccessible by Vue. More info:
|
|
|
|
// https://stackoverflow.com/a/52988020/1063392
|
|
|
|
this.debouncedSearch = debounce(function search() {
|
2021-03-05 10:09:12 -05:00
|
|
|
this.search();
|
2020-09-01 05:10:28 -04:00
|
|
|
}, SEARCH_DEBOUNCE_MS);
|
|
|
|
|
2020-07-14 05:09:02 -04:00
|
|
|
this.setProjectId(this.projectId);
|
2021-03-03 13:11:16 -05:00
|
|
|
|
|
|
|
this.$watch(
|
|
|
|
'enabledRefTypes',
|
|
|
|
() => {
|
|
|
|
this.setEnabledRefTypes(this.enabledRefTypes);
|
2021-03-05 10:09:12 -05:00
|
|
|
this.search();
|
2021-03-03 13:11:16 -05:00
|
|
|
},
|
|
|
|
{ immediate: true },
|
|
|
|
);
|
2020-07-14 05:09:02 -04:00
|
|
|
},
|
|
|
|
methods: {
|
2021-03-05 10:09:12 -05:00
|
|
|
...mapActions(['setEnabledRefTypes', 'setProjectId', 'setSelectedRef']),
|
|
|
|
...mapActions({ storeSearch: 'search' }),
|
2020-07-14 05:09:02 -04:00
|
|
|
focusSearchBox() {
|
|
|
|
this.$refs.searchBox.$el.querySelector('input').focus();
|
|
|
|
},
|
2020-09-01 05:10:28 -04:00
|
|
|
onSearchBoxEnter() {
|
|
|
|
this.debouncedSearch.cancel();
|
2021-03-05 10:09:12 -05:00
|
|
|
this.search();
|
2020-09-01 05:10:28 -04:00
|
|
|
},
|
|
|
|
onSearchBoxInput() {
|
|
|
|
this.debouncedSearch();
|
|
|
|
},
|
2020-07-14 05:09:02 -04:00
|
|
|
selectRef(ref) {
|
|
|
|
this.setSelectedRef(ref);
|
|
|
|
this.$emit('input', this.selectedRef);
|
|
|
|
},
|
2021-03-05 10:09:12 -05:00
|
|
|
search() {
|
|
|
|
this.storeSearch(this.query);
|
|
|
|
},
|
2020-07-14 05:09:02 -04:00
|
|
|
},
|
|
|
|
};
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<template>
|
2021-03-03 19:11:19 -05:00
|
|
|
<gl-dropdown
|
|
|
|
:header-text="i18n.dropdownHeader"
|
2021-03-05 10:09:12 -05:00
|
|
|
:toggle-class="toggleButtonClass"
|
2021-03-10 07:09:14 -05:00
|
|
|
:text="buttonText"
|
2021-03-03 19:11:19 -05:00
|
|
|
class="ref-selector"
|
2021-03-05 10:09:12 -05:00
|
|
|
v-bind="$attrs"
|
|
|
|
v-on="$listeners"
|
2021-03-03 19:11:19 -05:00
|
|
|
@shown="focusSearchBox"
|
|
|
|
>
|
|
|
|
<template #header>
|
2020-07-14 05:09:02 -04:00
|
|
|
<gl-search-box-by-type
|
|
|
|
ref="searchBox"
|
|
|
|
v-model.trim="query"
|
|
|
|
:placeholder="i18n.searchPlaceholder"
|
2021-03-10 04:09:29 -05:00
|
|
|
autocomplete="off"
|
2020-07-14 05:09:02 -04:00
|
|
|
@input="onSearchBoxInput"
|
2020-09-01 05:10:28 -04:00
|
|
|
@keydown.enter.prevent="onSearchBoxEnter"
|
2020-07-14 05:09:02 -04:00
|
|
|
/>
|
2021-03-03 19:11:19 -05:00
|
|
|
</template>
|
2020-07-14 05:09:02 -04:00
|
|
|
|
2021-03-03 19:11:19 -05:00
|
|
|
<gl-loading-icon v-if="isLoading" size="lg" class="gl-my-3" />
|
2020-07-14 05:09:02 -04:00
|
|
|
|
2021-03-03 19:11:19 -05:00
|
|
|
<div v-else-if="showNoResults" class="gl-text-center gl-mx-3 gl-py-3" data-testid="no-results">
|
|
|
|
<gl-sprintf v-if="lastQuery" :message="i18n.noResultsWithQuery">
|
|
|
|
<template #query>
|
|
|
|
<b class="gl-word-break-all">{{ lastQuery }}</b>
|
|
|
|
</template>
|
|
|
|
</gl-sprintf>
|
2020-07-14 05:09:02 -04:00
|
|
|
|
2021-03-03 19:11:19 -05:00
|
|
|
<span v-else>{{ i18n.noResults }}</span>
|
|
|
|
</div>
|
2020-07-14 05:09:02 -04:00
|
|
|
|
2021-03-03 19:11:19 -05:00
|
|
|
<template v-else>
|
|
|
|
<template v-if="showBranchesSection">
|
|
|
|
<ref-results-section
|
|
|
|
:section-title="i18n.branches"
|
|
|
|
:total-count="matches.branches.totalCount"
|
|
|
|
:items="matches.branches.list"
|
|
|
|
:selected-ref="selectedRef"
|
|
|
|
:error="matches.branches.error"
|
|
|
|
:error-message="i18n.branchesErrorMessage"
|
|
|
|
:show-header="showSectionHeaders"
|
|
|
|
data-testid="branches-section"
|
|
|
|
@selected="selectRef($event)"
|
|
|
|
/>
|
2020-07-14 05:09:02 -04:00
|
|
|
|
2021-03-03 19:11:19 -05:00
|
|
|
<gl-dropdown-divider v-if="showTagsSection || showCommitsSection" />
|
|
|
|
</template>
|
2020-07-14 05:09:02 -04:00
|
|
|
|
2021-03-03 19:11:19 -05:00
|
|
|
<template v-if="showTagsSection">
|
|
|
|
<ref-results-section
|
|
|
|
:section-title="i18n.tags"
|
|
|
|
:total-count="matches.tags.totalCount"
|
|
|
|
:items="matches.tags.list"
|
|
|
|
:selected-ref="selectedRef"
|
|
|
|
:error="matches.tags.error"
|
|
|
|
:error-message="i18n.tagsErrorMessage"
|
|
|
|
:show-header="showSectionHeaders"
|
|
|
|
data-testid="tags-section"
|
|
|
|
@selected="selectRef($event)"
|
|
|
|
/>
|
2020-07-14 05:09:02 -04:00
|
|
|
|
2021-03-03 19:11:19 -05:00
|
|
|
<gl-dropdown-divider v-if="showCommitsSection" />
|
|
|
|
</template>
|
2020-07-14 05:09:02 -04:00
|
|
|
|
2021-03-03 19:11:19 -05:00
|
|
|
<template v-if="showCommitsSection">
|
|
|
|
<ref-results-section
|
|
|
|
:section-title="i18n.commits"
|
|
|
|
:total-count="matches.commits.totalCount"
|
|
|
|
:items="matches.commits.list"
|
|
|
|
:selected-ref="selectedRef"
|
|
|
|
:error="matches.commits.error"
|
|
|
|
:error-message="i18n.commitsErrorMessage"
|
|
|
|
:show-header="showSectionHeaders"
|
|
|
|
data-testid="commits-section"
|
|
|
|
@selected="selectRef($event)"
|
|
|
|
/>
|
|
|
|
</template>
|
|
|
|
</template>
|
2021-03-05 10:09:12 -05:00
|
|
|
|
|
|
|
<template #footer>
|
|
|
|
<slot name="footer" v-bind="footerSlotProps"></slot>
|
|
|
|
</template>
|
2020-09-14 08:09:34 -04:00
|
|
|
</gl-dropdown>
|
2020-07-14 05:09:02 -04:00
|
|
|
</template>
|