2016-03-19 21:37:04 +01:00
|
|
|
'use strict';
|
|
|
|
|
2016-06-12 14:04:42 +02:00
|
|
|
const api = require('../api.js');
|
|
|
|
const misc = require('../util/misc.js');
|
2016-06-17 20:25:44 +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-12 14:04:42 +02:00
|
|
|
const CommentsPageView = require('../views/comments_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 = ['id', 'comments', 'commentCount', 'thumbnailUrl'];
|
|
|
|
|
2016-04-01 00:20:34 +02:00
|
|
|
class CommentsController {
|
2016-06-14 10:31:48 +02:00
|
|
|
constructor(ctx) {
|
2016-08-23 21:18:03 +02:00
|
|
|
if (!api.hasPrivilege('comments:list')) {
|
|
|
|
this._view = new EmptyView();
|
|
|
|
this._view.showError(
|
|
|
|
'You don\'t have privileges to view comments.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-14 10:31:48 +02:00
|
|
|
topNavigation.activate('comments');
|
2016-07-13 21:50:07 +02:00
|
|
|
topNavigation.setTitle('Listing comments');
|
2016-06-12 14:04:42 +02:00
|
|
|
|
2016-08-28 18:53:06 +02:00
|
|
|
this._pageController = new PageController();
|
|
|
|
this._pageController.run({
|
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 '/comments/' + misc.formatUrlParameters(parameters);
|
2016-07-05 21:20:28 +02:00
|
|
|
},
|
2016-06-17 20:25:44 +02:00
|
|
|
requestPage: page => {
|
2016-06-19 19:16:40 +02:00
|
|
|
return PostList.search(
|
|
|
|
'sort:comment-date+comment-count-min:1', page, 10, fields);
|
2016-06-17 20:25:44 +02:00
|
|
|
},
|
2016-06-14 10:31:48 +02:00
|
|
|
pageRenderer: pageCtx => {
|
|
|
|
Object.assign(pageCtx, {
|
|
|
|
canViewPosts: api.hasPrivilege('posts:view'),
|
|
|
|
});
|
2016-06-17 20:25:44 +02:00
|
|
|
const view = new CommentsPageView(pageCtx);
|
2016-08-22 20:45:58 +02:00
|
|
|
view.addEventListener('submit', e => this._evtUpdate(e));
|
2016-06-17 20:25:44 +02:00
|
|
|
view.addEventListener('score', e => this._evtScore(e));
|
|
|
|
view.addEventListener('delete', e => this._evtDelete(e));
|
|
|
|
return view;
|
2016-06-14 10:31:48 +02:00
|
|
|
},
|
2016-06-12 14:04:42 +02:00
|
|
|
});
|
2016-03-19 21:37:04 +01:00
|
|
|
}
|
2016-06-17 20:25:44 +02:00
|
|
|
|
2016-08-22 20:45:58 +02:00
|
|
|
_evtUpdate(e) {
|
2016-06-17 20:25:44 +02:00
|
|
|
// TODO: disable form
|
|
|
|
e.detail.comment.text = e.detail.text;
|
|
|
|
e.detail.comment.save()
|
|
|
|
.catch(errorMessage => {
|
|
|
|
e.detail.target.showError(errorMessage);
|
|
|
|
// TODO: enable form
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_evtScore(e) {
|
|
|
|
e.detail.comment.setScore(e.detail.score)
|
|
|
|
.catch(errorMessage => {
|
|
|
|
window.alert(errorMessage);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_evtDelete(e) {
|
|
|
|
e.detail.comment.delete()
|
|
|
|
.catch(errorMessage => {
|
|
|
|
window.alert(errorMessage);
|
|
|
|
});
|
|
|
|
}
|
2016-06-14 10:31:48 +02:00
|
|
|
};
|
2016-03-19 21:37:04 +01:00
|
|
|
|
2016-06-14 10:31:48 +02:00
|
|
|
module.exports = router => {
|
2016-07-07 21:18:35 +02:00
|
|
|
router.enter('/comments/:parameters?',
|
|
|
|
(ctx, next) => { misc.parseUrlParametersRoute(ctx, next); },
|
2016-06-14 10:31:48 +02:00
|
|
|
(ctx, next) => { new CommentsController(ctx); });
|
|
|
|
};
|