board_list.vue 7.23 KB
Newer Older
1
<script>
Kushal Pandya's avatar
Kushal Pandya committed
2
import Sortable from 'sortablejs';
3
import boardNewIssue from './board_new_issue.vue';
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import boardCard from './board_card.vue';
import eventHub from '../eventhub';
import loadingIcon from '../../vue_shared/components/loading_icon.vue';

const Store = gl.issueBoards.BoardsStore;

export default {
  name: 'BoardList',
  components: {
    boardCard,
    boardNewIssue,
    loadingIcon,
  },
  props: {
Felipe Artur's avatar
Felipe Artur committed
18
19
20
21
22
    groupId: {
      type: Number,
      required: false,
      default: 0,
    },
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
    disabled: {
      type: Boolean,
      required: true,
    },
    list: {
      type: Object,
      required: true,
    },
    issues: {
      type: Array,
      required: true,
    },
    loading: {
      type: Boolean,
      required: true,
    },
    issueLinkBase: {
      type: String,
      required: true,
    },
    rootPath: {
      type: String,
      required: true,
    },
  },
  data() {
    return {
      scrollOffset: 250,
      filters: Store.state.filters,
      showCount: false,
      showIssueForm: false,
    };
  },
  watch: {
    filters: {
      handler() {
        this.list.loadingMore = false;
        this.$refs.list.scrollTop = 0;
      },
      deep: true,
    },
    issues() {
      this.$nextTick(() => {
        if (this.scrollHeight() <= this.listHeight() &&
          this.list.issuesSize > this.list.issues.length) {
          this.list.page += 1;
          this.list.getIssues(false)
            .catch(() => {
              // TODO: handle request error
            });
        }

        if (this.scrollHeight() > Math.ceil(this.listHeight())) {
          this.showCount = true;
        } else {
          this.showCount = false;
        }
      });
    },
  },
  created() {
    eventHub.$on(`hide-issue-form-${this.list.id}`, this.toggleForm);
    eventHub.$on(`scroll-board-list-${this.list.id}`, this.scrollToTop);
  },
  mounted() {
    const options = gl.issueBoards.getBoardSortableDefaultOptions({
      scroll: true,
      disabled: this.disabled,
      filter: '.board-list-count, .is-disabled',
      dataIdAttr: 'data-issue-id',
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
      group: {
        name: 'issues',
        /**
         * Dynamically determine between which containers
         * items can be moved or copied as
         * Assignee lists (EE feature) require this behavior
         */
        pull: (to, from, dragEl, e) => {
          // As per Sortable's docs, `to` should provide
          // reference to exact sortable container on which
          // we're trying to drag element, but either it is
          // a library's bug or our markup structure is too complex
          // that `to` never points to correct container
          // See https://github.com/RubaXa/Sortable/issues/1037
          //
          // So we use `e.target` which is always accurate about
          // which element we're currently dragging our card upon
          // So from there, we can get reference to actual container
          // and thus the container type to enable Copy or Move
          if (e.target) {
            const containerEl = e.target.closest('.js-board-list') || e.target.querySelector('.js-board-list');
            const toBoardType = containerEl.dataset.boardType;
115
116
117
118
119
            const cloneActions = {
              label: ['milestone', 'assignee'],
              assignee: ['milestone', 'label'],
              milestone: ['label', 'assignee'],
            };
120
121
122

            if (toBoardType) {
              const fromBoardType = this.list.type;
123
124
125
126
              // For each list we check if the destination list is
              // a the list were we should clone the issue
              const shouldClone = Object.entries(cloneActions).some(entry => (
                 fromBoardType === entry[0] && entry[1].includes(toBoardType)));
127

128
              if (shouldClone) {
129
130
131
132
133
134
135
136
137
                return 'clone';
              }
            }
          }

          return true;
        },
        revertClone: true,
      },
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
      onStart: (e) => {
        const card = this.$refs.issue[e.oldIndex];

        card.showDetail = false;
        Store.moving.list = card.list;
        Store.moving.issue = Store.moving.list.findIssue(+e.item.dataset.issueId);

        gl.issueBoards.onStart();
      },
      onAdd: (e) => {
        gl.issueBoards.BoardsStore
          .moveIssueToList(Store.moving.list, this.list, Store.moving.issue, e.newIndex);

        this.$nextTick(() => {
          e.item.remove();
        });
      },
      onUpdate: (e) => {
156
157
        const sortedArray = this.sortable.toArray()
          .filter(id => id !== '-1');
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
198
199
200
201
202
203
204
205
        gl.issueBoards.BoardsStore
          .moveIssueInList(this.list, Store.moving.issue, e.oldIndex, e.newIndex, sortedArray);
      },
      onMove(e) {
        return !e.related.classList.contains('board-list-count');
      },
    });

    this.sortable = Sortable.create(this.$refs.list, options);

    // Scroll event on list to load more
    this.$refs.list.addEventListener('scroll', this.onScroll);
  },
  beforeDestroy() {
    eventHub.$off(`hide-issue-form-${this.list.id}`, this.toggleForm);
    eventHub.$off(`scroll-board-list-${this.list.id}`, this.scrollToTop);
    this.$refs.list.removeEventListener('scroll', this.onScroll);
  },
  methods: {
    listHeight() {
      return this.$refs.list.getBoundingClientRect().height;
    },
    scrollHeight() {
      return this.$refs.list.scrollHeight;
    },
    scrollTop() {
      return this.$refs.list.scrollTop + this.listHeight();
    },
    scrollToTop() {
      this.$refs.list.scrollTop = 0;
    },
    loadNextPage() {
      const getIssues = this.list.nextPage();
      const loadingDone = () => {
        this.list.loadingMore = false;
      };

      if (getIssues) {
        this.list.loadingMore = true;
        getIssues
          .then(loadingDone)
          .catch(loadingDone);
      }
    },
    toggleForm() {
      this.showIssueForm = !this.showIssueForm;
    },
    onScroll() {
Dicker Max's avatar
Dicker Max committed
206
      if (!this.list.loadingMore && (this.scrollTop() > this.scrollHeight() - this.scrollOffset)) {
207
208
209
210
211
212
213
214
215
216
        this.loadNextPage();
      }
    },
  },
};
</script>

