2016-03-19 21:37:04 +01:00
|
|
|
'use strict';
|
|
|
|
|
2016-06-02 00:07:51 +02:00
|
|
|
const api = require('../api.js');
|
2016-06-14 10:31:48 +02:00
|
|
|
const settings = require('../models/settings.js');
|
2016-06-03 19:12:10 +02:00
|
|
|
const misc = require('../util/misc.js');
|
2016-06-19 19:16:40 +02:00
|
|
|
const PostList = require('../models/post_list.js');
|
2016-06-14 10:31:48 +02:00
|
|
|
const topNavigation = require('../models/top_navigation.js');
|
|
|
|
const PageController = require('../controllers/page_controller.js');
|
2016-06-02 00:07:51 +02:00
|
|
|
const PostsHeaderView = require('../views/posts_header_view.js');
|
|
|
|
const PostsPageView = require('../views/posts_page_view.js');
|
2016-03-19 21:37:04 +01:00
|
|
|
|
2016-06-19 19:16:40 +02:00
|
|
|
const fields = [
|
|
|
|
'id', 'thumbnailUrl', 'type',
|
|
|
|
'score', 'favoriteCount', 'commentCount', 'tags'];
|
|
|
|
|
2016-06-13 23:10:53 +02:00
|
|
|
class PostListController {
|
2016-06-14 10:31:48 +02:00
|
|
|
constructor(ctx) {
|
|
|
|
topNavigation.activate('posts');
|
2016-07-13 21:50:07 +02:00
|
|
|
topNavigation.setTitle('Listing posts');
|
2016-03-19 21:37:04 +01:00
|
|
|
|
2016-07-05 21:20:28 +02:00
|
|
|
this._ctx = ctx;
|
2016-06-14 10:31:48 +02:00
|
|
|
this._pageController = new PageController({
|
2016-07-07 21:18:35 +02:00
|
|
|
parameters: ctx.parameters,
|
2016-07-05 21:20:28 +02:00
|
|
|
getClientUrlForPage: page => {
|
2016-07-07 21:18:35 +02:00
|
|
|
const parameters = Object.assign(
|
|
|
|
{}, ctx.parameters, {page: page});
|
|
|
|
return '/posts/' + misc.formatUrlParameters(parameters);
|
2016-07-05 21:20:28 +02:00
|
|
|
},
|
2016-06-19 19:16:40 +02:00
|
|
|
requestPage: page => {
|
|
|
|
return PostList.search(
|
2016-07-07 21:18:35 +02:00
|
|
|
this._decorateSearchQuery(ctx.parameters.query),
|
2016-06-19 19:16:40 +02:00
|
|
|
page, 40, fields);
|
|
|
|
},
|
2016-06-14 10:31:48 +02:00
|
|
|
headerRenderer: headerCtx => {
|
2016-07-05 21:20:28 +02:00
|
|
|
Object.assign(headerCtx, {
|
|
|
|
canMassTag: api.hasPrivilege('tags:masstag'),
|
|
|
|
massTagTags: this._massTagTags,
|
|
|
|
});
|
2016-06-14 10:31:48 +02:00
|
|
|
return new PostsHeaderView(headerCtx);
|
|
|
|
},
|
|
|
|
pageRenderer: pageCtx => {
|
|
|
|
Object.assign(pageCtx, {
|
|
|
|
canViewPosts: api.hasPrivilege('posts:view'),
|
2016-07-05 21:20:28 +02:00
|
|
|
massTagTags: this._massTagTags,
|
2016-06-14 10:31:48 +02:00
|
|
|
});
|
2016-07-05 21:20:28 +02:00
|
|
|
const view = new PostsPageView(pageCtx);
|
|
|
|
view.addEventListener('tag', e => this._evtTag(e));
|
|
|
|
view.addEventListener('untag', e => this._evtUntag(e));
|
|
|
|
return view;
|
2016-06-14 10:31:48 +02:00
|
|
|
},
|
2016-06-02 00:07:51 +02:00
|
|
|
});
|
2016-03-19 21:37:04 +01:00
|
|
|
}
|
|
|
|
|
2016-08-02 11:05:40 +02:00
|
|
|
showSuccess(message) {
|
|
|
|
this._pageController.showSuccess(message);
|
|
|
|
}
|
|
|
|
|
2016-07-05 21:20:28 +02:00
|
|
|
get _massTagTags() {
|
2016-07-07 21:18:35 +02:00
|
|
|
return (this._ctx.parameters.tag || '').split(/\s+/).filter(s => s);
|
2016-07-05 21:20:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_evtTag(e) {
|
|
|
|
for (let tag of this._massTagTags) {
|
|
|
|
e.detail.post.addTag(tag);
|
|
|
|
}
|
|
|
|
e.detail.post.save()
|
|
|
|
.catch(errorMessage => {
|
|
|
|
window.alert(errorMessage);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_evtUntag(e) {
|
|
|
|
for (let tag of this._massTagTags) {
|
|
|
|
e.detail.post.removeTag(tag);
|
|
|
|
}
|
|
|
|
e.detail.post.save()
|
|
|
|
.catch(errorMessage => {
|
|
|
|
window.alert(errorMessage);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-06-06 20:57:22 +02:00
|
|
|
_decorateSearchQuery(text) {
|
2016-06-14 10:31:48 +02:00
|
|
|
const browsingSettings = settings.get();
|
2016-06-06 20:57:22 +02:00
|
|
|
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-03-19 21:37:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-14 10:31:48 +02:00
|
|
|
module.exports = router => {
|
|
|
|
router.enter(
|
2016-07-07 21:18:35 +02:00
|
|
|
'/posts/:parameters?',
|
|
|
|
(ctx, next) => { misc.parseUrlParametersRoute(ctx, next); },
|
2016-06-14 10:31:48 +02:00
|
|
|
(ctx, next) => { ctx.controller = new PostListController(ctx); });
|
|
|
|
};
|