2016-06-11 17:41:28 +02:00
|
|
|
'use strict';
|
|
|
|
|
2016-06-17 20:25:44 +02:00
|
|
|
const events = require('../events.js');
|
2016-06-11 17:41:28 +02:00
|
|
|
const views = require('../util/views.js');
|
|
|
|
const CommentControl = require('../controls/comment_control.js');
|
|
|
|
|
2016-06-17 20:25:44 +02:00
|
|
|
const template = views.getTemplate('comment-list');
|
|
|
|
|
|
|
|
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 = {};
|
|
|
|
|
|
|
|
comments.addEventListener('add', e => this._evtAdd(e));
|
|
|
|
comments.addEventListener('remove', e => this._evtRemove(e));
|
|
|
|
|
|
|
|
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() {
|
|
|
|
return this._hostNode.querySelector('ul');
|
|
|
|
}
|
2016-06-11 17:41:28 +02:00
|
|
|
|
2016-06-17 20:25:44 +02:00
|
|
|
_installCommentNode(comment) {
|
|
|
|
const commentListItemNode = document.createElement('li');
|
|
|
|
const commentControl = new CommentControl(
|
|
|
|
commentListItemNode, comment);
|
2016-08-22 20:45:58 +02:00
|
|
|
events.proxyEvent(commentControl, this, 'submit');
|
2016-06-17 20:25:44 +02:00
|
|
|
events.proxyEvent(commentControl, this, 'score');
|
|
|
|
events.proxyEvent(commentControl, this, 'delete');
|
|
|
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = CommentListControl;
|