misuzu/assets/typescript/Comments.ts

510 lines
20 KiB
TypeScript
Raw Normal View History

2018-12-09 03:42:38 +00:00
/// <reference path="FormUtilities.ts" />
2018-12-09 23:56:36 +00:00
enum CommentVoteType {
Indifferent = 0,
Like = 1,
Dislike = -1,
}
2018-12-09 03:42:38 +00:00
interface CommentNotice {
error: string;
message: string;
}
2018-12-09 03:42:38 +00:00
interface CommentDeletionInfo extends CommentNotice {
id: number; // minor inconsistency, deal with it
2018-12-09 03:42:38 +00:00
}
interface CommentPostInfo extends CommentNotice {
comment_id: number;
category_id: number;
comment_text: string;
comment_html: string;
comment_created: Date;
comment_edited: Date | null;
comment_deleted: Date | null;
comment_reply_to: number;
comment_pinned: Date | null;
user_id: number;
username: string;
user_colour: number;
}
2018-12-09 23:56:36 +00:00
interface CommentVotesInfo extends CommentNotice {
comment_id: number;
likes: number;
dislikes: number;
}
function commentDeleteEventHandler(ev: Event): void {
const target: HTMLAnchorElement = ev.target as HTMLAnchorElement,
commentId: number = parseInt(target.dataset.commentId);
commentDelete(
commentId,
info => {
let elem = document.getElementById('comment-' + info.id);
if (elem)
elem.parentNode.removeChild(elem);
},
message => messageBox(message)
);
}
function commentDelete(commentId: number, onSuccess: (info: CommentDeletionInfo) => void = null, onFail: (message: string) => void = null): void
{
if (!checkUserPerm('comments', CommentPermission.Delete)) {
if (onFail)
onFail("You aren't allowed to delete comments.");
return;
}
const xhr: XMLHttpRequest = new XMLHttpRequest;
xhr.addEventListener('readystatechange', () => {
if (xhr.readyState !== 4)
return;
updateCSRF(xhr.getResponseHeader('X-Misuzu-CSRF'));
2018-12-09 03:42:38 +00:00
let json: CommentDeletionInfo = JSON.parse(xhr.responseText) as CommentDeletionInfo,
message = json.error || json.message;
if (message && onFail)
onFail(message);
else if (!message && onSuccess)
onSuccess(json);
});
xhr.open('GET', urlFormat('comments-delete', [{name:'comment',value:commentId}]));
xhr.setRequestHeader('X-Misuzu-XHR', 'comments');
xhr.send();
}
2018-12-09 03:42:38 +00:00
function commentPostEventHandler(ev: Event): void
{
2018-12-09 23:56:36 +00:00
const form: HTMLFormElement = ev.target as HTMLFormElement;
2018-12-09 03:42:38 +00:00
if (form.dataset.disabled)
return;
form.dataset.disabled = '1';
form.style.opacity = '0.5';
2018-12-09 03:42:38 +00:00
commentPost(
extractFormData(form, true),
2018-12-09 03:42:38 +00:00
info => commentPostSuccess(form, info),
message => commentPostFail(form, message)
2018-12-09 03:42:38 +00:00
);
}
function commentPost(formData: FormData, onSuccess: (comment: CommentPostInfo) => void = null, onFail: (message: string) => void = null): void
{
if (!checkUserPerm('comments', CommentPermission.Create)) {
if (onFail)
onFail("You aren't allowed to post comments.");
2018-12-09 03:42:38 +00:00
return;
}
2018-12-09 03:42:38 +00:00
const xhr = new XMLHttpRequest();
xhr.addEventListener('readystatechange', () => {
if (xhr.readyState !== 4)
return;
updateCSRF(xhr.getResponseHeader('X-Misuzu-CSRF'));
2018-12-09 23:56:36 +00:00
2018-12-09 03:42:38 +00:00
const json: CommentPostInfo = JSON.parse(xhr.responseText) as CommentPostInfo,
message: string = json.error || json.message;
if (message && onFail)
onFail(message);
else if (!message && onSuccess)
onSuccess(json);
});
xhr.open('POST', urlFormat('comment-create'));
2018-12-09 03:42:38 +00:00
xhr.setRequestHeader('X-Misuzu-XHR', 'comments');
xhr.send(formData);
}
function commentPostSuccess(form: HTMLFormElement, comment: CommentPostInfo): void {
if (form.classList.contains('comment--reply'))
(form.parentNode.parentNode.querySelector('label.comment__action') as HTMLLabelElement).click();
2018-12-09 23:56:36 +00:00
commentInsert(comment, form);
form.style.opacity = '1';
form.dataset.disabled = '';
2018-12-09 03:42:38 +00:00
}
function commentPostFail(form: HTMLFormElement, message: string): void {
messageBox(message);
form.style.opacity = '1';
form.dataset.disabled = '';
2018-12-09 03:42:38 +00:00
}
function commentsInit(): void {
const commentDeletes: HTMLCollectionOf<HTMLAnchorElement> = document.getElementsByClassName('comment__action--delete') as HTMLCollectionOf<HTMLAnchorElement>;
2018-12-09 03:42:38 +00:00
for (let i = 0; i < commentDeletes.length; i++) {
commentDeletes[i].addEventListener('click', commentDeleteEventHandler);
commentDeletes[i].dataset.href = commentDeletes[i].href;
commentDeletes[i].href = 'javascript:void(0);';
}
2018-12-09 03:42:38 +00:00
const commentInputs: HTMLCollectionOf<HTMLTextAreaElement> = document.getElementsByClassName('comment__text--input') as HTMLCollectionOf<HTMLTextAreaElement>;
for (let i = 0; i < commentInputs.length; i++) {
2018-12-09 23:56:36 +00:00
commentInputs[i].form.action = 'javascript:void(0);';
commentInputs[i].form.addEventListener('submit', commentPostEventHandler);
commentInputs[i].addEventListener('keydown', commentInputEventHandler);
}
const voteButtons: HTMLCollectionOf<HTMLAnchorElement> = document.getElementsByClassName('comment__action--vote') as HTMLCollectionOf<HTMLAnchorElement>;
for (let i = 0; i < voteButtons.length; i++)
{
voteButtons[i].href = 'javascript:void(0);';
voteButtons[i].addEventListener('click', commentVoteEventHandler);
}
const pinButtons: HTMLCollectionOf<HTMLAnchorElement> = document.getElementsByClassName('comment__action--pin') as HTMLCollectionOf<HTMLAnchorElement>;
for (let i = 0; i < pinButtons.length; i++) {
pinButtons[i].href = 'javascript:void(0);';
pinButtons[i].addEventListener('click', commentPinEventHandler);
}
}
function commentInputEventHandler(ev: KeyboardEvent): void {
if (ev.code === 'Enter' && ev.ctrlKey && !ev.altKey && !ev.shiftKey && !ev.metaKey) {
const form: HTMLFormElement = (ev.target as HTMLTextAreaElement).form;
commentPost(
extractFormData(form, true),
info => commentPostSuccess(form, info),
message => commentPostFail
);
2018-12-09 03:42:38 +00:00
}
}
function commentConstruct(comment: CommentPostInfo, layer: number = 0): HTMLElement {
const commentElement: HTMLDivElement = document.createElement('div');
commentElement.className = 'comment';
commentElement.id = 'comment-' + comment.comment_id;
// layer 2
const commentContainer: HTMLDivElement = commentElement.appendChild(document.createElement('div'));
commentContainer.className = 'comment__container';
const commentReplies: HTMLDivElement = commentElement.appendChild(document.createElement('div'));
commentReplies.className = 'comment__replies comment__replies--indent-' + layer;
commentReplies.id = commentElement.id + '-replies';
// container
const commentAvatar: HTMLAnchorElement = commentContainer.appendChild(document.createElement('a'));
commentAvatar.className = 'avatar comment__avatar';
commentAvatar.href = urlFormat('user-profile', [{name:'user',value:comment.user_id}]);
2019-03-25 20:11:31 +00:00
commentAvatar.style.backgroundImage = "url('{0}')".replace('{0}', urlFormat('user-avatar', [
{ name: 'user', value: comment.user_id },
{ name: 'res', value: layer < 1 ? 100 : 80 }
]));
2018-12-09 03:42:38 +00:00
const commentContent: HTMLDivElement = commentContainer.appendChild(document.createElement('div'));
commentContent.className = 'comment__content';
// content
const commentInfo = commentContent.appendChild(document.createElement('div'));
commentInfo.className = 'comment__info';
const commentText = commentContent.appendChild(document.createElement('div'));
commentText.className = 'comment__text';
if (comment.comment_html)
commentText.innerHTML = comment.comment_html;
else
commentText.textContent = comment.comment_text;
const commentActions = commentContent.appendChild(document.createElement('div'));
commentActions.className = 'comment__actions';
// info
2018-12-09 23:56:36 +00:00
const commentUser: HTMLAnchorElement = commentInfo.appendChild(document.createElement('a'));
2018-12-09 03:42:38 +00:00
commentUser.className = 'comment__user comment__user--link';
commentUser.textContent = comment.username;
commentUser.href = '/profile?u=' + comment.user_id;
2018-12-11 20:54:23 +00:00
commentUser.style.setProperty('--user-colour', colourGetCSS(comment.user_colour));
2018-12-09 03:42:38 +00:00
2018-12-09 23:56:36 +00:00
const commentLink: HTMLAnchorElement = commentInfo.appendChild(document.createElement('a'));
2018-12-09 03:42:38 +00:00
commentLink.className = 'comment__link';
commentLink.href = '#' + commentElement.id;
2018-12-09 23:56:36 +00:00
const commentTime: HTMLTimeElement = commentLink.appendChild(document.createElement('time')),
2018-12-09 03:42:38 +00:00
commentDate = new Date(comment.comment_created + 'Z');
commentTime.className = 'comment__date';
commentTime.title = commentDate.toLocaleString();
commentTime.dateTime = commentDate.toISOString();
commentTime.textContent = timeago().format(commentDate);
2018-12-09 23:56:36 +00:00
timeago().render(commentTime);
2018-12-09 03:42:38 +00:00
// actions
2018-12-09 23:56:36 +00:00
if (checkUserPerm('comments', CommentPermission.Vote)) {
const commentLike: HTMLAnchorElement = commentActions.appendChild(document.createElement('a'));
commentLike.dataset['commentId'] = comment.comment_id.toString();
commentLike.dataset['commentVote'] = CommentVoteType.Like.toString();
commentLike.className = 'comment__action comment__action--link comment__action--vote comment__action--like';
2018-12-09 03:42:38 +00:00
commentLike.href = 'javascript:void(0);';
commentLike.textContent = 'Like';
2018-12-09 23:56:36 +00:00
commentLike.addEventListener('click', commentVoteEventHandler);
2018-12-09 03:42:38 +00:00
2018-12-09 23:56:36 +00:00
const commentDislike: HTMLAnchorElement = commentActions.appendChild(document.createElement('a'));
commentDislike.dataset['commentId'] = comment.comment_id.toString();
commentDislike.dataset['commentVote'] = CommentVoteType.Dislike.toString();
commentDislike.className = 'comment__action comment__action--link comment__action--vote comment__action--dislike';
2018-12-09 03:42:38 +00:00
commentDislike.href = 'javascript:void(0);';
commentDislike.textContent = 'Dislike';
commentDislike.addEventListener('click', commentVoteEventHandler);
2018-12-09 03:42:38 +00:00
}
// if we're executing this it's fairly obvious that we can reply,
// so no need to have a permission check on it here
2018-12-09 23:56:36 +00:00
const commentReply: HTMLLabelElement = commentActions.appendChild(document.createElement('label'));
2018-12-09 03:42:38 +00:00
commentReply.className = 'comment__action comment__action--link';
commentReply.htmlFor = 'comment-reply-toggle-' + comment.comment_id;
commentReply.textContent = 'Reply';
// reply section
2018-12-09 23:56:36 +00:00
const commentReplyState: HTMLInputElement = commentReplies.appendChild(document.createElement('input'));
2018-12-09 03:42:38 +00:00
commentReplyState.id = commentReply.htmlFor;
commentReplyState.type = 'checkbox';
commentReplyState.className = 'comment__reply-toggle';
2018-12-09 23:56:36 +00:00
const commentReplyInput: HTMLFormElement = commentReplies.appendChild(document.createElement('form'));
2018-12-09 03:42:38 +00:00
commentReplyInput.id = 'comment-reply-' + comment.comment_id;
commentReplyInput.className = 'comment comment--input comment--reply';
commentReplyInput.method = 'post';
commentReplyInput.action = 'javascript:void(0);';
2018-12-09 23:56:36 +00:00
commentReplyInput.addEventListener('submit', commentPostEventHandler);
2018-12-09 03:42:38 +00:00
// reply attributes
2018-12-09 23:56:36 +00:00
const replyCategory: HTMLInputElement = commentReplyInput.appendChild(document.createElement('input'));
2018-12-09 03:42:38 +00:00
replyCategory.name = 'comment[category]';
2018-12-09 23:56:36 +00:00
replyCategory.value = comment.category_id.toString();
2018-12-09 03:42:38 +00:00
replyCategory.type = 'hidden';
2018-12-09 23:56:36 +00:00
const replyCsrf: HTMLInputElement = commentReplyInput.appendChild(document.createElement('input'));
replyCsrf.name = 'csrf[comments]';
replyCsrf.value = getCSRFToken('comments');
2018-12-09 03:42:38 +00:00
replyCsrf.type = 'hidden';
2018-12-09 23:56:36 +00:00
const replyId: HTMLInputElement = commentReplyInput.appendChild(document.createElement('input'));
2018-12-09 03:42:38 +00:00
replyId.name = 'comment[reply]';
2018-12-09 23:56:36 +00:00
replyId.value = comment.comment_id.toString();
2018-12-09 03:42:38 +00:00
replyId.type = 'hidden';
2018-12-09 23:56:36 +00:00
const replyContainer: HTMLDivElement = commentReplyInput.appendChild(document.createElement('div'));
2018-12-09 03:42:38 +00:00
replyContainer.className = 'comment__container';
// reply container
2018-12-09 23:56:36 +00:00
const replyAvatar: HTMLDivElement = replyContainer.appendChild(document.createElement('div'));
2018-12-09 03:42:38 +00:00
replyAvatar.className = 'avatar comment__avatar';
2019-03-25 20:11:31 +00:00
replyAvatar.style.backgroundImage = "url('{0}')".replace('{0}', urlFormat('user-avatar', [{name:'user',value:comment.user_id},{name:'res',value:80}]));
2018-12-09 03:42:38 +00:00
2018-12-09 23:56:36 +00:00
const replyContent: HTMLDivElement = replyContainer.appendChild(document.createElement('div'));
2018-12-09 03:42:38 +00:00
replyContent.className = 'comment__content';
// reply content
2018-12-09 23:56:36 +00:00
const replyInfo: HTMLDivElement = replyContent.appendChild(document.createElement('div'));
2018-12-09 03:42:38 +00:00
replyInfo.className = 'comment__info';
2018-12-09 23:56:36 +00:00
const replyText: HTMLTextAreaElement = replyContent.appendChild(document.createElement('textarea'));
2018-12-09 03:42:38 +00:00
replyText.className = 'comment__text input__textarea comment__text--input';
replyText.name = 'comment[text]';
replyText.placeholder = 'Share your extensive insights...';
replyText.addEventListener('keydown', commentInputEventHandler);
2018-12-09 03:42:38 +00:00
2018-12-09 23:56:36 +00:00
const replyActions: HTMLDivElement = replyContent.appendChild(document.createElement('div'));
2018-12-09 03:42:38 +00:00
replyActions.className = 'comment__actions';
2018-12-09 23:56:36 +00:00
const replyButton: HTMLButtonElement = replyActions.appendChild(document.createElement('button'));
2018-12-09 03:42:38 +00:00
replyButton.className = 'input__button comment__action comment__action--button comment__action--post';
replyButton.textContent = 'Reply';
2018-12-09 23:56:36 +00:00
return commentElement;
}
function commentInsert(comment: CommentPostInfo, form: HTMLFormElement): void
{
const isReply: boolean = form.classList.contains('comment--reply'),
parent: Element = isReply
? form.parentElement
: form.parentElement.parentElement.getElementsByClassName('comments__listing')[0],
repliesIndent: number = isReply
? (parseInt(parent.classList[1].substr(25)) + 1)
: 1,
commentElement: HTMLElement = commentConstruct(comment, repliesIndent);
2018-12-09 03:42:38 +00:00
if (isReply)
parent.appendChild(commentElement);
else
parent.insertBefore(commentElement, parent.firstElementChild);
2018-12-09 23:56:36 +00:00
const placeholder: HTMLElement = document.getElementById('_no_comments_notice_' + comment.category_id);
2018-12-09 03:42:38 +00:00
if (placeholder)
placeholder.parentNode.removeChild(placeholder);
}
2018-12-09 23:56:36 +00:00
function commentVoteEventHandler(ev: Event): void {
2018-12-11 20:42:59 +00:00
const target: HTMLAnchorElement = this as HTMLAnchorElement,
commentId: number = parseInt(target.dataset.commentId),
voteType: CommentVoteType = parseInt(target.dataset.commentVote),
buttons: NodeListOf<HTMLAnchorElement> = document.querySelectorAll(`.comment__action--vote[data-comment-id="${commentId}"]`),
likeButton: HTMLAnchorElement = document.querySelector(`.comment__action--like[data-comment-id="${commentId}"]`),
dislikeButton: HTMLAnchorElement = document.querySelector(`.comment__action--dislike[data-comment-id="${commentId}"]`),
classVoted: string = 'comment__action--voted';
for (let i = 0; i < buttons.length; i++) {
let button: HTMLAnchorElement = buttons[i];
button.textContent = button === target ? '...' : '';
button.classList.remove(classVoted);
if (button === likeButton) {
button.dataset.commentVote = (voteType === CommentVoteType.Like ? CommentVoteType.Indifferent : CommentVoteType.Like).toString();
} else if (button === dislikeButton) {
button.dataset.commentVote = (voteType === CommentVoteType.Dislike ? CommentVoteType.Indifferent : CommentVoteType.Dislike).toString();
}
}
commentVote(
commentId,
voteType,
info => {
switch (voteType) {
case CommentVoteType.Like:
likeButton.classList.add(classVoted);
break;
case CommentVoteType.Dislike:
dislikeButton.classList.add(classVoted);
break;
}
likeButton.textContent = info.likes > 0 ? `Like (${info.likes.toLocaleString()})` : 'Like';
dislikeButton.textContent = info.dislikes > 0 ? `Dislike (${info.dislikes.toLocaleString()})` : 'Dislike';
},
message => {
likeButton.textContent = 'Like';
dislikeButton.textContent = 'Dislike';
messageBox(message);
}
);
2018-12-09 23:56:36 +00:00
}
function commentVote(
2018-12-09 23:56:36 +00:00
commentId: number,
vote: CommentVoteType,
onSuccess: (voteInfo: CommentVotesInfo) => void = null,
onFail: (message: string) => void = null
2018-12-09 23:56:36 +00:00
): void {
if (!checkUserPerm('comments', CommentPermission.Vote)) {
if (onFail)
onFail("You aren't allowed to vote on comments.");
2018-12-09 23:56:36 +00:00
return;
}
2018-12-09 23:56:36 +00:00
const xhr: XMLHttpRequest = new XMLHttpRequest;
xhr.onreadystatechange = () => {
if (xhr.readyState !== 4)
return;
updateCSRF(xhr.getResponseHeader('X-Misuzu-CSRF'));
2018-12-09 23:56:36 +00:00
const json: CommentVotesInfo = JSON.parse(xhr.responseText),
message: string = json.error || json.message;
if (message && onFail)
onFail(message);
else if (!message && onSuccess)
onSuccess(json);
2018-12-09 23:56:36 +00:00
};
xhr.open('GET', urlFormat('comment-vote', [{name: 'comment', value: commentId}, {name: 'vote', value: vote}]));
2018-12-09 23:56:36 +00:00
xhr.setRequestHeader('X-Misuzu-XHR', 'comments');
xhr.send();
}
function commentPinEventHandler(ev: Event): void {
const target: HTMLAnchorElement = this as HTMLAnchorElement,
commentId: number = parseInt(target.dataset.commentId),
isPinned: boolean = target.dataset.commentPinned !== '0';
target.textContent = '...';
commentPin(
commentId,
!isPinned,
info => {
if (info.comment_pinned === null) {
target.textContent = 'Pin';
target.dataset.commentPinned = '0';
const pinElement: HTMLDivElement = document.querySelector(`#comment-${info.comment_id} .comment__pin`);
pinElement.parentElement.removeChild(pinElement);
} else {
target.textContent = 'Unpin';
target.dataset.commentPinned = '1';
const pinInfo: HTMLDivElement = document.querySelector(`#comment-${info.comment_id} .comment__info`),
pinElement: HTMLDivElement = document.createElement('div'),
pinTime: HTMLTimeElement = document.createElement('time'),
pinDateTime = new Date(info.comment_pinned + 'Z');
pinTime.title = pinDateTime.toLocaleString();
pinTime.dateTime = pinDateTime.toISOString();
pinTime.textContent = timeago().format(pinDateTime);
timeago().render(pinTime);
pinElement.className = 'comment__pin';
pinElement.appendChild(document.createTextNode('Pinned '));
pinElement.appendChild(pinTime);
pinInfo.appendChild(pinElement);
}
},
message => {
target.textContent = isPinned ? 'Unpin' : 'Pin';
messageBox(message);
}
);
}
function commentPin(
commentId: number,
pin: boolean,
onSuccess: (commentInfo: CommentPostInfo) => void = null,
onFail: (message: string) => void = null
): void {
if (!checkUserPerm('comments', CommentPermission.Pin)) {
if (onFail)
onFail("You aren't allowed to pin comments.");
return;
}
const mode: string = pin ? 'pin' : 'unpin';
const xhr: XMLHttpRequest = new XMLHttpRequest;
xhr.onreadystatechange = () => {
if (xhr.readyState !== 4)
return;
updateCSRF(xhr.getResponseHeader('X-Misuzu-CSRF'));
const json: CommentPostInfo = JSON.parse(xhr.responseText),
message: string = json.error || json.message;
if (message && onFail)
onFail(message);
else if (!message && onSuccess)
onSuccess(json);
};
xhr.open('GET', urlFormat(`comment-${mode}`, [{name: 'comment', value: commentId}]));
xhr.setRequestHeader('X-Misuzu-XHR', 'comments');
xhr.send();
}