BigW Consortium Gitlab

list.js 4.84 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 committed
11
    this._uid = this.guid();
12
    this.position = obj.position;
13
    this.title = obj.title;
14
    this.type = obj.list_type;
15 16 17
    this.preset = ['backlog', 'closed', 'blank'].indexOf(this.type) > -1;
    this.isExpandable = ['backlog', 'closed'].indexOf(this.type) > -1;
    this.isExpanded = true;
18
    this.page = 1;
19
    this.loading = true;
20
    this.loadingMore = false;
21
    this.issues = [];
22
    this.issuesSize = 0;
23
    this.defaultAvatar = defaultAvatar;
24 25

    if (obj.label) {
26
      this.label = new ListLabel(obj.label);
27 28
    }

29
    if (this.type !== 'blank' && this.id) {
30 31 32
      this.getIssues().catch(() => {
        // TODO: handle request error
      });
33 34 35
    }
  }

Phil Hughes committed
36
  guid() {
37
    const s4 = () => Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
Phil Hughes committed
38 39 40
    return `${s4()}${s4()}-${s4()}-${s4()}-${s4()}-${s4()}${s4()}${s4()}`;
  }

41
  save () {
42
    return gl.boardService.createList(this.label.id)
Filipa Lacerda committed
43 44
      .then(resp => resp.json())
      .then((data) => {
45 46 47
        this.id = data.id;
        this.type = data.list_type;
        this.position = data.position;
48

49
        return this.getIssues();
50 51 52
      });
  }

53
  destroy () {
Fatih Acet committed
54 55
    const index = gl.issueBoards.BoardsStore.state.lists.indexOf(this);
    gl.issueBoards.BoardsStore.state.lists.splice(index, 1);
56
    gl.issueBoards.BoardsStore.updateNewListDropdown(this.id);
57

58 59 60 61
    gl.boardService.destroyList(this.id)
      .catch(() => {
        // TODO: handle request error
      });
62 63 64
  }

  update () {
65 66 67 68
    gl.boardService.updateList(this.id, this.position)
      .catch(() => {
        // TODO: handle request error
      });
69 70
  }

71
  nextPage () {
72
    if (this.issuesSize > this.issues.length) {
73
      if (this.issues.length / PER_PAGE >= 1) {
74 75
        this.page += 1;
      }
76 77 78 79 80 81

      return this.getIssues(false);
    }
  }

  getIssues (emptyIssues = true) {
82
    const data = queryData(gl.issueBoards.BoardsStore.filter.path, { page: this.page });
Phil Hughes committed
83 84

    if (this.label && data.label_name) {
85
      data.label_name = data.label_name.filter(label => label !== this.label.title);
86
    }
87

88 89 90 91 92
    if (emptyIssues) {
      this.loading = true;
    }

    return gl.boardService.getIssuesForList(this.id, data)
Filipa Lacerda committed
93 94
      .then(resp => resp.json())
      .then((data) => {
95
        this.loading = false;
96
        this.issuesSize = data.size;
97

98 99 100 101
        if (emptyIssues) {
          this.issues = [];
        }

102
        this.createIssues(data.issues);
103 104 105
      });
  }

106
  newIssue (issue) {
107
    this.addIssue(issue, null, 0);
108
    this.issuesSize += 1;
109

110
    return gl.boardService.newIssue(this.id, issue)
Filipa Lacerda committed
111 112
      .then(resp => resp.json())
      .then((data) => {
113
        issue.id = data.iid;
114

115 116 117 118
        if (this.issuesSize > 1) {
          const moveBeforeIid = this.issues[1].id;
          gl.boardService.moveIssue(issue.id, null, null, null, moveBeforeIid);
        }
119 120 121
      });
  }

122
  createIssues (data) {
123
    data.forEach((issueObj) => {
124
      this.addIssue(new ListIssue(issueObj, this.defaultAvatar));
125
    });
126 127
  }

128
  addIssue (issue, listFrom, newIndex) {
129 130
    let moveBeforeIid = null;
    let moveAfterIid = null;
131

132
    if (!this.findIssue(issue.id)) {
133
      if (newIndex !== undefined) {
134
        this.issues.splice(newIndex, 0, issue);
135

136 137 138 139 140
        if (this.issues[newIndex - 1]) {
          moveBeforeIid = this.issues[newIndex - 1].id;
        }

        if (this.issues[newIndex + 1]) {
Phil Hughes committed
141
          moveAfterIid = this.issues[newIndex + 1].id;
142
        }
143 144 145
      } else {
        this.issues.push(issue);
      }
146

147 148 149
      if (this.label) {
        issue.addLabel(this.label);
      }
150

151
      if (listFrom) {
152
        this.issuesSize += 1;
Phil Hughes committed
153 154

        this.updateIssueLabel(issue, listFrom, moveBeforeIid, moveAfterIid);
155
      }
156
    }
157 158
  }

159 160 161 162
  moveIssue (issue, oldIndex, newIndex, moveBeforeIid, moveAfterIid) {
    this.issues.splice(oldIndex, 1);
    this.issues.splice(newIndex, 0, issue);

163 164 165 166
    gl.boardService.moveIssue(issue.id, null, null, moveBeforeIid, moveAfterIid)
      .catch(() => {
        // TODO: handle request error
      });
Phil Hughes committed
167 168 169
  }

  updateIssueLabel(issue, listFrom, moveBeforeIid, moveAfterIid) {
170 171 172 173
    gl.boardService.moveIssue(issue.id, listFrom.id, this.id, moveBeforeIid, moveAfterIid)
      .catch(() => {
        // TODO: handle request error
      });
174 175
  }

176
  findIssue (id) {
177
    return this.issues.filter(issue => issue.id === id)[0];
178 179
  }

180
  removeIssue (removeIssue) {
181
    this.issues = this.issues.filter((issue) => {
182 183 184
      const matchesRemove = removeIssue.id === issue.id;

      if (matchesRemove) {
185
        this.issuesSize -= 1;
186
        issue.removeLabel(this.label);
187 188
      }

189
      return !matchesRemove;
190 191 192
    });
  }
}
193 194

window.List = List;