Toggle menu
862
3.8K
30.2K
279.1K
Catglobe Wiki
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

MediaWiki:Common.js: Difference between revisions

MediaWiki interface page
No edit summary
No edit summary
Line 1: Line 1:
function attachModifiedClickHandler(element) {
function attachModifiedClickHandler(element) {
     const handlers = jQuery._data(element, 'events')?.click;
     const eventsData = $._data(element, 'events');
    const handlers = eventsData && eventsData.click;
   
     if (handlers && handlers.length > 0) {
     if (handlers && handlers.length > 0) {
         const originalHandler = handlers[0].handler;
         const originalHandler = handlers[0].handler;


        // Properly detach and reattach the click handler using jQuery
         $(element).off('click').on('click', function(event) {
         $(element).off('click').on('click', function(event) {
             event.stopPropagation();
             event.stopPropagation();
             originalHandler.call(this, event);
             originalHandler.call(this, event);
         });
         });
        // Use native JavaScript to add the fix class
         element.classList.add('FixCatTreeClick');
         element.classList.add('FixCatTreeClick');
     }
     }
Line 17: Line 22:
             const target = mutation.target;
             const target = mutation.target;


            // Check if the element has the specified class and has not already been processed
             if (
             if (
                 mutation.attributeName === 'class' &&
                 mutation.attributeName === 'class' &&
Line 27: Line 33:
     });
     });


    // Observe changes to attributes, specifically the 'class' attribute, throughout the document
     observer.observe(document.body, {
     observer.observe(document.body, {
         attributes: true,
         attributes: true,

Revision as of 10:38, 30 October 2024

function attachModifiedClickHandler(element) {
    const eventsData = $._data(element, 'events');
    const handlers = eventsData && eventsData.click;
    
    if (handlers && handlers.length > 0) {
        const originalHandler = handlers[0].handler;

        // Properly detach and reattach the click handler using jQuery
        $(element).off('click').on('click', function(event) {
            event.stopPropagation();
            originalHandler.call(this, event);
        });

        // Use native JavaScript to add the fix class
        element.classList.add('FixCatTreeClick');
    }
}

addOnLoadHandler(function() {
    const observer = new MutationObserver((mutations) => {
        mutations.forEach((mutation) => {
            const target = mutation.target;

            // Check if the element has the specified class and has not already been processed
            if (
                mutation.attributeName === 'class' &&
                target.classList.contains('CategoryTreeToggleHandlerAttached') &&
                !target.classList.contains('FixCatTreeClick')
            ) {
                attachModifiedClickHandler(target);
            }
        });
    });

    // Observe changes to attributes, specifically the 'class' attribute, throughout the document
    observer.observe(document.body, {
        attributes: true,
        attributeFilter: ['class'],
        subtree: true
    });
});