client/posts: split controllers
This commit is contained in:
parent
48cf3b47c0
commit
6fcf81e55d
|
@ -0,0 +1,64 @@
|
|||
'use strict';
|
||||
|
||||
const router = require('../router.js');
|
||||
const api = require('../api.js');
|
||||
const events = require('../events.js');
|
||||
const settings = require('../settings.js');
|
||||
const TopNavigation = require('../models/top_navigation.js');
|
||||
const PostView = require('../views/post_view.js');
|
||||
const EmptyView = require('../views/empty_view.js');
|
||||
|
||||
class PostController {
|
||||
constructor() {
|
||||
this._postView = new PostView();
|
||||
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) {
|
||||
TopNavigation.activate('posts');
|
||||
Promise.all([
|
||||
api.get('/post/' + id),
|
||||
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,
|
||||
canEditPosts: api.hasPrivilege('posts:edit'),
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new PostController();
|
|
@ -3,42 +3,23 @@
|
|||
const router = require('../router.js');
|
||||
const api = require('../api.js');
|
||||
const settings = require('../settings.js');
|
||||
const events = require('../events.js');
|
||||
const misc = require('../util/misc.js');
|
||||
const pageController = require('../controllers/page_controller.js');
|
||||
const TopNavigation = require('../models/top_navigation.js');
|
||||
const PostsHeaderView = require('../views/posts_header_view.js');
|
||||
const PostsPageView = require('../views/posts_page_view.js');
|
||||
const PostView = require('../views/post_view.js');
|
||||
const EmptyView = require('../views/empty_view.js');
|
||||
|
||||
class PostsController {
|
||||
class PostListController {
|
||||
constructor() {
|
||||
this._postsHeaderView = new PostsHeaderView();
|
||||
this._postsPageView = new PostsPageView();
|
||||
this._postView = new PostView();
|
||||
}
|
||||
|
||||
registerRoutes() {
|
||||
router.enter(
|
||||
'/upload',
|
||||
(ctx, next) => { this._uploadPostsRoute(); });
|
||||
router.enter(
|
||||
'/posts/:query?',
|
||||
(ctx, next) => { misc.parseSearchQueryRoute(ctx, next); },
|
||||
(ctx, next) => { this._listPostsRoute(ctx); });
|
||||
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); });
|
||||
this._emptyView = new EmptyView();
|
||||
}
|
||||
|
||||
_uploadPostsRoute() {
|
||||
TopNavigation.activate('upload');
|
||||
this._emptyView.render();
|
||||
}
|
||||
|
||||
_listPostsRoute(ctx) {
|
||||
|
@ -66,29 +47,6 @@ class PostsController {
|
|||
});
|
||||
}
|
||||
|
||||
_showPostRoute(id, editMode) {
|
||||
TopNavigation.activate('posts');
|
||||
Promise.all([
|
||||
api.get('/post/' + id),
|
||||
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,
|
||||
canEditPosts: api.hasPrivilege('posts:edit'),
|
||||
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 = [];
|
||||
|
@ -104,4 +62,4 @@ class PostsController {
|
|||
}
|
||||
}
|
||||
|
||||
module.exports = new PostsController();
|
||||
module.exports = new PostListController();
|
|
@ -0,0 +1,24 @@
|
|||
'use strict';
|
||||
|
||||
const router = require('../router.js');
|
||||
const TopNavigation = require('../models/top_navigation.js');
|
||||
const EmptyView = require('../views/empty_view.js');
|
||||
|
||||
class PostUploadController {
|
||||
constructor() {
|
||||
this._emptyView = new EmptyView();
|
||||
}
|
||||
|
||||
registerRoutes() {
|
||||
router.enter(
|
||||
'/upload',
|
||||
(ctx, next) => { this._uploadPostsRoute(); });
|
||||
}
|
||||
|
||||
_uploadPostsRoute() {
|
||||
TopNavigation.activate('upload');
|
||||
this._emptyView.render();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new PostUploadController();
|
|
@ -35,7 +35,9 @@ router.enter(
|
|||
|
||||
let controllers = [];
|
||||
controllers.push(require('./controllers/auth_controller.js'));
|
||||
controllers.push(require('./controllers/posts_controller.js'));
|
||||
controllers.push(require('./controllers/post_list_controller.js'));
|
||||
controllers.push(require('./controllers/post_upload_controller.js'));
|
||||
controllers.push(require('./controllers/post_controller.js'));
|
||||
controllers.push(require('./controllers/users_controller.js'));
|
||||
controllers.push(require('./controllers/help_controller.js'));
|
||||
controllers.push(require('./controllers/comments_controller.js'));
|
||||
|
|
Loading…
Reference in New Issue