debian-mirror-gitlab/app/assets/javascripts/ide/stores/actions/tree.js

101 lines
3 KiB
JavaScript
Raw Normal View History

2019-07-07 11:18:12 +05:30
import _ from 'underscore';
2018-11-08 19:23:39 +05:30
import { __ } from '../../../locale';
2018-05-09 12:01:36 +05:30
import service from '../../services';
import * as types from '../mutation_types';
2019-07-07 11:18:12 +05:30
import { decorateFiles } from '../../lib/files';
2018-05-09 12:01:36 +05:30
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,
});
};
2019-12-26 22:10:19 +05:30
export const getFiles = ({ state, commit, dispatch, getters }, { projectId, branchId } = {}) =>
2018-05-09 12:01:36 +05:30
new Promise((resolve, reject) => {
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
const selectedBranch = getters.findBranch(projectId, branchId);
2018-05-09 12:01:36 +05:30
2019-12-26 22:10:19 +05:30
commit(types.CREATE_TREE, { treePath: `${projectId}/${branchId}` });
2018-05-09 12:01:36 +05:30
service
2019-12-26 22:10:19 +05:30
.getFiles(selectedProject.web_url, selectedBranch.commit.id)
2018-11-08 19:23:39 +05:30
.then(({ data }) => {
2019-07-07 11:18:12 +05:30
const { entries, treeList } = decorateFiles({
2018-05-09 12:01:36 +05:30
data,
projectId,
branchId,
});
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.
_.defer(() => dispatch('setDirectoryData', { projectId, branchId, treeList }));
resolve();
2018-05-09 12:01:36 +05:30
})
.catch(e => {
2019-09-04 21:01:54 +05:30
dispatch('setErrorMessage', {
text: __('An error occurred whilst loading all the files.'),
action: payload =>
dispatch('getFiles', payload).then(() => dispatch('setErrorMessage', null)),
actionText: __('Please try again'),
actionPayload: { projectId, branchId },
});
2018-05-09 12:01:36 +05:30
reject(e);
});
} else {
resolve();
}
});
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);
}
};