summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/groups/stores/groups_store.js
blob: a10b6617f03452b3090e7078571194b44095993f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import Vue from 'vue';

export default class GroupsStore {
  constructor() {
    this.state = {};
    this.state.groups = {};
    this.state.pageInfo = {};
  }

  setGroups(rawGroups, parent) {
    const parentGroup = parent;
    const tree = this.buildTree(rawGroups, parentGroup);

    if (parentGroup) {
      parentGroup.subGroups = tree;
    } else {
      this.state.groups = tree;
    }

    return tree;
  }

  // eslint-disable-next-line class-methods-use-this
  resetGroups(parent) {
    const parentGroup = parent;
    parentGroup.subGroups = {};
  }

  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;
  }

  buildTree(rawGroups, parentGroup) {
    const groups = this.decorateGroups(rawGroups);
    const tree = {};
    const mappedGroups = {};
    const orphans = [];

    // Map groups to an object
    groups.map((group) => {
      mappedGroups[group.id] = group;
      mappedGroups[group.id].subGroups = {};
      return group;
    });

    Object.keys(mappedGroups).map((key) => {
      const currentGroup = mappedGroups[key];
      if (currentGroup.parentId) {
        // If the group is not at the root level, add it to its parent array of subGroups.
        const findParentGroup = mappedGroups[currentGroup.parentId];
        if (findParentGroup) {
          mappedGroups[currentGroup.parentId].subGroups[currentGroup.id] = currentGroup;
          mappedGroups[currentGroup.parentId].isOpen = true; // Expand group if it has subgroups
        } 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(this.decorateGroup);
    return this.groups;
  }

  // eslint-disable-next-line class-methods-use-this
  decorateGroup(rawGroup) {
    return {
      id: rawGroup.id,
      fullName: rawGroup.full_name,
      fullPath: rawGroup.full_path,
      avatarUrl: rawGroup.avatar_url,
      name: rawGroup.name,
      hasSubgroups: rawGroup.has_subgroups,
      canEdit: rawGroup.can_edit,
      description: rawGroup.description,
      webUrl: rawGroup.web_url,
      parentId: rawGroup.parent_id,
      visibility: rawGroup.visibility,
      leavePath: rawGroup.leave_path,
      editPath: rawGroup.edit_path,
      isOpen: false,
      isOrphan: false,
      numberProjects: rawGroup.number_projects_with_delimiter,
      numberUsers: rawGroup.number_users_with_delimiter,
      subGroups: {},
    };
  }

  // eslint-disable-next-line class-methods-use-this
  removeGroup(group, collection) {
    Vue.delete(collection, group.id);
  }

  // eslint-disable-next-line class-methods-use-this
  static toggleSubGroups(toggleGroup) {
    const group = toggleGroup;
    group.isOpen = !group.isOpen;
    return group;
  }
}