<template>
  <div class="board-list-component">
    <div
217
      v-if="loading"
218
      class="board-list-loading text-center"
219
      aria-label="Loading issues">
220
221
222
      <loading-icon />
    </div>
    <board-new-issue
223
      v-if="list.type !== 'closed' && showIssueForm"
Felipe Artur's avatar
Felipe Artur committed
224
      :group-id="groupId"
225
      :list="list"/>
226
227
228
229
    <ul
      v-show="!loading"
      ref="list"
      :data-board="list.id"
230
      :data-board-type="list.type"
231
232
      :class="{ 'is-smaller': showIssueForm }"
      class="board-list js-board-list">
233
234
235
236
237
238
239
      <board-card
        v-for="(issue, index) in issues"
        ref="issue"
        :index="index"
        :list="list"
        :issue="issue"
        :issue-link-base="issueLinkBase"
Felipe Artur's avatar
Felipe Artur committed
240
        :group-id="groupId"
241
242
243
244
        :root-path="rootPath"
        :disabled="disabled"
        :key="issue.id" />
      <li
245
        v-if="showCount"
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
        class="board-list-count text-center"
        data-issue-id="-1">
        <loading-icon
          v-show="list.loadingMore"
          label="Loading more issues"
        />
        <span
          v-if="list.issues.length === list.issuesSize"
        >
          Showing all issues
        </span>
        <span
          v-else
        >
          Showing {{ list.issues.length }} of {{ list.issuesSize }} issues
        </span>
      </li>
    </ul>
  </div>
</template>