summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/boards/models/list.js
blob: 09f5d5b4dd8832227ae38264cce37c8dac69b260 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* eslint-disable class-methods-use-this */
import { __ } from '~/locale';
import ListLabel from './label';
import ListAssignee from './assignee';
import { deprecatedCreateFlash as flash } from '~/flash';
import boardsStore from '../stores/boards_store';
import ListMilestone from './milestone';
import 'ee_else_ce/boards/models/issue';

const TYPES = {
  backlog: {
    isPreset: true,
    isExpandable: true,
    isBlank: false,
  },
  closed: {
    isPreset: true,
    isExpandable: true,
    isBlank: false,
  },
  blank: {
    isPreset: true,
    isExpandable: false,
    isBlank: true,
  },
  default: {
    // includes label, assignee, and milestone lists
    isPreset: false,
    isExpandable: true,
    isBlank: false,
  },
};

class List {
  constructor(obj) {
    this.id = obj.id;
    this.position = obj.position;
    this.title = (obj.list_type || obj.listType) === 'backlog' ? __('Open') : obj.title;
    this.type = obj.list_type || obj.listType;

    const typeInfo = this.getTypeInfo(this.type);
    this.preset = Boolean(typeInfo.isPreset);
    this.isExpandable = Boolean(typeInfo.isExpandable);
    this.isExpanded = !obj.collapsed;
    this.page = 1;
    this.loading = true;
    this.loadingMore = false;
    this.issues = obj.issues || [];
    this.issuesSize = obj.issuesSize || obj.issuesCount || 0;
    this.maxIssueCount = obj.maxIssueCount || obj.max_issue_count || 0;

    if (obj.label) {
      this.label = new ListLabel(obj.label);
    } else if (obj.user || obj.assignee) {
      this.assignee = new ListAssignee(obj.user || obj.assignee);
      this.title = this.assignee.name;
    } else if (IS_EE && obj.milestone) {
      this.milestone = new ListMilestone(obj.milestone);
      this.title = this.milestone.title;
    }

    // doNotFetchIssues is a temporary workaround until issues are fetched using GraphQL on issue boards
    // Issue: https://gitlab.com/gitlab-org/gitlab/-/issues/229416
    if (!typeInfo.isBlank && this.id && !obj.doNotFetchIssues) {
      this.getIssues().catch(() => {
        // TODO: handle request error
      });
    }
  }

  guid() {
    const s4 = () =>
      Math.floor((1 + Math.random()) * 0x10000)
        .toString(16)
        .substring(1);
    return `${s4()}${s4()}-${s4()}-${s4()}-${s4()}-${s4()}${s4()}${s4()}`;
  }

  save() {
    return boardsStore.saveList(this);
  }

  destroy() {
    boardsStore.destroy(this);
  }

  update() {
    return boardsStore.updateListFunc(this);
  }

  nextPage() {
    return boardsStore.goToNextPage(this);
  }

  getIssues(emptyIssues = true) {
    return boardsStore.getListIssues(this, emptyIssues);
  }

  newIssue(issue) {
    return boardsStore.newListIssue(this, issue);
  }

  addMultipleIssues(issues, listFrom, newIndex) {
    boardsStore.addMultipleListIssues(this, issues, listFrom, newIndex);
  }

  addIssue(issue, listFrom, newIndex) {
    boardsStore.addListIssue(this, issue, listFrom, newIndex);
  }

  moveIssue(issue, oldIndex, newIndex, moveBeforeId, moveAfterId) {
    boardsStore.moveListIssues(this, issue, oldIndex, newIndex, moveBeforeId, moveAfterId);
  }

  moveMultipleIssues({ issues, oldIndicies, newIndex, moveBeforeId, moveAfterId }) {
    boardsStore
      .moveListMultipleIssues({
        list: this,
        issues,
        oldIndicies,
        newIndex,
        moveBeforeId,
        moveAfterId,
      })
      .catch(() => flash(__('Something went wrong while moving issues.')));
  }

  updateIssueLabel(issue, listFrom, moveBeforeId, moveAfterId) {
    boardsStore.moveIssue(issue.id, listFrom.id, this.id, moveBeforeId, moveAfterId).catch(() => {
      // TODO: handle request error
    });
  }

  updateMultipleIssues(issues, listFrom, moveBeforeId, moveAfterId) {
    boardsStore
      .moveMultipleIssues({
        ids: issues.map(issue => issue.id),
        fromListId: listFrom.id,
        toListId: this.id,
        moveBeforeId,
        moveAfterId,
      })
      .catch(() => flash(__('Something went wrong while moving issues.')));
  }

  findIssue(id) {
    return boardsStore.findListIssue(this, id);
  }

  removeMultipleIssues(removeIssues) {
    return boardsStore.removeListMultipleIssues(this, removeIssues);
  }

  removeIssue(removeIssue) {
    return boardsStore.removeListIssues(this, removeIssue);
  }

  getTypeInfo(type) {
    return TYPES[type] || TYPES.default;
  }

  onNewIssueResponse(issue, data) {
    boardsStore.onNewListIssueResponse(this, issue, data);
  }
}

window.List = List;

export default List;