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