list.js 4.23 KB
Newer Older
1
/* eslint-disable space-before-function-paren, no-underscore-dangle, class-methods-use-this, consistent-return, no-shadow, no-param-reassign, max-len, no-unused-vars */
2 3
/* global ListIssue */
/* global ListLabel */
4
import queryData from '../utils/query_data';
5

6 7
const PER_PAGE = 20;

8
class List {
9
  constructor (obj, defaultAvatar) {
10
    this.id = obj.id;
Phil Hughes's avatar
Phil Hughes committed
11
    this._uid = this.guid();
12
    this.position = obj.position;
13
    this.title = obj.title;
14
    this.type = obj.list_type;
15
    this.preset = ['closed', 'blank'].indexOf(this.type) > -1;
16
    this.page = 1;
17
    this.loading = true;
18
    this.loadingMore = false;
Phil Hughes's avatar
Phil Hughes committed
19
    this.issues = [];
20
    this.issuesSize = 0;
21
    this.defaultAvatar = defaultAvatar;
22 23

    if (obj.label) {
Phil Hughes's avatar
Phil Hughes committed
24
      this.label = new ListLabel(obj.label);
25 26
    }

27
    if (this.type !== 'blank' && this.id) {
28
      this.getIssues();
29 30 31
    }
  }

Phil Hughes's avatar
Phil Hughes committed
32
  guid() {
Phil Hughes's avatar
Phil Hughes committed
33
    const s4 = () => Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
Phil Hughes's avatar
Phil Hughes committed
34 35 36
    return `${s4()}${s4()}-${s4()}-${s4()}-${s4()}-${s4()}${s4()}${s4()}`;
  }

37
  save () {
38
    return gl.boardService.createList(this.label.id)
39 40 41 42 43 44
      .then((resp) => {
        const data = resp.json();

        this.id = data.id;
        this.type = data.list_type;
        this.position = data.position;
45

46
        return this.getIssues();
47 48 49
      });
  }

50
  destroy () {
Fatih Acet's avatar
Fatih Acet committed
51 52
    const index = gl.issueBoards.BoardsStore.state.lists.indexOf(this);
    gl.issueBoards.BoardsStore.state.lists.splice(index, 1);
53
    gl.issueBoards.BoardsStore.updateNewListDropdown(this.id);
Phil Hughes's avatar
Phil Hughes committed
54

55
    gl.boardService.destroyList(this.id);
56 57 58
  }

  update () {
Phil Hughes's avatar
Phil Hughes committed
59
    gl.boardService.updateList(this.id, this.position);
60 61
  }

62
  nextPage () {
63
    if (this.issuesSize > this.issues.length) {
64
      if (this.issues.length / PER_PAGE >= 1) {
65 66
        this.page += 1;
      }
67 68 69 70 71 72

      return this.getIssues(false);
    }
  }

  getIssues (emptyIssues = true) {
73
    const data = queryData(gl.issueBoards.BoardsStore.filter.path, { page: this.page });
Phil Hughes's avatar
Phil Hughes committed
74 75

    if (this.label && data.label_name) {
76
      data.label_name = data.label_name.filter(label => label !== this.label.title);
Phil Hughes's avatar
Phil Hughes committed
77
    }
78

79 80 81 82 83
    if (emptyIssues) {
      this.loading = true;
    }

    return gl.boardService.getIssuesForList(this.id, data)
84 85 86
      .then((resp) => {
        const data = resp.json();
        this.loading = false;
87
        this.issuesSize = data.size;
88

89 90 91 92
        if (emptyIssues) {
          this.issues = [];
        }

93
        this.createIssues(data.issues);
94 95 96
      });
  }

97 98
  newIssue (issue) {
    this.addIssue(issue);
99
    this.issuesSize += 1;
100

101
    return gl.boardService.newIssue(this.id, issue)
102 103 104 105 106 107
      .then((resp) => {
        const data = resp.json();
        issue.id = data.iid;
      });
  }

108
  createIssues (data) {
Phil Hughes's avatar
Phil Hughes committed
109
    data.forEach((issueObj) => {
110
      this.addIssue(new ListIssue(issueObj, this.defaultAvatar));
Phil Hughes's avatar
Phil Hughes committed
111
    });
112 113
  }

114
  addIssue (issue, listFrom, newIndex) {
115 116
    let moveBeforeIid = null;
    let moveAfterIid = null;
117

118
    if (!this.findIssue(issue.id)) {
119
      if (newIndex !== undefined) {
120
        this.issues.splice(newIndex, 0, issue);
121

122 123 124 125 126
        if (this.issues[newIndex - 1]) {
          moveBeforeIid = this.issues[newIndex - 1].id;
        }

        if (this.issues[newIndex + 1]) {
Phil Hughes's avatar
Phil Hughes committed
127
          moveAfterIid = this.issues[newIndex + 1].id;
128
        }
129 130 131
      } else {
        this.issues.push(issue);
      }
132

133 134 135
      if (this.label) {
        issue.addLabel(this.label);
      }
136

137
      if (listFrom) {
138
        this.issuesSize += 1;
Phil Hughes's avatar
Phil Hughes committed
139 140

        this.updateIssueLabel(issue, listFrom, moveBeforeIid, moveAfterIid);
141
      }
142
    }
143 144
  }

Phil Hughes's avatar
Phil Hughes committed
145 146 147 148
  moveIssue (issue, oldIndex, newIndex, moveBeforeIid, moveAfterIid) {
    this.issues.splice(oldIndex, 1);
    this.issues.splice(newIndex, 0, issue);

Phil Hughes's avatar
Phil Hughes committed
149
    gl.boardService.moveIssue(issue.id, null, null, moveBeforeIid, moveAfterIid);
Phil Hughes's avatar
Phil Hughes committed
150 151 152
  }

  updateIssueLabel(issue, listFrom, moveBeforeIid, moveAfterIid) {
153
    gl.boardService.moveIssue(issue.id, listFrom.id, this.id, moveBeforeIid, moveAfterIid);
154 155
  }

156
  findIssue (id) {
157
    return this.issues.filter(issue => issue.id === id)[0];
158 159
  }

Phil Hughes's avatar
Phil Hughes committed
160
  removeIssue (removeIssue) {
Phil Hughes's avatar
Phil Hughes committed
161
    this.issues = this.issues.filter((issue) => {
162 163 164
      const matchesRemove = removeIssue.id === issue.id;

      if (matchesRemove) {
165
        this.issuesSize -= 1;
Phil Hughes's avatar
Phil Hughes committed
166
        issue.removeLabel(this.label);
167 168
      }

Phil Hughes's avatar
Phil Hughes committed
169
      return !matchesRemove;
170 171 172
    });
  }
}
173 174

window.List = List;