2020-06-06 00:03:37 +02:00
|
|
|
"use strict";
|
2016-03-19 21:37:04 +01:00
|
|
|
|
2020-06-06 00:03:37 +02:00
|
|
|
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");
|
2016-03-19 21:37:04 +01:00
|
|
|
|
2020-06-06 00:03:37 +02:00
|
|
|
const fields = ["id", "comments", "commentCount", "thumbnailUrl"];
|
2016-06-19 19:16:40 +02:00
|
|
|
|
2016-04-01 00:20:34 +02:00
|
|
|
class CommentsController {
|
2016-06-14 10:31:48 +02:00
|
|
|
constructor(ctx) {
|
2020-06-06 00:03:37 +02:00
|
|
|
if (!api.hasPrivilege("comments:list")) {
|
2016-08-23 21:18:03 +02:00
|
|
|
this._view = new EmptyView();
|
|
|
|
this._view.showError(
|
2020-06-06 00:03:37 +02:00
|
|
|
"You don't have privileges to view comments."
|
|
|
|
);
|
2016-08-23 21:18:03 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-06 00:03:37 +02:00
|
|
|
topNavigation.activate("comments");
|
|
|
|
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,
|
2017-02-09 00:48:06 +01:00
|
|
|
defaultLimit: 10,
|
|
|
|
getClientUrlForPage: (offset, limit) => {
|
2020-06-06 00:03:37 +02:00
|
|
|
const parameters = Object.assign({}, ctx.parameters, {
|
|
|
|
offset: offset,
|
|
|
|
limit: limit,
|
|
|
|
});
|
|
|
|
return uri.formatClientLink("comments", parameters);
|
2016-07-05 21:20:28 +02:00
|
|
|
},
|
2017-02-09 00:48:06 +01:00
|
|
|
requestPage: (offset, limit) => {
|
2016-06-19 19:16:40 +02:00
|
|
|
return PostList.search(
|
2020-06-06 00:03:37 +02:00
|
|
|
"sort:comment-date comment-count-min:1",
|
2020-06-04 20:09:35 +02:00
|
|
|
offset,
|
|
|
|
limit,
|
2020-06-06 00:03:37 +02:00
|
|
|
fields
|
|
|
|
);
|
2016-06-17 20:25:44 +02:00
|
|
|
},
|
2020-06-06 00:03:37 +02:00
|
|
|
pageRenderer: (pageCtx) => {
|
2016-06-14 10:31:48 +02:00
|
|
|
Object.assign(pageCtx, {
|
2020-06-06 00:03:37 +02:00
|
|
|
canViewPosts: api.hasPrivilege("posts:view"),
|
2016-06-14 10:31:48 +02:00
|
|
|
});
|
2016-06-17 20:25:44 +02:00
|
|
|
const view = new CommentsPageView(pageCtx);
|
2020-06-06 00:03:37 +02:00
|
|
|
view.addEventListener("submit", (e) => this._evtUpdate(e));
|
|
|
|
view.addEventListener("score", (e) => this._evtScore(e));
|
|
|
|
view.addEventListener("delete", (e) => this._evtDelete(e));
|
2016-06-17 20:25:44 +02:00
|
|
|
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;
|
2020-06-06 00:03:37 +02:00
|
|
|
e.detail.comment.save().catch((error) => {
|
|
|
|
e.detail.target.showError(error.message);
|
|
|
|
// TODO: enable form
|
|
|
|
});
|
2016-06-17 20:25:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_evtScore(e) {
|
2020-06-06 00:03:37 +02:00
|
|
|
e.detail.comment
|
|
|
|
.setScore(e.detail.score)
|
|
|
|
.catch((error) => window.alert(error.message));
|
2016-06-17 20:25:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_evtDelete(e) {
|
2020-06-06 00:03:37 +02:00
|
|
|
e.detail.comment
|
|
|
|
.delete()
|
|
|
|
.catch((error) => window.alert(error.message));
|
2016-06-17 20:25:44 +02:00
|
|
|
}
|
2020-06-04 20:09:35 +02:00
|
|
|
}
|
2016-03-19 21:37:04 +01:00
|
|
|
|
2020-06-06 00:03:37 +02:00
|
|
|
module.exports = (router) => {
|
|
|
|
router.enter(["comments"], (ctx, next) => {
|
2020-06-04 20:09:35 +02:00
|
|
|
new CommentsController(ctx);
|
|
|
|
});
|
2020-06-06 00:03:37 +02:00
|
|
|
};
|