debian-mirror-gitlab/app/assets/javascripts/notes/components/discussion_filter.vue

139 lines
3.9 KiB
Vue
Raw Normal View History

2018-12-13 13:39:08 +05:30
<script>
2021-01-03 14:25:43 +05:30
import { GlDropdown, GlDropdownItem, GlDropdownDivider } from '@gitlab/ui';
2021-03-11 19:13:27 +05:30
import { mapGetters, mapActions } from 'vuex';
2019-12-21 20:55:43 +05:30
import { getLocationHash, doesHashExistInUrl } from '../../lib/utils/url_utility';
2019-02-15 15:39:39 +05:30
import {
DISCUSSION_FILTERS_DEFAULT_VALUE,
HISTORY_ONLY_FILTER_VALUE,
2021-01-03 14:25:43 +05:30
COMMENTS_ONLY_FILTER_VALUE,
2019-02-15 15:39:39 +05:30
DISCUSSION_TAB_LABEL,
2019-07-07 11:18:12 +05:30
DISCUSSION_FILTER_TYPES,
2019-12-21 20:55:43 +05:30
NOTE_UNDERSCORE,
2019-02-15 15:39:39 +05:30
} from '../constants';
2019-07-07 11:18:12 +05:30
import notesEventHub from '../event_hub';
2018-12-13 13:39:08 +05:30
export default {
components: {
2021-01-03 14:25:43 +05:30
GlDropdown,
GlDropdownItem,
GlDropdownDivider,
2018-12-13 13:39:08 +05:30
},
props: {
filters: {
type: Array,
required: true,
},
selectedValue: {
type: Number,
2019-07-07 11:18:12 +05:30
default: DISCUSSION_FILTERS_DEFAULT_VALUE,
2018-12-13 13:39:08 +05:30
required: false,
},
},
data() {
return {
2019-12-21 20:55:43 +05:30
currentValue: doesHashExistInUrl(NOTE_UNDERSCORE)
? DISCUSSION_FILTERS_DEFAULT_VALUE
: this.selectedValue,
2018-12-13 13:39:08 +05:30
defaultValue: DISCUSSION_FILTERS_DEFAULT_VALUE,
2019-02-15 15:39:39 +05:30
displayFilters: true,
2018-12-13 13:39:08 +05:30
};
},
computed: {
2022-01-26 12:08:38 +05:30
...mapGetters(['getNotesDataByProp', 'timelineEnabled', 'isLoading']),
2018-12-13 13:39:08 +05:30
currentFilter() {
if (!this.currentValue) return this.filters[0];
2021-03-08 18:12:59 +05:30
return this.filters.find((filter) => filter.value === this.currentValue);
2018-12-13 13:39:08 +05:30
},
},
2019-02-15 15:39:39 +05:30
created() {
if (window.mrTabs) {
const { eventHub, currentTab } = window.mrTabs;
eventHub.$on('MergeRequestTabChange', this.toggleFilters);
this.toggleFilters(currentTab);
}
2019-03-02 22:35:43 +05:30
2019-07-07 11:18:12 +05:30
notesEventHub.$on('dropdownSelect', this.selectFilter);
2019-03-02 22:35:43 +05:30
window.addEventListener('hashchange', this.handleLocationHash);
2019-02-15 15:39:39 +05:30
},
2018-12-13 13:39:08 +05:30
mounted() {
this.toggleCommentsForm();
},
2019-03-02 22:35:43 +05:30
destroyed() {
2019-07-07 11:18:12 +05:30
notesEventHub.$off('dropdownSelect', this.selectFilter);
2019-03-02 22:35:43 +05:30
window.removeEventListener('hashchange', this.handleLocationHash);
},
2018-12-13 13:39:08 +05:30
methods: {
2021-01-03 14:25:43 +05:30
...mapActions([
'filterDiscussion',
'setCommentsDisabled',
'setTargetNoteHash',
'setTimelineView',
]),
2019-10-12 21:52:04 +05:30
selectFilter(value, persistFilter = true) {
2018-12-13 13:39:08 +05:30
const filter = parseInt(value, 10);
if (filter === this.currentValue) return;
2021-01-03 14:25:43 +05:30
if (this.timelineEnabled && filter !== COMMENTS_ONLY_FILTER_VALUE) {
this.setTimelineView(false);
}
2018-12-13 13:39:08 +05:30
this.currentValue = filter;
2019-10-12 21:52:04 +05:30
this.filterDiscussion({
path: this.getNotesDataByProp('discussionsPath'),
filter,
persistFilter,
});
2018-12-13 13:39:08 +05:30
this.toggleCommentsForm();
},
toggleCommentsForm() {
this.setCommentsDisabled(this.currentValue === HISTORY_ONLY_FILTER_VALUE);
},
2019-02-15 15:39:39 +05:30
toggleFilters(tab) {
this.displayFilters = tab === DISCUSSION_TAB_LABEL;
},
2019-03-02 22:35:43 +05:30
handleLocationHash() {
const hash = getLocationHash();
if (/^note_/.test(hash) && this.currentValue !== DISCUSSION_FILTERS_DEFAULT_VALUE) {
2019-10-12 21:52:04 +05:30
this.selectFilter(this.defaultValue, false);
2019-03-02 22:35:43 +05:30
this.setTargetNoteHash(hash);
}
},
2019-07-07 11:18:12 +05:30
filterType(value) {
if (value === 0) {
return DISCUSSION_FILTER_TYPES.ALL;
} else if (value === 1) {
return DISCUSSION_FILTER_TYPES.COMMENTS;
}
return DISCUSSION_FILTER_TYPES.HISTORY;
},
2018-12-13 13:39:08 +05:30
},
};
</script>
<template>
2021-01-03 14:25:43 +05:30
<gl-dropdown
2019-07-07 11:18:12 +05:30
v-if="displayFilters"
2021-01-03 14:25:43 +05:30
id="discussion-filter-dropdown"
2021-01-29 00:20:46 +05:30
class="gl-mr-3 full-width-mobile discussion-filter-container js-discussion-filter-container"
data-qa-selector="discussion_filter_dropdown"
2021-01-03 14:25:43 +05:30
:text="currentFilter.title"
2022-01-26 12:08:38 +05:30
:disabled="isLoading"
2019-07-07 11:18:12 +05:30
>
2021-01-03 14:25:43 +05:30
<div v-for="filter in filters" :key="filter.value" class="dropdown-item-wrapper">
<gl-dropdown-item
:is-check-item="true"
:is-checked="filter.value === currentValue"
:class="{ 'is-active': filter.value === currentValue }"
:data-filter-type="filterType(filter.value)"
2021-01-29 00:20:46 +05:30
data-qa-selector="filter_menu_item"
2021-01-03 14:25:43 +05:30
@click.prevent="selectFilter(filter.value)"
>
{{ filter.title }}
</gl-dropdown-item>
<gl-dropdown-divider v-if="filter.value === defaultValue" />
2018-12-13 13:39:08 +05:30
</div>
2021-01-03 14:25:43 +05:30
</gl-dropdown>
2018-12-13 13:39:08 +05:30
</template>