2017-05-30 05:50:29 +00:00
|
|
|
/* eslint-disable class-methods-use-this */
|
2017-05-05 01:49:07 +00:00
|
|
|
export default class GroupsStore {
|
|
|
|
constructor() {
|
2017-05-06 04:15:04 +00:00
|
|
|
this.state = {};
|
2017-05-23 23:01:47 +00:00
|
|
|
this.state.groups = {};
|
2017-05-22 17:47:05 +00:00
|
|
|
this.state.pageInfo = {};
|
2017-05-06 04:15:04 +00:00
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2017-05-12 08:36:08 +00:00
|
|
|
setGroups(rawGroups, parent = null) {
|
|
|
|
const parentGroup = parent;
|
2017-05-30 05:50:29 +00:00
|
|
|
const tree = this.buildTree(rawGroups, parentGroup);
|
2017-05-06 04:15:04 +00:00
|
|
|
|
2017-05-12 08:36:08 +00:00
|
|
|
if (parentGroup) {
|
2017-05-26 00:49:46 +00:00
|
|
|
parentGroup.subGroups = tree;
|
2017-05-12 08:36:08 +00:00
|
|
|
} else {
|
2017-05-26 00:49:46 +00:00
|
|
|
this.state.groups = tree;
|
2017-05-12 08:36:08 +00:00
|
|
|
}
|
|
|
|
|
2017-05-26 00:49:46 +00:00
|
|
|
return tree;
|
2017-05-12 08:36:08 +00:00
|
|
|
}
|
|
|
|
|
2017-05-30 05:50:29 +00:00
|
|
|
resetGroups(parent) {
|
|
|
|
const parentGroup = parent;
|
|
|
|
parentGroup.subGroups = {};
|
|
|
|
}
|
|
|
|
|
2017-05-22 17:47:05 +00:00
|
|
|
storePagination(pagination = {}) {
|
|
|
|
let paginationInfo;
|
|
|
|
|
|
|
|
if (Object.keys(pagination).length) {
|
|
|
|
const normalizedHeaders = gl.utils.normalizeHeaders(pagination);
|
|
|
|
paginationInfo = gl.utils.parseIntPagination(normalizedHeaders);
|
|
|
|
} else {
|
|
|
|
paginationInfo = pagination;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.state.pageInfo = paginationInfo;
|
|
|
|
}
|
|
|
|
|
2017-05-30 05:50:29 +00:00
|
|
|
buildTree(rawGroups, parentGroup) {
|
2017-05-23 23:01:47 +00:00
|
|
|
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.
|
2017-05-30 05:50:29 +00:00
|
|
|
const findParentGroup = mappedGroups[currentGroup.parentId];
|
|
|
|
|
2017-05-23 23:01:47 +00:00
|
|
|
if (currentGroup.parentId) {
|
2017-05-30 05:50:29 +00:00
|
|
|
if (findParentGroup) {
|
2017-05-23 23:01:47 +00:00
|
|
|
mappedGroups[currentGroup.parentId].subGroups[currentGroup.id] = currentGroup;
|
|
|
|
mappedGroups[currentGroup.parentId].isOpen = true; // Expand group if it has subgroups
|
2017-05-30 05:50:29 +00:00
|
|
|
} else if (parentGroup && parentGroup.id === currentGroup.parentId) {
|
|
|
|
tree[currentGroup.id] = currentGroup;
|
2017-05-23 23:01:47 +00:00
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
|
2017-05-08 20:22:26 +00:00
|
|
|
decorateGroups(rawGroups) {
|
|
|
|
this.groups = rawGroups.map(GroupsStore.decorateGroup);
|
|
|
|
return this.groups;
|
|
|
|
}
|
|
|
|
|
|
|
|
static decorateGroup(rawGroup) {
|
2017-05-11 17:10:54 +00:00
|
|
|
return {
|
2017-05-12 08:36:08 +00:00
|
|
|
id: rawGroup.id,
|
|
|
|
fullName: rawGroup.full_name,
|
2017-05-23 23:01:47 +00:00
|
|
|
fullPath: rawGroup.full_path,
|
|
|
|
name: rawGroup.name,
|
2017-05-30 08:53:40 +00:00
|
|
|
hasSubgroups: rawGroup.has_subgroups,
|
2017-05-30 08:29:21 +00:00
|
|
|
canEdit: rawGroup.can_edit,
|
2017-05-11 17:10:54 +00:00
|
|
|
description: rawGroup.description,
|
|
|
|
webUrl: rawGroup.web_url,
|
2017-05-12 08:36:08 +00:00
|
|
|
parentId: rawGroup.parent_id,
|
2017-05-16 14:29:38 +00:00
|
|
|
visibility: rawGroup.visibility,
|
2017-05-30 08:12:06 +00:00
|
|
|
leavePath: rawGroup.leave_path,
|
|
|
|
editPath: rawGroup.edit_path,
|
2017-05-11 17:10:54 +00:00
|
|
|
isOpen: false,
|
2017-05-23 23:01:47 +00:00
|
|
|
isOrphan: false,
|
2017-05-30 09:25:53 +00:00
|
|
|
numberProjects: rawGroup.number_projects,
|
|
|
|
numberUsers: rawGroup.number_users,
|
2017-05-23 23:01:47 +00:00
|
|
|
subGroups: {},
|
2017-05-11 17:10:54 +00:00
|
|
|
};
|
2017-05-08 20:22:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static toggleSubGroups(toggleGroup) {
|
|
|
|
const group = toggleGroup;
|
|
|
|
group.isOpen = !group.isOpen;
|
|
|
|
return group;
|
|
|
|
}
|
2017-05-05 01:49:07 +00:00
|
|
|
}
|