szurubooru/client/js/controllers/post_controller.js

65 lines
2.2 KiB
JavaScript
Raw Normal View History

'use strict';
const router = require('../router.js');
const api = require('../api.js');
const events = require('../events.js');
2016-06-13 23:10:53 +02:00
const settings = require('../settings.js');
2016-06-13 22:34:39 +02:00
const TopNavigation = require('../models/top_navigation.js');
const PostView = require('../views/post_view.js');
const EmptyView = require('../views/empty_view.js');
2016-06-13 23:10:53 +02:00
class PostController {
constructor() {
this._postView = new PostView();
2016-06-13 23:10:53 +02:00
this._emptyView = new EmptyView();
}
registerRoutes() {
router.enter(
'/post/:id',
(ctx, next) => { this._showPostRoute(ctx.params.id, false); });
router.enter(
'/post/:id/edit',
(ctx, next) => { this._showPostRoute(ctx.params.id, true); });
}
_showPostRoute(id, editMode) {
2016-06-13 22:34:39 +02:00
TopNavigation.activate('posts');
Promise.all([
api.get('/post/' + id),
2016-06-12 22:10:20 +02:00
api.get(`/post/${id}/around?fields=id&query=` +
this._decorateSearchQuery('')),
]).then(responses => {
const [postResponse, aroundResponse] = responses;
this._postView.render({
post: postResponse,
editMode: editMode,
nextPostId: aroundResponse.next ? aroundResponse.next.id : null,
prevPostId: aroundResponse.prev ? aroundResponse.prev.id : null,
2016-06-11 09:59:29 +02:00
canEditPosts: api.hasPrivilege('posts:edit'),
2016-06-12 18:08:50 +02:00
canListComments: api.hasPrivilege('comments:list'),
canCreateComments: api.hasPrivilege('comments:create'),
});
}, response => {
this._emptyView.render();
events.notify(events.Error, response.description);
});
}
_decorateSearchQuery(text) {
const browsingSettings = settings.getSettings();
let disabledSafety = [];
for (let key of Object.keys(browsingSettings.listPosts)) {
if (browsingSettings.listPosts[key] === false) {
disabledSafety.push(key);
}
}
if (disabledSafety.length) {
text = `-rating:${disabledSafety.join(',')} ${text}`;
}
return text.trim();
}
}
2016-06-13 23:10:53 +02:00
module.exports = new PostController();