summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/boards/components/board_list.js
blob: 86e6c26e570a8200df30fb0ff9d436e54b7c96cf (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
/* eslint-disable comma-dangle, space-before-function-paren, max-len */
/* global Sortable */

import Vue from 'vue';
import boardNewIssue from './board_new_issue';
import boardCard from './board_card';

(() => {
  const Store = gl.issueBoards.BoardsStore;

  window.gl = window.gl || {};
  window.gl.issueBoards = window.gl.issueBoards || {};

  gl.issueBoards.BoardList = Vue.extend({
    template: '#js-board-list-template',
    components: {
      boardCard,
      boardNewIssue,
    },
    props: {
      disabled: Boolean,
      list: Object,
      issues: Array,
      loading: Boolean,
      issueLinkBase: String,
      rootPath: String,
    },
    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);
          }

          if (this.scrollHeight() > Math.ceil(this.listHeight())) {
            this.showCount = true;
          } else {
            this.showCount = false;
          }
        });
      }
    },
    methods: {
      listHeight () {
        return this.$refs.list.getBoundingClientRect().height;
      },
      scrollHeight () {
        return this.$refs.list.scrollHeight;
      },
      scrollTop () {
        return this.$refs.list.scrollTop + this.listHeight();
      },
      loadNextPage () {
        const getIssues = this.list.nextPage();

        if (getIssues) {
          this.list.loadingMore = true;
          getIssues.then(() => {
            this.list.loadingMore = false;
          });
        }
      },
      toggleForm() {
        this.showIssueForm = !this.showIssueForm;
      },
    },
    created() {
      gl.IssueBoardsApp.$on(`hide-issue-form-${this.list.id}`, this.toggleForm);
    },
    mounted () {
      const options = gl.issueBoards.getBoardSortableDefaultOptions({
        scroll: document.querySelectorAll('.boards-list')[0],
        group: 'issues',
        disabled: this.disabled,
        filter: '.board-list-count, .is-disabled',
        dataIdAttr: 'data-issue-id',
        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) => {
          const sortedArray = this.sortable.toArray().filter(id => id !== '-1');
          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.onscroll = () => {
        if ((this.scrollTop() > this.scrollHeight() - this.scrollOffset) && !this.list.loadingMore) {
          this.loadNextPage();
        }
      };
    },
    beforeDestroy() {
      gl.IssueBoardsApp.$off(`hide-issue-form-${this.list.id}`, this.toggleForm);
    },
  });
})();