debian-mirror-gitlab/app/assets/javascripts/boards/components/boards_selector.vue

380 lines
10 KiB
Vue
Raw Normal View History

2019-09-30 21:07:59 +05:30
<script>
import {
GlLoadingIcon,
GlSearchBoxByType,
2021-02-22 17:27:13 +05:30
GlDropdown,
GlDropdownDivider,
GlDropdownSectionHeader,
GlDropdownItem,
GlModalDirective,
2019-09-30 21:07:59 +05:30
} from '@gitlab/ui';
2021-03-11 19:13:27 +05:30
import { throttle } from 'lodash';
2021-12-11 22:18:48 +05:30
import { mapActions, mapGetters, mapState } from 'vuex';
2021-04-17 20:07:23 +05:30
import BoardForm from 'ee_else_ce/boards/components/board_form.vue';
2019-09-30 21:07:59 +05:30
2021-03-11 19:13:27 +05:30
import { getIdFromGraphQLId } from '~/graphql_shared/utils';
import axios from '~/lib/utils/axios_utils';
2019-09-30 21:07:59 +05:30
import httpStatusCodes from '~/lib/utils/http_status';
2021-12-11 22:18:48 +05:30
import { s__ } from '~/locale';
2020-04-08 14:13:33 +05:30
2021-03-11 19:13:27 +05:30
import eventHub from '../eventhub';
2021-12-11 22:18:48 +05:30
import groupBoardsQuery from '../graphql/group_boards.query.graphql';
import projectBoardsQuery from '../graphql/project_boards.query.graphql';
import groupBoardQuery from '../graphql/group_board.query.graphql';
import projectBoardQuery from '../graphql/project_board.query.graphql';
2020-04-08 14:13:33 +05:30
2019-09-30 21:07:59 +05:30
const MIN_BOARDS_TO_VIEW_RECENT = 10;
export default {
name: 'BoardsSelector',
components: {
BoardForm,
GlLoadingIcon,
GlSearchBoxByType,
2021-02-22 17:27:13 +05:30
GlDropdown,
GlDropdownDivider,
GlDropdownSectionHeader,
GlDropdownItem,
},
directives: {
GlModalDirective,
2019-09-30 21:07:59 +05:30
},
2021-03-11 19:13:27 +05:30
inject: ['fullPath', 'recentBoardsEndpoint'],
2019-09-30 21:07:59 +05:30
props: {
throttleDuration: {
type: Number,
default: 200,
2020-04-22 19:07:51 +05:30
required: false,
2019-09-30 21:07:59 +05:30
},
boardBaseUrl: {
type: String,
required: true,
},
hasMissingBoards: {
type: Boolean,
required: true,
},
canAdminBoard: {
type: Boolean,
required: true,
},
multipleIssueBoardsAvailable: {
type: Boolean,
required: true,
},
scopedIssueBoardFeatureEnabled: {
type: Boolean,
required: true,
},
weights: {
type: Array,
required: true,
},
},
data() {
return {
hasScrollFade: false,
2020-04-08 14:13:33 +05:30
loadingBoards: 0,
loadingRecentBoards: false,
2019-09-30 21:07:59 +05:30
scrollFadeInitialized: false,
boards: [],
recentBoards: [],
throttledSetScrollFade: throttle(this.setScrollFade, this.throttleDuration),
contentClientHeight: 0,
maxPosition: 0,
filterTerm: '',
2021-03-11 19:13:27 +05:30
currentPage: '',
2021-12-11 22:18:48 +05:30
board: {},
2019-09-30 21:07:59 +05:30
};
},
2021-12-11 22:18:48 +05:30
apollo: {
board: {
query() {
return this.currentBoardQuery;
},
variables() {
return {
fullPath: this.fullPath,
boardId: this.fullBoardId,
};
},
update(data) {
const board = data.workspace?.board;
return {
...board,
labels: board?.labels?.nodes,
};
},
error() {
this.setError({ message: this.$options.i18n.errorFetchingBoard });
},
},
},
2019-09-30 21:07:59 +05:30
computed: {
2021-12-11 22:18:48 +05:30
...mapState(['boardType', 'fullBoardId']),
...mapGetters(['isGroupBoard', 'isProjectBoard']),
2020-04-08 14:13:33 +05:30
parentType() {
2021-04-17 20:07:23 +05:30
return this.boardType;
2020-04-08 14:13:33 +05:30
},
2021-12-11 22:18:48 +05:30
currentBoardQueryCE() {
return this.isGroupBoard ? groupBoardQuery : projectBoardQuery;
},
currentBoardQuery() {
return this.currentBoardQueryCE;
},
isBoardLoading() {
return this.$apollo.queries.board.loading;
},
2020-04-08 14:13:33 +05:30
loading() {
2021-02-22 17:27:13 +05:30
return this.loadingRecentBoards || Boolean(this.loadingBoards);
2020-04-08 14:13:33 +05:30
},
2019-09-30 21:07:59 +05:30
filteredBoards() {
2021-03-08 18:12:59 +05:30
return this.boards.filter((board) =>
2019-09-30 21:07:59 +05:30
board.name.toLowerCase().includes(this.filterTerm.toLowerCase()),
);
},
2021-04-17 20:07:23 +05:30
showCreate() {
return this.multipleIssueBoardsAvailable;
},
2019-09-30 21:07:59 +05:30
showDelete() {
return this.boards.length > 1;
},
scrollFadeClass() {
return {
'fade-out': !this.hasScrollFade,
};
},
showRecentSection() {
return (
this.recentBoards.length &&
this.boards.length > MIN_BOARDS_TO_VIEW_RECENT &&
!this.filterTerm.length
);
},
},
watch: {
filteredBoards() {
this.scrollFadeInitialized = false;
this.$nextTick(this.setScrollFade);
},
},
created() {
2021-03-11 19:13:27 +05:30
eventHub.$on('showBoardModal', this.showPage);
},
beforeDestroy() {
eventHub.$off('showBoardModal', this.showPage);
2019-09-30 21:07:59 +05:30
},
methods: {
2021-12-11 22:18:48 +05:30
...mapActions(['setError']),
2019-09-30 21:07:59 +05:30
showPage(page) {
2021-03-11 19:13:27 +05:30
this.currentPage = page;
},
cancel() {
this.showPage('');
2019-09-30 21:07:59 +05:30
},
2021-04-17 20:07:23 +05:30
boardUpdate(data) {
if (!data?.[this.parentType]) {
return [];
}
return data[this.parentType].boards.edges.map(({ node }) => ({
id: getIdFromGraphQLId(node.id),
name: node.name,
}));
},
boardQuery() {
2021-12-11 22:18:48 +05:30
return this.isGroupBoard ? groupBoardsQuery : projectBoardsQuery;
2021-04-17 20:07:23 +05:30
},
2019-09-30 21:07:59 +05:30
loadBoards(toggleDropdown = true) {
if (toggleDropdown && this.boards.length > 0) {
return;
}
2020-04-08 14:13:33 +05:30
this.$apollo.addSmartQuery('boards', {
variables() {
2021-03-11 19:13:27 +05:30
return { fullPath: this.fullPath };
2020-04-08 14:13:33 +05:30
},
2021-04-17 20:07:23 +05:30
query: this.boardQuery,
2020-04-08 14:13:33 +05:30
loadingKey: 'loadingBoards',
2021-04-17 20:07:23 +05:30
update: this.boardUpdate,
2020-04-08 14:13:33 +05:30
});
2019-09-30 21:07:59 +05:30
2021-04-17 20:07:23 +05:30
this.loadRecentBoards();
},
loadRecentBoards() {
2020-04-08 14:13:33 +05:30
this.loadingRecentBoards = true;
2021-03-11 19:13:27 +05:30
// Follow up to fetch recent boards using GraphQL
// https://gitlab.com/gitlab-org/gitlab/-/issues/300985
axios
.get(this.recentBoardsEndpoint)
2021-03-08 18:12:59 +05:30
.then((res) => {
2020-04-08 14:13:33 +05:30
this.recentBoards = res.data;
})
2021-03-08 18:12:59 +05:30
.catch((err) => {
2020-04-08 14:13:33 +05:30
/**
* If user is unauthorized we'd still want to resolve the
* request to display all boards.
*/
if (err?.response?.status === httpStatusCodes.UNAUTHORIZED) {
this.recentBoards = []; // recent boards are empty
return;
}
throw err;
2019-09-30 21:07:59 +05:30
})
.then(() => this.$nextTick()) // Wait for boards list in DOM
.then(() => {
this.setScrollFade();
})
2020-04-08 14:13:33 +05:30
.catch(() => {})
.finally(() => {
this.loadingRecentBoards = false;
2019-09-30 21:07:59 +05:30
});
},
isScrolledUp() {
const { content } = this.$refs;
2020-04-08 14:13:33 +05:30
if (!content) {
return false;
}
2019-09-30 21:07:59 +05:30
const currentPosition = this.contentClientHeight + content.scrollTop;
2020-04-08 14:13:33 +05:30
return currentPosition < this.maxPosition;
2019-09-30 21:07:59 +05:30
},
initScrollFade() {
const { content } = this.$refs;
2020-04-08 14:13:33 +05:30
if (!content) {
return;
}
this.scrollFadeInitialized = true;
2019-09-30 21:07:59 +05:30
this.contentClientHeight = content.clientHeight;
this.maxPosition = content.scrollHeight;
},
setScrollFade() {
if (!this.scrollFadeInitialized) this.initScrollFade();
this.hasScrollFade = this.isScrolledUp();
},
},
2021-12-11 22:18:48 +05:30
i18n: {
errorFetchingBoard: s__('Board|An error occurred while fetching the board, please try again.'),
},
2019-09-30 21:07:59 +05:30
};
</script>
<template>
2020-07-28 23:09:34 +05:30
<div class="boards-switcher js-boards-selector gl-mr-3">
2019-09-30 21:07:59 +05:30
<span class="boards-selector-wrapper js-boards-selector-wrapper">
2021-02-22 17:27:13 +05:30
<gl-dropdown
2019-12-04 20:38:33 +05:30
data-qa-selector="boards_dropdown"
2019-09-30 21:07:59 +05:30
toggle-class="dropdown-menu-toggle js-dropdown-toggle"
menu-class="flex-column dropdown-extended-height"
2021-12-11 22:18:48 +05:30
:loading="isBoardLoading"
2019-09-30 21:07:59 +05:30
:text="board.name"
@show="loadBoards"
>
2021-02-22 17:27:13 +05:30
<p class="gl-new-dropdown-header-top" @mousedown.prevent>
{{ s__('IssueBoards|Switch board') }}
</p>
<gl-search-box-by-type ref="searchBox" v-model="filterTerm" class="m-2" />
2019-09-30 21:07:59 +05:30
<div
v-if="!loading"
ref="content"
2019-12-04 20:38:33 +05:30
data-qa-selector="boards_dropdown_content"
2019-09-30 21:07:59 +05:30
class="dropdown-content flex-fill"
@scroll.passive="throttledSetScrollFade"
>
2021-02-22 17:27:13 +05:30
<gl-dropdown-item
2019-09-30 21:07:59 +05:30
v-show="filteredBoards.length === 0"
2021-01-29 00:20:46 +05:30
class="gl-pointer-events-none text-secondary"
2019-09-30 21:07:59 +05:30
>
{{ s__('IssueBoards|No matching boards found') }}
2021-02-22 17:27:13 +05:30
</gl-dropdown-item>
2019-09-30 21:07:59 +05:30
2021-02-22 17:27:13 +05:30
<gl-dropdown-section-header v-if="showRecentSection">
2019-09-30 21:07:59 +05:30
{{ __('Recent') }}
2021-02-22 17:27:13 +05:30
</gl-dropdown-section-header>
2019-09-30 21:07:59 +05:30
<template v-if="showRecentSection">
2021-02-22 17:27:13 +05:30
<gl-dropdown-item
2019-09-30 21:07:59 +05:30
v-for="recentBoard in recentBoards"
:key="`recent-${recentBoard.id}`"
class="js-dropdown-item"
:href="`${boardBaseUrl}/${recentBoard.id}`"
>
{{ recentBoard.name }}
2021-02-22 17:27:13 +05:30
</gl-dropdown-item>
2019-09-30 21:07:59 +05:30
</template>
2021-02-22 17:27:13 +05:30
<gl-dropdown-divider v-if="showRecentSection" />
2019-09-30 21:07:59 +05:30
2021-02-22 17:27:13 +05:30
<gl-dropdown-section-header v-if="showRecentSection">
2019-09-30 21:07:59 +05:30
{{ __('All') }}
2021-02-22 17:27:13 +05:30
</gl-dropdown-section-header>
2019-09-30 21:07:59 +05:30
2021-02-22 17:27:13 +05:30
<gl-dropdown-item
2019-09-30 21:07:59 +05:30
v-for="otherBoard in filteredBoards"
:key="otherBoard.id"
class="js-dropdown-item"
:href="`${boardBaseUrl}/${otherBoard.id}`"
>
{{ otherBoard.name }}
2021-02-22 17:27:13 +05:30
</gl-dropdown-item>
<gl-dropdown-item v-if="hasMissingBoards" class="no-pointer-events">
2019-09-30 21:07:59 +05:30
{{
s__(
'IssueBoards|Some of your boards are hidden, activate a license to see them again.',
)
}}
2021-02-22 17:27:13 +05:30
</gl-dropdown-item>
2019-09-30 21:07:59 +05:30
</div>
<div
v-show="filteredBoards.length > 0"
class="dropdown-content-faded-mask"
:class="scrollFadeClass"
></div>
2021-09-30 23:02:18 +05:30
<gl-loading-icon v-if="loading" size="sm" />
2019-09-30 21:07:59 +05:30
<div v-if="canAdminBoard">
2021-02-22 17:27:13 +05:30
<gl-dropdown-divider />
2019-09-30 21:07:59 +05:30
2021-02-22 17:27:13 +05:30
<gl-dropdown-item
2021-04-17 20:07:23 +05:30
v-if="showCreate"
2021-02-22 17:27:13 +05:30
v-gl-modal-directive="'board-config-modal'"
2019-12-21 20:55:43 +05:30
data-qa-selector="create_new_board_button"
@click.prevent="showPage('new')"
>
2019-09-30 21:07:59 +05:30
{{ s__('IssueBoards|Create new board') }}
2021-02-22 17:27:13 +05:30
</gl-dropdown-item>
2019-09-30 21:07:59 +05:30
2021-02-22 17:27:13 +05:30
<gl-dropdown-item
2019-09-30 21:07:59 +05:30
v-if="showDelete"
2021-02-22 17:27:13 +05:30
v-gl-modal-directive="'board-config-modal'"
2020-01-01 13:55:28 +05:30
class="text-danger js-delete-board"
2019-09-30 21:07:59 +05:30
@click.prevent="showPage('delete')"
>
{{ s__('IssueBoards|Delete board') }}
2021-02-22 17:27:13 +05:30
</gl-dropdown-item>
2019-09-30 21:07:59 +05:30
</div>
2021-02-22 17:27:13 +05:30
</gl-dropdown>
2019-09-30 21:07:59 +05:30
<board-form
v-if="currentPage"
:can-admin-board="canAdminBoard"
:scoped-issue-board-feature-enabled="scopedIssueBoardFeatureEnabled"
:weights="weights"
2021-12-11 22:18:48 +05:30
:current-board="board"
2021-03-11 19:13:27 +05:30
:current-page="currentPage"
@cancel="cancel"
2019-09-30 21:07:59 +05:30
/>
</span>
</div>
</template>