2022-09-02 20:03:25 -07:00
|
|
|
import { post } from '@rails/request.js';
|
2022-01-08 15:56:46 -08:00
|
|
|
|
2022-01-13 14:25:46 -08:00
|
|
|
import I18n from 'retrospring/i18n';
|
2022-01-08 15:56:46 -08:00
|
|
|
import { showNotification, showErrorNotification } from 'utilities/notifications';
|
|
|
|
|
2022-10-25 08:13:07 -07:00
|
|
|
let compositionJustEnded = false;
|
|
|
|
|
2023-03-18 07:24:36 -07:00
|
|
|
function createComment(input: HTMLInputElement, id: string, counter: Element, group: Element) {
|
|
|
|
if (input.value.length > 512) {
|
|
|
|
group.classList.add('has-error');
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
input.disabled = true;
|
|
|
|
|
|
|
|
post('/ajax/create_comment', {
|
|
|
|
body: {
|
|
|
|
answer: id,
|
|
|
|
comment: input.value
|
|
|
|
},
|
|
|
|
contentType: 'application/json'
|
|
|
|
})
|
|
|
|
.then(async response => {
|
|
|
|
const data = await response.json;
|
|
|
|
|
|
|
|
if (data.success) {
|
|
|
|
document.querySelector(`#ab-comments-${id}`).innerHTML = data.render;
|
|
|
|
const commentCount = document.getElementById(`#ab-comment-count-${id}`);
|
|
|
|
if (commentCount) {
|
|
|
|
commentCount.innerHTML = data.count;
|
|
|
|
}
|
|
|
|
input.value = '';
|
|
|
|
counter.innerHTML = String(512);
|
|
|
|
}
|
|
|
|
|
|
|
|
showNotification(data.message, data.success);
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.log(err);
|
|
|
|
showErrorNotification(I18n.translate('frontend.error.message'));
|
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
input.disabled = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function commentCreateKeyboardHandler(event: KeyboardEvent): boolean {
|
2022-10-25 08:13:07 -07:00
|
|
|
if (compositionJustEnded && event.which == 13) {
|
|
|
|
compositionJustEnded = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-08 15:56:46 -08:00
|
|
|
const input = event.target as HTMLInputElement;
|
|
|
|
const id = input.dataset.aId;
|
|
|
|
const counter = document.querySelector(`#ab-comment-charcount-${id}`);
|
|
|
|
const group = document.querySelector(`[name=ab-comment-new-group][data-a-id="${id}"]`);
|
|
|
|
|
2023-03-07 08:56:39 -08:00
|
|
|
if ((event.ctrlKey || event.metaKey) && event.which === 13) {
|
2022-01-08 15:56:46 -08:00
|
|
|
event.preventDefault();
|
|
|
|
|
2023-03-18 07:24:36 -07:00
|
|
|
createComment(input, id, counter, group);
|
|
|
|
}
|
|
|
|
}
|
2022-01-08 15:56:46 -08:00
|
|
|
|
2023-03-18 07:24:36 -07:00
|
|
|
export function commentCreateClickHandler(event: MouseEvent): void {
|
|
|
|
event.preventDefault();
|
2022-01-08 15:56:46 -08:00
|
|
|
|
2023-03-18 07:24:36 -07:00
|
|
|
const button = event.target as HTMLButtonElement;
|
|
|
|
const id = button.dataset.aId;
|
|
|
|
const input = document.querySelector<HTMLInputElement>(`[name="ab-comment-new"][data-a-id="${id}"]`);
|
|
|
|
const counter = document.querySelector(`#ab-comment-charcount-${id}`);
|
|
|
|
const group = document.querySelector(`[name=ab-comment-new-group][data-a-id="${id}"]`);
|
2022-01-08 15:56:46 -08:00
|
|
|
|
2023-03-18 07:24:36 -07:00
|
|
|
createComment(input, id, counter, group);
|
2022-10-25 08:13:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export function commentComposeStart(): boolean {
|
|
|
|
compositionJustEnded = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function commentComposeEnd(): boolean {
|
|
|
|
compositionJustEnded = true;
|
|
|
|
return true;
|
|
|
|
}
|