code.gitea.io/gitea@v1.22.3/web_src/js/features/comp/ReactionSelector.js (about)

     1  import $ from 'jquery';
     2  import {POST} from '../../modules/fetch.js';
     3  
     4  export function initCompReactionSelector() {
     5    for (const container of document.querySelectorAll('.issue-content, .diff-file-body')) {
     6      container.addEventListener('click', async (e) => {
     7        // there are 2 places for the "reaction" buttons, one is the top-right reaction menu, one is the bottom of the comment
     8        const target = e.target.closest('.comment-reaction-button');
     9        if (!target) return;
    10        e.preventDefault();
    11  
    12        if (target.classList.contains('disabled')) return;
    13  
    14        const actionUrl = target.closest('[data-action-url]').getAttribute('data-action-url');
    15        const reactionContent = target.getAttribute('data-reaction-content');
    16  
    17        const commentContainer = target.closest('.comment-container');
    18  
    19        const bottomReactions = commentContainer.querySelector('.bottom-reactions'); // may not exist if there is no reaction
    20        const bottomReactionBtn = bottomReactions?.querySelector(`a[data-reaction-content="${CSS.escape(reactionContent)}"]`);
    21        const hasReacted = bottomReactionBtn?.getAttribute('data-has-reacted') === 'true';
    22  
    23        const res = await POST(`${actionUrl}/${hasReacted ? 'unreact' : 'react'}`, {
    24          data: new URLSearchParams({content: reactionContent}),
    25        });
    26  
    27        const data = await res.json();
    28        bottomReactions?.remove();
    29        if (data.html) {
    30          commentContainer.insertAdjacentHTML('beforeend', data.html);
    31          const bottomReactionsDropdowns = commentContainer.querySelectorAll('.bottom-reactions .dropdown.select-reaction');
    32          $(bottomReactionsDropdowns).dropdown(); // re-init the dropdown
    33        }
    34      });
    35    }
    36  }