User:Monster Domosed/common.js: Difference between revisions

Jump to navigation Jump to search
No edit summary
No edit summary
Line 8: Line 8:


     var tooltipCache = {};
     var tooltipCache = {};
     var styleCache = {}; // Cache loaded styles
     var stylesLoaded = {}; // Track which stylesheets have been loaded
     var $tooltip = null;
     var $tooltip = null;
     var currentTarget = null;
     var currentTarget = null;
Line 17: Line 17:
     var HIDE_DELAY = 300;
     var HIDE_DELAY = 300;
     var EDGE_MARGIN = 10; // margin from viewport edges
     var EDGE_MARGIN = 10; // margin from viewport edges
    /* ─── Load and inject CSS from a template page ─── */
    function loadTemplateStyles( pageName, callback ) {
        // Already loaded, just callback
        if ( stylesLoaded[ pageName ] ) {
            if ( callback ) callback();
            return;
        }
        // Fetch the CSS content
        new mw.Api().get( {
            action: 'query',
            prop: 'revisions',
            titles: pageName,
            rvprop: 'content',
            rvslots: 'main',
            formatversion: 2
        } ).then( function ( data ) {
            if ( data.query && data.query.pages && data.query.pages[0] ) {
                var page = data.query.pages[0];
                if ( page.revisions && page.revisions[0] ) {
                    var css = page.revisions[0].slots.main.content;
                   
                    // Create a style element and inject the CSS
                    var $style = $( '<style>' )
                        .attr( 'data-template-styles', pageName )
                        .text( css )
                        .appendTo( 'head' );
                   
                    stylesLoaded[ pageName ] = true;
                    if ( callback ) callback();
                }
            }
        } ).catch( function ( error ) {
            console.error( 'Failed to load template styles:', pageName, error );
            if ( callback ) callback(); // Continue anyway
        } );
    }


     /* ─── Create tooltip DOM element ─── */
     /* ─── Create tooltip DOM element ─── */
Line 122: Line 160:
             return char.toUpperCase();
             return char.toUpperCase();
         } );
         } );
    }
    /* ─── Extract templatestyles tags from HTML ─── */
    function extractTemplateStyles( html ) {
        var regex = /<link[^>]+href="[^"]*\/load\.php\?[^"]*modules=ext\.templateStyles[^"]*"[^>]*>/gi;
        var stylesheets = [];
        var match;
       
        // Try to find templatestyles link tags
        while ( ( match = regex.exec( html ) ) !== null ) {
            stylesheets.push( match[0] );
        }
       
        return stylesheets;
     }
     }


Line 172: Line 224:
             }
             }


             // Fetch rendered template with styles
             // Load the template styles first
             new mw.Api().post( {
             var stylesheetToLoad = type === 'item' ? 'Template:Infobox item/styles.css' : 'Template:Ability card v2/styles.css';
                action: 'parse',
           
                text: templateText,
            loadTemplateStyles( stylesheetToLoad, function () {
                title: mw.config.get( 'wgPageName' ),
                // Now fetch the rendered template
                prop: 'text|modules|modulestyles',
                new mw.Api().post( {
                disablelimitreport: true,
                    action: 'parse',
                disableeditsection: true,
                    text: templateText,
                wrapoutputclass: '',
                    title: mw.config.get( 'wgPageName' ),
                format: 'json'
                    prop: 'text',
            } ).then( function ( data ) {
                    disablelimitreport: true,
                var html = data.parse.text[ '*' ];
                    disableeditsection: true,
                var modulestyles = data.parse.modulestyles || [];
                    wrapoutputclass: '',
                var modules = data.parse.modules || [];
                    format: 'json'
               
                } ).then( function ( data ) {
                // Combine all modules to load
                    var html = data.parse.text[ '*' ];
                var allModules = modulestyles.concat( modules );
                    tooltipCache[ cacheKey ] = html;
               
                tooltipCache[ cacheKey ] = html;


                // Load the required style modules
                     // Only show if the mouse is still on this element
                if ( allModules.length > 0 ) {
                     mw.loader.using( allModules ).then( function () {
                        // Only show if the mouse is still on this element
                        if ( currentTarget === anchor ) {
                            tt.html( html ).show();
                            positionTooltip( anchor );
                           
                            // Initialize ability upgrades for new content
                            if ( type === 'ability' ) {
                                // Trigger MediaWiki's content hook so other gadgets can initialize
                                mw.hook( 'wikipage.content' ).fire( tt );
                                initAbilityUpgrades( tt );
                            }
                        }
                    } );
                } else {
                    // No modules to load, show immediately
                     if ( currentTarget === anchor ) {
                     if ( currentTarget === anchor ) {
                         tt.html( html ).show();
                         tt.html( html ).show();
                         positionTooltip( anchor );
                         positionTooltip( anchor );
                          
                          
                        // Initialize ability upgrades for new content
                         if ( type === 'ability' ) {
                         if ( type === 'ability' ) {
                            // Trigger MediaWiki's content hook so other gadgets can initialize
                             mw.hook( 'wikipage.content' ).fire( tt );
                             mw.hook( 'wikipage.content' ).fire( tt );
                             initAbilityUpgrades( tt );
                             initAbilityUpgrades( tt );
                         }
                         }
                     }
                     }
                 }
                 } ).catch( function () {
            } ).catch( function () {
                    if ( currentTarget === anchor ) {
                if ( currentTarget === anchor ) {
                        tt.hide();
                    tt.hide();
                    }
                 }
                 } );
             } );
             } );


Line 299: Line 334:


} )();
} )();


/**
/**