board_list.js 5.0 KB
Newer Older
1
/* global Sortable */
2 3
import boardNewIssue from './board_new_issue';
import boardCard from './board_card';
4
import eventHub from '../eventhub';
5

6
const Store = gl.issueBoards.BoardsStore;
7

8 9 10 11 12
export default {
  template: `
    <div class="board-list-component">
      <div
        class="board-list-loading text-center"
13
        aria-label="Loading issues"
14
        v-if="loading">
15 16 17 18
        <i
          class="fa fa-spinner fa-spin"
          aria-hidden="true">
        </i>
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
      </div>
      <board-new-issue
        :list="list"
        v-if="list.type !== 'closed' && showIssueForm"/>
      <ul
        class="board-list"
        v-show="!loading"
        ref="list"
        :data-board="list.id"
        :class="{ 'is-smaller': showIssueForm }">
        <board-card
          v-for="(issue, index) in issues"
          ref="issue"
          :index="index"
          :list="list"
          :issue="issue"
          :issue-link-base="issueLinkBase"
          :root-path="rootPath"
          :disabled="disabled"
          :key="issue.id" />
        <li
          class="board-list-count text-center"
          v-if="showCount"
          data-id="-1">
          <i
            class="fa fa-spinner fa-spin"
45 46
            aria-label="Loading more issues"
            aria-hidden="true"
47 48 49
            v-show="list.loadingMore">
          </i>
          <span v-if="list.issues.length === list.issuesSize">
50
            Showing all issues
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
          </span>
          <span v-else>
            Showing {{ list.issues.length }} of {{ list.issuesSize }} issues
          </span>
        </li>
      </ul>
    </div>
  `,
  components: {
    boardCard,
    boardNewIssue,
  },
  props: {
    disabled: {
      type: Boolean,
      required: true,
67
    },
68 69 70
    list: {
      type: Object,
      required: true,
P
Phil Hughes 已提交
71
    },
72 73 74
    issues: {
      type: Array,
      required: true,
P
Phil Hughes 已提交
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
    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;
P
Phil Hughes 已提交
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
      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();
133

134 135 136 137
      if (getIssues) {
        this.list.loadingMore = true;
        getIssues.then(() => {
          this.list.loadingMore = false;
P
Phil Hughes 已提交
138
        });
139 140
      }
    },
141 142
    toggleForm() {
      this.showIssueForm = !this.showIssueForm;
143
    },
144 145 146 147
    onScroll() {
      if ((this.scrollTop() > this.scrollHeight() - this.scrollOffset) && !this.list.loadingMore) {
        this.loadNextPage();
      }
P
Phil Hughes 已提交
148
    },
149 150 151 152 153 154 155 156 157 158 159 160 161
  },
  created() {
    eventHub.$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];
P
Phil Hughes 已提交
162

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

167 168 169 170 171
        gl.issueBoards.onStart();
      },
      onAdd: (e) => {
        gl.issueBoards.BoardsStore
          .moveIssueToList(Store.moving.list, this.list, Store.moving.issue, e.newIndex);
172

173 174 175 176 177 178 179 180 181 182 183 184 185
        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');
      },
    });
186

187
    this.sortable = Sortable.create(this.$refs.list, options);
P
Phil Hughes 已提交
188

189 190 191 192 193 194 195 196
    // 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);
    this.$refs.list.removeEventListener('scroll', this.onScroll);
  },
};