list.js.es6 2.71 KB
Newer Older
1 2 3
class List {
  constructor (obj) {
    this.id = obj.id;
Phil Hughes's avatar
Phil Hughes committed
4
    this._uid = this.guid();
5
    this.position = obj.position;
6
    this.title = obj.title;
7
    this.type = obj.list_type;
Phil Hughes's avatar
Phil Hughes committed
8
    this.preset = ['backlog', 'done', 'blank'].indexOf(this.type) > -1;
9
    this.filters = gl.issueBoards.BoardsStore.state.filters;
10
    this.page = 1;
11
    this.loading = true;
12
    this.loadingMore = false;
Phil Hughes's avatar
Phil Hughes committed
13
    this.issues = [];
14 15

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

19
    if (this.type !== 'blank' && this.id) {
20
      this.getIssues();
21 22 23
    }
  }

Phil Hughes's avatar
Phil Hughes committed
24
  guid() {
Phil Hughes's avatar
Phil Hughes committed
25
    const s4 = () => Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
Phil Hughes's avatar
Phil Hughes committed
26 27 28
    return `${s4()}${s4()}-${s4()}-${s4()}-${s4()}-${s4()}${s4()}${s4()}`;
  }

29
  save () {
30
    return gl.boardService.createList(this.label.id)
31 32 33 34 35 36
      .then((resp) => {
        const data = resp.json();

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

38
        return this.getIssues();
39 40 41
      });
  }

42
  destroy () {
43 44
    gl.issueBoards.BoardsStore.state.lists.$remove(this);
    gl.issueBoards.BoardsStore.updateNewListDropdown(this.id);
Phil Hughes's avatar
Phil Hughes committed
45

46
    gl.boardService.destroyList(this.id);
47 48 49
  }

  update () {
Phil Hughes's avatar
Phil Hughes committed
50
    gl.boardService.updateList(this.id, this.position);
51 52
  }

53
  nextPage () {
54
    if (Math.floor(this.issues.length / 20) === this.page) {
55 56 57 58 59 60
      this.page++;

      return this.getIssues(false);
    }
  }

61 62 63 64
  canSearch () {
    return this.type === 'backlog';
  }

65
  getIssues (emptyIssues = true) {
Phil Hughes's avatar
Phil Hughes committed
66 67 68 69
    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
70 71

    if (this.label) {
Phil Hughes's avatar
Phil Hughes committed
72
      data.label_name = data.label_name.filter( label => label !== this.label.title );
Phil Hughes's avatar
Phil Hughes committed
73
    }
74

75 76 77 78 79
    if (emptyIssues) {
      this.loading = true;
    }

    return gl.boardService.getIssuesForList(this.id, data)
80 81 82 83
      .then((resp) => {
        const data = resp.json();
        this.loading = false;

84 85 86 87
        if (emptyIssues) {
          this.issues = [];
        }

88 89 90 91 92
        this.createIssues(data);
      });
  }

  createIssues (data) {
Phil Hughes's avatar
Phil Hughes committed
93
    data.forEach((issueObj) => {
94
      this.addIssue(new ListIssue(issueObj));
Phil Hughes's avatar
Phil Hughes committed
95
    });
96 97
  }

98 99
  addIssue (issue, listFrom) {
    this.issues.push(issue);
100

101 102 103
    if (this.label) {
      issue.addLabel(this.label);
    }
104

105 106 107
    if (listFrom) {
      gl.boardService.moveIssue(issue.id, listFrom.id, this.id);
    }
108 109 110
  }

  findIssue (id) {
Phil Hughes's avatar
Phil Hughes committed
111
    return this.issues.filter( issue => issue.id === id )[0];
112 113
  }

Phil Hughes's avatar
Phil Hughes committed
114
  removeIssue (removeIssue) {
Phil Hughes's avatar
Phil Hughes committed
115
    this.issues = this.issues.filter((issue) => {
116 117 118
      const matchesRemove = removeIssue.id === issue.id;

      if (matchesRemove) {
Phil Hughes's avatar
Phil Hughes committed
119
        issue.removeLabel(this.label);
120 121
      }

Phil Hughes's avatar
Phil Hughes committed
122
      return !matchesRemove;
123 124 125
    });
  }
}