2020-05-07 17:09:26 -04:00
|
|
|
<script>
|
|
|
|
import {
|
2020-09-14 08:09:34 -04:00
|
|
|
GlDropdown,
|
2020-09-14 14:09:48 -04:00
|
|
|
GlDropdownDivider,
|
2020-09-14 20:09:47 -04:00
|
|
|
GlDropdownSectionHeader,
|
2020-09-14 14:09:48 -04:00
|
|
|
GlDropdownItem,
|
2020-05-07 17:09:26 -04:00
|
|
|
GlLoadingIcon,
|
|
|
|
GlSearchBoxByType,
|
|
|
|
GlIcon,
|
|
|
|
} from '@gitlab/ui';
|
2020-08-17 17:09:56 -04:00
|
|
|
import { intersection, debounce } from 'lodash';
|
2020-05-07 17:09:26 -04:00
|
|
|
import { __, sprintf } from '~/locale';
|
|
|
|
import Api from '~/api';
|
2020-08-20 05:09:55 -04:00
|
|
|
import { deprecatedCreateFlash as createFlash } from '~/flash';
|
2020-05-07 17:09:26 -04:00
|
|
|
|
2020-09-03 14:08:29 -04:00
|
|
|
const SEARCH_DEBOUNCE_MS = 250;
|
|
|
|
|
2020-05-07 17:09:26 -04:00
|
|
|
export default {
|
|
|
|
components: {
|
2020-09-14 08:09:34 -04:00
|
|
|
GlDropdown,
|
2020-09-14 14:09:48 -04:00
|
|
|
GlDropdownDivider,
|
2020-09-14 20:09:47 -04:00
|
|
|
GlDropdownSectionHeader,
|
2020-09-14 14:09:48 -04:00
|
|
|
GlDropdownItem,
|
2020-05-07 17:09:26 -04:00
|
|
|
GlLoadingIcon,
|
|
|
|
GlSearchBoxByType,
|
|
|
|
GlIcon,
|
|
|
|
},
|
|
|
|
model: {
|
|
|
|
prop: 'preselectedMilestones',
|
|
|
|
event: 'change',
|
|
|
|
},
|
|
|
|
props: {
|
|
|
|
projectId: {
|
|
|
|
type: String,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
preselectedMilestones: {
|
|
|
|
type: Array,
|
|
|
|
default: () => [],
|
|
|
|
required: false,
|
|
|
|
},
|
|
|
|
extraLinks: {
|
|
|
|
type: Array,
|
|
|
|
default: () => [],
|
|
|
|
required: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
searchQuery: '',
|
|
|
|
projectMilestones: [],
|
|
|
|
searchResults: [],
|
|
|
|
selectedMilestones: [],
|
|
|
|
requestCount: 0,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
translations: {
|
|
|
|
milestone: __('Milestone'),
|
|
|
|
selectMilestone: __('Select milestone'),
|
|
|
|
noMilestone: __('No milestone'),
|
|
|
|
noResultsLabel: __('No matching results'),
|
|
|
|
searchMilestones: __('Search Milestones'),
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
selectedMilestonesLabel() {
|
|
|
|
if (this.milestoneTitles.length === 1) {
|
|
|
|
return this.milestoneTitles[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.milestoneTitles.length > 1) {
|
|
|
|
const firstMilestoneName = this.milestoneTitles[0];
|
|
|
|
const numberOfOtherMilestones = this.milestoneTitles.length - 1;
|
|
|
|
return sprintf(__('%{firstMilestoneName} + %{numberOfOtherMilestones} more'), {
|
|
|
|
firstMilestoneName,
|
|
|
|
numberOfOtherMilestones,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.$options.translations.noMilestone;
|
|
|
|
},
|
|
|
|
milestoneTitles() {
|
|
|
|
return this.preselectedMilestones.map(milestone => milestone.title);
|
|
|
|
},
|
|
|
|
dropdownItems() {
|
|
|
|
return this.searchResults.length ? this.searchResults : this.projectMilestones;
|
|
|
|
},
|
|
|
|
noResults() {
|
|
|
|
return this.searchQuery.length > 2 && this.searchResults.length === 0;
|
|
|
|
},
|
|
|
|
isLoading() {
|
|
|
|
return this.requestCount !== 0;
|
|
|
|
},
|
|
|
|
},
|
2020-09-01 05:10:28 -04:00
|
|
|
created() {
|
|
|
|
// 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
|
2020-09-03 14:08:29 -04:00
|
|
|
this.debouncedSearchMilestones = debounce(this.searchMilestones, SEARCH_DEBOUNCE_MS);
|
2020-09-01 05:10:28 -04:00
|
|
|
},
|
2020-05-07 17:09:26 -04:00
|
|
|
mounted() {
|
|
|
|
this.fetchMilestones();
|
|
|
|
},
|
|
|
|
methods: {
|
2020-09-03 14:08:29 -04:00
|
|
|
focusSearchBox() {
|
|
|
|
this.$refs.searchBox.$el.querySelector('input').focus();
|
|
|
|
},
|
2020-05-07 17:09:26 -04:00
|
|
|
fetchMilestones() {
|
|
|
|
this.requestCount += 1;
|
|
|
|
|
|
|
|
Api.projectMilestones(this.projectId)
|
|
|
|
.then(({ data }) => {
|
|
|
|
this.projectMilestones = this.getTitles(data);
|
|
|
|
this.selectedMilestones = intersection(this.projectMilestones, this.milestoneTitles);
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
createFlash(__('An error occurred while loading milestones'));
|
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
this.requestCount -= 1;
|
|
|
|
});
|
|
|
|
},
|
2020-09-01 05:10:28 -04:00
|
|
|
searchMilestones() {
|
2020-05-07 17:09:26 -04:00
|
|
|
this.requestCount += 1;
|
|
|
|
const options = {
|
|
|
|
search: this.searchQuery,
|
|
|
|
scope: 'milestones',
|
|
|
|
};
|
|
|
|
|
|
|
|
if (this.searchQuery.length < 3) {
|
|
|
|
this.requestCount -= 1;
|
|
|
|
this.searchResults = [];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Api.projectSearch(this.projectId, options)
|
|
|
|
.then(({ data }) => {
|
|
|
|
const searchResults = this.getTitles(data);
|
|
|
|
|
|
|
|
this.searchResults = searchResults.length ? searchResults : [];
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
createFlash(__('An error occurred while searching for milestones'));
|
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
this.requestCount -= 1;
|
|
|
|
});
|
2020-09-01 05:10:28 -04:00
|
|
|
},
|
|
|
|
onSearchBoxInput() {
|
|
|
|
this.debouncedSearchMilestones();
|
|
|
|
},
|
|
|
|
onSearchBoxEnter() {
|
|
|
|
this.debouncedSearchMilestones.cancel();
|
|
|
|
this.searchMilestones();
|
|
|
|
},
|
2020-05-07 17:09:26 -04:00
|
|
|
toggleMilestoneSelection(clickedMilestone) {
|
|
|
|
if (!clickedMilestone) return [];
|
|
|
|
|
|
|
|
let milestones = [...this.preselectedMilestones];
|
|
|
|
const hasMilestone = this.milestoneTitles.includes(clickedMilestone);
|
|
|
|
|
|
|
|
if (hasMilestone) {
|
|
|
|
milestones = milestones.filter(({ title }) => title !== clickedMilestone);
|
|
|
|
} else {
|
|
|
|
milestones.push({ title: clickedMilestone });
|
|
|
|
}
|
|
|
|
|
|
|
|
return milestones;
|
|
|
|
},
|
|
|
|
onMilestoneClicked(clickedMilestone) {
|
|
|
|
const milestones = this.toggleMilestoneSelection(clickedMilestone);
|
|
|
|
this.$emit('change', milestones);
|
|
|
|
|
|
|
|
this.selectedMilestones = intersection(
|
|
|
|
this.projectMilestones,
|
|
|
|
milestones.map(milestone => milestone.title),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
isSelectedMilestone(milestoneTitle) {
|
|
|
|
return this.selectedMilestones.includes(milestoneTitle);
|
|
|
|
},
|
|
|
|
getTitles(milestones) {
|
|
|
|
return milestones.filter(({ state }) => state === 'active').map(({ title }) => title);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<template>
|
2020-09-14 08:09:34 -04:00
|
|
|
<gl-dropdown v-bind="$attrs" class="project-milestone-combobox" @shown="focusSearchBox">
|
2020-05-07 17:09:26 -04:00
|
|
|
<template slot="button-content">
|
|
|
|
<span ref="buttonText" class="flex-grow-1 ml-1 text-muted">{{
|
|
|
|
selectedMilestonesLabel
|
|
|
|
}}</span>
|
|
|
|
<gl-icon name="chevron-down" />
|
|
|
|
</template>
|
|
|
|
|
2020-09-14 20:09:47 -04:00
|
|
|
<gl-dropdown-section-header>
|
2020-05-07 17:09:26 -04:00
|
|
|
<span class="text-center d-block">{{ $options.translations.selectMilestone }}</span>
|
2020-09-14 20:09:47 -04:00
|
|
|
</gl-dropdown-section-header>
|
2020-05-07 17:09:26 -04:00
|
|
|
|
2020-09-14 14:09:48 -04:00
|
|
|
<gl-dropdown-divider />
|
2020-05-07 17:09:26 -04:00
|
|
|
|
|
|
|
<gl-search-box-by-type
|
2020-09-03 14:08:29 -04:00
|
|
|
ref="searchBox"
|
2020-05-07 17:09:26 -04:00
|
|
|
v-model.trim="searchQuery"
|
2020-08-27 11:10:21 -04:00
|
|
|
class="gl-m-3"
|
2020-05-07 17:09:26 -04:00
|
|
|
:placeholder="this.$options.translations.searchMilestones"
|
2020-09-01 05:10:28 -04:00
|
|
|
@input="onSearchBoxInput"
|
|
|
|
@keydown.enter.prevent="onSearchBoxEnter"
|
2020-05-07 17:09:26 -04:00
|
|
|
/>
|
|
|
|
|
2020-09-14 14:09:48 -04:00
|
|
|
<gl-dropdown-item @click="onMilestoneClicked(null)">
|
2020-05-07 17:09:26 -04:00
|
|
|
<span :class="{ 'pl-4': true, 'selected-item': selectedMilestones.length === 0 }">
|
|
|
|
{{ $options.translations.noMilestone }}
|
|
|
|
</span>
|
2020-09-14 14:09:48 -04:00
|
|
|
</gl-dropdown-item>
|
2020-05-07 17:09:26 -04:00
|
|
|
|
2020-09-14 14:09:48 -04:00
|
|
|
<gl-dropdown-divider />
|
2020-05-07 17:09:26 -04:00
|
|
|
|
|
|
|
<template v-if="isLoading">
|
|
|
|
<gl-loading-icon />
|
2020-09-14 14:09:48 -04:00
|
|
|
<gl-dropdown-divider />
|
2020-05-07 17:09:26 -04:00
|
|
|
</template>
|
|
|
|
<template v-else-if="noResults">
|
|
|
|
<div class="dropdown-item-space">
|
|
|
|
<span ref="noResults" class="pl-4">{{ $options.translations.noResultsLabel }}</span>
|
|
|
|
</div>
|
2020-09-14 14:09:48 -04:00
|
|
|
<gl-dropdown-divider />
|
2020-05-07 17:09:26 -04:00
|
|
|
</template>
|
|
|
|
<template v-else-if="dropdownItems.length">
|
2020-09-14 14:09:48 -04:00
|
|
|
<gl-dropdown-item
|
2020-05-07 17:09:26 -04:00
|
|
|
v-for="item in dropdownItems"
|
|
|
|
:key="item"
|
|
|
|
role="milestone option"
|
|
|
|
@click="onMilestoneClicked(item)"
|
|
|
|
>
|
|
|
|
<span :class="{ 'pl-4': true, 'selected-item': isSelectedMilestone(item) }">
|
|
|
|
{{ item }}
|
|
|
|
</span>
|
2020-09-14 14:09:48 -04:00
|
|
|
</gl-dropdown-item>
|
|
|
|
<gl-dropdown-divider />
|
2020-05-07 17:09:26 -04:00
|
|
|
</template>
|
|
|
|
|
2020-09-14 14:09:48 -04:00
|
|
|
<gl-dropdown-item v-for="(item, idx) in extraLinks" :key="idx" :href="item.url">
|
2020-05-07 17:09:26 -04:00
|
|
|
<span class="pl-4">{{ item.text }}</span>
|
2020-09-14 14:09:48 -04:00
|
|
|
</gl-dropdown-item>
|
2020-09-14 08:09:34 -04:00
|
|
|
</gl-dropdown>
|
2020-05-07 17:09:26 -04:00
|
|
|
</template>
|