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

350 lines
9 KiB
Vue
Raw Normal View History

2019-09-30 21:07:59 +05:30
<script>
2020-04-08 14:13:33 +05:30
import { throttle } from 'lodash';
2019-09-30 21:07:59 +05:30
import {
GlLoadingIcon,
GlSearchBoxByType,
2020-10-24 23:57:45 +05:30
GlDeprecatedDropdown,
GlDeprecatedDropdownDivider,
GlDeprecatedDropdownHeader,
GlDeprecatedDropdownItem,
2019-09-30 21:07:59 +05:30
} from '@gitlab/ui';
import httpStatusCodes from '~/lib/utils/http_status';
2020-04-08 14:13:33 +05:30
import { getIdFromGraphQLId } from '~/graphql_shared/utils';
import projectQuery from '../queries/project_boards.query.graphql';
import groupQuery from '../queries/group_boards.query.graphql';
2019-09-30 21:07:59 +05:30
import boardsStore from '../stores/boards_store';
import BoardForm from './board_form.vue';
const MIN_BOARDS_TO_VIEW_RECENT = 10;
export default {
name: 'BoardsSelector',
components: {
BoardForm,
GlLoadingIcon,
GlSearchBoxByType,
2020-10-24 23:57:45 +05:30
GlDeprecatedDropdown,
GlDeprecatedDropdownDivider,
GlDeprecatedDropdownHeader,
GlDeprecatedDropdownItem,
2019-09-30 21:07:59 +05:30
},
props: {
currentBoard: {
type: Object,
required: true,
},
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,
},
labelsPath: {
type: String,
required: true,
},
2020-11-24 15:15:51 +05:30
labelsWebUrl: {
type: String,
required: true,
},
2019-09-30 21:07:59 +05:30
projectId: {
type: Number,
required: true,
},
groupId: {
type: Number,
required: true,
},
scopedIssueBoardFeatureEnabled: {
type: Boolean,
required: true,
},
weights: {
type: Array,
required: true,
},
enabledScopedLabels: {
type: Boolean,
required: false,
default: false,
},
},
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: [],
state: boardsStore.state,
throttledSetScrollFade: throttle(this.setScrollFade, this.throttleDuration),
contentClientHeight: 0,
maxPosition: 0,
store: boardsStore,
filterTerm: '',
};
},
computed: {
2020-04-08 14:13:33 +05:30
parentType() {
return this.groupId ? 'group' : 'project';
},
loading() {
return this.loadingRecentBoards && this.loadingBoards;
},
2019-09-30 21:07:59 +05:30
currentPage() {
return this.state.currentPage;
},
filteredBoards() {
return this.boards.filter(board =>
board.name.toLowerCase().includes(this.filterTerm.toLowerCase()),
);
},
board() {
return this.state.currentBoard;
},
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() {
boardsStore.setCurrentBoard(this.currentBoard);
},
methods: {
showPage(page) {
boardsStore.showPage(page);
},
loadBoards(toggleDropdown = true) {
if (toggleDropdown && this.boards.length > 0) {
return;
}
2020-04-08 14:13:33 +05:30
this.$apollo.addSmartQuery('boards', {
variables() {
return { fullPath: this.state.endpoints.fullPath };
},
query() {
return this.groupId ? groupQuery : projectQuery;
},
loadingKey: 'loadingBoards',
update(data) {
if (!data?.[this.parentType]) {
return [];
}
return data[this.parentType].boards.edges.map(({ node }) => ({
id: getIdFromGraphQLId(node.id),
name: node.name,
}));
},
});
2019-09-30 21:07:59 +05:30
2020-04-08 14:13:33 +05:30
this.loadingRecentBoards = true;
boardsStore
.recentBoards()
.then(res => {
this.recentBoards = res.data;
})
.catch(err => {
/**
* 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();
},
},
};
</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">
2020-10-24 23:57:45 +05:30
<gl-deprecated-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"
:text="board.name"
@show="loadBoards"
>
<div>
<div class="dropdown-title mb-0" @mousedown.prevent>
{{ s__('IssueBoards|Switch board') }}
</div>
</div>
2020-10-24 23:57:45 +05:30
<gl-deprecated-dropdown-header class="mt-0">
2019-09-30 21:07:59 +05:30
<gl-search-box-by-type ref="searchBox" v-model="filterTerm" />
2020-10-24 23:57:45 +05:30
</gl-deprecated-dropdown-header>
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"
>
2020-10-24 23:57:45 +05:30
<gl-deprecated-dropdown-item
2019-09-30 21:07:59 +05:30
v-show="filteredBoards.length === 0"
class="no-pointer-events text-secondary"
>
{{ s__('IssueBoards|No matching boards found') }}
2020-10-24 23:57:45 +05:30
</gl-deprecated-dropdown-item>
2019-09-30 21:07:59 +05:30
<h6 v-if="showRecentSection" class="dropdown-bold-header my-0">
{{ __('Recent') }}
</h6>
<template v-if="showRecentSection">
2020-10-24 23:57:45 +05:30
<gl-deprecated-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 }}
2020-10-24 23:57:45 +05:30
</gl-deprecated-dropdown-item>
2019-09-30 21:07:59 +05:30
</template>
<hr v-if="showRecentSection" class="my-1" />
<h6 v-if="showRecentSection" class="dropdown-bold-header my-0">
{{ __('All') }}
</h6>
2020-10-24 23:57:45 +05:30
<gl-deprecated-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 }}
2020-10-24 23:57:45 +05:30
</gl-deprecated-dropdown-item>
<gl-deprecated-dropdown-item v-if="hasMissingBoards" class="small unclickable">
2019-09-30 21:07:59 +05:30
{{
s__(
'IssueBoards|Some of your boards are hidden, activate a license to see them again.',
)
}}
2020-10-24 23:57:45 +05:30
</gl-deprecated-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>
<gl-loading-icon v-if="loading" />
<div v-if="canAdminBoard">
2020-10-24 23:57:45 +05:30
<gl-deprecated-dropdown-divider />
2019-09-30 21:07:59 +05:30
2020-10-24 23:57:45 +05:30
<gl-deprecated-dropdown-item
2019-12-21 20:55:43 +05:30
v-if="multipleIssueBoardsAvailable"
data-qa-selector="create_new_board_button"
@click.prevent="showPage('new')"
>
2019-09-30 21:07:59 +05:30
{{ s__('IssueBoards|Create new board') }}
2020-10-24 23:57:45 +05:30
</gl-deprecated-dropdown-item>
2019-09-30 21:07:59 +05:30
2020-10-24 23:57:45 +05:30
<gl-deprecated-dropdown-item
2019-09-30 21:07:59 +05:30
v-if="showDelete"
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') }}
2020-10-24 23:57:45 +05:30
</gl-deprecated-dropdown-item>
2019-09-30 21:07:59 +05:30
</div>
2020-10-24 23:57:45 +05:30
</gl-deprecated-dropdown>
2019-09-30 21:07:59 +05:30
<board-form
v-if="currentPage"
:labels-path="labelsPath"
2020-11-24 15:15:51 +05:30
:labels-web-url="labelsWebUrl"
2019-09-30 21:07:59 +05:30
:project-id="projectId"
:group-id="groupId"
:can-admin-board="canAdminBoard"
:scoped-issue-board-feature-enabled="scopedIssueBoardFeatureEnabled"
:weights="weights"
:enable-scoped-labels="enabledScopedLabels"
/>
</span>
</div>
</template>