szurubooru/client/js/controllers/post_list_controller.js

66 lines
2.2 KiB
JavaScript
Raw Normal View History

'use strict';
const router = require('../router.js');
const api = require('../api.js');
2016-06-03 19:12:10 +02:00
const settings = require('../settings.js');
const misc = require('../util/misc.js');
const pageController = require('../controllers/page_controller.js');
2016-06-13 22:34:39 +02:00
const TopNavigation = require('../models/top_navigation.js');
const PostsHeaderView = require('../views/posts_header_view.js');
const PostsPageView = require('../views/posts_page_view.js');
2016-06-13 23:10:53 +02:00
class PostListController {
constructor() {
this._postsHeaderView = new PostsHeaderView();
this._postsPageView = new PostsPageView();
}
registerRoutes() {
router.enter(
'/posts/:query?',
2016-05-29 12:22:06 +02:00
(ctx, next) => { misc.parseSearchQueryRoute(ctx, next); },
(ctx, next) => { this._listPostsRoute(ctx); });
}
_listPostsRoute(ctx) {
2016-06-13 22:34:39 +02:00
TopNavigation.activate('posts');
pageController.run({
2016-06-11 09:59:29 +02:00
searchQuery: ctx.searchQuery,
clientUrl: '/posts/' + misc.formatSearchQuery({
text: ctx.searchQuery.text, page: '{page}'}),
requestPage: pageController.createHistoryCacheProxy(
ctx,
page => {
2016-06-12 22:10:20 +02:00
const text
= this._decorateSearchQuery(ctx.searchQuery.text);
return api.get(
`/posts/?query=${text}&page=${page}&pageSize=40` +
'&fields=id,type,tags,score,favoriteCount,' +
'commentCount,thumbnailUrl');
}),
headerRenderer: this._postsHeaderView,
pageRenderer: this._postsPageView,
2016-06-11 09:59:29 +02:00
pageContext: {
canViewPosts: api.hasPrivilege('posts:view'),
}
});
}
_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 PostListController();