Commit 0b351d55 authored by Phil Hughes's avatar Phil Hughes

Moved some code around to make it easier to read & work with

parent db2eabfd
...@@ -20,13 +20,11 @@ $(function () { ...@@ -20,13 +20,11 @@ $(function () {
const boards = resp.json(); const boards = resp.json();
boards.forEach((board) => { boards.forEach((board) => {
const list = new List(board); const list = BoardsStore.new(board, false);
if (list.type === 'done') { if (list.type === 'done') {
list.position = 9999999; list.position = 9999999;
} }
BoardsStore.state.lists.push(list);
}); });
BoardsStore.addBlankState(); BoardsStore.addBlankState();
......
...@@ -71,7 +71,7 @@ ...@@ -71,7 +71,7 @@
toListId = parseInt(toListId) || toListId; toListId = parseInt(toListId) || toListId;
const issueId = parseInt(e.item.getAttribute('data-issue')); const issueId = parseInt(e.item.getAttribute('data-issue'));
BoardsStore.moveCardToList(fromListId, toListId, issueId, e.newIndex); BoardsStore.moveCardToList(fromListId, toListId, issueId);
} }
}); });
......
...@@ -6,7 +6,7 @@ class Issue { ...@@ -6,7 +6,7 @@ class Issue {
if (obj.assignee) { if (obj.assignee) {
this.assignee = new User(obj.assignee); this.assignee = new User(obj.assignee);
} }
this.labels = []; this.labels = [];
obj.labels.forEach((label) => { obj.labels.forEach((label) => {
...@@ -37,4 +37,10 @@ class Issue { ...@@ -37,4 +37,10 @@ class Issue {
}); });
} }
} }
getLists () {
return _.filter(BoardsStore.state.lists, (list) => {
return list.findIssue(this.id);
});
}
} }
...@@ -24,6 +24,17 @@ class List { ...@@ -24,6 +24,17 @@ class List {
} }
} }
save () {
service.createList(this.label.id)
.then((resp) => {
const data = resp.json();
this.id = data.id;
this.type = data.list_type;
this.position = data.position;
});
}
destroy () { destroy () {
service.destroyList(this.id); service.destroyList(this.id);
} }
...@@ -36,10 +47,12 @@ class List { ...@@ -36,10 +47,12 @@ class List {
return this.type === 'backlog'; return this.type === 'backlog';
} }
addIssue (issue, index) { addIssue (issue, listFrom) {
this.issues.splice(index, 0, issue); this.issues.push(issue);
issue.addLabel(this.label); issue.addLabel(this.label);
service.moveIssue(issue.id, listFrom.id, this.id);
} }
findIssue (id) { findIssue (id) {
......
...@@ -6,30 +6,23 @@ ...@@ -6,30 +6,23 @@
author: {}, author: {},
assignee: {}, assignee: {},
milestone: {}, milestone: {},
label: []
} }
}, },
new: function (board) { new: function (board, persist = true) {
const doneList = this.getDoneList(), const doneList = this.getDoneList(),
list = new List(board); list = new List(board);
this.state.lists.push(list); this.state.lists.push(list);
if (list.type !== 'blank') { if (persist) {
service.createList(list.label.id) list.save();
.then(function (resp) {
const data = resp.json();
list.id = data.id;
list.type = data.list_type;
list.position = data.position;
});
this.removeBlankState(); this.removeBlankState();
this.addBlankState(); this.addBlankState();
} }
},
addBlankState: function () {
const doneList = this.getDoneList();
return list;
},
shouldAddBlankState: function () {
// Decide whether to add the blank state // Decide whether to add the blank state
let addBlankState = true; let addBlankState = true;
...@@ -39,6 +32,11 @@ ...@@ -39,6 +32,11 @@
return; return;
} }
}); });
return addBlankState;
},
addBlankState: function () {
const doneList = this.getDoneList(),
addBlankState = this.shouldAddBlankState();
if (addBlankState) { if (addBlankState) {
this.new({ this.new({
...@@ -46,21 +44,17 @@ ...@@ -46,21 +44,17 @@
list_type: 'blank', list_type: 'blank',
title: 'Welcome to your Issue Board!', title: 'Welcome to your Issue Board!',
position: 0 position: 0
}); }, false);
} }
}, },
removeBlankState: function () { removeBlankState: function () {
this.removeList('blank'); this.removeList('blank');
}, },
getDoneList: function () { getDoneList: function () {
return _.find(this.state.lists, (list) => { return this.findList('type', 'done');
return list.type === 'done';
});
}, },
removeList: function (id) { removeList: function (id) {
const list = _.find(this.state.lists, (list) => { const list = this.findList('id', id);
return list.id === id;
});
if (id !== 'blank') { if (id !== 'blank') {
list.destroy(); list.destroy();
...@@ -75,13 +69,8 @@ ...@@ -75,13 +69,8 @@
} }
}, },
moveList: function (oldIndex, newIndex) { moveList: function (oldIndex, newIndex) {
const listFrom = _.find(this.state.lists, (list) => { const listFrom = this.findList('position', oldIndex),
return list.position === oldIndex; istTo = this.findList('position', newIndex);
});
const listTo = _.find(this.state.lists, (list) => {
return list.position === newIndex;
});
listFrom.position = newIndex; listFrom.position = newIndex;
if (newIndex > listTo.position) { if (newIndex > listTo.position) {
...@@ -92,42 +81,31 @@ ...@@ -92,42 +81,31 @@
listFrom.update(); listFrom.update();
}, },
moveCardToList: function (listFromId, listToId, issueId, toIndex) { moveCardToList: function (listFromId, listToId, issueId) {
const listFrom = _.find(this.state.lists, (list) => { const listFrom = this.findList('id', listFromId),
return list.id === listFromId; listTo = this.findList('id', listToId),
}); issueTo = listTo.findIssue(issueId);
const listTo = _.find(this.state.lists, (list) => {
return list.id === listToId;
});
const issueTo = listTo.findIssue(issueId);
let issue = listFrom.findIssue(issueId); let issue = listFrom.findIssue(issueId);
const issueLists = this.getListsForIssue(issue); const issueLists = issue.getLists();
listFrom.removeIssue(issue); listFrom.removeIssue(issue);
// Add to new lists issues if it doesn't already exist // Add to new lists issues if it doesn't already exist
if (issueTo) { if (issueTo) {
issue = issueTo; listTo.removeIssue(issueTo);
issue.removeLabel(listFrom.label);
} else { } else {
listTo.addIssue(issue, toIndex); listTo.addIssue(issue, listFrom);
} }
if (listTo.id === 'done' && listFrom.id !== 'backlog') { if (listTo.id === 'done' && listFrom.id !== 'backlog') {
issueLists.forEach((list) => { issueLists.forEach((list) => {
issue.removeLabel(list.label);
list.removeIssue(issue); list.removeIssue(issue);
}); });
} }
service.moveIssue(issue.id, listFrom.id, listTo.id);
}, },
getListsForIssue: function (issue) { findList: function (key, val) {
return _.filter(this.state.lists, (list) => { return _.find(this.state.lists, (list) => {
return list.findIssue(issue.id); return list[key] === val;
}); });
},
clearDone: function () {
Vue.set(BoardsStore.state, 'done', {});
} }
}; };
}(window)); }(window));
...@@ -237,8 +237,3 @@ ...@@ -237,8 +237,3 @@
margin-right: 8px; margin-right: 8px;
font-weight: 500; font-weight: 500;
} }
.card-avatar {
margin-top: -2px;
border-radius: 50%;
}
...@@ -25,6 +25,8 @@ ...@@ -25,6 +25,8 @@
":issue-link-base" => "'#{namespace_project_issues_path(@project.namespace, @project)}'" } ":issue-link-base" => "'#{namespace_project_issues_path(@project.namespace, @project)}'" }
.board-list-loading.text-center{ "v-if" => "loading" } .board-list-loading.text-center{ "v-if" => "loading" }
= icon("spinner spin") = icon("spinner spin")
%ul.board-list{ "v-el:list" => true, ":data-board" => "boardId" } %ul.board-list{ "v-el:list" => true,
"v-show" => "!loading",
":data-board" => "boardId" }
= render "projects/boards/components/card" = render "projects/boards/components/card"
= render "projects/boards/components/blank_state" = render "projects/boards/components/blank_state"
...@@ -9,4 +9,4 @@ ...@@ -9,4 +9,4 @@
%span.label.color-label{ "v-for" => "label in issue.labels", ":style" => "{ backgroundColor: label.color, color: label.textColor }" } %span.label.color-label{ "v-for" => "label in issue.labels", ":style" => "{ backgroundColor: label.color, color: label.textColor }" }
{{ label.title }} {{ label.title }}
%a{ ":href" => "'/u/' + issue.assignee.username", ":title" => "issue.assignee.name", "v-if" => "issue.assignee" } %a{ ":href" => "'/u/' + issue.assignee.username", ":title" => "issue.assignee.name", "v-if" => "issue.assignee" }
%img.card-avatar{ ":src" => "issue.assignee.avatar", width: 20, height: 20 } %img.avatar.avatar-inline.s20{ ":src" => "issue.assignee.avatar", width: 20, height: 20 }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment