2016-06-11 15:41:28 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const api = require('../api.js');
|
|
|
|
const views = require('../util/views.js');
|
2016-06-12 16:08:50 +00:00
|
|
|
const CommentFormControl = require('../controls/comment_form_control.js');
|
2016-06-11 15:41:28 +00:00
|
|
|
|
|
|
|
class CommentControl {
|
2016-06-12 16:08:50 +00:00
|
|
|
constructor(hostNode, comment, settings) {
|
2016-06-11 15:41:28 +00:00
|
|
|
this._hostNode = hostNode;
|
|
|
|
this._comment = comment;
|
|
|
|
this._template = views.getTemplate('comment');
|
|
|
|
this._scoreTemplate = views.getTemplate('score');
|
2016-06-12 16:08:50 +00:00
|
|
|
this._settings = settings;
|
2016-06-11 15:41:28 +00:00
|
|
|
|
|
|
|
this.install();
|
|
|
|
}
|
|
|
|
|
|
|
|
install() {
|
|
|
|
const isLoggedIn = api.isLoggedIn(this._comment.user);
|
|
|
|
const infix = isLoggedIn ? 'own' : 'any';
|
|
|
|
const sourceNode = this._template({
|
|
|
|
comment: this._comment,
|
|
|
|
canViewUsers: api.hasPrivilege('users:view'),
|
|
|
|
canEditComment: api.hasPrivilege(`comments:edit:${infix}`),
|
|
|
|
canDeleteComment: api.hasPrivilege(`comments:delete:${infix}`),
|
|
|
|
});
|
|
|
|
|
2016-06-14 08:31:48 +00:00
|
|
|
views.replaceContent(
|
2016-06-11 15:41:28 +00:00
|
|
|
sourceNode.querySelector('.score-container'),
|
|
|
|
this._scoreTemplate({
|
|
|
|
score: this._comment.score,
|
|
|
|
ownScore: this._comment.ownScore,
|
|
|
|
canScore: api.hasPrivilege('comments:score'),
|
|
|
|
}));
|
|
|
|
|
|
|
|
const editButton = sourceNode.querySelector('.edit');
|
|
|
|
const deleteButton = sourceNode.querySelector('.delete');
|
|
|
|
const upvoteButton = sourceNode.querySelector('.upvote');
|
|
|
|
const downvoteButton = sourceNode.querySelector('.downvote');
|
|
|
|
|
|
|
|
if (editButton) {
|
|
|
|
editButton.addEventListener(
|
|
|
|
'click', e => this._evtEditClick(e));
|
|
|
|
}
|
|
|
|
if (deleteButton) {
|
|
|
|
deleteButton.addEventListener(
|
|
|
|
'click', e => this._evtDeleteClick(e));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (upvoteButton) {
|
|
|
|
upvoteButton.addEventListener(
|
|
|
|
'click',
|
|
|
|
e => this._evtScoreClick(
|
|
|
|
e, () => this._comment.ownScore === 1 ? 0 : 1));
|
|
|
|
}
|
|
|
|
if (downvoteButton) {
|
|
|
|
downvoteButton.addEventListener(
|
|
|
|
'click',
|
|
|
|
e => this._evtScoreClick(
|
|
|
|
e, () => this._comment.ownScore === -1 ? 0 : -1));
|
|
|
|
}
|
|
|
|
|
2016-06-12 16:08:50 +00:00
|
|
|
this._formControl = new CommentFormControl(
|
|
|
|
sourceNode.querySelector('.comment-form-container'),
|
|
|
|
this._comment,
|
|
|
|
{
|
|
|
|
onSave: text => {
|
|
|
|
return api.put('/comment/' + this._comment.id, {
|
|
|
|
text: text,
|
|
|
|
}).then(response => {
|
|
|
|
this._comment = response;
|
|
|
|
this.install();
|
|
|
|
}, response => {
|
|
|
|
this._formControl.showError(response.description);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
canCancel: true
|
2016-06-11 15:41:28 +00:00
|
|
|
});
|
|
|
|
|
2016-06-14 08:31:48 +00:00
|
|
|
views.replaceContent(this._hostNode, sourceNode);
|
2016-06-11 15:41:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_evtScoreClick(e, scoreGetter) {
|
|
|
|
e.preventDefault();
|
|
|
|
api.put(
|
|
|
|
'/comment/' + this._comment.id + '/score',
|
|
|
|
{score: scoreGetter()})
|
|
|
|
.then(
|
|
|
|
response => {
|
|
|
|
this._comment.score = parseInt(response.score);
|
|
|
|
this._comment.ownScore = parseInt(response.ownScore);
|
|
|
|
this.install();
|
|
|
|
}, response => {
|
|
|
|
window.alert(response.description);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-06-12 16:08:50 +00:00
|
|
|
_evtEditClick(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
this._formControl.enterEditMode();
|
|
|
|
}
|
|
|
|
|
2016-06-11 15:41:28 +00:00
|
|
|
_evtDeleteClick(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
if (!window.confirm('Are you sure you want to delete this comment?')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
api.delete('/comment/' + this._comment.id)
|
|
|
|
.then(response => {
|
2016-06-12 16:08:50 +00:00
|
|
|
if (this._settings.onDelete) {
|
|
|
|
this._settings.onDelete(this._comment);
|
|
|
|
}
|
2016-06-11 15:41:28 +00:00
|
|
|
this._hostNode.parentNode.removeChild(this._hostNode);
|
|
|
|
}, response => {
|
|
|
|
window.alert(response.description);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = CommentControl;
|