Move repo_helper methods to related classes

This commit is contained in:
Luke "Jared" Bennett 2017-07-20 15:43:00 +01:00
parent 65e8edae6e
commit 78c4a27566
No known key found for this signature in database
GPG key ID: 402ED51FB5D306C2
7 changed files with 161 additions and 158 deletions

View file

@ -16,7 +16,7 @@ export default class RepoEditor {
} }
onMonacoEditorKeysPressed() { onMonacoEditorKeysPressed() {
Helper.setActiveFileContents(this.monacoEditor.getValue()); Store.setActiveFileContents(this.monacoEditor.getValue());
} }
initMonaco() { initMonaco() {
@ -110,7 +110,7 @@ export default class RepoEditor {
monaco.editor.createModel( monaco.editor.createModel(
this.blobRaw, this.blobRaw,
Helper Helper
.getLanguageForFile( .getLanguageIDForFile(
this.activeFile, this.activeFile,
monaco.languages.getLanguages(), monaco.languages.getLanguages(),
), ),

View file

@ -52,9 +52,7 @@ export default class RepoFileButtons {
}, },
methods: { methods: {
rawPreviewToggle() { rawPreviewToggle: Store.toggleRawPreview,
Helper.setCurrentFileRawOrPreview();
},
}, },
}); });
} }

View file

