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

250 lines
7.1 KiB
JavaScript
Raw Normal View History

2018-11-20 20:47:30 +05:30
import Vue from 'vue';
2018-05-09 12:01:36 +05:30
import * as types from './mutation_types';
2021-06-08 01:23:25 +05:30
import alertMutations from './mutations/alert';
2021-03-11 19:13:27 +05:30
import branchMutations from './mutations/branch';
2018-05-09 12:01:36 +05:30
import fileMutations from './mutations/file';
2021-03-11 19:13:27 +05:30
import mergeRequestMutation from './mutations/merge_request';
import projectMutations from './mutations/project';
2018-05-09 12:01:36 +05:30
import treeMutations from './mutations/tree';
2019-12-21 20:55:43 +05:30
import {
sortTree,
swapInParentTreeWithSorting,
updateFileCollections,
removeFromParentTree,
pathsAreEqual,
} from './utils';
2018-05-09 12:01:36 +05:30
export default {
[types.SET_INITIAL_DATA](state, data) {
Object.assign(state, data);
},
[types.TOGGLE_LOADING](state, { entry, forceValue = undefined }) {
if (entry.path) {
Object.assign(state.entries[entry.path], {
loading: forceValue !== undefined ? forceValue : !state.entries[entry.path].loading,
});
} else {
Object.assign(entry, {
loading: forceValue !== undefined ? forceValue : !entry.loading,
});
}
},
[types.SET_RESIZING_STATUS](state, resizing) {
Object.assign(state, {
panelResizing: resizing,
});
},
[types.SET_LAST_COMMIT_MSG](state, lastCommitMsg) {
Object.assign(state, {
lastCommitMsg,
});
},
2018-10-15 14:42:47 +05:30
[types.CLEAR_STAGED_CHANGES](state) {
Object.assign(state, {
stagedFiles: [],
});
},
2018-05-09 12:01:36 +05:30
[types.SET_ENTRIES](state, entries) {
Object.assign(state, {
entries,
});
},
2020-11-24 15:15:51 +05:30
[types.CREATE_TMP_ENTRY](state, { data }) {
2018-05-09 12:01:36 +05:30
Object.keys(data.entries).reduce((acc, key) => {
const entry = data.entries[key];
const foundEntry = state.entries[key];
2019-12-21 20:55:43 +05:30
// NOTE: We can't clone `entry` in any of the below assignments because
// we need `state.entries` and the `entry.tree` to reference the same object.
2020-06-23 00:09:42 +05:30
if (!foundEntry || foundEntry.deleted) {
2018-05-09 12:01:36 +05:30
Object.assign(state.entries, {
[key]: entry,
});
} else {
const tree = entry.tree.filter(
2021-03-08 18:12:59 +05:30
(f) => foundEntry.tree.find((e) => e.path === f.path) === undefined,
2018-05-09 12:01:36 +05:30
);
Object.assign(foundEntry, {
2018-10-15 14:42:47 +05:30
tree: sortTree(foundEntry.tree.concat(tree)),
2018-05-09 12:01:36 +05:30
});
}
return acc.concat(key);
}, []);
2020-11-24 15:15:51 +05:30
const currentTree = state.trees[`${state.currentProjectId}/${state.currentBranchId}`];
2021-03-08 18:12:59 +05:30
const foundEntry = currentTree.tree.find((e) => e.path === data.treeList[0].path);
2018-05-09 12:01:36 +05:30
if (!foundEntry) {
2020-11-24 15:15:51 +05:30
Object.assign(currentTree, {
tree: sortTree(currentTree.tree.concat(data.treeList)),
2018-05-09 12:01:36 +05:30
});
}
},
2018-10-15 14:42:47 +05:30
[types.UPDATE_TEMP_FLAG](state, { path, tempFile }) {
Object.assign(state.entries[path], {
tempFile,
changed: tempFile,
});
},
2018-05-09 12:01:36 +05:30
[types.UPDATE_VIEWER](state, viewer) {
Object.assign(state, {
viewer,
});
},
[types.UPDATE_DELAY_VIEWER_CHANGE](state, delayViewerUpdated) {
Object.assign(state, {
delayViewerUpdated,
});
},
2018-10-15 14:42:47 +05:30
[types.UPDATE_ACTIVITY_BAR_VIEW](state, currentActivityView) {
Object.assign(state, {
currentActivityView,
});
},
[types.SET_EMPTY_STATE_SVGS](
state,
2018-11-18 11:00:15 +05:30
{
emptyStateSvgPath,
noChangesStateSvgPath,
committedStateSvgPath,
pipelinesEmptyStateSvgPath,
promotionSvgPath,
},
2018-10-15 14:42:47 +05:30
) {
Object.assign(state, {
emptyStateSvgPath,
noChangesStateSvgPath,
committedStateSvgPath,
2018-11-08 19:23:39 +05:30
pipelinesEmptyStateSvgPath,
2018-11-18 11:00:15 +05:30
promotionSvgPath,
2018-10-15 14:42:47 +05:30
});
},
[types.TOGGLE_FILE_FINDER](state, fileFindVisible) {
Object.assign(state, {
fileFindVisible,
});
},
[types.UPDATE_FILE_AFTER_COMMIT](state, { file, lastCommit }) {
2021-03-08 18:12:59 +05:30
const changedFile = state.changedFiles.find((f) => f.path === file.path);
2018-11-18 11:00:15 +05:30
const { prevPath } = file;
2018-10-15 14:42:47 +05:30
Object.assign(state.entries[file.path], {
raw: file.content,
2019-09-04 21:01:54 +05:30
changed: Boolean(changedFile),
2018-10-15 14:42:47 +05:30
staged: false,
2018-11-20 20:47:30 +05:30
lastCommitSha: lastCommit.commit.id,
2019-12-21 20:55:43 +05:30
prevId: undefined,
prevPath: undefined,
prevName: undefined,
prevKey: undefined,
prevParentPath: undefined,
2018-10-15 14:42:47 +05:30
});
2018-11-18 11:00:15 +05:30
if (prevPath) {
// Update URLs after file has moved
const regex = new RegExp(`${prevPath}$`);
Object.assign(state.entries[file.path], {
rawPath: file.rawPath.replace(regex, file.path),
});
}
2018-10-15 14:42:47 +05:30
},
2018-11-08 19:23:39 +05:30
[types.SET_LINKS](state, links) {
Object.assign(state, { links });
},
[types.CLEAR_PROJECTS](state) {
Object.assign(state, { projects: {}, trees: {} });
},
[types.RESET_OPEN_FILES](state) {
Object.assign(state, { openFiles: [] });
},
[types.SET_ERROR_MESSAGE](state, errorMessage) {
Object.assign(state, { errorMessage });
},
2018-11-18 11:00:15 +05:30
[types.DELETE_ENTRY](state, path) {
const entry = state.entries[path];
2018-11-20 20:47:30 +05:30
const { tempFile = false } = entry;
2018-11-18 11:00:15 +05:30
const parent = entry.parentPath
? state.entries[entry.parentPath]
: state.trees[`${state.currentProjectId}/${state.currentBranchId}`];
entry.deleted = true;
2019-12-21 20:55:43 +05:30
if (parent) {
2021-03-08 18:12:59 +05:30
parent.tree = parent.tree.filter((f) => f.path !== entry.path);
2019-12-21 20:55:43 +05:30
}
2018-11-18 11:00:15 +05:30
if (entry.type === 'blob') {
2018-11-20 20:47:30 +05:30
if (tempFile) {
2020-04-25 10:58:03 +05:30
// Since we only support one list of file changes, it's safe to just remove from both
// changed and staged. Otherwise, we'd need to somehow evaluate the difference between
// changed and HEAD.
// https://gitlab.com/gitlab-org/create-stage/-/issues/12669
2021-03-08 18:12:59 +05:30
state.changedFiles = state.changedFiles.filter((f) => f.path !== path);
state.stagedFiles = state.stagedFiles.filter((f) => f.path !== path);
2018-11-20 20:47:30 +05:30
} else {
state.changedFiles = state.changedFiles.concat(entry);
}
2018-11-18 11:00:15 +05:30
}
},
2019-12-21 20:55:43 +05:30
[types.RENAME_ENTRY](state, { path, name, parentPath }) {
const oldEntry = state.entries[path];
const newPath = parentPath ? `${parentPath}/${name}` : name;
const isRevert = newPath === oldEntry.prevPath;
const newKey = oldEntry.key.replace(new RegExp(oldEntry.path, 'g'), newPath);
const baseProps = {
2018-11-18 11:00:15 +05:30
...oldEntry,
2019-12-21 20:55:43 +05:30
name,
2018-11-18 11:00:15 +05:30
id: newPath,
path: newPath,
2019-12-21 20:55:43 +05:30
key: newKey,
parentPath: parentPath || '',
};
2019-07-07 11:18:12 +05:30
2019-12-21 20:55:43 +05:30
const prevProps =
oldEntry.tempFile || isRevert
? {
prevId: undefined,
prevPath: undefined,
prevName: undefined,
prevKey: undefined,
prevParentPath: undefined,
}
: {
prevId: oldEntry.prevId || oldEntry.id,
prevPath: oldEntry.prevPath || oldEntry.path,
prevName: oldEntry.prevName || oldEntry.name,
prevKey: oldEntry.prevKey || oldEntry.key,
prevParentPath: oldEntry.prevParentPath || oldEntry.parentPath,
};
2018-11-18 11:00:15 +05:30
2019-12-21 20:55:43 +05:30
Vue.set(state.entries, newPath, {
...baseProps,
...prevProps,
});
2018-11-18 11:00:15 +05:30
2019-12-21 20:55:43 +05:30
if (pathsAreEqual(oldEntry.parentPath, parentPath)) {
swapInParentTreeWithSorting(state, oldEntry.key, newPath, parentPath);
} else {
removeFromParentTree(state, oldEntry.key, oldEntry.parentPath);
swapInParentTreeWithSorting(state, oldEntry.key, newPath, parentPath);
2018-11-18 11:00:15 +05:30
}
2018-11-20 20:47:30 +05:30
2019-12-21 20:55:43 +05:30
if (oldEntry.type === 'blob') {
updateFileCollections(state, oldEntry.key, newPath);
2018-11-20 20:47:30 +05:30
}
2019-12-21 20:55:43 +05:30
Vue.delete(state.entries, oldEntry.path);
2018-11-18 11:00:15 +05:30
},
2019-07-07 11:18:12 +05:30
2018-05-09 12:01:36 +05:30
...projectMutations,
...mergeRequestMutation,
...fileMutations,
...treeMutations,
...branchMutations,
2021-06-08 01:23:25 +05:30
...alertMutations,
2018-05-09 12:01:36 +05:30
};