2020-04-22 19:07:51 +05:30
|
|
|
import { defer } from 'lodash';
|
2021-02-22 17:27:13 +05:30
|
|
|
import {
|
|
|
|
WEBIDE_MARK_FETCH_FILES_FINISH,
|
|
|
|
WEBIDE_MEASURE_FETCH_FILES,
|
|
|
|
WEBIDE_MARK_FETCH_FILES_START,
|
|
|
|
} from '~/performance/constants';
|
2021-03-11 19:13:27 +05:30
|
|
|
import { performanceMarkAndMeasure } from '~/performance/utils';
|
2018-11-08 19:23:39 +05:30
|
|
|
import { __ } from '../../../locale';
|
2021-03-11 19:13:27 +05:30
|
|
|
import { decorateFiles } from '../../lib/files';
|
2018-05-09 12:01:36 +05:30
|
|
|
import service from '../../services';
|
|
|
|
import * as types from '../mutation_types';
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
export const toggleTreeOpen = ({ commit }, path) => {
|
2018-05-09 12:01:36 +05:30
|
|
|
commit(types.TOGGLE_TREE_OPEN, path);
|
|
|
|
};
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
export const handleTreeEntryAction = ({ commit, dispatch }, row) => {
|
|
|
|
if (row.type === 'tree') {
|
|
|
|
dispatch('toggleTreeOpen', row.path);
|
2018-11-18 11:00:15 +05:30
|
|
|
} else if (row.type === 'blob') {
|
|
|
|
if (!row.opened) {
|
2018-05-09 12:01:36 +05:30
|
|
|
commit(types.TOGGLE_FILE_OPEN, row.path);
|
|
|
|
}
|
|
|
|
|
|
|
|
dispatch('setFileActive', row.path);
|
|
|
|
}
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
dispatch('showTreeEntry', row.path);
|
2018-05-09 12:01:36 +05:30
|
|
|
};
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
export const setDirectoryData = ({ state, commit }, { projectId, branchId, treeList }) => {
|
|
|
|
const selectedTree = state.trees[`${projectId}/${branchId}`];
|
|
|
|
|
|
|
|
commit(types.SET_DIRECTORY_DATA, {
|
|
|
|
treePath: `${projectId}/${branchId}`,
|
|
|
|
data: treeList,
|
|
|
|
});
|
|
|
|
commit(types.TOGGLE_LOADING, {
|
|
|
|
entry: selectedTree,
|
|
|
|
forceValue: false,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
export const getFiles = ({ state, commit, dispatch }, payload = {}) => {
|
|
|
|
performanceMarkAndMeasure({ mark: WEBIDE_MARK_FETCH_FILES_START });
|
|
|
|
return new Promise((resolve, reject) => {
|
2020-03-13 15:44:24 +05:30
|
|
|
const { projectId, branchId, ref = branchId } = payload;
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
if (
|
|
|
|
!state.trees[`${projectId}/${branchId}`] ||
|
|
|
|
(state.trees[`${projectId}/${branchId}`].tree &&
|
|
|
|
state.trees[`${projectId}/${branchId}`].tree.length === 0)
|
|
|
|
) {
|
2018-05-09 12:01:36 +05:30
|
|
|
const selectedProject = state.projects[projectId];
|
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
commit(types.CREATE_TREE, { treePath: `${projectId}/${branchId}` });
|
2018-05-09 12:01:36 +05:30
|
|
|
service
|
2020-05-24 23:13:21 +05:30
|
|
|
.getFiles(selectedProject.path_with_namespace, ref)
|
2018-11-08 19:23:39 +05:30
|
|
|
.then(({ data }) => {
|
2021-02-22 17:27:13 +05:30
|
|
|
performanceMarkAndMeasure({
|
|
|
|
mark: WEBIDE_MARK_FETCH_FILES_FINISH,
|
|
|
|
measures: [
|
|
|
|
{
|
|
|
|
name: WEBIDE_MEASURE_FETCH_FILES,
|
|
|
|
start: WEBIDE_MARK_FETCH_FILES_START,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
2020-11-24 15:15:51 +05:30
|
|
|
const { entries, treeList } = decorateFiles({ data });
|
2019-07-07 11:18:12 +05:30
|
|
|
|
|
|
|
commit(types.SET_ENTRIES, entries);
|
|
|
|
|
|
|
|
// Defer setting the directory data because this triggers some intense rendering.
|
|
|
|
// The entries is all we need to load the file editor.
|
2020-04-22 19:07:51 +05:30
|
|
|
defer(() => dispatch('setDirectoryData', { projectId, branchId, treeList }));
|
2019-07-07 11:18:12 +05:30
|
|
|
|
|
|
|
resolve();
|
2018-05-09 12:01:36 +05:30
|
|
|
})
|
2021-03-08 18:12:59 +05:30
|
|
|
.catch((e) => {
|
2019-09-04 21:01:54 +05:30
|
|
|
dispatch('setErrorMessage', {
|
2020-03-13 15:44:24 +05:30
|
|
|
text: __('An error occurred while loading all the files.'),
|
2021-03-08 18:12:59 +05:30
|
|
|
action: (actionPayload) =>
|
2020-03-13 15:44:24 +05:30
|
|
|
dispatch('getFiles', actionPayload).then(() => dispatch('setErrorMessage', null)),
|
2019-09-04 21:01:54 +05:30
|
|
|
actionText: __('Please try again'),
|
|
|
|
actionPayload: { projectId, branchId },
|
|
|
|
});
|
2018-05-09 12:01:36 +05:30
|
|
|
reject(e);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
2021-02-22 17:27:13 +05:30
|
|
|
};
|
2018-11-18 11:00:15 +05:30
|
|
|
|
|
|
|
export const restoreTree = ({ dispatch, commit, state }, path) => {
|
|
|
|
const entry = state.entries[path];
|
|
|
|
|
|
|
|
commit(types.RESTORE_TREE, path);
|
|
|
|
|
|
|
|
if (entry.parentPath) {
|
|
|
|
dispatch('restoreTree', entry.parentPath);
|
|
|
|
}
|
|
|
|
};
|