static-cms/src/backends/test-repo/implementation.js

58 lines
1.4 KiB
JavaScript
Raw Normal View History

import AuthenticationPage from './AuthenticationPage';
function getSlug(path) {
2016-05-30 16:55:32 -07:00
const m = path.match(/([^\/]+?)(\.[^\/\.]+)?$/);
return m && m[1];
}
export default class TestRepo {
constructor(config) {
this.config = config;
if (window.repoFiles == null) {
throw 'The TestRepo backend needs a "window.repoFiles" object.';
}
}
setUser() {}
authComponent() {
return AuthenticationPage;
}
authenticate(state) {
return Promise.resolve({email: state.email});
}
entries(collection) {
const entries = [];
const folder = collection.get('folder');
if (folder) {
for (var path in window.repoFiles[folder]) {
entries.push({
path: folder + '/' + path,
slug: getSlug(path),
raw: window.repoFiles[folder][path].content
});
}
}
return Promise.resolve({
pagination: {},
entries
});
}
entry(collection, slug) {
return this.entries(collection).then((response) => (
response.entries.filter((entry) => entry.slug === slug)[0]
2016-05-30 16:55:32 -07:00
));
}
2016-06-07 20:15:28 -03:00
persist(collection, entry, mediaFiles = []) {
2016-06-10 00:16:01 -03:00
const folder = collection.get('folder');
const fileName = entry.path.substring(entry.path.lastIndexOf('/') + 1);
window.repoFiles[folder][fileName]['content'] = entry.raw;
return Promise.resolve({persistedEntry:entry, persistedMediaFiles:[]});
}
}