2016-03-19 21:37:04 +01:00
|
|
|
'use strict';
|
|
|
|
|
2017-03-30 20:50:12 +02:00
|
|
|
const config = require('../config.js');
|
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');
|
2017-01-20 21:51:04 +01:00
|
|
|
const uri = require('../util/uri.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-08-23 21:18:03 +02:00
|
|
|
const EmptyView = require('../views/empty_view.js');
|
2016-03-19 21:37:04 +01:00
|
|
|
|
2016-06-19 19:16:40 +02:00
|
|
|
const fields = [
|
2017-02-11 21:58:18 +01:00
|
|
|
'id', 'thumbnailUrl', 'type', 'safety',
|
2016-08-08 17:45:41 +02:00
|
|
|
'score', 'favoriteCount', 'commentCount', 'tags', 'version'];
|
2016-06-19 19:16:40 +02:00
|
|
|
|
2016-06-13 23:10:53 +02:00
|
|
|
class PostListController {
|
2016-06-14 10:31:48 +02:00
|
|
|
constructor(ctx) {
|
2016-08-23 21:18:03 +02:00
|
|
|
if (!api.hasPrivilege('posts:list')) {
|
|
|
|
this._view = new EmptyView();
|
|
|
|
this._view.showError('You don\'t have privileges to view posts.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-14 10:31:48 +02:00
|
|
|
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-08-28 18:53:06 +02:00
|
|
|
this._pageController = new PageController();
|
|
|
|
|
|
|
|
this._headerView = new PostsHeaderView({
|
|
|
|
hostNode: this._pageController.view.pageHeaderHolderNode,
|
2016-07-07 21:18:35 +02:00
|
|
|
parameters: ctx.parameters,
|
2017-03-30 20:50:12 +02:00
|
|
|
enableSafety: config.enableSafety,
|
2017-02-11 19:50:22 +01:00
|
|
|
canBulkEditTags: api.hasPrivilege('posts:bulkEdit:tags'),
|
2017-02-11 21:58:18 +01:00
|
|
|
canBulkEditSafety: api.hasPrivilege('posts:bulkEdit:safety'),
|
2017-02-11 19:50:22 +01:00
|
|
|
bulkEdit: {
|
|
|
|
tags: this._bulkEditTags
|
|
|
|
},
|
2016-06-02 00:07:51 +02:00
|
|
|
});
|
2016-08-28 18:53:06 +02:00
|
|
|
this._headerView.addEventListener(
|
|
|
|
'navigate', e => this._evtNavigate(e));
|
|
|
|
|
|
|
|
this._syncPageController();
|
2016-03-19 21:37:04 +01:00
|
|
|
}
|
|
|
|
|
2016-08-02 11:05:40 +02:00
|
|
|
showSuccess(message) {
|
|
|
|
this._pageController.showSuccess(message);
|
|
|
|
}
|
|
|
|
|
2017-02-11 19:50:22 +01:00
|
|
|
get _bulkEditTags() {
|
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
|
|
|
}
|
|
|
|
|
2016-08-28 18:53:06 +02:00
|
|
|
_evtNavigate(e) {
|
|
|
|
history.pushState(
|
|
|
|
null,
|
|
|
|
window.title,
|
2017-01-20 21:51:04 +01:00
|
|
|
uri.formatClientLink('posts', e.detail.parameters));
|
2016-08-28 18:53:06 +02:00
|
|
|
Object.assign(this._ctx.parameters, e.detail.parameters);
|
|
|
|
this._syncPageController();
|
|
|
|
}
|
|
|
|
|
2016-07-05 21:20:28 +02:00
|
|
|
_evtTag(e) {
|
2017-02-11 19:50:22 +01:00
|
|
|
for (let tag of this._bulkEditTags) {
|
2016-07-05 21:20:28 +02:00
|
|
|
e.detail.post.addTag(tag);
|
|
|
|
}
|
2017-01-08 02:12:38 +01:00
|
|
|
e.detail.post.save().catch(error => window.alert(error.message));
|
2016-07-05 21:20:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_evtUntag(e) {
|
2017-02-11 19:50:22 +01:00
|
|
|
for (let tag of this._bulkEditTags) {
|
2016-07-05 21:20:28 +02:00
|
|
|
e.detail.post.removeTag(tag);
|
|
|
|
}
|
2017-01-08 02:12:38 +01:00
|
|
|
e.detail.post.save().catch(error => window.alert(error.message));
|
2016-07-05 21:20:28 +02:00
|
|
|
}
|
|
|
|
|
2017-02-11 21:58:18 +01:00
|
|
|
_evtChangeSafety(e) {
|
|
|
|
e.detail.post.safety = e.detail.safety;
|
|
|
|
e.detail.post.save().catch(error => window.alert(error.message));
|
|
|
|
}
|
|
|
|
|
2016-08-28 18:53:06 +02:00
|
|
|
_syncPageController() {
|
|
|
|
this._pageController.run({
|
|
|
|
parameters: this._ctx.parameters,
|
2017-02-09 00:48:06 +01:00
|
|
|
defaultLimit: parseInt(settings.get().postsPerPage),
|
|
|
|
getClientUrlForPage: (offset, limit) => {
|
|
|
|
const parameters = Object.assign(
|
|
|
|
{}, this._ctx.parameters, {offset: offset, limit: limit});
|
|
|
|
return uri.formatClientLink('posts', parameters);
|
2016-08-28 18:53:06 +02:00
|
|
|
},
|
2017-02-09 00:48:06 +01:00
|
|
|
requestPage: (offset, limit) => {
|
2016-08-28 18:53:06 +02:00
|
|
|
return PostList.search(
|
2017-03-30 20:50:12 +02:00
|
|
|
this._ctx.parameters.query, offset, limit, fields);
|
2016-08-28 18:53:06 +02:00
|
|
|
},
|
|
|
|
pageRenderer: pageCtx => {
|
|
|
|
Object.assign(pageCtx, {
|
|
|
|
canViewPosts: api.hasPrivilege('posts:view'),
|
2017-02-11 19:50:22 +01:00
|
|
|
canBulkEditTags: api.hasPrivilege('posts:bulkEdit:tags'),
|
2017-02-11 21:58:18 +01:00
|
|
|
canBulkEditSafety:
|
|
|
|
api.hasPrivilege('posts:bulkEdit:safety'),
|
2017-02-11 19:50:22 +01:00
|
|
|
bulkEdit: {
|
|
|
|
tags: this._bulkEditTags,
|
|
|
|
},
|
2016-08-28 18:53:06 +02:00
|
|
|
});
|
|
|
|
const view = new PostsPageView(pageCtx);
|
|
|
|
view.addEventListener('tag', e => this._evtTag(e));
|
|
|
|
view.addEventListener('untag', e => this._evtUntag(e));
|
2017-02-11 21:58:18 +01:00
|
|
|
view.addEventListener(
|
|
|
|
'changeSafety', e => this._evtChangeSafety(e));
|
2016-08-28 18:53:06 +02:00
|
|
|
return view;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2016-03-19 21:37:04 +01:00
|
|
|
}
|
|
|
|
|
2016-06-14 10:31:48 +02:00
|
|
|
module.exports = router => {
|
|
|
|
router.enter(
|
2017-01-20 21:51:04 +01:00
|
|
|
['posts'],
|
2016-06-14 10:31:48 +02:00
|
|
|
(ctx, next) => { ctx.controller = new PostListController(ctx); });
|
|
|
|
};
|