summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/boards/components/board_content.vue
blob: 92f79e61f14c6a5132b9deb1acebcdd5c87e9f27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<script>
import { GlAlert } from '@gitlab/ui';
import { breakpoints } from '@gitlab/ui/dist/utils';
import { sortBy, throttle } from 'lodash';
import Draggable from 'vuedraggable';
import { mapState, mapGetters, mapActions } from 'vuex';
import { contentTop } from '~/lib/utils/common_utils';
import { s__ } from '~/locale';
import { formatBoardLists } from 'ee_else_ce/boards/boards_util';
import BoardAddNewColumn from 'ee_else_ce/boards/components/board_add_new_column.vue';
import { defaultSortableOptions } from '~/sortable/constants';
import { DraggableItemTypes, listsQuery } from 'ee_else_ce/boards/constants';
import BoardColumn from './board_column.vue';

export default {
  i18n: {
    fetchError: s__(
      'Boards|An error occurred while fetching the board lists. Please reload the page.',
    ),
  },
  draggableItemTypes: DraggableItemTypes,
  components: {
    BoardAddNewColumn,
    BoardColumn,
    BoardContentSidebar: () => import('~/boards/components/board_content_sidebar.vue'),
    EpicBoardContentSidebar: () =>
      import('ee_component/boards/components/epic_board_content_sidebar.vue'),
    EpicsSwimlanes: () => import('ee_component/boards/components/epics_swimlanes.vue'),
    GlAlert,
  },
  inject: [
    'canAdminList',
    'boardType',
    'fullPath',
    'issuableType',
    'isIssueBoard',
    'isEpicBoard',
    'isGroupBoard',
    'disabled',
    'isApolloBoard',
  ],
  props: {
    boardId: {
      type: String,
      required: true,
    },
  },
  data() {
    return {
      boardHeight: null,
      boardListsApollo: {},
      apolloError: null,
      updatedBoardId: this.boardId,
    };
  },
  apollo: {
    boardListsApollo: {
      query() {
        return listsQuery[this.issuableType].query;
      },
      variables() {
        return this.queryVariables;
      },
      skip() {
        return !this.isApolloBoard;
      },
      update(data) {
        const { lists } = data[this.boardType].board;
        return formatBoardLists(lists);
      },
      result() {
        // this allows us to delay fetching lists when we switch a board to fetch the actual board lists
        // instead of fetching lists for the "previous" board
        this.updatedBoardId = this.boardId;
      },
      error() {
        this.apolloError = this.$options.i18n.fetchError;
      },
    },
  },
  computed: {
    ...mapState(['boardLists', 'error', 'addColumnForm']),
    ...mapGetters(['isSwimlanesOn']),
    addColumnFormVisible() {
      return this.addColumnForm?.visible;
    },
    queryVariables() {
      return {
        ...(this.isIssueBoard && {
          isGroup: this.isGroupBoard,
          isProject: !this.isGroupBoard,
        }),
        fullPath: this.fullPath,
        boardId: this.boardId,
        filterParams: this.filterParams,
      };
    },
    boardListsToUse() {
      const lists = this.isApolloBoard ? this.boardListsApollo : this.boardLists;
      return sortBy([...Object.values(lists)], 'position');
    },
    canDragColumns() {
      return this.canAdminList;
    },
    boardColumnWrapper() {
      return this.canDragColumns ? Draggable : 'div';
    },
    draggableOptions() {
      const options = {
        ...defaultSortableOptions,
        disabled: this.disabled,
        draggable: '.is-draggable',
        fallbackOnBody: false,
        group: 'boards-list',
        tag: 'div',
        value: this.boardListsToUse,
        delay: 100,
        delayOnTouchOnly: true,
      };

      return this.canDragColumns ? options : {};
    },
    errorToDisplay() {
      return this.isApolloBoard ? this.apolloError : this.error;
    },
  },
  mounted() {
    this.setBoardHeight();

    this.resizeObserver = new ResizeObserver(
      throttle(() => {
        this.setBoardHeight();
      }, 150),
    );
    this.resizeObserver.observe(document.body);
  },
  unmounted() {
    this.resizeObserver.disconnect();
  },
  methods: {
    ...mapActions(['moveList', 'unsetError']),
    afterFormEnters() {
      const el = this.canDragColumns ? this.$refs.list.$el : this.$refs.list;
      el.scrollTo({ left: el.scrollWidth, behavior: 'smooth' });
    },
    setBoardHeight() {
      if (window.innerWidth < breakpoints.md) {
        this.boardHeight = `${window.innerHeight - contentTop()}px`;
      } else {
        this.boardHeight = `${window.innerHeight - this.$el.getBoundingClientRect().top}px`;
      }
    },
  },
};
</script>

<template>
  <div v-cloak data-qa-selector="boards_list">
    <gl-alert v-if="errorToDisplay" variant="danger" :dismissible="true" @dismiss="unsetError">
      {{ errorToDisplay }}
    </gl-alert>
    <component
      :is="boardColumnWrapper"
      v-if="!isSwimlanesOn"
      ref="list"
      v-bind="draggableOptions"
      class="boards-list gl-w-full gl-py-5 gl-pr-3 gl-white-space-nowrap gl-overflow-x-scroll"
      :style="{ height: boardHeight }"
      @end="moveList"
    >
      <board-column
        v-for="(list, index) in boardListsToUse"
        :key="index"
        ref="board"
        :list="list"
        :data-draggable-item-type="$options.draggableItemTypes.list"
        :class="{ 'gl-xs-display-none!': addColumnFormVisible }"
      />

      <transition name="slide" @after-enter="afterFormEnters">
        <board-add-new-column v-if="addColumnFormVisible" class="gl-xs-w-full!" />
      </transition>
    </component>

    <epics-swimlanes
      v-else-if="boardListsToUse.length"
      ref="swimlanes"
      :lists="boardListsToUse"
      :can-admin-list="canAdminList"
      :style="{ height: boardHeight }"
    />

    <board-content-sidebar v-if="isIssueBoard" data-testid="issue-boards-sidebar" />

    <epic-board-content-sidebar v-else-if="isEpicBoard" data-testid="epic-boards-sidebar" />
  </div>
</template>