gitlab-org--gitlab-foss/app/assets/javascripts/boards/models/list.js

188 lines
4.6 KiB
JavaScript
Raw Normal View History

/* eslint-disable space-before-function-paren, no-underscore-dangle, class-methods-use-this, consistent-return, no-shadow, no-param-reassign, max-len, no-unused-vars */
/* global ListIssue */
/* global ListLabel */
2016-08-01 09:18:30 -04:00
class List {
constructor (obj) {
this.id = obj.id;
2016-08-16 07:57:59 -04:00
this._uid = this.guid();
this.position = obj.position;
2016-08-01 09:18:30 -04:00
this.title = obj.title;
this.type = obj.list_type;
2017-01-27 12:13:37 -05:00
this.preset = ['done', 'blank'].indexOf(this.type) > -1;
this.filterPath = gl.issueBoards.BoardsStore.filter.path;
this.page = 1;
this.loading = true;
this.loadingMore = false;
2016-08-04 12:24:53 -04:00
this.issues = [];
this.issuesSize = 0;
2016-08-01 09:18:30 -04:00
if (obj.label) {
2016-08-10 13:29:55 -04:00
this.label = new ListLabel(obj.label);
2016-08-01 09:18:30 -04:00
}
if (this.type !== 'blank' && this.id) {
2016-08-05 09:24:27 -04:00
this.getIssues();
2016-08-01 09:18:30 -04:00
}
}
2016-08-16 07:57:59 -04:00
guid() {
2016-08-17 07:02:24 -04:00
const s4 = () => Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
2016-08-16 07:57:59 -04:00
return `${s4()}${s4()}-${s4()}-${s4()}-${s4()}-${s4()}${s4()}${s4()}`;
}
save () {
2016-08-08 06:18:38 -04:00
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;
2016-08-08 06:18:38 -04:00
return this.getIssues();
});
}
destroy () {
2016-11-02 20:37:33 -04:00
const index = gl.issueBoards.BoardsStore.state.lists.indexOf(this);
gl.issueBoards.BoardsStore.state.lists.splice(index, 1);
gl.issueBoards.BoardsStore.updateNewListDropdown(this.id);
gl.boardService.destroyList(this.id);
}
update () {
2016-08-16 12:32:38 -04:00
gl.boardService.updateList(this.id, this.position);
}
nextPage () {
if (this.issuesSize > this.issues.length) {
this.page += 1;
return this.getIssues(false);
}
}
getIssues (emptyIssues = true) {
const data = { page: this.page };
gl.issueBoards.BoardsStore.filter.path.split('&').forEach((filterParam) => {
const paramSplit = filterParam.split('=');
const paramKeyNormalized = paramSplit[0].replace('[]', '');
const isArray = paramSplit[0].indexOf('[]');
if (isArray >= 0) {
if (!data[paramKeyNormalized]) {
data[paramKeyNormalized] = [];
}
data[paramKeyNormalized].push(paramSplit[1]);
} else {
data[paramKeyNormalized] = paramSplit[1];
}
});
if (this.label && data.label_name) {
data.label_name = data.label_name.filter(label => label !== this.label.title);
2016-08-05 13:27:12 -04:00
}
if (emptyIssues) {
this.loading = true;
}
return gl.boardService.getIssuesForList(this.id, data)
2016-08-05 09:24:27 -04:00
.then((resp) => {
const data = resp.json();
this.loading = false;
this.issuesSize = data.size;
2016-08-05 09:24:27 -04:00
if (emptyIssues) {
this.issues = [];
}
this.createIssues(data.issues);
2016-08-05 09:24:27 -04:00
});
}
2016-10-03 09:29:21 -04:00
newIssue (issue) {
this.addIssue(issue);
this.issuesSize += 1;
2016-10-03 09:29:21 -04:00
return gl.boardService.newIssue(this.id, issue)
2016-10-03 09:29:21 -04:00
.then((resp) => {
const data = resp.json();
issue.id = data.iid;
});
}
2016-08-05 09:24:27 -04:00
createIssues (data) {
2016-08-17 07:02:24 -04:00
data.forEach((issueObj) => {
this.addIssue(new ListIssue(issueObj));
2016-08-17 07:02:24 -04:00
});
2016-08-05 09:24:27 -04:00
}
addIssue (issue, listFrom, newIndex) {
let moveBeforeIid = null;
let moveAfterIid = null;
2017-02-01 13:41:01 -05:00
if (!this.findIssue(issue.id)) {
if (newIndex !== undefined) {
this.issues.splice(newIndex, 0, issue);
2017-02-01 13:41:01 -05:00
if (this.issues[newIndex - 1]) {
moveBeforeIid = this.issues[newIndex - 1].id;
}
if (this.issues[newIndex + 1]) {
2017-02-13 09:46:28 -05:00
moveAfterIid = this.issues[newIndex + 1].id;
}
} else {
this.issues.push(issue);
}
2016-08-01 09:18:30 -04:00
if (this.label) {
issue.addLabel(this.label);
}
if (listFrom) {
this.issuesSize += 1;
2017-02-27 07:36:43 -05:00
this.updateIssueLabel(issue, listFrom, moveBeforeIid, moveAfterIid);
}
}
2016-08-01 09:18:30 -04:00
}
2017-02-13 07:15:20 -05:00
moveIssue (issue, oldIndex, newIndex, moveBeforeIid, moveAfterIid) {
this.issues.splice(oldIndex, 1);
this.issues.splice(newIndex, 0, issue);
2017-02-14 07:25:45 -05:00
gl.boardService.moveIssue(issue.id, null, null, moveBeforeIid, moveAfterIid);
2017-02-27 07:36:43 -05:00
}
updateIssueLabel(issue, listFrom, moveBeforeIid, moveAfterIid) {
gl.boardService.moveIssue(issue.id, listFrom.id, this.id, moveBeforeIid, moveAfterIid)
.then(() => {
listFrom.getIssues(false);
});
2017-02-01 13:41:01 -05:00
}
2016-08-01 09:18:30 -04:00
findIssue (id) {
return this.issues.filter(issue => issue.id === id)[0];
2016-08-01 09:18:30 -04:00
}
2016-08-05 13:27:12 -04:00
removeIssue (removeIssue) {
this.issues = this.issues.filter((issue) => {
2016-08-01 09:18:30 -04:00
const matchesRemove = removeIssue.id === issue.id;
if (matchesRemove) {
this.issuesSize -= 1;
2016-08-05 13:27:12 -04:00
issue.removeLabel(this.label);
2016-08-01 09:18:30 -04:00
}
return !matchesRemove;
2016-08-01 09:18:30 -04:00
});
}
}
window.List = List;