summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/ide/stores/actions/tree.js
blob: 25909400a75a0d22150c7001143a94563ee914af (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import { visitUrl } from '../../../lib/utils/url_utility';
import { normalizeHeaders } from '../../../lib/utils/common_utils';
import flash from '../../../flash';
import service from '../../services';
import * as types from '../mutation_types';
import router from '../../ide_router';
import {
  setPageTitle,
  findEntry,
  createTemp,
  createOrMergeEntry,
} from '../utils';

export const getTreeData = (
  { commit, state, dispatch },
  { endpoint, tree = null, projectId, branch, force = false } = {},
) => new Promise((resolve, reject) => {
  // We already have the base tree so we resolve immediately
  if (!tree && state.trees[`${projectId}/${branch}`] && !force) {
    resolve();
  } else {
    if (tree) commit(types.TOGGLE_LOADING, tree);
    const selectedProject = state.projects[projectId];
    // We are merging the web_url that we got on the project info with the endpoint
    // we got on the tree entry, as both contain the projectId, we replace it in the tree endpoint
    const completeEndpoint = selectedProject.web_url + (endpoint).replace(projectId, '');
    if (completeEndpoint && (!tree || !tree.tempFile)) {
      service.getTreeData(completeEndpoint)
      .then((res) => {
        const pageTitle = decodeURI(normalizeHeaders(res.headers)['PAGE-TITLE']);

        setPageTitle(pageTitle);

        return res.json();
      })
      .then((data) => {
        if (!state.isInitialRoot) {
          commit(types.SET_ROOT, data.path === '/');
        }

        dispatch('updateDirectoryData', { data, tree, projectId, branch });
        const selectedTree = tree || state.trees[`${projectId}/${branch}`];

        commit(types.SET_PARENT_TREE_URL, data.parent_tree_url);
        commit(types.SET_LAST_COMMIT_URL, { tree: selectedTree, url: data.last_commit_path });
        if (tree) commit(types.TOGGLE_LOADING, selectedTree);

        const prevLastCommitPath = selectedTree.lastCommitPath;
        if (prevLastCommitPath !== null) {
          dispatch('getLastCommitData', selectedTree);
        }
        resolve(data);
      })
      .catch((e) => {
        flash('Error loading tree data. Please try again.');
        if (tree) commit(types.TOGGLE_LOADING, tree);
        reject(e);
      });
    } else {
      resolve();
    }
  }
});

export const toggleTreeOpen = ({ commit, dispatch }, { endpoint, tree }) => {
  if (tree.opened) {
    // send empty data to clear the tree
    const data = { trees: [], blobs: [], submodules: [] };

    dispatch('updateDirectoryData', { data, tree, projectId: tree.projectId, branchId: tree.branchId });
  } else {
    dispatch('getTreeData', { endpoint, tree, projectId: tree.projectId, branch: tree.branchId });
  }

  commit(types.TOGGLE_TREE_OPEN, tree);
};

export const handleTreeEntryAction = ({ commit, dispatch }, row) => {
  if (row.type === 'tree') {
    dispatch('toggleTreeOpen', {
      endpoint: row.url,
      tree: row,
    });
  } else if (row.type === 'submodule') {
    commit(types.TOGGLE_LOADING, row);
    visitUrl(row.url);
  } else if (row.type === 'blob' && row.opened) {
    dispatch('setFileActive', row);
  } else {
    dispatch('getFileData', row);
  }
};

export const createTempTree = (
  { state, commit, dispatch },
  { projectId, branchId, parent, name },
) => {
  let selectedTree = parent;
  const dirNames = name.replace(new RegExp(`^${state.path}/`), '').split('/');

  dirNames.forEach((dirName) => {
    const foundEntry = findEntry(selectedTree.tree, 'tree', dirName);

    if (!foundEntry) {
      const path = selectedTree.path !== undefined ? selectedTree.path : '';
      const tmpEntry = createTemp({
        projectId,
        branchId,
        name: dirName,
        path,
        type: 'tree',
        level: selectedTree.level !== undefined ? selectedTree.level + 1 : 0,
        tree: [],
        url: `/${projectId}/blob/${branchId}/${path}${path ? '/' : ''}${dirName}`,
      });

      commit(types.CREATE_TMP_TREE, {
        parent: selectedTree,
        tmpEntry,
      });
      commit(types.TOGGLE_TREE_OPEN, tmpEntry);

      router.push(`/project${tmpEntry.url}`);

      selectedTree = tmpEntry;
    } else {
      selectedTree = foundEntry;
    }
  });
};

export const getLastCommitData = ({ state, commit, dispatch, getters }, tree = state) => {
  if (!tree || tree.lastCommitPath === null || !tree.lastCommitPath) return;

  service.getTreeLastCommit(tree.lastCommitPath)
    .then((res) => {
      const lastCommitPath = normalizeHeaders(res.headers)['MORE-LOGS-URL'] || null;

      commit(types.SET_LAST_COMMIT_URL, { tree, url: lastCommitPath });

      return res.json();
    })
    .then((data) => {
      data.forEach((lastCommit) => {
        const entry = findEntry(tree.tree, lastCommit.type, lastCommit.file_name);

        if (entry) {
          commit(types.SET_LAST_COMMIT_DATA, { entry, lastCommit });
        }
      });

      dispatch('getLastCommitData', tree);
    })
    .catch(() => flash('Error fetching log data.'));
};

export const updateDirectoryData = (
  { commit, state },
  { data, tree, projectId, branch },
) => {
  if (!tree) {
    const existingTree = state.trees[`${projectId}/${branch}`];
    if (!existingTree) {
      commit(types.CREATE_TREE, { treePath: `${projectId}/${branch}` });
    }
  }

  const selectedTree = tree || state.trees[`${projectId}/${branch}`];
  const level = selectedTree.level !== undefined ? selectedTree.level + 1 : 0;
  const parentTreeUrl = data.parent_tree_url ? `${data.parent_tree_url}${data.path}` : state.endpoints.rootUrl;
  const createEntry = (entry, type) => createOrMergeEntry({
    tree: selectedTree,
    projectId: `${projectId}`,
    branchId: branch,
    entry,
    level,
    type,
    parentTreeUrl,
  });

  const formattedData = [
    ...data.trees.map(t => createEntry(t, 'tree')),
    ...data.submodules.map(m => createEntry(m, 'submodule')),
    ...data.blobs.map(b => createEntry(b, 'blob')),
  ];

  commit(types.SET_DIRECTORY_DATA, { tree: selectedTree, data: formattedData });
};