code.gitea.io/gitea@v1.21.7/web_src/js/features/comp/ReactionSelector.js (about) 1 import $ from 'jquery'; 2 import {POST} from '../../modules/fetch.js'; 3 4 export function initCompReactionSelector($parent) { 5 $parent.find(`.select-reaction .item.reaction, .comment-reaction-button`).on('click', async function (e) { 6 e.preventDefault(); 7 8 if ($(this).hasClass('disabled')) return; 9 10 const actionUrl = $(this).closest('[data-action-url]').attr('data-action-url'); 11 const reactionContent = $(this).attr('data-reaction-content'); 12 const hasReacted = $(this).closest('.ui.segment.reactions').find(`a[data-reaction-content="${reactionContent}"]`).attr('data-has-reacted') === 'true'; 13 14 const res = await POST(`${actionUrl}/${hasReacted ? 'unreact' : 'react'}`, { 15 data: new URLSearchParams({content: reactionContent}), 16 }); 17 18 const data = await res.json(); 19 if (data && (data.html || data.empty)) { 20 const content = $(this).closest('.content'); 21 let react = content.find('.segment.reactions'); 22 if ((!data.empty || data.html === '') && react.length > 0) { 23 react.remove(); 24 } 25 if (!data.empty) { 26 const attachments = content.find('.segment.bottom:first'); 27 react = $(data.html); 28 if (attachments.length > 0) { 29 react.insertBefore(attachments); 30 } else { 31 react.appendTo(content); 32 } 33 react.find('.dropdown').dropdown(); 34 initCompReactionSelector(react); 35 } 36 } 37 }); 38 }