Commit 65e8edae authored by Luke "Jared" Bennett's avatar Luke "Jared" Bennett

Tidy up repo_helper

parent b1850676
...@@ -4,6 +4,8 @@ import Store from './repo_store'; ...@@ -4,6 +4,8 @@ import Store from './repo_store';
import '../flash'; import '../flash';
const RepoHelper = { const RepoHelper = {
key: '',
isTree(data) { isTree(data) {
return Object.hasOwnProperty.call(data, 'blobs'); return Object.hasOwnProperty.call(data, 'blobs');
}, },
...@@ -14,56 +16,51 @@ const RepoHelper = { ...@@ -14,56 +16,51 @@ const RepoHelper = {
: Date, : Date,
getLanguageForFile(file, langs) { getLanguageForFile(file, langs) {
const ext = `.${file.name.split('.').pop()}`; const ext = file.name.split('.').pop();
let foundLang; const foundLang = this.findLanguage(ext, langs);
if (langs.some((l) => {
if (l.extensions && l.extensions.indexOf(ext) > -1) { return foundLang ? foundLang.id : 'plain';
foundLang = l; },
return true;
} findLanguage(ext, langs) {
return false; langs.find(lang => lang.extensions && lang.extensions.indexOf(`.${ext}`) > -1);
})) {
return foundLang.id;
}
return 'plain';
}, },
blobURLtoParent(url) { blobURLtoParent(url) {
const split = url.split('/'); const urlArray = url.split('/');
split.pop(); urlArray.pop();
const blobIndex = split.indexOf('blob'); const blobIndex = urlArray.indexOf('blob');
if (blobIndex > -1) {
split[blobIndex] = 'tree'; if (blobIndex > -1) urlArray[blobIndex] = 'tree';
}
return split.join('/'); return urlArray.join('/');
}, },
insertNewFilesIntoParentDir(inDirectory, oldList, newList) { insertNewFilesIntoParentDir(inDirectory, oldList, newList) {
let indexOfFile; if (!inDirectory) return newList;
if (!inDirectory) {
return newList; const indexOfFile = oldList.findIndex(file => file.url === inDirectory.url);
}
oldList.find((file, i) => { if (!indexOfFile) return newList;
if (file.url === inDirectory.url) {
indexOfFile = i + 1; return this.mergeNewListToOldList(newList, oldList, inDirectory, indexOfFile);
return true; },
}
return false; mergeNewListToOldList(newList, oldList, inDirectory, indexOfFile) {
newList.forEach((newFile) => {
const file = newFile;
file.level = inDirectory.level + 1;
oldList.splice(indexOfFile, 0, file);
}); });
if (indexOfFile) {
// insert new list into old list return oldList;
newList.forEach((newFile) => {
const file = newFile;
file.level = inDirectory.level + 1;
oldList.splice(indexOfFile, 0, file);
});
return oldList;
}
return newList;
}, },
resetBinaryTypes() { resetBinaryTypes() {
Object.keys(Store.binaryTypes).forEach((typeKey) => { const binaryTypeKeys = Object.keys(Store.binaryTypes);
binaryTypeKeys.forEach((typeKey) => {
Store.binaryTypes[typeKey] = false; Store.binaryTypes[typeKey] = false;
}); });
}, },
...@@ -74,38 +71,45 @@ const RepoHelper = { ...@@ -74,38 +71,45 @@ const RepoHelper = {
}, },
setActiveFile(file) { setActiveFile(file) {
// don't load the file that is already loaded if (this.isActiveFile(file)) return;
if (file.url === Store.activeFile.url) return;
Store.openedFiles = Store.openedFiles.map((openedFile, i) => { Store.openedFiles = Store.openedFiles.map((openedFile, i) => {
const activeFile = openedFile; const activeFile = openedFile;
activeFile.active = file.url === activeFile.url; activeFile.active = file.url === activeFile.url;
if (activeFile.active) { if (activeFile.active) {
Store.activeFile = activeFile; Store.activeFile = activeFile;
Store.activeFileIndex = i; Store.activeFileIndex = i;
} }
return activeFile; return activeFile;
}); });
// reset the active file raw this.setActiveToRaw();
Store.activeFile.raw = false;
// can't get vue to listen to raw for some reason so this for now.
Store.activeFileLabel = 'Raw';
if (file.binary) { if (file.binary) {
Store.blobRaw = file.base64; Store.blobRaw = file.base64;
} else { } else {
Store.blobRaw = file.plain; Store.blobRaw = file.plain;
} }
if (!file.loading) {
this.toURL(file.url); if (!file.loading) this.toURL(file.url);
}
Store.binary = file.binary; Store.binary = file.binary;
}, },
setActiveToRaw() {
Store.activeFile.raw = false;
// can't get vue to listen to raw for some reason so this for now.
Store.activeFileLabel = 'Raw';
},
isActiveFile(file) {
return file && file.url === Store.activeFile.url;
},
removeFromOpenedFiles(file) { removeFromOpenedFiles(file) {
if (file.type === 'tree') return; if (file.type === 'tree') return;
Store.openedFiles = Store.openedFiles.filter(openedFile => openedFile.url !== file.url); Store.openedFiles = Store.openedFiles.filter(openedFile => openedFile.url !== file.url);
}, },
...@@ -114,20 +118,21 @@ const RepoHelper = { ...@@ -114,20 +118,21 @@ const RepoHelper = {
const openedFilesAlreadyExists = Store.openedFiles const openedFilesAlreadyExists = Store.openedFiles
.some(openedFile => openedFile.url === openFile.url); .some(openedFile => openedFile.url === openFile.url);
if (!openedFilesAlreadyExists) {
openFile.changed = false; if (openedFilesAlreadyExists) return;
Store.openedFiles.push(openFile);
} openFile.changed = false;
Store.openedFiles.push(openFile);
}, },
/* eslint-disable no-param-reassign */
setDirectoryOpen(tree) { setDirectoryOpen(tree) {
if (tree) { if (!tree) return;
tree.opened = true;
tree.icon = 'fa-folder-open'; /* eslint-disable no-param-reassign */
} tree.opened = true;
tree.icon = 'fa-folder-open';
/* eslint-enable no-param-reassign */
}, },
/* eslint-enable no-param-reassign */
getRawURLFromBlobURL(url) { getRawURLFromBlobURL(url) {
return url.replace('blob', 'raw'); return url.replace('blob', 'raw');
...@@ -152,72 +157,70 @@ const RepoHelper = { ...@@ -152,72 +157,70 @@ const RepoHelper = {
setActiveFileContents(contents) { setActiveFileContents(contents) {
if (!Store.editMode) return; if (!Store.editMode) return;
Store.activeFile.newContent = contents; Store.activeFile.newContent = contents;
Store.activeFile.changed = Store.activeFile.plain !== Store.activeFile.newContent; Store.activeFile.changed = Store.activeFile.plain !== Store.activeFile.newContent;
Store.openedFiles[Store.activeFileIndex].changed = Store.activeFile.changed; Store.openedFiles[Store.activeFileIndex].changed = Store.activeFile.changed;
}, },
toggleFakeTab(loading, file) { toggleFakeTab(loading, file) {
if (loading) { if (loading) return this.addPlaceholderFile();
const randomURL = this.Time.now(); return this.removeFromOpenedFiles(file);
const newFakeFile = { },
active: false,
binary: true, addPlaceholderFile() {
type: 'blob', const randomURL = this.Time.now();
loading: true, const newFakeFile = {
mime_type: 'loading', active: false,
name: 'loading', binary: true,
url: randomURL, type: 'blob',
}; loading: true,
Store.openedFiles.push(newFakeFile); mime_type: 'loading',
return newFakeFile; name: 'loading',
} url: randomURL,
this.removeFromOpenedFiles(file); };
return null;
Store.openedFiles.push(newFakeFile);
return newFakeFile;
}, },
setLoading(loading, file) { setLoading(loading, file) {
if (Service.url.indexOf('tree') > -1) { if (Service.url.indexOf('blob') > -1) {
Store.loading.tree = loading;
} else if (Service.url.indexOf('blob') > -1) {
Store.loading.blob = loading; Store.loading.blob = loading;
return this.toggleFakeTab(loading, file); return this.toggleFakeTab(loading, file);
} }
if (Service.url.indexOf('tree') > -1) Store.loading.tree = loading;
return undefined; return undefined;
}, },
// may be tree or file.
getContent(treeOrFile) { getContent(treeOrFile) {
let file = treeOrFile; let file = treeOrFile;
// don't load the same active file. That's silly.
// if(file && file.url === this.activeFile.url) return;
const loadingData = this.setLoading(true); const loadingData = this.setLoading(true);
Service.getContent() Service.getContent()
.then((response) => { .then((response) => {
const data = response.data; const data = response.data;
this.setLoading(false, loadingData); this.setLoading(false, loadingData);
Store.isTree = this.isTree(data); Store.isTree = this.isTree(data);
if (!Store.isTree) { if (!Store.isTree) {
if (!file) { if (!file) file = data;
file = data;
}
// it's a blob
Store.binary = data.binary; Store.binary = data.binary;
if (data.binary) { if (data.binary) {
Store.binaryMimeType = data.mime_type; Store.binaryMimeType = data.mime_type;
this.setBinaryDataAsBase64( const rawUrl = this.getRawURLFromBlobURL(file.url);
this.getRawURLFromBlobURL(file.url), this.setBinaryDataAsBase64(rawUrl, data);
data,
);
data.binary = true; data.binary = true;
} else { } else {
Store.blobRaw = data.plain; Store.blobRaw = data.plain;
data.binary = false; data.binary = false;
} }
if (!file.url) {
file.url = location.pathname; if (!file.url) file.url = location.pathname;
}
data.url = file.url; data.url = file.url;
data.newContent = ''; data.newContent = '';
this.addToOpenedFiles(data); this.addToOpenedFiles(data);
...@@ -251,12 +254,9 @@ const RepoHelper = { ...@@ -251,12 +254,9 @@ const RepoHelper = {
removeChildFilesOfTree(tree) { removeChildFilesOfTree(tree) {
let foundTree = false; let foundTree = false;
Store.files = Store.files.filter((file) => { Store.files = Store.files.filter((file) => {
if (file.url === tree.url) { if (file.url === tree.url) foundTree = true;
foundTree = true;
} if (foundTree) return file.level <= tree.level;
if (foundTree) {
return file.level <= tree.level;
}
return true; return true;
}); });
...@@ -316,8 +316,6 @@ const RepoHelper = { ...@@ -316,8 +316,6 @@ const RepoHelper = {
return this.Time.now().toFixed(3); return this.Time.now().toFixed(3);
}, },
key: '',
getStateKey() { getStateKey() {
return this.key; return this.key;
}, },
...@@ -328,7 +326,9 @@ const RepoHelper = { ...@@ -328,7 +326,9 @@ const RepoHelper = {
toURL(url) { toURL(url) {
const history = window.history; const history = window.history;
this.key = this.genKey(); this.key = this.genKey();
history.pushState({ key: this.key }, '', url); history.pushState({ key: this.key }, '', url);
}, },
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment