gitlab-org--gitlab-foss/app/assets/javascripts/groups/stores/groups_store.js

142 lines
3.8 KiB
JavaScript
Raw Normal View History

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() {
this.state = {};
this.state.groups = {};
2017-05-22 17:47:05 +00:00
this.state.pageInfo = {};
return this;
}
setGroups(rawGroups, parent = null) {
const parentGroup = parent;
2017-05-30 05:50:29 +00:00
const tree = this.buildTree(rawGroups, parentGroup);
if (parentGroup) {
parentGroup.subGroups = tree;
} else {
this.state.groups = tree;
}
return tree;
}
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) {
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];
if (currentGroup.parentId) {
2017-05-30 05:50:29 +00:00
if (findParentGroup) {
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;
} 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) {
this.groups = rawGroups.map(GroupsStore.decorateGroup);
return this.groups;
}
static decorateGroup(rawGroup) {
return {
id: rawGroup.id,
fullName: rawGroup.full_name,
fullPath: rawGroup.full_path,
name: rawGroup.name,
hasSubgroups: rawGroup.has_subgroups,
canEdit: rawGroup.can_edit,
description: rawGroup.description,
webUrl: rawGroup.web_url,
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,
isOpen: false,
isOrphan: false,
numberProjects: rawGroup.number_projects,
numberUsers: rawGroup.number_users,
subGroups: {},
};
}
static toggleSubGroups(toggleGroup) {
const group = toggleGroup;
group.isOpen = !group.isOpen;
return group;
}
2017-05-05 01:49:07 +00:00
}