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

314 lines
9 KiB
JavaScript
Raw Normal View History

2020-05-24 23:13:21 +05:30
import { escape } from 'lodash';
2021-03-11 19:13:27 +05:30
import Vue from 'vue';
2021-09-30 23:02:18 +05:30
import createFlash from '~/flash';
2021-03-11 19:13:27 +05:30
import { visitUrl } from '~/lib/utils/url_utility';
import { __, sprintf } from '~/locale';
2021-02-22 17:27:13 +05:30
import {
WEBIDE_MARK_FETCH_BRANCH_DATA_START,
WEBIDE_MARK_FETCH_BRANCH_DATA_FINISH,
WEBIDE_MEASURE_FETCH_BRANCH_DATA,
} from '~/performance/constants';
2021-03-11 19:13:27 +05:30
import { performanceMarkAndMeasure } from '~/performance/utils';
2021-01-29 00:20:46 +05:30
import { stageKeys, commitActionTypes } from '../constants';
2019-12-21 20:55:43 +05:30
import eventHub from '../eventhub';
2021-03-11 19:13:27 +05:30
import { decorateFiles } from '../lib/files';
import service from '../services';
import * as types from './mutation_types';
2018-05-09 12:01:36 +05:30
2019-09-04 21:01:54 +05:30
export const redirectToUrl = (self, url) => visitUrl(url);
2018-05-09 12:01:36 +05:30
2021-06-08 01:23:25 +05:30
export const init = ({ commit }, data) => commit(types.SET_INITIAL_DATA, data);
2018-05-09 12:01:36 +05:30
export const discardAllChanges = ({ state, commit, dispatch }) => {
2021-03-08 18:12:59 +05:30
state.changedFiles.forEach((file) => dispatch('restoreOriginalFile', file.path));
2018-05-09 12:01:36 +05:30
commit(types.REMOVE_ALL_CHANGES_FILES);
};
export const setResizingStatus = ({ commit }, resizing) => {
commit(types.SET_RESIZING_STATUS, resizing);
};
export const createTempEntry = (
2020-03-13 15:44:24 +05:30
{ state, commit, dispatch, getters },
2021-03-08 18:12:59 +05:30
{ name, type, content = '', rawPath = '', openFile = true, makeFileActive = true, mimeType = '' },
2020-03-13 15:44:24 +05:30
) => {
const fullName = name.slice(-1) !== '/' && type === 'tree' ? `${name}/` : name;
2020-06-23 00:09:42 +05:30
if (getters.entryExists(name)) {
2021-09-30 23:02:18 +05:30
createFlash({
message: sprintf(__('The name "%{name}" is already taken in this directory.'), {
2020-03-13 15:44:24 +05:30
name: name.split('/').pop(),
}),
2021-09-30 23:02:18 +05:30
fadeTransition: false,
addBodyClass: true,
});
2018-05-09 12:01:36 +05:30
2020-06-23 00:09:42 +05:30
return undefined;
2020-03-13 15:44:24 +05:30
}
2019-07-07 11:18:12 +05:30
2020-03-13 15:44:24 +05:30
const data = decorateFiles({
data: [fullName],
type,
tempFile: true,
content,
rawPath,
2021-03-08 18:12:59 +05:30
blobData: {
mimeType,
},
2020-03-13 15:44:24 +05:30
});
const { file, parentPath } = data;
2019-07-07 11:18:12 +05:30
2020-11-24 15:15:51 +05:30
commit(types.CREATE_TMP_ENTRY, { data });
2019-07-07 11:18:12 +05:30
2020-03-13 15:44:24 +05:30
if (type === 'blob') {
2020-06-23 00:09:42 +05:30
if (openFile) commit(types.TOGGLE_FILE_OPEN, file.path);
2020-03-13 15:44:24 +05:30
commit(types.STAGE_CHANGE, { path: file.path, diffInfo: getters.getDiffInfo(file.path) });
2019-07-07 11:18:12 +05:30
2020-06-23 00:09:42 +05:30
if (openFile && makeFileActive) dispatch('setFileActive', file.path);
2020-03-13 15:44:24 +05:30
dispatch('triggerFilesChange');
}
2018-05-09 12:01:36 +05:30
2020-03-13 15:44:24 +05:30
if (parentPath && !state.entries[parentPath].opened) {
commit(types.TOGGLE_TREE_OPEN, parentPath);
}
2020-06-23 00:09:42 +05:30
return file;
2020-03-13 15:44:24 +05:30
};
2018-05-09 12:01:36 +05:30
2021-09-30 23:02:18 +05:30
export const addTempImage = ({ dispatch, getters }, { name, rawPath = '', content = '' }) =>
2020-06-23 00:09:42 +05:30
dispatch('createTempEntry', {
name: getters.getAvailableFileName(name),
type: 'blob',
2021-09-30 23:02:18 +05:30
content,
2020-06-23 00:09:42 +05:30
rawPath,
openFile: false,
makeFileActive: false,
});
2018-05-09 12:01:36 +05:30
export const scrollToTab = () => {
Vue.nextTick(() => {
const tabs = document.getElementById('tabs');
if (tabs) {
const tabEl = tabs.querySelector('.active .repo-tab');
tabEl.focus();
}
});
};
2020-03-13 15:44:24 +05:30
export const stageAllChanges = ({ state, commit, dispatch, getters }) => {
2018-11-20 20:47:30 +05:30
const openFile = state.openFiles[0];
2018-10-15 14:42:47 +05:30
commit(types.SET_LAST_COMMIT_MSG, '');
2021-03-08 18:12:59 +05:30
state.changedFiles.forEach((file) =>
2020-03-13 15:44:24 +05:30
commit(types.STAGE_CHANGE, { path: file.path, diffInfo: getters.getDiffInfo(file.path) }),
);
2018-11-20 20:47:30 +05:30
2020-03-13 15:44:24 +05:30
const file = getters.getStagedFile(openFile.path);
if (file) {
dispatch('openPendingTab', {
file,
keyPrefix: stageKeys.staged,
});
}
2018-10-15 14:42:47 +05:30
};
2020-03-13 15:44:24 +05:30
export const unstageAllChanges = ({ state, commit, dispatch, getters }) => {
2018-11-20 20:47:30 +05:30
const openFile = state.openFiles[0];
2021-03-08 18:12:59 +05:30
state.stagedFiles.forEach((file) =>
2020-03-13 15:44:24 +05:30
commit(types.UNSTAGE_CHANGE, { path: file.path, diffInfo: getters.getDiffInfo(file.path) }),
);
2018-11-20 20:47:30 +05:30
2020-03-13 15:44:24 +05:30
const file = getters.getChangedFile(openFile.path);
if (file) {
dispatch('openPendingTab', {
file,
keyPrefix: stageKeys.unstaged,
});
}
2018-10-15 14:42:47 +05:30
};
2018-05-09 12:01:36 +05:30
export const updateViewer = ({ commit }, viewer) => {
commit(types.UPDATE_VIEWER, viewer);
};
export const updateDelayViewerUpdated = ({ commit }, delay) => {
commit(types.UPDATE_DELAY_VIEWER_CHANGE, delay);
};
2018-10-15 14:42:47 +05:30
export const updateActivityBarView = ({ commit }, view) => {
commit(types.UPDATE_ACTIVITY_BAR_VIEW, view);
};
export const setEmptyStateSvgs = ({ commit }, svgs) => {
commit(types.SET_EMPTY_STATE_SVGS, svgs);
};
export const setCurrentBranchId = ({ commit }, currentBranchId) => {
commit(types.SET_CURRENT_BRANCH, currentBranchId);
};
export const updateTempFlagForEntry = ({ commit, dispatch, state }, { file, tempFile }) => {
commit(types.UPDATE_TEMP_FLAG, { path: file.path, tempFile });
2019-12-21 20:55:43 +05:30
const parent = file.parentPath && state.entries[file.parentPath];
if (parent) {
dispatch('updateTempFlagForEntry', { file: parent, tempFile });
2018-10-15 14:42:47 +05:30
}
};
export const toggleFileFinder = ({ commit }, fileFindVisible) =>
commit(types.TOGGLE_FILE_FINDER, fileFindVisible);
2018-11-08 19:23:39 +05:30
export const setLinks = ({ commit }, links) => commit(types.SET_LINKS, links);
export const setErrorMessage = ({ commit }, errorMessage) =>
commit(types.SET_ERROR_MESSAGE, errorMessage);
2018-11-18 11:00:15 +05:30
export const deleteEntry = ({ commit, dispatch, state }, path) => {
const entry = state.entries[path];
2019-12-21 20:55:43 +05:30
const { prevPath, prevName, prevParentPath } = entry;
const isTree = entry.type === 'tree';
2020-03-13 15:44:24 +05:30
const prevEntry = prevPath && state.entries[prevPath];
2019-12-21 20:55:43 +05:30
2020-03-13 15:44:24 +05:30
if (prevPath && (!prevEntry || prevEntry.deleted)) {
2019-12-21 20:55:43 +05:30
dispatch('renameEntry', {
path,
name: prevName,
parentPath: prevParentPath,
});
dispatch('deleteEntry', prevPath);
return;
}
2020-03-13 15:44:24 +05:30
2018-11-18 11:00:15 +05:30
if (entry.opened) dispatch('closeFile', entry);
2019-12-21 20:55:43 +05:30
if (isTree) {
2021-03-08 18:12:59 +05:30
entry.tree.forEach((f) => dispatch('deleteEntry', f.path));
2018-11-18 11:00:15 +05:30
}
commit(types.DELETE_ENTRY, path);
2019-12-21 20:55:43 +05:30
// Only stage if we're not a directory or a new file
if (!isTree && !entry.tempFile) {
dispatch('stageChange', path);
}
2019-09-04 21:01:54 +05:30
dispatch('triggerFilesChange');
2018-11-18 11:00:15 +05:30
};
export const resetOpenFiles = ({ commit }) => commit(types.RESET_OPEN_FILES);
2020-03-13 15:44:24 +05:30
export const renameEntry = ({ dispatch, commit, state, getters }, { path, name, parentPath }) => {
2019-12-21 20:55:43 +05:30
const entry = state.entries[path];
const newPath = parentPath ? `${parentPath}/${name}` : name;
2020-03-13 15:44:24 +05:30
const existingParent = parentPath && state.entries[parentPath];
if (parentPath && (!existingParent || existingParent.deleted)) {
dispatch('createTempEntry', { name: parentPath, type: 'tree' });
}
2018-11-20 20:47:30 +05:30
2019-12-21 20:55:43 +05:30
commit(types.RENAME_ENTRY, { path, name, parentPath });
2018-11-18 11:00:15 +05:30
if (entry.type === 'tree') {
2021-03-08 18:12:59 +05:30
state.entries[newPath].tree.forEach((f) => {
2019-07-07 11:18:12 +05:30
dispatch('renameEntry', {
2019-12-21 20:55:43 +05:30
path: f.path,
name: f.name,
parentPath: newPath,
2019-07-07 11:18:12 +05:30
});
});
2019-09-30 21:07:59 +05:30
} else {
const newEntry = state.entries[newPath];
2019-12-21 20:55:43 +05:30
const isRevert = newPath === entry.prevPath;
const isReset = isRevert && !newEntry.changed && !newEntry.tempFile;
const isInChanges = state.changedFiles
.concat(state.stagedFiles)
.some(({ key }) => key === newEntry.key);
if (isReset) {
commit(types.REMOVE_FILE_FROM_STAGED_AND_CHANGED, newEntry);
} else if (!isInChanges) {
2020-03-13 15:44:24 +05:30
commit(types.STAGE_CHANGE, { path: newPath, diffInfo: getters.getDiffInfo(newPath) });
2019-12-21 20:55:43 +05:30
}
if (!newEntry.tempFile) {
eventHub.$emit(`editor.update.model.dispose.${entry.key}`);
}
2018-11-18 11:00:15 +05:30
2019-12-21 20:55:43 +05:30
if (newEntry.opened) {
2020-11-24 15:15:51 +05:30
dispatch('router/push', getters.getUrlForPath(newEntry.path), { root: true });
2019-09-30 21:07:59 +05:30
}
2018-11-18 11:00:15 +05:30
}
2019-09-04 21:01:54 +05:30
2021-01-29 00:20:46 +05:30
dispatch('triggerFilesChange', { type: commitActionTypes.move, path, newPath });
2018-11-18 11:00:15 +05:30
};
2021-02-22 17:27:13 +05:30
export const getBranchData = ({ commit, state }, { projectId, branchId, force = false } = {}) => {
return new Promise((resolve, reject) => {
performanceMarkAndMeasure({ mark: WEBIDE_MARK_FETCH_BRANCH_DATA_START });
2019-09-04 21:01:54 +05:30
const currentProject = state.projects[projectId];
if (!currentProject || !currentProject.branches[branchId] || force) {
service
.getBranchData(projectId, branchId)
.then(({ data }) => {
2021-02-22 17:27:13 +05:30
performanceMarkAndMeasure({
mark: WEBIDE_MARK_FETCH_BRANCH_DATA_FINISH,
measures: [
{
name: WEBIDE_MEASURE_FETCH_BRANCH_DATA,
start: WEBIDE_MARK_FETCH_BRANCH_DATA_START,
},
],
});
2019-09-04 21:01:54 +05:30
const { id } = data.commit;
commit(types.SET_BRANCH, {
projectPath: projectId,
branchName: branchId,
branch: data,
});
commit(types.SET_BRANCH_WORKING_REFERENCE, { projectId, branchId, reference: id });
resolve(data);
})
2021-03-08 18:12:59 +05:30
.catch((e) => {
2019-09-04 21:01:54 +05:30
if (e.response.status === 404) {
reject(e);
} else {
2021-09-30 23:02:18 +05:30
createFlash({
message: __('Error loading branch data. Please try again.'),
fadeTransition: false,
addBodyClass: true,
});
2019-09-04 21:01:54 +05:30
reject(
new Error(
sprintf(
__('Branch not loaded - %{branchId}'),
{
2020-05-24 23:13:21 +05:30
branchId: `<strong>${escape(projectId)}/${escape(branchId)}</strong>`,
2019-09-04 21:01:54 +05:30
},
false,
),
),
);
}
});
} else {
resolve(currentProject.branches[branchId]);
}
});
2021-02-22 17:27:13 +05:30
};
2019-09-04 21:01:54 +05:30
2018-05-09 12:01:36 +05:30
export * from './actions/tree';
export * from './actions/file';
export * from './actions/project';
export * from './actions/merge_request';
2021-06-08 01:23:25 +05:30
export * from './actions/alert';