2020-06-06 00:03:37 +02:00
|
|
|
"use strict";
|
2016-06-11 17:41:28 +02:00
|
|
|
|
2020-06-06 00:03:37 +02:00
|
|
|
const events = require("../events.js");
|
|
|
|
const views = require("../util/views.js");
|
|
|
|
const CommentControl = require("../controls/comment_control.js");
|
2016-06-11 17:41:28 +02:00
|
|
|
|
2020-06-06 00:03:37 +02:00
|
|
|
const template = views.getTemplate("comment-list");
|
2016-06-17 20:25:44 +02:00
|
|
|
|
|
|
|
class CommentListControl extends events.EventTarget {
|
|
|
|
constructor(hostNode, comments, reversed) {
|
|
|
|
super();
|
2016-06-11 17:41:28 +02:00
|
|
|
this._hostNode = hostNode;
|
|
|
|
this._comments = comments;
|
2016-06-17 20:25:44 +02:00
|
|
|
this._commentIdToNode = {};
|
|
|
|
|
2020-06-06 00:03:37 +02:00
|
|
|
comments.addEventListener("add", (e) => this._evtAdd(e));
|
|
|
|
comments.addEventListener("remove", (e) => this._evtRemove(e));
|
2016-06-17 20:25:44 +02:00
|
|
|
|
|
|
|
views.replaceContent(this._hostNode, template());
|
2016-06-11 17:41:28 +02:00
|
|
|
|
2016-06-17 20:25:44 +02:00
|
|
|
const commentList = Array.from(comments);
|
|
|
|
if (reversed) {
|
|
|
|
commentList.reverse();
|
|
|
|
}
|
|
|
|
for (let comment of commentList) {
|
|
|
|
this._installCommentNode(comment);
|
|
|
|
}
|
2016-06-11 17:41:28 +02:00
|
|
|
}
|
|
|
|
|
2016-06-17 20:25:44 +02:00
|
|
|
get _commentListNode() {
|
2020-06-06 00:03:37 +02:00
|
|
|
return this._hostNode.querySelector("ul");
|
2016-06-17 20:25:44 +02:00
|
|
|
}
|
2016-06-11 17:41:28 +02:00
|
|
|
|
2016-06-17 20:25:44 +02:00
|
|
|
_installCommentNode(comment) {
|
2020-06-06 00:03:37 +02:00
|
|
|
const commentListItemNode = document.createElement("li");
|
2016-06-17 20:25:44 +02:00
|
|
|
const commentControl = new CommentControl(
|
2020-06-06 00:03:37 +02:00
|
|
|
commentListItemNode,
|
|
|
|
comment,
|
|
|
|
false
|
|
|
|
);
|
|
|
|
events.proxyEvent(commentControl, this, "submit");
|
|
|
|
events.proxyEvent(commentControl, this, "score");
|
|
|
|
events.proxyEvent(commentControl, this, "delete");
|
2016-06-17 20:25:44 +02:00
|
|
|
this._commentIdToNode[comment.id] = commentListItemNode;
|
|
|
|
this._commentListNode.appendChild(commentListItemNode);
|
|
|
|
}
|
2016-06-11 17:41:28 +02:00
|
|
|
|
2016-06-17 20:25:44 +02:00
|
|
|
_uninstallCommentNode(comment) {
|
|
|
|
const commentListItemNode = this._commentIdToNode[comment.id];
|
|
|
|
commentListItemNode.parentNode.removeChild(commentListItemNode);
|
2016-06-11 17:41:28 +02:00
|
|
|
}
|
|
|
|
|
2016-06-17 20:25:44 +02:00
|
|
|
_evtAdd(e) {
|
|
|
|
this._installCommentNode(e.detail.comment);
|
|
|
|
}
|
|
|
|
|
|
|
|
_evtRemove(e) {
|
|
|
|
this._uninstallCommentNode(e.detail.comment);
|
2016-06-11 17:41:28 +02:00
|
|
|
}
|
2020-06-04 20:09:35 +02:00
|
|
|
}
|
2016-06-11 17:41:28 +02:00
|
|
|
|
|
|
|
module.exports = CommentListControl;
|