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

152 lines
4.2 KiB
Vue
Raw Normal View History

2018-12-13 13:39:08 +05:30
<script>
import $ from 'jquery';
import { mapGetters, mapActions } from 'vuex';
2019-12-21 20:55:43 +05:30
import { getLocationHash, doesHashExistInUrl } from '../../lib/utils/url_utility';
2018-12-13 13:39:08 +05:30
import Icon from '~/vue_shared/components/icon.vue';
2019-02-15 15:39:39 +05:30
import {
DISCUSSION_FILTERS_DEFAULT_VALUE,
HISTORY_ONLY_FILTER_VALUE,
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: {
Icon,
},
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: {
...mapGetters(['getNotesDataByProp']),
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: {
2019-03-02 22:35:43 +05:30
...mapActions(['filterDiscussion', 'setCommentsDisabled', 'setTargetNoteHash']),
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);
// close dropdown
2019-03-02 22:35:43 +05:30
this.toggleDropdown();
2018-12-13 13:39:08 +05:30
if (filter === this.currentValue) return;
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();
},
2019-03-02 22:35:43 +05:30
toggleDropdown() {
$(this.$refs.dropdownToggle).dropdown('toggle');
},
2018-12-13 13:39:08 +05:30
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.toggleDropdown(); // close dropdown
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>
2019-07-07 11:18:12 +05:30
<div
v-if="displayFilters"
2019-09-04 21:01:54 +05:30
class="discussion-filter-container js-discussion-filter-container d-inline-block align-bottom full-width-mobile"
2019-07-07 11:18:12 +05:30
>
2018-12-13 13:39:08 +05:30
<button
id="discussion-filter-dropdown"
ref="dropdownToggle"
2019-09-04 21:01:54 +05:30
class="btn btn-sm qa-discussion-filter"
2018-12-13 13:39:08 +05:30
data-toggle="dropdown"
aria-expanded="false"
>
2019-02-15 15:39:39 +05:30
{{ currentFilter.title }} <icon name="chevron-down" />
2018-12-13 13:39:08 +05:30
</button>
<div
2019-07-07 11:18:12 +05:30
ref="dropdownMenu"
2018-12-13 13:39:08 +05:30
class="dropdown-menu dropdown-menu-selectable dropdown-menu-right"
2019-02-15 15:39:39 +05:30
aria-labelledby="discussion-filter-dropdown"
>
2018-12-13 13:39:08 +05:30
<div class="dropdown-content">
<ul>
2019-07-07 11:18:12 +05:30
<li
v-for="filter in filters"
:key="filter.value"
:data-filter-type="filterType(filter.value)"
>
2018-12-13 13:39:08 +05:30
<button
:class="{ 'is-active': filter.value === currentValue }"
class="qa-filter-options"
type="button"
2019-03-02 22:35:43 +05:30
@click="selectFilter(filter.value)"
2018-12-13 13:39:08 +05:30
>
{{ filter.title }}
</button>
2019-02-15 15:39:39 +05:30
<div v-if="filter.value === defaultValue" class="dropdown-divider"></div>
2018-12-13 13:39:08 +05:30
</li>
</ul>
</div>
</div>
</div>
</template>