2018-11-08 19:23:39 +05:30
|
|
|
import { __ } from '../../../locale';
|
|
|
|
import { normalizeHeaders } from '../../../lib/utils/common_utils';
|
2018-05-09 12:01:36 +05:30
|
|
|
import eventHub from '../../eventhub';
|
|
|
|
import service from '../../services';
|
|
|
|
import * as types from '../mutation_types';
|
|
|
|
import router from '../../ide_router';
|
|
|
|
import { setPageTitle } from '../utils';
|
2018-11-20 20:47:30 +05:30
|
|
|
import { viewerTypes, stageKeys } from '../../constants';
|
2018-05-09 12:01:36 +05:30
|
|
|
|
|
|
|
export const closeFile = ({ commit, state, dispatch }, file) => {
|
2018-11-08 19:23:39 +05:30
|
|
|
const { path } = file;
|
2018-05-09 12:01:36 +05:30
|
|
|
const indexOfClosedFile = state.openFiles.findIndex(f => f.key === file.key);
|
|
|
|
const fileWasActive = file.active;
|
|
|
|
|
|
|
|
if (file.pending) {
|
|
|
|
commit(types.REMOVE_PENDING_TAB, file);
|
|
|
|
} else {
|
|
|
|
commit(types.TOGGLE_FILE_OPEN, path);
|
|
|
|
commit(types.SET_FILE_ACTIVE, { path, active: false });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state.openFiles.length > 0 && fileWasActive) {
|
|
|
|
const nextIndexToOpen = indexOfClosedFile === 0 ? 0 : indexOfClosedFile - 1;
|
|
|
|
const nextFileToOpen = state.openFiles[nextIndexToOpen];
|
|
|
|
|
|
|
|
if (nextFileToOpen.pending) {
|
2018-10-15 14:42:47 +05:30
|
|
|
dispatch('updateViewer', viewerTypes.diff);
|
|
|
|
dispatch('openPendingTab', {
|
|
|
|
file: nextFileToOpen,
|
|
|
|
keyPrefix: nextFileToOpen.staged ? 'staged' : 'unstaged',
|
|
|
|
});
|
2018-05-09 12:01:36 +05:30
|
|
|
} else {
|
|
|
|
router.push(`/project${nextFileToOpen.url}`);
|
|
|
|
}
|
|
|
|
} else if (!state.openFiles.length) {
|
|
|
|
router.push(`/project/${file.projectId}/tree/${file.branchId}/`);
|
|
|
|
}
|
|
|
|
|
|
|
|
eventHub.$emit(`editor.update.model.dispose.${file.key}`);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const setFileActive = ({ commit, state, getters, dispatch }, path) => {
|
|
|
|
const file = state.entries[path];
|
|
|
|
const currentActiveFile = getters.activeFile;
|
|
|
|
|
|
|
|
if (file.active) return;
|
|
|
|
|
|
|
|
if (currentActiveFile) {
|
|
|
|
commit(types.SET_FILE_ACTIVE, {
|
|
|
|
path: currentActiveFile.path,
|
|
|
|
active: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
commit(types.SET_FILE_ACTIVE, { path, active: true });
|
|
|
|
dispatch('scrollToTab');
|
|
|
|
};
|
|
|
|
|
2018-12-23 12:14:25 +05:30
|
|
|
export const getFileData = (
|
|
|
|
{ state, commit, dispatch },
|
|
|
|
{ path, makeFileActive = true, openFile = makeFileActive },
|
|
|
|
) => {
|
2018-05-09 12:01:36 +05:30
|
|
|
const file = state.entries[path];
|
2018-11-18 11:00:15 +05:30
|
|
|
|
|
|
|
if (file.raw || (file.tempFile && !file.prevPath)) return Promise.resolve();
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
commit(types.TOGGLE_LOADING, { entry: file });
|
2018-11-18 11:00:15 +05:30
|
|
|
|
|
|
|
const url = file.prevPath ? file.url.replace(file.path, file.prevPath) : file.url;
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
return service
|
2018-11-18 11:00:15 +05:30
|
|
|
.getFileData(`${gon.relative_url_root ? gon.relative_url_root : ''}${url.replace('/-/', '/')}`)
|
2018-11-08 19:23:39 +05:30
|
|
|
.then(({ data, headers }) => {
|
|
|
|
const normalizedHeaders = normalizeHeaders(headers);
|
|
|
|
setPageTitle(decodeURI(normalizedHeaders['PAGE-TITLE']));
|
2018-05-09 12:01:36 +05:30
|
|
|
|
2018-12-23 12:14:25 +05:30
|
|
|
if (data) commit(types.SET_FILE_DATA, { data, file });
|
|
|
|
if (openFile) commit(types.TOGGLE_FILE_OPEN, path);
|
2018-05-09 12:01:36 +05:30
|
|
|
if (makeFileActive) dispatch('setFileActive', path);
|
|
|
|
commit(types.TOGGLE_LOADING, { entry: file });
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
commit(types.TOGGLE_LOADING, { entry: file });
|
2018-11-08 19:23:39 +05:30
|
|
|
dispatch('setErrorMessage', {
|
2018-12-23 12:14:25 +05:30
|
|
|
text: __('An error occurred whilst loading the file.'),
|
2018-11-08 19:23:39 +05:30
|
|
|
action: payload =>
|
|
|
|
dispatch('getFileData', payload).then(() => dispatch('setErrorMessage', null)),
|
|
|
|
actionText: __('Please try again'),
|
|
|
|
actionPayload: { path, makeFileActive },
|
|
|
|
});
|
2018-05-09 12:01:36 +05:30
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
export const setFileMrChange = ({ commit }, { file, mrChange }) => {
|
2018-05-09 12:01:36 +05:30
|
|
|
commit(types.SET_FILE_MERGE_REQUEST_CHANGE, { file, mrChange });
|
|
|
|
};
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
export const getRawFileData = ({ state, commit, dispatch, getters }, { path }) => {
|
2018-05-09 12:01:36 +05:30
|
|
|
const file = state.entries[path];
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
service
|
|
|
|
.getRawFileData(file)
|
|
|
|
.then(raw => {
|
2018-11-18 11:00:15 +05:30
|
|
|
if (!(file.tempFile && !file.prevPath)) commit(types.SET_FILE_RAW_DATA, { file, raw });
|
2018-05-09 12:01:36 +05:30
|
|
|
if (file.mrChange && file.mrChange.new_file === false) {
|
2018-11-18 11:00:15 +05:30
|
|
|
const baseSha =
|
|
|
|
(getters.currentMergeRequest && getters.currentMergeRequest.baseCommitSha) || '';
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
service
|
|
|
|
.getBaseRawFileData(file, baseSha)
|
|
|
|
.then(baseRaw => {
|
|
|
|
commit(types.SET_FILE_BASE_RAW_DATA, {
|
|
|
|
file,
|
|
|
|
baseRaw,
|
|
|
|
});
|
|
|
|
resolve(raw);
|
|
|
|
})
|
|
|
|
.catch(e => {
|
|
|
|
reject(e);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
resolve(raw);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(() => {
|
2018-11-08 19:23:39 +05:30
|
|
|
dispatch('setErrorMessage', {
|
2018-12-23 12:14:25 +05:30
|
|
|
text: __('An error occurred whilst loading the file content.'),
|
2018-11-08 19:23:39 +05:30
|
|
|
action: payload =>
|
|
|
|
dispatch('getRawFileData', payload).then(() => dispatch('setErrorMessage', null)),
|
|
|
|
actionText: __('Please try again'),
|
2018-11-18 11:00:15 +05:30
|
|
|
actionPayload: { path },
|
2018-11-08 19:23:39 +05:30
|
|
|
});
|
2018-05-09 12:01:36 +05:30
|
|
|
reject();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
export const changeFileContent = ({ commit, dispatch, state }, { path, content }) => {
|
2018-05-09 12:01:36 +05:30
|
|
|
const file = state.entries[path];
|
|
|
|
commit(types.UPDATE_FILE_CONTENT, { path, content });
|
|
|
|
|
|
|
|
const indexOfChangedFile = state.changedFiles.findIndex(f => f.path === path);
|
|
|
|
|
|
|
|
if (file.changed && indexOfChangedFile === -1) {
|
|
|
|
commit(types.ADD_FILE_TO_CHANGED, path);
|
|
|
|
} else if (!file.changed && indexOfChangedFile !== -1) {
|
|
|
|
commit(types.REMOVE_FILE_FROM_CHANGED, path);
|
|
|
|
}
|
2018-10-15 14:42:47 +05:30
|
|
|
|
|
|
|
dispatch('burstUnusedSeal', {}, { root: true });
|
2018-05-09 12:01:36 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
export const setFileLanguage = ({ getters, commit }, { fileLanguage }) => {
|
|
|
|
if (getters.activeFile) {
|
|
|
|
commit(types.SET_FILE_LANGUAGE, { file: getters.activeFile, fileLanguage });
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const setFileEOL = ({ getters, commit }, { eol }) => {
|
|
|
|
if (getters.activeFile) {
|
|
|
|
commit(types.SET_FILE_EOL, { file: getters.activeFile, eol });
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const setEditorPosition = ({ getters, commit }, { editorRow, editorColumn }) => {
|
|
|
|
if (getters.activeFile) {
|
|
|
|
commit(types.SET_FILE_POSITION, {
|
|
|
|
file: getters.activeFile,
|
|
|
|
editorRow,
|
|
|
|
editorColumn,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
export const setFileViewMode = ({ commit }, { file, viewMode }) => {
|
2018-05-09 12:01:36 +05:30
|
|
|
commit(types.SET_FILE_VIEWMODE, { file, viewMode });
|
|
|
|
};
|
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
export const discardFileChanges = ({ dispatch, state, commit, getters }, path) => {
|
2018-05-09 12:01:36 +05:30
|
|
|
const file = state.entries[path];
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
if (file.deleted && file.parentPath) {
|
|
|
|
dispatch('restoreTree', file.parentPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file.movedPath) {
|
|
|
|
commit(types.DISCARD_FILE_CHANGES, file.movedPath);
|
|
|
|
commit(types.REMOVE_FILE_FROM_CHANGED, file.movedPath);
|
|
|
|
}
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
commit(types.DISCARD_FILE_CHANGES, path);
|
|
|
|
commit(types.REMOVE_FILE_FROM_CHANGED, path);
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
if (file.prevPath) {
|
|
|
|
dispatch('discardFileChanges', file.prevPath);
|
|
|
|
}
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
if (file.tempFile && file.opened) {
|
|
|
|
commit(types.TOGGLE_FILE_OPEN, path);
|
2018-10-15 14:42:47 +05:30
|
|
|
} else if (getters.activeFile && file.path === getters.activeFile.path) {
|
|
|
|
dispatch('updateDelayViewerUpdated', true)
|
|
|
|
.then(() => {
|
|
|
|
router.push(`/project${file.url}`);
|
|
|
|
})
|
|
|
|
.catch(e => {
|
|
|
|
throw e;
|
|
|
|
});
|
2018-05-09 12:01:36 +05:30
|
|
|
}
|
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
eventHub.$emit(`editor.update.model.new.content.${file.key}`, file.content);
|
|
|
|
eventHub.$emit(`editor.update.model.dispose.unstaged-${file.key}`, file.content);
|
2018-05-09 12:01:36 +05:30
|
|
|
};
|
|
|
|
|
2018-11-20 20:47:30 +05:30
|
|
|
export const stageChange = ({ commit, state, dispatch }, path) => {
|
2018-10-15 14:42:47 +05:30
|
|
|
const stagedFile = state.stagedFiles.find(f => f.path === path);
|
2018-11-20 20:47:30 +05:30
|
|
|
const openFile = state.openFiles.find(f => f.path === path);
|
2018-10-15 14:42:47 +05:30
|
|
|
|
|
|
|
commit(types.STAGE_CHANGE, path);
|
|
|
|
commit(types.SET_LAST_COMMIT_MSG, '');
|
|
|
|
|
|
|
|
if (stagedFile) {
|
|
|
|
eventHub.$emit(`editor.update.model.new.content.staged-${stagedFile.key}`, stagedFile.content);
|
2018-05-09 12:01:36 +05:30
|
|
|
}
|
2018-11-20 20:47:30 +05:30
|
|
|
|
|
|
|
if (openFile && openFile.active) {
|
|
|
|
const file = state.stagedFiles.find(f => f.path === path);
|
|
|
|
|
|
|
|
dispatch('openPendingTab', {
|
|
|
|
file,
|
|
|
|
keyPrefix: stageKeys.staged,
|
|
|
|
});
|
|
|
|
}
|
2018-10-15 14:42:47 +05:30
|
|
|
};
|
|
|
|
|
2018-11-20 20:47:30 +05:30
|
|
|
export const unstageChange = ({ commit, dispatch, state }, path) => {
|
|
|
|
const openFile = state.openFiles.find(f => f.path === path);
|
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
commit(types.UNSTAGE_CHANGE, path);
|
2018-11-20 20:47:30 +05:30
|
|
|
|
|
|
|
if (openFile && openFile.active) {
|
|
|
|
const file = state.changedFiles.find(f => f.path === path);
|
|
|
|
|
|
|
|
dispatch('openPendingTab', {
|
|
|
|
file,
|
|
|
|
keyPrefix: stageKeys.unstaged,
|
|
|
|
});
|
|
|
|
}
|
2018-10-15 14:42:47 +05:30
|
|
|
};
|
|
|
|
|
2018-11-20 20:47:30 +05:30
|
|
|
export const openPendingTab = ({ commit, getters, state }, { file, keyPrefix }) => {
|
2018-10-15 14:42:47 +05:30
|
|
|
if (getters.activeFile && getters.activeFile.key === `${keyPrefix}-${file.key}`) return false;
|
|
|
|
|
|
|
|
state.openFiles.forEach(f => eventHub.$emit(`editor.update.model.dispose.${f.key}`));
|
2018-05-09 12:01:36 +05:30
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
commit(types.ADD_PENDING_TAB, { file, keyPrefix });
|
2018-05-09 12:01:36 +05:30
|
|
|
|
|
|
|
router.push(`/project/${file.projectId}/tree/${state.currentBranchId}/`);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const removePendingTab = ({ commit }, file) => {
|
|
|
|
commit(types.REMOVE_PENDING_TAB, file);
|
|
|
|
|
|
|
|
eventHub.$emit(`editor.update.model.dispose.${file.key}`);
|
|
|
|
};
|