2023-04-19 05:20:02 +08:00
|
|
|
import {Component} from './component';
|
2023-06-08 00:47:37 +08:00
|
|
|
import {getLoading, htmlToDom} from '../services/dom';
|
2019-06-08 07:02:51 +08:00
|
|
|
|
2022-11-15 19:24:31 +08:00
|
|
|
export class PageComments extends Component {
|
2017-09-03 23:37:51 +08:00
|
|
|
|
2020-07-29 01:19:18 +08:00
|
|
|
setup() {
|
|
|
|
this.elem = this.$el;
|
|
|
|
this.pageId = Number(this.$opts.pageId);
|
|
|
|
|
|
|
|
// Element references
|
|
|
|
this.container = this.$refs.commentContainer;
|
|
|
|
this.commentCountBar = this.$refs.commentCountBar;
|
2023-06-08 00:47:37 +08:00
|
|
|
this.commentsTitle = this.$refs.commentsTitle;
|
2020-07-29 01:19:18 +08:00
|
|
|
this.addButtonContainer = this.$refs.addButtonContainer;
|
|
|
|
this.replyToRow = this.$refs.replyToRow;
|
2023-06-08 00:47:37 +08:00
|
|
|
this.formContainer = this.$refs.formContainer;
|
|
|
|
this.form = this.$refs.form;
|
|
|
|
this.formInput = this.$refs.formInput;
|
2023-06-10 00:36:30 +08:00
|
|
|
this.formReplyLink = this.$refs.formReplyLink;
|
2023-06-08 00:47:37 +08:00
|
|
|
this.addCommentButton = this.$refs.addCommentButton;
|
|
|
|
this.hideFormButton = this.$refs.hideFormButton;
|
|
|
|
this.removeReplyToButton = this.$refs.removeReplyToButton;
|
2020-07-29 01:19:18 +08:00
|
|
|
|
|
|
|
// Translations
|
|
|
|
this.createdText = this.$opts.createdText;
|
|
|
|
this.countText = this.$opts.countText;
|
|
|
|
|
|
|
|
// Internal State
|
2017-09-10 00:06:30 +08:00
|
|
|
this.parentId = null;
|
2023-09-12 01:40:40 +08:00
|
|
|
this.formReplyText = this.formReplyLink?.textContent || '';
|
2017-09-03 23:37:51 +08:00
|
|
|
|
2023-06-08 00:47:37 +08:00
|
|
|
this.setupListeners();
|
2017-09-03 23:37:51 +08:00
|
|
|
}
|
|
|
|
|
2023-06-08 00:47:37 +08:00
|
|
|
setupListeners() {
|
|
|
|
this.elem.addEventListener('page-comment-delete', () => {
|
2017-09-03 23:37:51 +08:00
|
|
|
this.updateCount();
|
2019-04-13 19:58:57 +08:00
|
|
|
this.hideForm();
|
2017-09-03 23:37:51 +08:00
|
|
|
});
|
2023-06-08 00:47:37 +08:00
|
|
|
|
|
|
|
this.elem.addEventListener('page-comment-reply', event => {
|
2023-06-08 22:03:38 +08:00
|
|
|
this.setReply(event.detail.id, event.detail.element);
|
2023-06-08 00:47:37 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
if (this.form) {
|
2023-09-12 01:40:40 +08:00
|
|
|
this.removeReplyToButton.addEventListener('click', this.removeReplyTo.bind(this));
|
|
|
|
this.hideFormButton.addEventListener('click', this.hideForm.bind(this));
|
|
|
|
this.addCommentButton.addEventListener('click', this.showForm.bind(this));
|
2023-06-08 00:47:37 +08:00
|
|
|
this.form.addEventListener('submit', this.saveComment.bind(this));
|
|
|
|
}
|
2017-09-03 23:37:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
saveComment(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
2023-06-08 00:47:37 +08:00
|
|
|
|
|
|
|
const loading = getLoading();
|
|
|
|
loading.classList.add('px-l');
|
|
|
|
this.form.after(loading);
|
|
|
|
this.form.toggleAttribute('hidden', true);
|
|
|
|
|
2023-04-19 05:20:02 +08:00
|
|
|
const text = this.formInput.value;
|
|
|
|
const reqData = {
|
|
|
|
text,
|
2017-09-09 22:56:24 +08:00
|
|
|
parent_id: this.parentId || null,
|
2017-09-03 23:37:51 +08:00
|
|
|
};
|
2023-06-08 00:47:37 +08:00
|
|
|
|
2020-07-29 01:19:18 +08:00
|
|
|
window.$http.post(`/comment/${this.pageId}`, reqData).then(resp => {
|
2022-11-15 19:24:31 +08:00
|
|
|
const newElem = htmlToDom(resp.data);
|
2023-06-08 22:03:38 +08:00
|
|
|
this.formContainer.after(newElem);
|
2020-07-29 01:19:18 +08:00
|
|
|
window.$events.success(this.createdText);
|
2023-06-08 22:03:38 +08:00
|
|
|
this.hideForm();
|
2017-09-03 23:37:51 +08:00
|
|
|
this.updateCount();
|
2020-07-29 01:19:18 +08:00
|
|
|
}).catch(err => {
|
2023-06-08 00:47:37 +08:00
|
|
|
this.form.toggleAttribute('hidden', false);
|
2020-07-29 01:19:18 +08:00
|
|
|
window.$events.showValidationErrors(err);
|
2017-09-03 23:37:51 +08:00
|
|
|
});
|
2023-06-08 00:47:37 +08:00
|
|
|
|
2023-06-08 22:03:38 +08:00
|
|
|
this.form.toggleAttribute('hidden', false);
|
2023-06-08 00:47:37 +08:00
|
|
|
loading.remove();
|
2017-09-03 23:37:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
updateCount() {
|
2023-06-08 00:47:37 +08:00
|
|
|
const count = this.getCommentCount();
|
|
|
|
this.commentsTitle.textContent = window.trans_plural(this.countText, count, {count});
|
2017-09-03 23:37:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
resetForm() {
|
|
|
|
this.formInput.value = '';
|
2023-06-10 00:36:30 +08:00
|
|
|
this.parentId = null;
|
|
|
|
this.replyToRow.toggleAttribute('hidden', true);
|
2023-06-08 22:03:38 +08:00
|
|
|
this.container.append(this.formContainer);
|
2017-09-03 23:37:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
showForm() {
|
2023-06-08 00:47:37 +08:00
|
|
|
this.formContainer.toggleAttribute('hidden', false);
|
|
|
|
this.addButtonContainer.toggleAttribute('hidden', true);
|
2023-06-10 00:36:30 +08:00
|
|
|
this.formContainer.scrollIntoView({behavior: 'smooth', block: 'nearest'});
|
2023-06-08 22:03:38 +08:00
|
|
|
setTimeout(() => {
|
|
|
|
this.formInput.focus();
|
|
|
|
}, 100);
|
2017-09-03 23:37:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
hideForm() {
|
2023-06-08 22:03:38 +08:00
|
|
|
this.resetForm();
|
2023-06-08 00:47:37 +08:00
|
|
|
this.formContainer.toggleAttribute('hidden', true);
|
2019-04-13 19:58:57 +08:00
|
|
|
if (this.getCommentCount() > 0) {
|
2023-06-08 22:03:38 +08:00
|
|
|
this.elem.append(this.addButtonContainer);
|
2019-04-13 19:58:57 +08:00
|
|
|
} else {
|
2023-06-08 22:03:38 +08:00
|
|
|
this.commentCountBar.append(this.addButtonContainer);
|
2019-04-13 19:58:57 +08:00
|
|
|
}
|
2023-06-08 00:47:37 +08:00
|
|
|
this.addButtonContainer.toggleAttribute('hidden', false);
|
2019-04-13 19:58:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
getCommentCount() {
|
2023-06-08 22:03:38 +08:00
|
|
|
return this.container.querySelectorAll('[component="page-comment"]').length;
|
2017-09-03 23:37:51 +08:00
|
|
|
}
|
|
|
|
|
2023-06-08 22:03:38 +08:00
|
|
|
setReply(commentLocalId, commentElement) {
|
|
|
|
const targetFormLocation = commentElement.closest('.comment-branch').querySelector('.comment-branch-children');
|
|
|
|
targetFormLocation.append(this.formContainer);
|
2023-06-10 00:36:30 +08:00
|
|
|
this.showForm();
|
2023-06-08 00:47:37 +08:00
|
|
|
this.parentId = commentLocalId;
|
|
|
|
this.replyToRow.toggleAttribute('hidden', false);
|
2023-09-12 01:40:40 +08:00
|
|
|
this.formReplyLink.textContent = this.formReplyText.replace('1234', this.parentId);
|
|
|
|
this.formReplyLink.href = `#comment${this.parentId}`;
|
2017-09-09 22:56:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
removeReplyTo() {
|
|
|
|
this.parentId = null;
|
2023-06-08 00:47:37 +08:00
|
|
|
this.replyToRow.toggleAttribute('hidden', true);
|
2023-06-10 00:36:30 +08:00
|
|
|
this.container.append(this.formContainer);
|
|
|
|
this.showForm();
|
2017-09-10 00:06:30 +08:00
|
|
|
}
|
2017-09-03 23:37:51 +08:00
|
|
|
|
2023-04-19 05:20:02 +08:00
|
|
|
}
|