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

149 lines
3.2 KiB
JavaScript
Raw Normal View History

2018-05-09 12:01:36 +05:30
export const dataStructure = () => ({
id: '',
// Key will contain a mixture of ID and path
// it can also contain a prefix `pending-` for files opened in review mode
key: '',
type: '',
projectId: '',
branchId: '',
name: '',
url: '',
path: '',
tempFile: false,
tree: [],
loading: false,
opened: false,
active: false,
changed: false,
2018-10-15 14:42:47 +05:30
staged: false,
2018-05-09 12:01:36 +05:30
lastCommitPath: '',
2018-11-08 19:23:39 +05:30
lastCommitSha: '',
2018-05-09 12:01:36 +05:30
lastCommit: {
id: '',
url: '',
message: '',
updatedAt: '',
author: '',
},
blamePath: '',
commitsPath: '',
permalink: '',
rawPath: '',
binary: false,
html: '',
raw: '',
content: '',
parentTreeUrl: '',
renderError: false,
base64: false,
editorRow: 1,
editorColumn: 1,
fileLanguage: '',
eol: '',
2018-11-08 19:23:39 +05:30
viewMode: 'editor',
2018-05-09 12:01:36 +05:30
previewMode: null,
size: 0,
2018-10-15 14:42:47 +05:30
parentPath: null,
lastOpenedAt: 0,
mrChange: null,
2018-05-09 12:01:36 +05:30
});
export const decorateData = entity => {
const {
id,
projectId,
branchId,
type,
url,
name,
path,
renderError,
content = '',
tempFile = false,
active = false,
opened = false,
changed = false,
parentTreeUrl = '',
base64 = false,
previewMode,
file_lock,
html,
2018-10-15 14:42:47 +05:30
parentPath = '',
2018-05-09 12:01:36 +05:30
} = entity;
return {
...dataStructure(),
id,
projectId,
branchId,
key: `${name}-${type}-${id}`,
type,
name,
url,
path,
tempFile,
opened,
active,
parentTreeUrl,
changed,
renderError,
content,
base64,
previewMode,
file_lock,
html,
2018-10-15 14:42:47 +05:30
parentPath,
2018-05-09 12:01:36 +05:30
};
};
export const findEntry = (tree, type, name, prop = 'name') =>
tree.find(f => f.type === type && f[prop] === name);
export const findIndexOfFile = (state, file) => state.findIndex(f => f.path === file.path);
export const setPageTitle = title => {
document.title = title;
};
2018-11-08 19:23:39 +05:30
export const createCommitPayload = ({ branch, getters, newBranch, state, rootState }) => ({
2018-05-09 12:01:36 +05:30
branch,
2018-11-08 19:23:39 +05:30
commit_message: state.commitMessage || getters.preBuiltCommitMessage,
2018-10-15 14:42:47 +05:30
actions: rootState.stagedFiles.map(f => ({
2018-05-09 12:01:36 +05:30
action: f.tempFile ? 'create' : 'update',
file_path: f.path,
content: f.content,
encoding: f.base64 ? 'base64' : 'text',
2018-11-08 19:23:39 +05:30
last_commit_id: newBranch ? undefined : f.lastCommitSha,
2018-05-09 12:01:36 +05:30
})),
start_branch: newBranch ? rootState.currentBranchId : undefined,
});
export const createNewMergeRequestUrl = (projectUrl, source, target) =>
`${projectUrl}/merge_requests/new?merge_request[source_branch]=${source}&merge_request[target_branch]=${target}`;
const sortTreesByTypeAndName = (a, b) => {
if (a.type === 'tree' && b.type === 'blob') {
return -1;
} else if (a.type === 'blob' && b.type === 'tree') {
return 1;
}
2018-10-15 14:42:47 +05:30
if (a.name < b.name) return -1;
if (a.name > b.name) return 1;
2018-05-09 12:01:36 +05:30
return 0;
};
export const sortTree = sortedTree =>
sortedTree
.map(entity =>
Object.assign(entity, {
tree: entity.tree.length ? sortTree(entity.tree) : [],
}),
)
.sort(sortTreesByTypeAndName);
2018-10-15 14:42:47 +05:30
export const filePathMatches = (f, path) =>
f.path.replace(new RegExp(`${f.name}$`), '').indexOf(`${path}/`) === 0;
export const getChangesCountForFiles = (files, path) =>
files.filter(f => filePathMatches(f, path)).length;