gallery.accords-library.com/client/js/controllers/comments_controller.js

80 lines
2.6 KiB
JavaScript
Raw Normal View History

'use strict';
const api = require('../api.js');
const uri = require('../util/uri.js');
const PostList = require('../models/post_list.js');
const topNavigation = require('../models/top_navigation.js');
const PageController = require('../controllers/page_controller.js');
const CommentsPageView = require('../views/comments_page_view.js');
const EmptyView = require('../views/empty_view.js');
const fields = ['id', 'comments', 'commentCount', 'thumbnailUrl'];
class CommentsController {
constructor(ctx) {
if (!api.hasPrivilege('comments:list')) {
this._view = new EmptyView();
this._view.showError(
'You don\'t have privileges to view comments.');
return;
}
topNavigation.activate('comments');
topNavigation.setTitle('Listing comments');
this._pageController = new PageController();
this._pageController.run({
parameters: ctx.parameters,
2017-02-08 23:48:06 +00:00
defaultLimit: 10,
getClientUrlForPage: (offset, limit) => {
const parameters = Object.assign(
2017-02-08 23:48:06 +00:00
{}, ctx.parameters, {offset: offset, limit: limit});
return uri.formatClientLink('comments', parameters);
2016-07-05 19:20:28 +00:00
},
2017-02-08 23:48:06 +00:00
requestPage: (offset, limit) => {
return PostList.search(
2017-02-08 23:48:06 +00:00
'sort:comment-date comment-count-min:1',
2020-06-04 18:09:35 +00:00
offset,
limit,
fields);
},
pageRenderer: pageCtx => {
Object.assign(pageCtx, {
canViewPosts: api.hasPrivilege('posts:view'),
});
const view = new CommentsPageView(pageCtx);
view.addEventListener('submit', e => this._evtUpdate(e));
view.addEventListener('score', e => this._evtScore(e));
view.addEventListener('delete', e => this._evtDelete(e));
return view;
},
});
}
_evtUpdate(e) {
// 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);
// 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));
}
_evtDelete(e) {
e.detail.comment.delete()
2017-01-08 01:12:38 +00:00
.catch(error => window.alert(error.message));
}
2020-06-04 18:09:35 +00:00
}
module.exports = router => {
2020-06-04 18:09:35 +00:00
router.enter(['comments'], (ctx, next) => {
new CommentsController(ctx);
});
}