2016-03-19 20:37:04 +00:00
|
|
|
'use strict';
|
|
|
|
|
2016-06-12 12:04:42 +00:00
|
|
|
const api = require('../api.js');
|
2017-01-20 20:51:04 +00:00
|
|
|
const uri = require('../util/uri.js');
|
2016-06-17 18:25:44 +00:00
|
|
|
const PostList = require('../models/post_list.js');
|
2016-06-14 08:31:48 +00:00
|
|
|
const topNavigation = require('../models/top_navigation.js');
|
|
|
|
const PageController = require('../controllers/page_controller.js');
|
2016-06-12 12:04:42 +00:00
|
|
|
const CommentsPageView = require('../views/comments_page_view.js');
|
2016-08-23 19:18:03 +00:00
|
|
|
const EmptyView = require('../views/empty_view.js');
|
2016-03-19 20:37:04 +00:00
|
|
|
|
2016-06-19 17:16:40 +00:00
|
|
|
const fields = ['id', 'comments', 'commentCount', 'thumbnailUrl'];
|
|
|
|
|
2016-03-31 22:20:34 +00:00
|
|
|
class CommentsController {
|
2016-06-14 08:31:48 +00:00
|
|
|
constructor(ctx) {
|
2016-08-23 19:18:03 +00: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 08:31:48 +00:00
|
|
|
topNavigation.activate('comments');
|
2016-07-13 19:50:07 +00:00
|
|
|
topNavigation.setTitle('Listing comments');
|
2016-06-12 12:04:42 +00:00
|
|
|
|
2016-08-28 16:53:06 +00:00
|
|
|
this._pageController = new PageController();
|
|
|
|
this._pageController.run({
|
2016-07-07 19:18:35 +00:00
|
|
|
parameters: ctx.parameters,
|
2016-07-05 19:20:28 +00:00
|
|
|
getClientUrlForPage: page => {
|
2016-07-07 19:18:35 +00:00
|
|
|
const parameters = Object.assign(
|
|
|
|
{}, ctx.parameters, {page: page});
|
2017-01-20 20:51:04 +00:00
|
|
|
return uri.formatClientLink('comments', parameters);
|
2016-07-05 19:20:28 +00:00
|
|
|
},
|
2016-06-17 18:25:44 +00:00
|
|
|
requestPage: page => {
|
2016-06-19 17:16:40 +00:00
|
|
|
return PostList.search(
|
2016-09-03 23:25:19 +00:00
|
|
|
'sort:comment-date comment-count-min:1', page, 10, fields);
|
2016-06-17 18:25:44 +00:00
|
|
|
},
|
2016-06-14 08:31:48 +00:00
|
|
|
pageRenderer: pageCtx => {
|
|
|
|
Object.assign(pageCtx, {
|
|
|
|
canViewPosts: api.hasPrivilege('posts:view'),
|
|
|
|
});
|
2016-06-17 18:25:44 +00:00
|
|
|
const view = new CommentsPageView(pageCtx);
|
2016-08-22 18:45:58 +00:00
|
|
|
view.addEventListener('submit', e => this._evtUpdate(e));
|
2016-06-17 18:25:44 +00:00
|
|
|
view.addEventListener('score', e => this._evtScore(e));
|
|
|
|
view.addEventListener('delete', e => this._evtDelete(e));
|
|
|
|
return view;
|
2016-06-14 08:31:48 +00:00
|
|
|
},
|
2016-06-12 12:04:42 +00:00
|
|
|
});
|
2016-03-19 20:37:04 +00:00
|
|
|
}
|
2016-06-17 18:25:44 +00:00
|
|
|
|
2016-08-22 18:45:58 +00:00
|
|
|
_evtUpdate(e) {
|
2016-06-17 18:25:44 +00:00
|
|
|
// TODO: disable form
|
|
|
|
e.detail.comment.text = e.detail.text;
|
|
|
|
e.detail.comment.save()
|
2017-01-08 01:12:38 +00:00
|
|
|
.catch(error => {
|
|
|
|
e.detail.target.showError(error.message);
|
2016-06-17 18:25:44 +00:00
|
|
|
// TODO: enable form
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_evtScore(e) {
|
|
|
|
e.detail.comment.setScore(e.detail.score)
|
2017-01-08 01:12:38 +00:00
|
|
|
.catch(error => window.alert(error.message));
|
2016-06-17 18:25:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_evtDelete(e) {
|
|
|
|
e.detail.comment.delete()
|
2017-01-08 01:12:38 +00:00
|
|
|
.catch(error => window.alert(error.message));
|
2016-06-17 18:25:44 +00:00
|
|
|
}
|
2016-06-14 08:31:48 +00:00
|
|
|
};
|
2016-03-19 20:37:04 +00:00
|
|
|
|
2016-06-14 08:31:48 +00:00
|
|
|
module.exports = router => {
|
2017-01-20 20:51:04 +00:00
|
|
|
router.enter(['comments'],
|
2016-06-14 08:31:48 +00:00
|
|
|
(ctx, next) => { new CommentsController(ctx); });
|
|
|
|
};
|