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

View file

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

View file

@ -15,7 +15,7 @@ const RepoHelper = {
? window.performance
: Date,
getLanguageForFile(file, langs) {
getLanguageIDForFile(file, langs) {
const ext = file.name.split('.').pop();
const foundLang = this.findLanguage(ext, langs);
@ -26,105 +26,6 @@ const RepoHelper = {
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) {
if (!tree) return;
@ -155,34 +56,9 @@ const RepoHelper = {
.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) {
if (loading) return this.addPlaceholderFile();
return this.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;
if (loading) return Store.addPlaceholderFile();
return Store.removeFromOpenedFiles(file);
},
setLoading(loading, file) {
@ -196,6 +72,27 @@ const RepoHelper = {
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) {
let file = treeOrFile;
const loadingData = this.setLoading(true);
@ -223,12 +120,13 @@ const RepoHelper = {
data.url = file.url;
data.newContent = '';
this.addToOpenedFiles(data);
this.setActiveFile(data);
Store.addToOpenedFiles(data);
Store.setActiveFiles(data);
// if the file tree is empty
if (Store.files.length === 0) {
const parentURL = this.blobURLtoParent(Service.url);
const parentURL = Service.blobURLtoParentTree(Service.url);
Service.url = parentURL;
this.getContent();
}
@ -236,8 +134,8 @@ const RepoHelper = {
// it's a tree
this.setDirectoryOpen(file);
const newDirectory = this.dataToListOfFiles(data);
Store.files = this.insertNewFilesIntoParentDir(file, Store.files, newDirectory);
Store.prevURL = this.blobURLtoParent(Service.url);
Store.addFilesToDirectory(file, Store.files, newDirectory);
Store.prevURL = Service.blobURLtoParentTree(Service.url);
}
})
.catch(() => {
@ -250,21 +148,6 @@ const RepoHelper = {
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) {
const simpleBlob = this.serializeRepoEntity('blob', blob);
simpleBlob.lastCommitMessage = blob.last_commit.message;

View file

@ -43,6 +43,16 @@ const RepoService = {
bufferToBase64(data) {
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;

View file

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

View file

@ -1,3 +1,5 @@
import RepoHelper from './repo_helper';
const RepoStore = {
ideEl: {},
monacoInstance: {},
@ -44,5 +46,117 @@ const RepoStore = {
tree: 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;

View file

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