summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/ide/stores/actions/tree.js
blob: 9288bbe32f5b56c8469d370200a9b5b33e9b1019 (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
import { __ } from '../../../locale';
import service from '../../services';
import * as types from '../mutation_types';
import FilesDecoratorWorker from '../workers/files_decorator_worker';

export const toggleTreeOpen = ({ commit }, path) => {
  commit(types.TOGGLE_TREE_OPEN, path);
};

export const showTreeEntry = ({ commit, dispatch, state }, path) => {
  const entry = state.entries[path];
  const parentPath = entry ? entry.parentPath : '';

  if (parentPath) {
    commit(types.SET_TREE_OPEN, parentPath);

    dispatch('showTreeEntry', parentPath);
  }
};

export const handleTreeEntryAction = ({ commit, dispatch }, row) => {
  if (row.type === 'tree') {
    dispatch('toggleTreeOpen', row.path);
  } else if (row.type === 'blob') {
    if (!row.opened) {
      commit(types.TOGGLE_FILE_OPEN, row.path);
    }

    dispatch('setFileActive', row.path);
  }

  dispatch('showTreeEntry', row.path);
};

export const getFiles = ({ state, commit, dispatch }, { projectId, branchId } = {}) =>
  new Promise((resolve, reject) => {
    if (
      !state.trees[`${projectId}/${branchId}`] ||
      (state.trees[`${projectId}/${branchId}`].tree &&
        state.trees[`${projectId}/${branchId}`].tree.length === 0)
    ) {
      const selectedProject = state.projects[projectId];
      commit(types.CREATE_TREE, { treePath: `${projectId}/${branchId}` });

      service
        .getFiles(selectedProject.web_url, branchId)
        .then(({ data }) => {
          const worker = new FilesDecoratorWorker();
          worker.addEventListener('message', e => {
            const { entries, treeList } = e.data;
            const selectedTree = state.trees[`${projectId}/${branchId}`];

            commit(types.SET_ENTRIES, entries);
            commit(types.SET_DIRECTORY_DATA, {
              treePath: `${projectId}/${branchId}`,
              data: treeList,
            });
            commit(types.TOGGLE_LOADING, {
              entry: selectedTree,
              forceValue: false,
            });

            worker.terminate();

            resolve();
          });

          worker.postMessage({
            data,
            projectId,
            branchId,
          });
        })
        .catch(e => {
          if (e.response.status === 404) {
            dispatch('showBranchNotFoundError', branchId);
          } else {
            dispatch('setErrorMessage', {
              text: __('An error occured whilst loading all the files.'),
              action: payload =>
                dispatch('getFiles', payload).then(() => dispatch('setErrorMessage', null)),
              actionText: __('Please try again'),
              actionPayload: { projectId, branchId },
            });
          }
          reject(e);
        });
    } else {
      resolve();
    }
  });

export const restoreTree = ({ dispatch, commit, state }, path) => {
  const entry = state.entries[path];

  commit(types.RESTORE_TREE, path);

  if (entry.parentPath) {
    dispatch('restoreTree', entry.parentPath);
  }
};