BigW Consortium Gitlab

list.js.es6 3.55 KB
Newer Older
1 2 3 4
/* eslint-disable space-before-function-paren, no-underscore-dangle, class-methods-use-this, consistent-return, no-plusplus, prefer-const, space-in-parens, no-shadow, no-param-reassign, max-len, no-unused-vars */
/* global ListIssue */
/* global ListLabel */

5 6 7
class List {
  constructor (obj) {
    this.id = obj.id;
Phil Hughes committed
8
    this._uid = this.guid();
9
    this.position = obj.position;
10
    this.title = obj.title;
11
    this.type = obj.list_type;
12
    this.preset = ['backlog', 'done', 'blank'].indexOf(this.type) > -1;
13
    this.filters = gl.issueBoards.BoardsStore.state.filters;
14
    this.page = 1;
15
    this.loading = true;
16
    this.loadingMore = false;
17
    this.issues = [];
18
    this.issuesSize = 0;
19 20

    if (obj.label) {
21
      this.label = new ListLabel(obj.label);
22 23
    }

24
    if (this.type !== 'blank' && this.id) {
25
      this.getIssues();
26 27 28
    }
  }

Phil Hughes committed
29
  guid() {
30
    const s4 = () => Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
Phil Hughes committed
31 32 33
    return `${s4()}${s4()}-${s4()}-${s4()}-${s4()}-${s4()}${s4()}${s4()}`;
  }

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

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

43
        return this.getIssues();
44 45 46
      });
  }

47
  destroy () {
Fatih Acet committed
48 49
    const index = gl.issueBoards.BoardsStore.state.lists.indexOf(this);
    gl.issueBoards.BoardsStore.state.lists.splice(index, 1);
50
    gl.issueBoards.BoardsStore.updateNewListDropdown(this.id);
51

52
    gl.boardService.destroyList(this.id);
53 54 55
  }

  update () {
Phil Hughes committed
56
    gl.boardService.updateList(this.id, this.position);
57 58
  }

59
  nextPage () {
60
    if (this.issuesSize > this.issues.length) {
61 62 63 64 65 66 67
      this.page++;

      return this.getIssues(false);
    }
  }

  getIssues (emptyIssues = true) {
68 69 70 71
    const filters = this.filters;
    let data = { page: this.page };

    Object.keys(filters).forEach((key) => { data[key] = filters[key]; });
72 73

    if (this.label) {
74
      data.label_name = data.label_name.filter( label => label !== this.label.title );
75
    }
76

77 78 79 80 81
    if (emptyIssues) {
      this.loading = true;
    }

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

87 88 89 90
        if (emptyIssues) {
          this.issues = [];
        }

91
        this.createIssues(data.issues);
92 93 94
      });
  }

95 96 97 98
  newIssue (issue) {
    this.addIssue(issue);
    this.issuesSize++;

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

106
  createIssues (data) {
107
    data.forEach((issueObj) => {
108
      this.addIssue(new ListIssue(issueObj));
109
    });
110 111
  }

112
  addIssue (issue, listFrom, newIndex) {
113
    if (!this.findIssue(issue.id)) {
114 115 116 117 118
      if (newIndex !== undefined) {
        this.issues.splice(newIndex, 0, issue);
      } else {
        this.issues.push(issue);
      }
119

120 121 122
      if (this.label) {
        issue.addLabel(this.label);
      }
123

124 125 126 127 128 129 130
      if (listFrom) {
        this.issuesSize++;
        gl.boardService.moveIssue(issue.id, listFrom.id, this.id)
          .then(() => {
            listFrom.getIssues(false);
          });
      }
131
    }
132 133 134
  }

  findIssue (id) {
135
    return this.issues.filter( issue => issue.id === id )[0];
136 137
  }

138
  removeIssue (removeIssue) {
139
    this.issues = this.issues.filter((issue) => {
140 141 142
      const matchesRemove = removeIssue.id === issue.id;

      if (matchesRemove) {
143
        this.issuesSize--;
144
        issue.removeLabel(this.label);
145 146
      }

147
      return !matchesRemove;
148 149 150
    });
  }
}