Commit 5eb885e5 authored by Alfredo Sumaran's avatar Alfredo Sumaran

Build tree from server response

[ci skip]
parent 04917d05
...@@ -2,13 +2,13 @@ ...@@ -2,13 +2,13 @@
export default { export default {
props: { props: {
groups: { groups: {
type: Array, type: Object,
required: true, required: true,
}, },
}, },
computed: { computed: {
hasGroups() { hasGroups() {
return this.groups.length > 0; return Object.keys(this.groups).length > 0;
}, },
}, },
}; };
......
...@@ -29,7 +29,7 @@ export default { ...@@ -29,7 +29,7 @@ export default {
}; };
}, },
isExpandable() { isExpandable() {
return this.group.subGroups.length > 0; return Object.keys(this.group.subGroups).length > 0;
}, },
}, },
}; };
...@@ -92,7 +92,7 @@ export default { ...@@ -92,7 +92,7 @@ export default {
</div> </div>
<div class="title"> <div class="title">
<a :href="group.webUrl">{{group.fullName}}</a> <a :href="group.webUrl">{{group.isOrphan ? group.fullName : group.name}}</a>
</div> </div>
<div class="description"> <div class="description">
......
...@@ -7,7 +7,7 @@ export default { ...@@ -7,7 +7,7 @@ export default {
}, },
props: { props: {
groups: { groups: {
type: Array, type: Object,
required: true, required: true,
}, },
pageInfo: { pageInfo: {
......
...@@ -13,11 +13,9 @@ export default class GroupsService { ...@@ -13,11 +13,9 @@ export default class GroupsService {
if (parentId) { if (parentId) {
data.parent_id = parentId; data.parent_id = parentId;
} else {
// Do not send this param for sub groups // Do not send this param for sub groups
if (page) { } else if (page) {
data.page = page; data.page = page;
}
} }
return this.groups.get(data); return this.groups.get(data);
......
export default class GroupsStore { export default class GroupsStore {
constructor() { constructor() {
this.state = {}; this.state = {};
this.state.groups = []; this.state.groups = {};
this.state.pageInfo = {}; this.state.pageInfo = {};
return this; return this;
...@@ -11,9 +11,9 @@ export default class GroupsStore { ...@@ -11,9 +11,9 @@ export default class GroupsStore {
const parentGroup = parent; const parentGroup = parent;
if (parentGroup) { if (parentGroup) {
parentGroup.subGroups = this.decorateGroups(rawGroups); parentGroup.subGroups = this.buildTree(rawGroups);
} else { } else {
this.state.groups = this.decorateGroups(rawGroups); this.state.groups = this.buildTree(rawGroups);
} }
return rawGroups; return rawGroups;
...@@ -32,6 +32,70 @@ export default class GroupsStore { ...@@ -32,6 +32,70 @@ export default class GroupsStore {
this.state.pageInfo = paginationInfo; this.state.pageInfo = paginationInfo;
} }
buildTree(rawGroups) {
const groups = this.decorateGroups(rawGroups);
const tree = {};
const mappedGroups = {};
const orphans = [];
// Map groups to an object
for (let i = 0, len = groups.length; i < len; i += 1) {
const group = groups[i];
mappedGroups[group.id] = group;
mappedGroups[group.id].subGroups = {};
}
Object.keys(mappedGroups).map((key) => {
const currentGroup = mappedGroups[key];
// If the group is not at the root level, add it to its parent array of subGroups.
const parentGroup = mappedGroups[currentGroup.parentId];
if (currentGroup.parentId) {
if (parentGroup) {
mappedGroups[currentGroup.parentId].subGroups[currentGroup.id] = currentGroup;
mappedGroups[currentGroup.parentId].isOpen = true; // Expand group if it has subgroups
} else {
// Means the groups hast no direct parent.
// Save for later processing, we will add them to its corresponding base group
orphans.push(currentGroup);
}
} else {
// If the group is at the root level, add it to first level elements array.
tree[currentGroup.id] = currentGroup;
}
return key;
});
// Hopefully this array will be empty for most cases
if (orphans.length) {
orphans.map((orphan) => {
let found = false;
const currentOrphan = orphan;
Object.keys(tree).map((key) => {
const group = tree[key];
if (currentOrphan.fullPath.lastIndexOf(group.fullPath) === 0) {
group.subGroups[currentOrphan.id] = currentOrphan;
group.isOpen = true;
currentOrphan.isOrphan = true;
found = true;
}
return key;
});
if (!found) {
currentOrphan.isOrphan = true;
tree[currentOrphan.id] = currentOrphan;
}
return orphan;
});
}
return tree;
}
decorateGroups(rawGroups) { decorateGroups(rawGroups) {
this.groups = rawGroups.map(GroupsStore.decorateGroup); this.groups = rawGroups.map(GroupsStore.decorateGroup);
return this.groups; return this.groups;
...@@ -41,14 +105,17 @@ export default class GroupsStore { ...@@ -41,14 +105,17 @@ export default class GroupsStore {
return { return {
id: rawGroup.id, id: rawGroup.id,
fullName: rawGroup.full_name, fullName: rawGroup.full_name,
fullPath: rawGroup.full_path,
name: rawGroup.name,
description: rawGroup.description, description: rawGroup.description,
webUrl: rawGroup.web_url, webUrl: rawGroup.web_url,
parentId: rawGroup.parent_id, parentId: rawGroup.parent_id,
visibility: rawGroup.visibility, visibility: rawGroup.visibility,
isOpen: false, isOpen: false,
isOrphan: false,
numberProjects: 10, numberProjects: 10,
numberMembers: 10, numberMembers: 10,
subGroups: [], subGroups: {},
}; };
} }
......
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