2016-03-19 21:37:04 +01:00
|
|
|
'use strict';
|
|
|
|
|
2016-04-06 21:49:26 +02:00
|
|
|
const page = require('page');
|
2016-06-02 00:07:51 +02:00
|
|
|
const api = require('../api.js');
|
2016-06-03 19:12:10 +02:00
|
|
|
const settings = require('../settings.js');
|
|
|
|
const misc = require('../util/misc.js');
|
2016-04-01 00:20:34 +02:00
|
|
|
const topNavController = require('../controllers/top_nav_controller.js');
|
2016-06-02 00:07:51 +02:00
|
|
|
const pageController = require('../controllers/page_controller.js');
|
|
|
|
const PostsHeaderView = require('../views/posts_header_view.js');
|
|
|
|
const PostsPageView = require('../views/posts_page_view.js');
|
2016-05-29 12:21:25 +02:00
|
|
|
const EmptyView = require('../views/empty_view.js');
|
2016-03-19 21:37:04 +01:00
|
|
|
|
2016-04-01 00:20:34 +02:00
|
|
|
class PostsController {
|
2016-06-02 00:07:51 +02:00
|
|
|
constructor() {
|
|
|
|
this._postsHeaderView = new PostsHeaderView();
|
|
|
|
this._postsPageView = new PostsPageView();
|
|
|
|
}
|
|
|
|
|
2016-04-06 21:49:26 +02:00
|
|
|
registerRoutes() {
|
2016-05-20 21:35:12 +02:00
|
|
|
page('/upload', (ctx, next) => { this._uploadPostsRoute(); });
|
2016-05-29 12:22:06 +02:00
|
|
|
page('/posts/:query?',
|
|
|
|
(ctx, next) => { misc.parseSearchQueryRoute(ctx, next); },
|
2016-06-02 00:07:51 +02:00
|
|
|
(ctx, next) => { this._listPostsRoute(ctx); });
|
2016-04-06 21:49:26 +02:00
|
|
|
page(
|
|
|
|
'/post/:id',
|
2016-05-20 21:35:12 +02:00
|
|
|
(ctx, next) => { this._showPostRoute(ctx.params.id); });
|
2016-04-06 21:49:26 +02:00
|
|
|
page(
|
|
|
|
'/post/:id/edit',
|
2016-05-20 21:35:12 +02:00
|
|
|
(ctx, next) => { this._editPostRoute(ctx.params.id); });
|
2016-05-29 12:21:25 +02:00
|
|
|
this._emptyView = new EmptyView();
|
2016-04-06 21:49:26 +02:00
|
|
|
}
|
|
|
|
|
2016-05-20 21:35:12 +02:00
|
|
|
_uploadPostsRoute() {
|
2016-04-01 00:20:34 +02:00
|
|
|
topNavController.activate('upload');
|
2016-05-29 12:21:25 +02:00
|
|
|
this._emptyView.render();
|
2016-03-19 21:37:04 +01:00
|
|
|
}
|
|
|
|
|
2016-06-02 00:07:51 +02:00
|
|
|
_listPostsRoute(ctx) {
|
2016-04-01 00:20:34 +02:00
|
|
|
topNavController.activate('posts');
|
2016-06-02 00:07:51 +02:00
|
|
|
|
|
|
|
pageController.run({
|
|
|
|
state: ctx.state,
|
|
|
|
requestPage: page => {
|
2016-06-03 19:12:10 +02:00
|
|
|
const browsingSettings = settings.getSettings();
|
|
|
|
let text = ctx.searchQuery.text;
|
|
|
|
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}`;
|
|
|
|
}
|
|
|
|
text = text.trim();
|
2016-06-02 00:07:51 +02:00
|
|
|
return api.get(
|
|
|
|
`/posts/?query=${text}&page=${page}&pageSize=40&_fields=` +
|
2016-06-03 19:12:10 +02:00
|
|
|
`id,type,tags,score,favoriteCount,` +
|
|
|
|
`commentCount,thumbnailUrl`);
|
2016-06-02 00:07:51 +02:00
|
|
|
},
|
|
|
|
clientUrl: '/posts/' + misc.formatSearchQuery({
|
|
|
|
text: ctx.searchQuery.text, page: '{page}'}),
|
|
|
|
searchQuery: ctx.searchQuery,
|
|
|
|
headerRenderer: this._postsHeaderView,
|
|
|
|
pageRenderer: this._postsPageView,
|
|
|
|
});
|
2016-03-19 21:37:04 +01:00
|
|
|
}
|
|
|
|
|
2016-05-20 21:35:12 +02:00
|
|
|
_showPostRoute(id) {
|
2016-04-01 00:20:34 +02:00
|
|
|
topNavController.activate('posts');
|
2016-05-29 12:21:25 +02:00
|
|
|
this._emptyView.render();
|
2016-03-19 21:37:04 +01:00
|
|
|
}
|
|
|
|
|
2016-05-20 21:35:12 +02:00
|
|
|
_editPostRoute(id) {
|
2016-04-01 00:20:34 +02:00
|
|
|
topNavController.activate('posts');
|
2016-05-29 12:21:25 +02:00
|
|
|
this._emptyView.render();
|
2016-03-19 21:37:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-01 00:20:34 +02:00
|
|
|
module.exports = new PostsController();
|