2017-01-12 00:55:11 -05:00
|
|
|
/* 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 */
|
2016-12-13 22:01:05 -05:00
|
|
|
/* global ListIssue */
|
|
|
|
/* global ListLabel */
|
2017-03-15 06:52:49 -04:00
|
|
|
import queryData from '../utils/query_data';
|
2016-12-13 22:01:05 -05:00
|
|
|
|
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();
|
2016-08-04 11:16:50 -04:00
|
|
|
this.position = obj.position;
|
2016-08-01 09:18:30 -04:00
|
|
|
this.title = obj.title;
|
2016-08-04 11:16:50 -04:00
|
|
|
this.type = obj.list_type;
|
2017-03-24 08:40:35 -04:00
|
|
|
this.preset = ['closed', 'blank'].indexOf(this.type) > -1;
|
2016-08-05 09:58:42 -04:00
|
|
|
this.page = 1;
|
2016-08-05 09:36:12 -04:00
|
|
|
this.loading = true;
|
2016-08-12 11:58:34 -04:00
|
|
|
this.loadingMore = false;
|
2016-08-04 12:24:53 -04:00
|
|
|
this.issues = [];
|
2016-08-19 12:19:20 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-08-05 09:36:12 -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()}`;
|
|
|
|
}
|
|
|
|
|
2016-08-05 08:40:35 -04:00
|
|
|
save () {
|
2016-08-08 06:18:38 -04:00
|
|
|
return gl.boardService.createList(this.label.id)
|
2016-08-05 08:40:35 -04:00
|
|
|
.then((resp) => {
|
|
|
|
const data = resp.json();
|
|
|
|
|
|
|
|
this.id = data.id;
|
|
|
|
this.type = data.list_type;
|
|
|
|
this.position = data.position;
|
2016-08-05 09:36:12 -04:00
|
|
|
|
2016-08-08 06:18:38 -04:00
|
|
|
return this.getIssues();
|
2016-08-05 08:40:35 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-08-04 11:16:50 -04:00
|
|
|
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);
|
2016-08-17 09:11:09 -04:00
|
|
|
gl.issueBoards.BoardsStore.updateNewListDropdown(this.id);
|
2016-08-08 09:30:38 -04:00
|
|
|
|
2016-08-17 09:11:09 -04:00
|
|
|
gl.boardService.destroyList(this.id);
|
2016-08-04 11:16:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
update () {
|
2016-08-16 12:32:38 -04:00
|
|
|
gl.boardService.updateList(this.id, this.position);
|
2016-08-04 11:16:50 -04:00
|
|
|
}
|
|
|
|
|
2016-08-05 09:58:42 -04:00
|
|
|
nextPage () {
|
2016-09-01 05:48:34 -04:00
|
|
|
if (this.issuesSize > this.issues.length) {
|
2017-01-10 17:35:09 -05:00
|
|
|
this.page += 1;
|
2016-08-05 09:58:42 -04:00
|
|
|
|
|
|
|
return this.getIssues(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getIssues (emptyIssues = true) {
|
2017-03-15 06:52:49 -04:00
|
|
|
const data = queryData(gl.issueBoards.BoardsStore.filter.path, { page: this.page });
|
2017-03-07 06:05:37 -05:00
|
|
|
|
|
|
|
if (this.label && data.label_name) {
|
2017-01-12 00:55:11 -05:00
|
|
|
data.label_name = data.label_name.filter(label => label !== this.label.title);
|
2016-08-05 13:27:12 -04:00
|
|
|
}
|
2016-08-05 09:36:12 -04:00
|
|
|
|
2016-08-05 09:58:42 -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;
|
2016-08-19 12:19:20 -04:00
|
|
|
this.issuesSize = data.size;
|
2016-08-05 09:24:27 -04:00
|
|
|
|
2016-08-05 09:58:42 -04:00
|
|
|
if (emptyIssues) {
|
|
|
|
this.issues = [];
|
|
|
|
}
|
|
|
|
|
2016-08-19 12:19:20 -04:00
|
|
|
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);
|
2017-01-10 17:35:09 -05:00
|
|
|
this.issuesSize += 1;
|
2016-10-03 09:29:21 -04:00
|
|
|
|
2016-10-05 09:27:29 -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) => {
|
2016-08-17 09:11:09 -04:00
|
|
|
this.addIssue(new ListIssue(issueObj));
|
2016-08-17 07:02:24 -04:00
|
|
|
});
|
2016-08-05 09:24:27 -04:00
|
|
|
}
|
|
|
|
|
2016-11-24 05:32:55 -05:00
|
|
|
addIssue (issue, listFrom, newIndex) {
|
2017-02-13 04:42:09 -05:00
|
|
|
let moveBeforeIid = null;
|
|
|
|
let moveAfterIid = null;
|
2017-02-01 13:41:01 -05:00
|
|
|
|
2017-02-07 03:56:33 -05:00
|
|
|
if (!this.findIssue(issue.id)) {
|
2017-02-13 04:42:09 -05:00
|
|
|
if (newIndex !== undefined) {
|
2016-11-24 05:32:55 -05:00
|
|
|
this.issues.splice(newIndex, 0, issue);
|
2017-02-01 13:41:01 -05:00
|
|
|
|
2017-02-13 04:42:09 -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;
|
2017-02-13 04:42:09 -05:00
|
|
|
}
|
2016-11-24 05:32:55 -05:00
|
|
|
} else {
|
|
|
|
this.issues.push(issue);
|
|
|
|
}
|
2016-08-01 09:18:30 -04:00
|
|
|
|
2016-09-01 05:48:34 -04:00
|
|
|
if (this.label) {
|
|
|
|
issue.addLabel(this.label);
|
|
|
|
}
|
2016-08-05 08:40:35 -04:00
|
|
|
|
2016-09-01 05:48:34 -04:00
|
|
|
if (listFrom) {
|
2017-01-10 17:35:09 -05:00
|
|
|
this.issuesSize += 1;
|
2017-02-27 07:36:43 -05:00
|
|
|
|
|
|
|
this.updateIssueLabel(issue, listFrom, moveBeforeIid, moveAfterIid);
|
2016-09-01 05:48:34 -04:00
|
|
|
}
|
2016-08-17 09:11:09 -04:00
|
|
|
}
|
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)
|
2017-02-21 11:00:33 -05:00
|
|
|
.then(() => {
|
|
|
|
listFrom.getIssues(false);
|
|
|
|
});
|
2017-02-01 13:41:01 -05:00
|
|
|
}
|
|
|
|
|
2016-08-01 09:18:30 -04:00
|
|
|
findIssue (id) {
|
2017-01-12 00:55:11 -05:00
|
|
|
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) {
|
2016-08-11 04:51:52 -04:00
|
|
|
this.issues = this.issues.filter((issue) => {
|
2016-08-01 09:18:30 -04:00
|
|
|
const matchesRemove = removeIssue.id === issue.id;
|
|
|
|
|
|
|
|
if (matchesRemove) {
|
2017-01-10 17:35:09 -05:00
|
|
|
this.issuesSize -= 1;
|
2016-08-05 13:27:12 -04:00
|
|
|
issue.removeLabel(this.label);
|
2016-08-01 09:18:30 -04:00
|
|
|
}
|
|
|
|
|
2016-08-11 04:51:52 -04:00
|
|
|
return !matchesRemove;
|
2016-08-01 09:18:30 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2016-12-14 01:51:53 -05:00
|
|
|
|
|
|
|
window.List = List;
|