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

102 lines
2.9 KiB
JavaScript
Raw Normal View History

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';
import FilesDecoratorWorker from '../workers/files_decorator_worker';
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
};
export const getFiles = ({ state, commit, dispatch }, { projectId, branchId } = {}) =>
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];
commit(types.CREATE_TREE, { treePath: `${projectId}/${branchId}` });
service
.getFiles(selectedProject.web_url, branchId)
2018-11-08 19:23:39 +05:30
.then(({ data }) => {
2018-05-09 12:01:36 +05:30
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 => {
2018-11-08 19:23:39 +05:30
if (e.response.status === 404) {
dispatch('showBranchNotFoundError', branchId);
} else {
dispatch('setErrorMessage', {
2018-12-23 12:14:25 +05:30
text: __('An error occurred whilst loading all the files.'),
2018-11-08 19:23:39 +05:30
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);
}
};