2022-01-28 16:00:11 -05:00
|
|
|
import $ from 'jquery';
|
2022-08-09 08:37:34 -04:00
|
|
|
import {createTippy} from '../../modules/tippy.js';
|
|
|
|
|
2021-10-21 03:37:43 -04:00
|
|
|
const {csrfToken} = window.config;
|
2021-10-16 13:28:04 -04:00
|
|
|
|
|
|
|
export function initCompReactionSelector(parent) {
|
2022-08-09 08:37:34 -04:00
|
|
|
let selector = 'a.label';
|
2021-10-16 13:28:04 -04:00
|
|
|
if (!parent) {
|
|
|
|
parent = $(document);
|
2022-08-09 08:37:34 -04:00
|
|
|
selector = `.reactions ${selector}`;
|
2021-10-16 13:28:04 -04:00
|
|
|
}
|
|
|
|
|
2022-08-09 08:37:34 -04:00
|
|
|
for (const el of parent[0].querySelectorAll(selector)) {
|
|
|
|
createTippy(el, {placement: 'bottom-start', content: el.getAttribute('data-title')});
|
|
|
|
}
|
2021-10-16 13:28:04 -04:00
|
|
|
|
2022-08-09 08:37:34 -04:00
|
|
|
parent.find(`.select-reaction > .menu > .item, ${selector}`).on('click', function (e) {
|
2021-10-16 13:28:04 -04: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-19 18:08:08 -04:00
|
|
|
const url = `${actionURL}/${$(this).hasClass('primary') ? 'unreact' : 'react'}`;
|
2021-10-16 13:28:04 -04:00
|
|
|
$.ajax({
|
|
|
|
type: 'POST',
|
|
|
|
url,
|
|
|
|
data: {
|
2021-10-21 03:37:43 -04:00
|
|
|
_csrf: csrfToken,
|
2021-10-16 13:28:04 -04: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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|