2018-12-13 13:39:08 +05:30
|
|
|
<script>
|
|
|
|
import { mapGetters, mapActions } from 'vuex';
|
2021-01-03 14:25:43 +05:30
|
|
|
import { GlDropdown, GlDropdownItem, GlDropdownDivider } from '@gitlab/ui';
|
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: {
|
2021-01-03 14:25:43 +05:30
|
|
|
...mapGetters(['getNotesDataByProp', 'timelineEnabled']),
|
2018-12-13 13:39:08 +05:30
|
|
|
currentFilter() {
|
|
|
|
if (!this.currentValue) return this.filters[0];
|
|
|
|
return this.filters.find(filter => filter.value === this.currentValue);
|
|
|
|
},
|
|
|
|
},
|
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"
|
|
|
|
class="gl-mr-3 full-width-mobile discussion-filter-container js-discussion-filter-container qa-discussion-filter"
|
|
|
|
:text="currentFilter.title"
|
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)"
|
|
|
|
class="qa-filter-options"
|
|
|
|
@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>
|