@ -15,7 +15,7 @@ const RepoHelper = {
? window.performance ? window.performance
: Date, : Date,
getLanguageForFile(file, langs) { getLanguageIDForFile(file, langs) {
const ext = file.name.split('.').pop(); const ext = file.name.split('.').pop();
const foundLang = this.findLanguage(ext, langs); const foundLang = this.findLanguage(ext, langs);
@ -26,105 +26,6 @@ const RepoHelper = {
langs.find(lang => lang.extensions && lang.extensions.indexOf(`.${ext}`) > -1); langs.find(lang => lang.extensions && lang.extensions.indexOf(`.${ext}`) > -1);
}, },
blobURLtoParent(url) {
const urlArray = url.split('/');
urlArray.pop();
const blobIndex = urlArray.indexOf('blob');
if (blobIndex > -1) urlArray[blobIndex] = 'tree';
return urlArray.join('/');
},
insertNewFilesIntoParentDir(inDirectory, oldList, newList) {
if (!inDirectory) return newList;
const indexOfFile = oldList.findIndex(file => file.url === inDirectory.url);
if (!indexOfFile) return newList;
return this.mergeNewListToOldList(newList, oldList, inDirectory, indexOfFile);
},
mergeNewListToOldList(newList, oldList, inDirectory, indexOfFile) {
newList.forEach((newFile) => {
const file = newFile;
file.level = inDirectory.level + 1;
oldList.splice(indexOfFile, 0, file);
});
return oldList;
},
resetBinaryTypes() {
const binaryTypeKeys = Object.keys(Store.binaryTypes);
binaryTypeKeys.forEach((typeKey) => {
Store.binaryTypes[typeKey] = false;
});
},
setCurrentFileRawOrPreview() {
Store.activeFile.raw = !Store.activeFile.raw;
Store.activeFileLabel = Store.activeFile.raw ? 'Preview' : 'Raw';
},
setActiveFile(file) {
if (this.isActiveFile(file)) return;
Store.openedFiles = Store.openedFiles.map((openedFile, i) => {
const activeFile = openedFile;
activeFile.active = file.url === activeFile.url;
if (activeFile.active) {
Store.activeFile = activeFile;
Store.activeFileIndex = i;
}
return activeFile;
});
this.setActiveToRaw();
if (file.binary) {
Store.blobRaw = file.base64;
} else {
Store.blobRaw = file.plain;
}
if (!file.loading) this.toURL(file.url);
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) {
if (file.type === 'tree') return;
Store.openedFiles = Store.openedFiles.filter(openedFile => openedFile.url !== file.url);
},
addToOpenedFiles(file) {
const openFile = file;
const openedFilesAlreadyExists = Store.openedFiles
.some(openedFile => openedFile.url === openFile.url);
if (openedFilesAlreadyExists) return;
openFile.changed = false;
Store.openedFiles.push(openFile);
},
setDirectoryOpen(tree) { setDirectoryOpen(tree) {
if (!tree) return; if (!tree) return;
@ -155,34 +56,9 @@ const RepoHelper = {
.catch(this.loadingError); .catch(this.loadingError);
}, },
setActiveFileContents(contents) {
if (!Store.editMode) return;
Store.activeFile.newContent = contents;
Store.activeFile.changed = Store.activeFile.plain !== Store.activeFile.newContent;
Store.openedFiles[Store.activeFileIndex].changed = Store.activeFile.changed;
},
toggleFakeTab(loading, file) { toggleFakeTab(loading, file) {
if (loading) return this.addPlaceholderFile(); if (loading) return Store.addPlaceholderFile();
return this.removeFromOpenedFiles(file); return Store.removeFromOpenedFiles(file);
},
addPlaceholderFile() {
const randomURL = this.Time.now();
const newFakeFile = {
active: false,
binary: true,
type: 'blob',
loading: true,
mime_type: 'loading',
name: 'loading',
url: randomURL,
};
Store.openedFiles.push(newFakeFile);
return newFakeFile;
}, },
setLoading(loading, file) { setLoading(loading, file) {
@ -196,6 +72,27 @@ const RepoHelper = {
return undefined; return undefined;
}, },
getNewMergedList(inDirectory, currentList, newList) {
if (!inDirectory) return newList;
const indexOfFile = currentList.findIndex(file => file.url === inDirectory.url);
if (!indexOfFile) return newList;
return this.mergeNewListToOldList(newList, currentList, inDirectory, indexOfFile);
},
mergeNewListToOldList(newList, oldList, inDirectory, indexOfFile) {
newList.forEach((newFile) => {
const file = newFile;
file.level = inDirectory.level + 1;
oldList.splice(indexOfFile, 0, file);
});
return oldList;
},
getContent(treeOrFile) { getContent(treeOrFile) {
let file = treeOrFile; let file = treeOrFile;
const loadingData = this.setLoading(true); const loadingData = this.setLoading(true);
@ -223,12 +120,13 @@ const RepoHelper = {
data.url = file.url; data.url = file.url;
data.newContent = ''; data.newContent = '';
this.addToOpenedFiles(data);
this.setActiveFile(data); Store.addToOpenedFiles(data);
Store.setActiveFiles(data);
// if the file tree is empty // if the file tree is empty
if (Store.files.length === 0) { if (Store.files.length === 0) {
const parentURL = this.blobURLtoParent(Service.url); const parentURL = Service.blobURLtoParentTree(Service.url);
Service.url = parentURL; Service.url = parentURL;
this.getContent(); this.getContent();
} }
@ -236,8 +134,8 @@ const RepoHelper = {
// it's a tree // it's a tree
this.setDirectoryOpen(file); this.setDirectoryOpen(file);
const newDirectory = this.dataToListOfFiles(data); const newDirectory = this.dataToListOfFiles(data);
Store.files = this.insertNewFilesIntoParentDir(file, Store.files, newDirectory); Store.addFilesToDirectory(file, Store.files, newDirectory);
Store.prevURL = this.blobURLtoParent(Service.url); Store.prevURL = Service.blobURLtoParentTree(Service.url);
} }
}) })
.catch(() => { .catch(() => {
@ -250,21 +148,6 @@ const RepoHelper = {
return `fa-${icon}`; return `fa-${icon}`;
}, },
/* eslint-disable no-param-reassign */
removeChildFilesOfTree(tree) {
let foundTree = false;
Store.files = Store.files.filter((file) => {
if (file.url === tree.url) foundTree = true;
if (foundTree) return file.level <= tree.level;
return true;
});
tree.opened = false;
tree.icon = 'fa-folder';
},
/* eslint-enable no-param-reassign */
serializeBlob(blob) { serializeBlob(blob) {
const simpleBlob = this.serializeRepoEntity('blob', blob); const simpleBlob = this.serializeRepoEntity('blob', blob);
simpleBlob.lastCommitMessage = blob.last_commit.message; simpleBlob.lastCommitMessage = blob.last_commit.message;

View file

@ -43,6 +43,16 @@ const RepoService = {
bufferToBase64(data) { bufferToBase64(data) {
return new Buffer(data, 'binary').toString('base64'); return new Buffer(data, 'binary').toString('base64');
}, },
blobURLtoParentTree(url) {
const urlArray = url.split('/');
urlArray.pop();
const blobIndex = urlArray.indexOf('blob');
if (blobIndex > -1) urlArray[blobIndex] = 'tree';
return urlArray.join('/');
},
}; };
export default RepoService; export default RepoService;

View file

@ -44,7 +44,7 @@ export default class RepoSidebar {
let url = ''; let url = '';
if (typeof file === 'object') { if (typeof file === 'object') {
if (file.type === 'tree' && file.opened) { if (file.type === 'tree' && file.opened) {
Helper.removeChildFilesOfTree(file); Store.removeChildFilesOfTree(file);
} }
url = file.url; url = file.url;
Service.url = url; Service.url = url;

View file

@ -1,3 +1,5 @@
import RepoHelper from './repo_helper';
const RepoStore = { const RepoStore = {
ideEl: {}, ideEl: {},
monacoInstance: {}, monacoInstance: {},
@ -44,5 +46,117 @@ const RepoStore = {
tree: false, tree: false,
blob: false, blob: false,
}, },
// mutations
addFilesToDirectory(inDirectory, currentList, newList) {
this.files = RepoHelper.getNewMergedList(inDirectory, currentList, newList);
},
toggleRawPreview() {
this.activeFile.raw = !this.activeFile.raw;
this.activeFileLabel = this.activeFile.raw ? 'Preview' : 'Raw';
},
setActiveFiles(file) {
if (this.isActiveFile(file)) return;
this.openedFiles = this.openedFiles.map((openedFile, i) => this.setFileToActive(openedFile, i));
this.setActiveToRaw();
if (file.binary) {
this.blobRaw = file.base64;
} else {
this.blobRaw = file.plain;
}
if (!file.loading) RepoHelper.toURL(file.url);
this.binary = file.binary;
},
setFileToActive(file, i) {
const activeFile = file;
activeFile.active = activeFile.url === activeFile.url;
if (activeFile.active) this.setActiveFile(activeFile, i);
return activeFile;
},
setActiveFile(activeFile, i) {
this.activeFile = activeFile;
this.activeFileIndex = i;
},
setActiveToRaw() {
this.activeFile.raw = false;
// can't get vue to listen to raw for some reason so this for now.
this.activeFileLabel = 'Raw';
},
/* eslint-disable no-param-reassign */
removeChildFilesOfTree(tree) {
let foundTree = false;
this.files = this.files.filter((file) => {
if (file.url === tree.url) foundTree = true;
if (foundTree) return file.level <= tree.level;
return true;
});
tree.opened = false;
tree.icon = 'fa-folder';
},
/* eslint-enable no-param-reassign */
removeFromOpenedFiles(file) {
if (file.type === 'tree') return;
this.openedFiles = this.openedFiles.filter(openedFile => openedFile.url !== file.url);
},
addPlaceholderFile() {
const randomURL = RepoHelper.Time.now();
const newFakeFile = {
active: false,
binary: true,
type: 'blob',
loading: true,
mime_type: 'loading',
name: 'loading',
url: randomURL,
};
this.openedFiles.push(newFakeFile);
return newFakeFile;
},
addToOpenedFiles(file) {
const openFile = file;
const openedFilesAlreadyExists = this.openedFiles
.some(openedFile => openedFile.url === openFile.url);
if (openedFilesAlreadyExists) return;
openFile.changed = false;
this.openedFiles.push(openFile);
},
setActiveFileContents(contents) {
if (!this.editMode) return;
this.activeFile.newContent = contents;
this.activeFile.changed = this.activeFile.plain !== this.activeFile.newContent;
this.openedFiles[this.activeFileIndex].changed = this.activeFile.changed;
},
// getters
isActiveFile(file) {
return file && file.url === this.activeFile.url;
},
}; };
export default RepoStore; export default RepoStore;

View file

@ -1,4 +1,4 @@
import RepoHelper from './repo_helper'; import RepoStore from './repo_store';
const RepoTab = { const RepoTab = {
template: ` template: `
@ -27,13 +27,11 @@ const RepoTab = {
}, },
methods: { methods: {
tabClicked(file) { tabClicked: RepoStore.setActiveFiles,
RepoHelper.setActiveFile(file);
},
xClicked(file) { xClicked(file) {
if (file.changed) return; if (file.changed) return;
RepoHelper.removeFromOpenedFiles(file); RepoStore.removeFromOpenedFiles(file);
}, },
}, },
}; };