2022-01-29 06:00:11 +09:00
|
|
|
import $ from 'jquery';
|
2022-08-09 21:37:34 +09:00
|
|
|
import {createTippy} from '../../modules/tippy.js';
|
|
|
|
|
2021-10-21 16:37:43 +09:00
|
|
|
const {csrfToken} = window.config;
|
2021-10-17 02:28:04 +09:00
|
|
|
|
|
|
|
export function initCompReactionSelector(parent) {
|
2022-08-09 21:37:34 +09:00
|
|
|
let selector = 'a.label';
|
2021-10-17 02:28:04 +09:00
|
|
|
if (!parent) {
|
|
|
|
parent = $(document);
|
2022-08-09 21:37:34 +09:00
|
|
|
selector = `.reactions ${selector}`;
|
2021-10-17 02:28:04 +09:00
|
|
|
}
|
|
|
|
|
2022-08-09 21:37:34 +09:00
|
|
|
for (const el of parent[0].querySelectorAll(selector)) {
|
|
|
|
createTippy(el, {placement: 'bottom-start', content: el.getAttribute('data-title')});
|
|
|
|
}
|
2021-10-17 02:28:04 +09:00
|
|
|
|
2022-08-09 21:37:34 +09:00
|
|
|
parent.find(`.select-reaction > .menu > .item, ${selector}`).on('click', function (e) {
|
2021-10-17 02:28:04 +09:00
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
if ($(this).hasClass('disabled')) return;
|
|
|
|
|
|
|
|
const actionURL = $(this).hasClass('item') ? $(this).closest('.select-reaction').data('action-url') : $(this).data('action-url');
|
2022-05-20 07:08:08 +09:00
|
|
|
const url = `${actionURL}/${$(this).hasClass('primary') ? 'unreact' : 'react'}`;
|
2021-10-17 02:28:04 +09:00
|
|
|
$.ajax({
|
|
|
|
type: 'POST',
|
|
|
|
url,
|
|
|
|
data: {
|
2021-10-21 16:37:43 +09:00
|
|
|
_csrf: csrfToken,
|
2021-10-17 02:28:04 +09:00
|
|
|
content: $(this).data('content')
|
|
|
|
}
|
|
|
|
}).done((resp) => {
|
|
|
|
if (resp && (resp.html || resp.empty)) {
|
|
|
|
const content = $(this).closest('.content');
|
|
|
|
let react = content.find('.segment.reactions');
|
|
|
|
if ((!resp.empty || resp.html === '') && react.length > 0) {
|
|
|
|
react.remove();
|
|
|
|
}
|
|
|
|
if (!resp.empty) {
|
|
|
|
react = $('<div class="ui attached segment reactions"></div>');
|
|
|
|
const attachments = content.find('.segment.bottom:first');
|
|
|
|
if (attachments.length > 0) {
|
|
|
|
react.insertBefore(attachments);
|
|
|
|
} else {
|
|
|
|
react.appendTo(content);
|
|
|
|
}
|
|
|
|
react.html(resp.html);
|
|
|
|
react.find('.dropdown').dropdown();
|
|
|
|
initCompReactionSelector(react);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|