BigW Consortium Gitlab

Commit 1604c764 by Alfredo Sumaran

Build tree object from endpoint response

parent fa6d7094
...@@ -30,8 +30,8 @@ export default class FilterableList { ...@@ -30,8 +30,8 @@ export default class FilterableList {
} }
filterResults(url, data) { filterResults(url, data) {
const endpoint = url || this.filterForm.getAttribute('action'); const endpoint = url || this.filterForm.getAttribute('action');
const additionalData = data || $(this.filterForm).serialize(); const additionalData = data || $(this.filterForm).serialize();
$(this.listHolderElement).fadeTo(250, 0.5); $(this.listHolderElement).fadeTo(250, 0.5);
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
export default { export default {
props: { props: {
groups: { groups: {
type: Array, type: Object,
required: true, required: true,
}, },
}, },
......
...@@ -10,10 +10,6 @@ export default { ...@@ -10,10 +10,6 @@ export default {
}, },
methods: { methods: {
toggleSubGroups() { toggleSubGroups() {
if (!this.group.hasSubgroups) {
return;
}
eventHub.$emit('toggleSubGroups', this.group); eventHub.$emit('toggleSubGroups', this.group);
}, },
}, },
...@@ -21,8 +17,11 @@ export default { ...@@ -21,8 +17,11 @@ export default {
</script> </script>
<template> <template>
<li @click="toggleSubGroups" class="list-group-item"> <li
<span v-show="group.hasSubgroups"> @click="toggleSubGroups" class="list-group-item"
:id="group.id"
>
<span v-show="group.expandable">
<i <i
v-show="group.isOpen" v-show="group.isOpen"
class="fa fa-caret-down" class="fa fa-caret-down"
...@@ -33,9 +32,9 @@ export default { ...@@ -33,9 +32,9 @@ export default {
aria-hidden="true"/> aria-hidden="true"/>
</span> </span>
<p><a :href="group.webUrl">{{group.fullName}}</a></p> <code>{{group.id}}</code> - <code v-show="group.isOrphan">Orphan</code> <a :href="group.webUrl">{{group.fullName}}</a></span>
<p>{{group.description}}</p> <span>{{group.description}}</span>
<group-folder v-if="group.subGroups && group.isOpen" :groups="group.subGroups" /> <group-folder v-if="group.isOpen" :groups="group.subGroups" />
</li> </li>
</template> </template>
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
export default { export default {
props: { props: {
groups: { groups: {
type: Array, type: Object,
required: true, required: true,
}, },
}, },
......
import FilterableList from '~/filterable_list'; import FilterableList from '~/filterable_list';
export default class GroupFilterableList extends FilterableList { export default class GroupFilterableList extends FilterableList {
constructor(form, filter, holder, store) { constructor(form, filter, holder, store) {
super(form, filter, holder); super(form, filter, holder);
......
...@@ -31,19 +31,24 @@ $(() => { ...@@ -31,19 +31,24 @@ $(() => {
}; };
}, },
methods: { methods: {
fetchGroups() { fetchGroups(parentGroup) {
service.getGroups() let parentId = null;
if (parentGroup) {
parentId = parentGroup.id;
}
service.getGroups(parentId)
.then((response) => { .then((response) => {
store.setGroups(response.json()); store.setGroups(response.json(), parentGroup);
}) })
.catch(() => { .catch(() => {
// TODO: Handler error // TODO: Handler error
}); });
}, },
toggleSubGroups(group) { toggleSubGroups(parentGroup = null) {
GroupsStore.toggleSubGroups(group); GroupsStore.toggleSubGroups(parentGroup);
this.fetchGroups(parentGroup);
this.fetchGroups();
}, },
}, },
created() { created() {
......
...@@ -8,7 +8,15 @@ export default class GroupsService { ...@@ -8,7 +8,15 @@ export default class GroupsService {
this.groups = Vue.resource(endpoint); this.groups = Vue.resource(endpoint);
} }
getGroups() { getGroups(parentId) {
return this.groups.get(); let data = {};
if (parentId) {
data = {
parent_id: parentId,
};
}
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 = {};
return this; return this;
} }
setGroups(groups) { setGroups(rawGroups, parent = null) {
this.state.groups = this.decorateGroups(groups); const parentGroup = parent;
return groups; if (parentGroup) {
parentGroup.subGroups = this.buildTree(rawGroups);
} else {
this.state.groups = this.buildTree(rawGroups);
}
return rawGroups;
}
buildTree(rawGroups) {
const groups = this.decorateGroups(rawGroups);
const tree = {};
const mappedGroups = {};
// 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).forEach((key) => {
const currentGroup = mappedGroups[key];
// If the group is not at the root level, add it to its parent array of subGroups.
if (currentGroup.parentId) {
mappedGroups[currentGroup.parentId].subGroups[currentGroup.id] = currentGroup;
mappedGroups[currentGroup.parentId].isOpen = true; // Expand group if it has subgroups
} else {
// If the group is at the root level, add it to first level elements array.
tree[currentGroup.id] = currentGroup;
}
});
return tree;
} }
decorateGroups(rawGroups) { decorateGroups(rawGroups) {
...@@ -19,12 +52,14 @@ export default class GroupsStore { ...@@ -19,12 +52,14 @@ export default class GroupsStore {
static decorateGroup(rawGroup) { static decorateGroup(rawGroup) {
return { return {
fullName: rawGroup.name, id: rawGroup.id,
fullName: rawGroup.full_name,
description: rawGroup.description, description: rawGroup.description,
webUrl: rawGroup.web_url, webUrl: rawGroup.web_url,
parentId: rawGroup.parentId, parentId: rawGroup.parent_id,
hasSubgroups: !!rawGroup.parent_id, expandable: true,
isOpen: false, isOpen: false,
subGroups: {},
}; };
} }
......
...@@ -111,3 +111,9 @@ ...@@ -111,3 +111,9 @@
height: 50px; height: 50px;
} }
} }
.list-group .list-group {
margin-top: 10px;
margin-bottom: 0;
}
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