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

268 lines
7.9 KiB
Vue
Raw Normal View History

2018-03-17 18:26:18 +05:30
<script>
2021-09-04 01:27:46 +05:30
import { GlLoadingIcon, GlIntersectionObserver } from '@gitlab/ui';
2021-03-08 18:12:59 +05:30
import Draggable from 'vuedraggable';
2021-04-17 20:07:23 +05:30
import { mapActions, mapGetters, mapState } from 'vuex';
2021-03-08 18:12:59 +05:30
import { sortableStart, sortableEnd } from '~/boards/mixins/sortable_default_options';
2019-12-21 20:55:43 +05:30
import { sprintf, __ } from '~/locale';
2021-03-11 19:13:27 +05:30
import defaultSortableConfig from '~/sortable/sortable_config';
2021-09-04 01:27:46 +05:30
import Tracking from '~/tracking';
2021-03-11 19:13:27 +05:30
import eventHub from '../eventhub';
import BoardCard from './board_card.vue';
import BoardNewIssue from './board_new_issue.vue';
2017-08-17 22:00:37 +05:30
export default {
name: 'BoardList',
2021-03-08 18:12:59 +05:30
i18n: {
2021-04-17 20:07:23 +05:30
loading: __('Loading'),
loadingMoreboardItems: __('Loading more'),
2021-03-08 18:12:59 +05:30
showingAllIssues: __('Showing all issues'),
2021-04-17 20:07:23 +05:30
showingAllEpics: __('Showing all epics'),
2021-03-08 18:12:59 +05:30
},
2018-03-17 18:26:18 +05:30
components: {
2021-03-08 18:12:59 +05:30
BoardCard,
BoardNewIssue,
2018-12-13 13:39:08 +05:30
GlLoadingIcon,
2021-09-04 01:27:46 +05:30
GlIntersectionObserver,
},
mixins: [Tracking.mixin()],
inject: {
canAdminList: {
default: false,
},
2018-03-17 18:26:18 +05:30
},
2017-08-17 22:00:37 +05:30
props: {
disabled: {
type: Boolean,
required: true,
},
list: {
type: Object,
required: true,
},
2021-04-17 20:07:23 +05:30
boardItems: {
2017-08-17 22:00:37 +05:30
type: Array,
required: true,
},
},
data() {
return {
scrollOffset: 250,
showCount: false,
showIssueForm: false,
};
},
2019-12-21 20:55:43 +05:30
computed: {
2021-03-08 18:12:59 +05:30
...mapState(['pageInfoByListId', 'listsFlags']),
2021-04-17 20:07:23 +05:30
...mapGetters(['isEpicBoard']),
listItemsCount() {
return this.isEpicBoard ? this.list.epicsCount : this.list.issuesCount;
},
2019-12-21 20:55:43 +05:30
paginatedIssueText() {
2021-04-17 20:07:23 +05:30
return sprintf(__('Showing %{pageSize} of %{total} %{issuableType}'), {
pageSize: this.boardItems.length,
total: this.listItemsCount,
issuableType: this.isEpicBoard ? 'epics' : 'issues',
2019-12-21 20:55:43 +05:30
});
},
2021-04-17 20:07:23 +05:30
boardItemsSizeExceedsMax() {
return this.list.maxIssueCount > 0 && this.listItemsCount > this.list.maxIssueCount;
2021-03-08 18:12:59 +05:30
},
hasNextPage() {
2021-09-04 01:27:46 +05:30
return this.pageInfoByListId[this.list.id]?.hasNextPage;
2020-01-01 13:55:28 +05:30
},
2021-01-03 14:25:43 +05:30
loading() {
2021-03-08 18:12:59 +05:30
return this.listsFlags[this.list.id]?.isLoading;
},
loadingMore() {
return this.listsFlags[this.list.id]?.isLoadingMore;
},
listRef() {
// When list is draggable, the reference to the list needs to be accessed differently
return this.canAdminList ? this.$refs.list.$el : this.$refs.list;
},
2021-04-17 20:07:23 +05:30
showingAllItems() {
return this.boardItems.length === this.listItemsCount;
},
showingAllItemsText() {
return this.isEpicBoard
? this.$options.i18n.showingAllEpics
: this.$options.i18n.showingAllIssues;
2021-03-08 18:12:59 +05:30
},
treeRootWrapper() {
2021-09-04 01:27:46 +05:30
return this.canAdminList && !this.listsFlags[this.list.id]?.addItemToListInProgress
? Draggable
: 'ul';
2021-03-08 18:12:59 +05:30
},
treeRootOptions() {
const options = {
...defaultSortableConfig,
fallbackOnBody: false,
group: 'board-list',
tag: 'ul',
'ghost-class': 'board-card-drag-active',
'data-list-id': this.list.id,
2021-04-17 20:07:23 +05:30
value: this.boardItems,
2021-03-08 18:12:59 +05:30
};
return this.canAdminList ? options : {};
2021-01-03 14:25:43 +05:30
},
2019-12-21 20:55:43 +05:30
},
2017-08-17 22:00:37 +05:30
watch: {
2021-04-17 20:07:23 +05:30
boardItems() {
2017-08-17 22:00:37 +05:30
this.$nextTick(() => {
2021-03-08 18:12:59 +05:30
this.showCount = this.scrollHeight() > Math.ceil(this.listHeight());
2017-08-17 22:00:37 +05:30
});
},
2021-09-04 01:27:46 +05:30
'list.id': {
handler(id, oldVal) {
if (id) {
eventHub.$on(`toggle-issue-form-${this.list.id}`, this.toggleForm);
eventHub.$on(`scroll-board-list-${this.list.id}`, this.scrollToTop);
eventHub.$off(`toggle-issue-form-${oldVal}`, this.toggleForm);
eventHub.$off(`scroll-board-list-${oldVal}`, this.scrollToTop);
}
},
immediate: true,
},
2017-08-17 22:00:37 +05:30
},
beforeDestroy() {
2020-06-23 00:09:42 +05:30
eventHub.$off(`toggle-issue-form-${this.list.id}`, this.toggleForm);
2017-09-10 17:25:29 +05:30
eventHub.$off(`scroll-board-list-${this.list.id}`, this.scrollToTop);
2017-08-17 22:00:37 +05:30
},
2018-03-17 18:26:18 +05:30
methods: {
2021-04-17 20:07:23 +05:30
...mapActions(['fetchItemsForList', 'moveItem']),
2018-03-17 18:26:18 +05:30
listHeight() {
2021-03-08 18:12:59 +05:30
return this.listRef.getBoundingClientRect().height;
2018-03-17 18:26:18 +05:30
},
scrollHeight() {
2021-03-08 18:12:59 +05:30
return this.listRef.scrollHeight;
2018-03-17 18:26:18 +05:30
},
scrollTop() {
2021-03-08 18:12:59 +05:30
return this.listRef.scrollTop + this.listHeight();
2018-03-17 18:26:18 +05:30
},
scrollToTop() {
2021-03-08 18:12:59 +05:30
this.listRef.scrollTop = 0;
2018-03-17 18:26:18 +05:30
},
loadNextPage() {
2021-04-17 20:07:23 +05:30
this.fetchItemsForList({ listId: this.list.id, fetchNext: true });
2018-03-17 18:26:18 +05:30
},
toggleForm() {
this.showIssueForm = !this.showIssueForm;
},
2021-09-04 01:27:46 +05:30
onReachingListBottom() {
if (!this.loadingMore && this.hasNextPage) {
this.showCount = true;
this.loadNextPage();
}
2021-03-08 18:12:59 +05:30
},
handleDragOnStart() {
sortableStart();
2021-09-04 01:27:46 +05:30
this.track('drag_card', { label: 'board' });
2021-03-08 18:12:59 +05:30
},
handleDragOnEnd(params) {
sortableEnd();
2021-09-04 01:27:46 +05:30
const { oldIndex, from, to, item } = params;
let { newIndex } = params;
2021-04-17 20:07:23 +05:30
const { itemId, itemIid, itemPath } = item.dataset;
2021-09-04 01:27:46 +05:30
let { children } = to;
2021-03-08 18:12:59 +05:30
let moveBeforeId;
let moveAfterId;
2021-09-04 01:27:46 +05:30
children = Array.from(children).filter((card) => card.classList.contains('board-card'));
if (newIndex > children.length) {
newIndex = children.length;
}
2021-04-17 20:07:23 +05:30
const getItemId = (el) => Number(el.dataset.itemId);
2021-03-08 18:12:59 +05:30
2021-04-17 20:07:23 +05:30
// If item is being moved within the same list
2021-03-08 18:12:59 +05:30
if (from === to) {
if (newIndex > oldIndex && children.length > 1) {
2021-04-17 20:07:23 +05:30
// If item is being moved down we look for the item that ends up before
moveBeforeId = getItemId(children[newIndex]);
2021-03-08 18:12:59 +05:30
} else if (newIndex < oldIndex && children.length > 1) {
2021-04-17 20:07:23 +05:30
// If item is being moved up we look for the item that ends up after
moveAfterId = getItemId(children[newIndex]);
2021-03-08 18:12:59 +05:30
} else {
2021-04-17 20:07:23 +05:30
// If item remains in the same list at the same position we do nothing
2021-03-08 18:12:59 +05:30
return;
}
} else {
2021-04-17 20:07:23 +05:30
// We look for the item that ends up before the moved item if it exists
2021-03-08 18:12:59 +05:30
if (children[newIndex - 1]) {
2021-04-17 20:07:23 +05:30
moveBeforeId = getItemId(children[newIndex - 1]);
2021-03-08 18:12:59 +05:30
}
2021-04-17 20:07:23 +05:30
// We look for the item that ends up after the moved item if it exists
2021-03-08 18:12:59 +05:30
if (children[newIndex]) {
2021-04-17 20:07:23 +05:30
moveAfterId = getItemId(children[newIndex]);
2021-03-08 18:12:59 +05:30
}
2018-03-17 18:26:18 +05:30
}
2021-03-08 18:12:59 +05:30
2021-04-17 20:07:23 +05:30
this.moveItem({
2021-04-29 21:17:54 +05:30
itemId: Number(itemId),
2021-04-17 20:07:23 +05:30
itemIid,
itemPath,
2021-03-08 18:12:59 +05:30
fromListId: from.dataset.listId,
toListId: to.dataset.listId,
moveBeforeId,
moveAfterId,
});
2018-03-17 18:26:18 +05:30
},
},
};
</script>
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
<template>
2019-07-31 22:56:46 +05:30
<div
2021-03-08 18:12:59 +05:30
v-show="!list.collapsed"
class="board-list-component gl-relative gl-h-full gl-display-flex gl-flex-direction-column"
2019-12-04 20:38:33 +05:30
data-qa-selector="board_list_cards_area"
2019-07-31 22:56:46 +05:30
>
2021-03-08 18:12:59 +05:30
<div
v-if="loading"
class="gl-mt-4 gl-text-center"
2021-04-17 20:07:23 +05:30
:aria-label="$options.i18n.loading"
2021-03-08 18:12:59 +05:30
data-testid="board_list_loading"
>
2018-12-05 23:21:45 +05:30
<gl-loading-icon />
2017-08-17 22:00:37 +05:30
</div>
2021-03-08 18:12:59 +05:30
<board-new-issue v-if="list.listType !== 'closed' && showIssueForm" :list="list" />
<component
:is="treeRootWrapper"
2018-03-17 18:26:18 +05:30
v-show="!loading"
ref="list"
2021-03-08 18:12:59 +05:30
v-bind="treeRootOptions"
2018-03-17 18:26:18 +05:30
:data-board="list.id"
2021-03-08 18:12:59 +05:30
:data-board-type="list.listType"
2021-04-17 20:07:23 +05:30
:class="{ 'bg-danger-100': boardItemsSizeExceedsMax }"
2021-09-04 01:27:46 +05:30
draggable=".board-card"
2021-03-08 18:12:59 +05:30
class="board-list gl-w-full gl-h-full gl-list-style-none gl-mb-0 gl-p-2 js-board-list"
data-testid="tree-root-wrapper"
@start="handleDragOnStart"
@end="handleDragOnEnd"
2019-02-15 15:39:39 +05:30
>
2018-03-17 18:26:18 +05:30
<board-card
2021-04-17 20:07:23 +05:30
v-for="(item, index) in boardItems"
2018-03-17 18:26:18 +05:30
ref="issue"
2021-04-17 20:07:23 +05:30
:key="item.id"
2018-03-17 18:26:18 +05:30
:index="index"
:list="list"
2021-04-17 20:07:23 +05:30
:item="item"
2019-02-15 15:39:39 +05:30
:disabled="disabled"
/>
2021-09-04 01:27:46 +05:30
<gl-intersection-observer @appear="onReachingListBottom">
<li v-if="showCount" class="board-list-count gl-text-center" data-issue-id="-1">
<gl-loading-icon
v-if="loadingMore"
:label="$options.i18n.loadingMoreboardItems"
data-testid="count-loading-icon"
/>
<span v-if="showingAllItems">{{ showingAllItemsText }}</span>
<span v-else>{{ paginatedIssueText }}</span>
</li>
</gl-intersection-observer>
2021-03-08 18:12:59 +05:30
</component>
2018-03-17 18:26:18 +05:30
</div>
</template>