debian-mirror-gitlab/app/assets/javascripts/boards/models/list.js.es6

142 lines
3.1 KiB
JavaScript
Raw Normal View History

2016-09-13 17:45:13 +05:30
class List {
constructor (obj) {
this.id = obj.id;
this._uid = this.guid();
this.position = obj.position;
this.title = obj.title;
this.type = obj.list_type;
this.preset = ['backlog', 'done', 'blank'].indexOf(this.type) > -1;
this.filters = gl.issueBoards.BoardsStore.state.filters;
this.page = 1;
this.loading = true;
this.loadingMore = false;
this.issues = [];
2016-09-29 09:46:39 +05:30
this.issuesSize = 0;
2016-09-13 17:45:13 +05:30
if (obj.label) {
this.label = new ListLabel(obj.label);
}
if (this.type !== 'blank' && this.id) {
this.getIssues();
}
}
guid() {
const s4 = () => Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
return `${s4()}${s4()}-${s4()}-${s4()}-${s4()}-${s4()}${s4()}${s4()}`;
}
save () {
return gl.boardService.createList(this.label.id)
.then((resp) => {
const data = resp.json();
this.id = data.id;
this.type = data.list_type;
this.position = data.position;
return this.getIssues();
});
}
destroy () {
gl.issueBoards.BoardsStore.state.lists.$remove(this);
gl.issueBoards.BoardsStore.updateNewListDropdown(this.id);
gl.boardService.destroyList(this.id);
}
update () {
gl.boardService.updateList(this.id, this.position);
}
nextPage () {
2016-09-29 09:46:39 +05:30
if (this.issuesSize > this.issues.length) {
2016-09-13 17:45:13 +05:30
this.page++;
return this.getIssues(false);
}
}
getIssues (emptyIssues = true) {
const filters = this.filters;
let data = { page: this.page };
Object.keys(filters).forEach((key) => { data[key] = filters[key]; });
if (this.label) {
data.label_name = data.label_name.filter( label => label !== this.label.title );
}
if (emptyIssues) {
this.loading = true;
}
return gl.boardService.getIssuesForList(this.id, data)
.then((resp) => {
const data = resp.json();
this.loading = false;
2016-09-29 09:46:39 +05:30
this.issuesSize = data.size;
2016-09-13 17:45:13 +05:30
if (emptyIssues) {
this.issues = [];
}
2016-09-29 09:46:39 +05:30
this.createIssues(data.issues);
2016-09-13 17:45:13 +05:30
});
}
2016-11-03 12:29:30 +05:30
newIssue (issue) {
this.addIssue(issue);
this.issuesSize++;
return gl.boardService.newIssue(this.id, issue)
.then((resp) => {
const data = resp.json();
issue.id = data.iid;
});
}
2016-09-13 17:45:13 +05:30
createIssues (data) {
data.forEach((issueObj) => {
this.addIssue(new ListIssue(issueObj));
});
}
addIssue (issue, listFrom) {
2016-09-29 09:46:39 +05:30
if (!this.findIssue(issue.id)) {
this.issues.push(issue);
2016-09-13 17:45:13 +05:30
2016-09-29 09:46:39 +05:30
if (this.label) {
issue.addLabel(this.label);
}
2016-09-13 17:45:13 +05:30
2016-09-29 09:46:39 +05:30
if (listFrom) {
this.issuesSize++;
gl.boardService.moveIssue(issue.id, listFrom.id, this.id)
.then(() => {
listFrom.getIssues(false);
});
}
2016-09-13 17:45:13 +05:30
}
}
findIssue (id) {
return this.issues.filter( issue => issue.id === id )[0];
}
removeIssue (removeIssue) {
this.issues = this.issues.filter((issue) => {
const matchesRemove = removeIssue.id === issue.id;
if (matchesRemove) {
2016-09-29 09:46:39 +05:30
this.issuesSize--;
2016-09-13 17:45:13 +05:30
issue.removeLabel(this.label);
}
return !matchesRemove;
});
}
}