list.js.es6 2.5 KB
Newer Older
Phil Hughes's avatar
Phil Hughes committed
1 2 3
class List {
  constructor (obj) {
    this.id = obj.id;
4
    this.position = obj.position;
Phil Hughes's avatar
Phil Hughes committed
5
    this.title = obj.title;
6
    this.type = obj.list_type;
7
    this.filters = gl.issueBoards.BoardsStore.state.filters;
8
    this.page = 1;
9
    this.loading = true;
10
    this.loadingMore = false;
Phil Hughes's avatar
Phil Hughes committed
11
    this.issues = [];
Phil Hughes's avatar
Phil Hughes committed
12 13

    if (obj.label) {
Phil Hughes's avatar
Phil Hughes committed
14
      this.label = new ListLabel(obj.label);
Phil Hughes's avatar
Phil Hughes committed
15 16
    }

17
    if (this.type !== 'blank' && this.id) {
18
      this.getIssues();
Phil Hughes's avatar
Phil Hughes committed
19 20 21
    }
  }

22
  save () {
23
    return gl.boardService.createList(this.label.id)
24 25 26 27 28 29
      .then((resp) => {
        const data = resp.json();

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

31
        return this.getIssues();
32 33 34
      });
  }

35
  destroy () {
36
    if (this.type !== 'blank') {
Phil Hughes's avatar
Phil Hughes committed
37
      gl.issueBoards.BoardsStore.state.lists.$remove(this);
38
      gl.issueBoards.BoardsStore.updateNewListDropdown(this.id);
Phil Hughes's avatar
Phil Hughes committed
39

40 41
      gl.boardService.destroyList(this.id);
    }
42 43 44
  }

  update () {
45
    gl.boardService.updateList(this);
46 47
  }

48 49 50 51 52 53 54 55
  nextPage () {
    if (this.issues.length / 20 === this.page) {
      this.page++;

      return this.getIssues(false);
    }
  }

56 57 58 59
  canSearch () {
    return this.type === 'backlog';
  }

60
  getIssues (emptyIssues = true) {
Phil Hughes's avatar
Phil Hughes committed
61 62 63 64
    const filters = this.filters;
    let data = { page: this.page };

    Object.keys(filters).forEach((key) => { data[key] = filters[key]; });
Phil Hughes's avatar
Phil Hughes committed
65 66

    if (this.label) {
Phil Hughes's avatar
Phil Hughes committed
67 68
      data.label_name = data.label_name.filter((label) => {
        return label !== this.label.title;
Phil Hughes's avatar
Phil Hughes committed
69 70
      });
    }
71

72 73 74 75 76
    if (emptyIssues) {
      this.loading = true;
    }

    return gl.boardService.getIssuesForList(this.id, data)
77 78 79 80
      .then((resp) => {
        const data = resp.json();
        this.loading = false;

81 82 83 84
        if (emptyIssues) {
          this.issues = [];
        }

85 86 87 88 89
        this.createIssues(data);
      });
  }

  createIssues (data) {
Phil Hughes's avatar
Phil Hughes committed
90 91
    for (let i = 0, dataLength = data.length; i < dataLength; i++) {
      const issueObj = data[i];
Phil Hughes's avatar
Phil Hughes committed
92
      this.issues.push(new ListIssue(issueObj));
Phil Hughes's avatar
Phil Hughes committed
93
    }
94 95
  }

96 97
  addIssue (issue, listFrom) {
    this.issues.push(issue);
Phil Hughes's avatar
Phil Hughes committed
98 99

    issue.addLabel(this.label);
100

101
    gl.boardService.moveIssue(issue.id, listFrom.id, this.id);
Phil Hughes's avatar
Phil Hughes committed
102 103 104
  }

  findIssue (id) {
Phil Hughes's avatar
Phil Hughes committed
105
    return this.issues.filter((issue) => {
Phil Hughes's avatar
Phil Hughes committed
106
      return issue.id === id;
Phil Hughes's avatar
Phil Hughes committed
107
    })[0];
Phil Hughes's avatar
Phil Hughes committed
108 109
  }

Phil Hughes's avatar
Phil Hughes committed
110
  removeIssue (removeIssue) {
Phil Hughes's avatar
Phil Hughes committed
111
    this.issues = this.issues.filter((issue) => {
Phil Hughes's avatar
Phil Hughes committed
112 113 114
      const matchesRemove = removeIssue.id === issue.id;

      if (matchesRemove) {
Phil Hughes's avatar
Phil Hughes committed
115
        issue.removeLabel(this.label);
Phil Hughes's avatar
Phil Hughes committed
116 117
      }

Phil Hughes's avatar
Phil Hughes committed
118
      return !matchesRemove;
Phil Hughes's avatar
Phil Hughes committed
119 120 121
    });
  }
}