debian-mirror-gitlab/app/assets/javascripts/ide/lib/files.js

119 lines
2.6 KiB
JavaScript
Raw Normal View History

2019-07-07 11:18:12 +05:30
import { viewerInformationForPath } from '~/vue_shared/components/content_viewer/lib/viewer_utils';
2020-01-01 13:55:28 +05:30
import { decorateData, sortTree } from '../stores/utils';
2019-07-07 11:18:12 +05:30
export const splitParent = path => {
const idx = path.lastIndexOf('/');
return {
parent: idx >= 0 ? path.substring(0, idx) : null,
name: idx >= 0 ? path.substring(idx + 1) : path,
};
};
/**
* Create file objects from a list of file paths.
*/
export const decorateFiles = ({
data,
projectId,
branchId,
tempFile = false,
content = '',
base64 = false,
2019-07-31 22:56:46 +05:30
binary = false,
rawPath = '',
2019-07-07 11:18:12 +05:30
}) => {
const treeList = [];
const entries = {};
// These mutable variable references end up being exported and used by `createTempEntry`
let file;
let parentPath;
const insertParent = path => {
if (!path) {
return null;
} else if (entries[path]) {
return entries[path];
}
const { parent, name } = splitParent(path);
const parentFolder = parent && insertParent(parent);
parentPath = parentFolder && parentFolder.path;
const tree = decorateData({
projectId,
branchId,
id: path,
name,
path,
2020-03-09 13:42:32 +05:30
url: `/${projectId}/tree/${branchId}/-/${path}/`,
2019-07-07 11:18:12 +05:30
type: 'tree',
parentTreeUrl: parentFolder ? parentFolder.url : `/${projectId}/tree/${branchId}/`,
tempFile,
changed: tempFile,
opened: tempFile,
parentPath,
});
Object.assign(entries, {
[path]: tree,
});
if (parentFolder) {
parentFolder.tree.push(tree);
} else {
treeList.push(tree);
}
return tree;
};
data.forEach(path => {
const { parent, name } = splitParent(path);
const fileFolder = parent && insertParent(parent);
if (name) {
2019-09-30 21:07:59 +05:30
const previewMode = viewerInformationForPath(name);
2019-07-07 11:18:12 +05:30
parentPath = fileFolder && fileFolder.path;
file = decorateData({
projectId,
branchId,
id: path,
name,
path,
2020-03-09 13:42:32 +05:30
url: `/${projectId}/blob/${branchId}/-/${path}`,
2019-07-07 11:18:12 +05:30
type: 'blob',
parentTreeUrl: fileFolder ? fileFolder.url : `/${projectId}/blob/${branchId}`,
tempFile,
changed: tempFile,
content,
base64,
2019-09-30 21:07:59 +05:30
binary: (previewMode && previewMode.binary) || binary,
2019-07-31 22:56:46 +05:30
rawPath,
2019-09-30 21:07:59 +05:30
previewMode,
2019-07-07 11:18:12 +05:30
parentPath,
});
Object.assign(entries, {
[path]: file,
});
if (fileFolder) {
fileFolder.tree.push(file);
} else {
treeList.push(file);
}
}
});
return {
entries,
treeList: sortTree(treeList),
file,
parentPath,
};
};