MediaWiki:Common.js: Difference between revisions

From Ragnafied Wiki
No edit summary
Tag: Reverted
No edit summary
Tag: Reverted
Line 1: Line 1:
$(document).ready(function() {
$(document).ready(function() {
     // ===========================================
     // ===========================================
     // Configuration
     // 1. Back to Top Button - FIXED VERSION
     // ===========================================
     // ===========================================
     const CONFIG = {
      
         scrollThreshold: 300,
    // Remove any existing back-to-top buttons first
         menuHideThreshold: 500,
    $('.back-to-top-fixed').remove();
        desktopBreakpoint: 768,
   
        progressBar: true,
    // Create the button with explicit inline styles
        backToTop: true,
    var topButtonHTML = `
        floatingMenu: true,
         <div class="back-to-top-fixed" id="back-to-top-btn" style="
        smoothScroll: true,
            position: fixed;
        stickyHeader: true,
            bottom: 30px;
        readingTime: true,
            right: 30px;
         tableOfContents: true
            z-index: 9999;
     };
            display: none;
            opacity: 0;
            visibility: hidden;
            transition: opacity 0.3s ease, visibility 0.3s ease;
         ">
            <a href="#" style="
                background: linear-gradient(135deg, #FF6A00, #D93800);
                color: white;
                width: 60px;
                height: 60px;
                border-radius: 50%;
                display: flex;
                align-items: center;
                justify-content: center;
                font-weight: bold;
                font-size: 16px;
                text-decoration: none;
                box-shadow: 0 4px 15px rgba(0,0,0,0.3);
                border: 3px solid white;
                transition: all 0.3s ease;
                cursor: pointer;
            ">↑ TOP</a>
         </div>
    `;
   
    // Append to body
    $('body').append(topButtonHTML);
   
    // Debug: Check if button was added
     console.log('Back to top button added:', $('#back-to-top-btn').length);
      
      
     // ===========================================
     // ===========================================
     // 1. Initialize UI Elements
     // 2. Alternative Button (if first one fails)
     // ===========================================
     // ===========================================
     function initializeUI() {
     setTimeout(function() {
        // Progress Bar
         if ($('#back-to-top-btn').length === 0) {
         if (CONFIG.progressBar) {
            console.log('First button failed, adding alternative...');
             $('body').append('<div id="scroll-progress"></div>');
             $('body').append('<div style="position:fixed;bottom:30px;right:30px;z-index:99999;background:red;color:white;padding:10px;" id="emergency-top">TOP</div>');
        }
       
        // Back to Top Button
        if (CONFIG.backToTop) {
            var topButtonHTML = '<div class="back-to-top-fixed" style="display:none;"><a href="#">TOP</a></div>';
            $('body').append(topButtonHTML);
         }
         }
       
     }, 1000);
        // Floating Menu
        if (CONFIG.floatingMenu) {
            var menuHTML = `
                <div class="floating-menu" id="floatingMenu">
                    <div class="floating-menu-header" id="menuHeader">
                        <span>📋 QUICK NAV</span>
                        <span class="arrow">▼</span>
                    </div>
                    <div class="floating-menu-items">
                        <a href="/index.php/Main_Page">🏠 Main Page</a>
                        <a href="/index.php/First_Steps">🚀 First Steps</a>
                        <a href="/index.php/VIP">👑 VIP</a>
                        <a href="/index.php/Enchant_Stones">💎 Enchant Stones</a>
                        <a href="/index.php/Nyangvines">🐱 Nyangvines</a>
                        <a href="/index.php/Custom_Systems">⚙️ Custom Systems</a>
                        <a href="/index.php/Monthly_Kachua">📅 Monthly Kachua</a>
                        <a href="/index.php/Troubleshooting">🔧 Troubleshooting</a>
                        <a href="/index.php/Card_Trader_and_Kachua_Tokens">🃏 Card and Tokens</a>
                        <a href="/index.php/Equipment_Enhancement">🛡️ EQ Enhancement</a>
                    </div>
                </div>
                <div class="floating-menu-toggle" id="menuToggle">☰</div>
            `;
            $('body').append(menuHTML);
        }
     }
   
    initializeUI();
      
      
     // ===========================================
     // ===========================================
     // 2. Scroll Event Handler
     // 3. Scroll Handler for Button Visibility
     // ===========================================
     // ===========================================
     var lastScrollTop = 0;
     var lastScrollTop = 0;
     var scrollTimer = null;
     var scrollTimeout;
      
      
     $(window).scroll(function() {
     $(window).scroll(function() {
         var st = $(this).scrollTop();
         var st = $(this).scrollTop();
          
          
         // Throttle scroll events for better performance
         // Clear previous timeout
         if (scrollTimer) {
         if (scrollTimeout) {
             clearTimeout(scrollTimer);
             clearTimeout(scrollTimeout);
         }
         }
          
          
         scrollTimer = setTimeout(function() {
         scrollTimeout = setTimeout(function() {
             // A. Progress Bar
             // Show/hide based on scroll position
             if (CONFIG.progressBar) {
             if (st > 300) {
                 var docHeight = $(document).height() - $(window).height();
                 $('#back-to-top-btn').css({
                var scrolled = (st / docHeight) * 100;
                    'display': 'block',
                 $('#scroll-progress').css("width", scrolled + "%");
                    'opacity': '1',
                    'visibility': 'visible'
                }).fadeIn(300);
            } else {
                 $('#back-to-top-btn').fadeOut(300, function() {
                    $(this).css({
                        'display': 'none',
                        'opacity': '0',
                        'visibility': 'hidden'
                    });
                });
             }
             }
 
              
            // B. Top Button Fade
             // Hide on scroll down (optional)
            if (CONFIG.backToTop) {
             if ($(window).width() > 768) {
                if (st > CONFIG.scrollThreshold) {
                 if (st > lastScrollTop && st > 500) {
                    $('.back-to-top-fixed').fadeIn();
                     $('#back-to-top-btn').css('right', '-100px');
                } else {
                    $('.back-to-top-fixed').fadeOut();
                }
             }
 
             // C. Hide Menu on Scroll Down (Desktop Only)
             if (CONFIG.floatingMenu && $(window).width() > CONFIG.desktopBreakpoint) {
                 if (st > lastScrollTop && st > CONFIG.menuHideThreshold) {
                     $('#floatingMenu').css('right', '-220px');
                 } else {
                 } else {
                     $('#floatingMenu').css('right', '20px');
                     $('#back-to-top-btn').css('right', '30px');
                 }
                 }
             }
             }
              
              
             lastScrollTop = st;
             lastScrollTop = st;
         }, 10); // 10ms throttle
         }, 50);
 
        // D. Sticky Header
        if (CONFIG.stickyHeader) {
            if (st > 100) {
                $('#mw-head').addClass('sticky-header');
            } else {
                $('#mw-head').removeClass('sticky-header');
            }
        }
     });
     });
      
      
     // ===========================================
     // ===========================================
     // 3. Section Tracker (run on load and scroll)
     // 4. Click Handler
     // ===========================================
     // ===========================================
     function updateActiveSection() {
     $(document).on('click', '#back-to-top-btn a, #emergency-top', function(e) {
        var currentPath = window.location.pathname;
        $('.floating-menu-items a').each(function() {
            var href = $(this).attr('href');
            if (currentPath.includes(href) || currentPath === href) {
                $(this).addClass('reading-now');
            } else {
                $(this).removeClass('reading-now');
            }
        });
    }
   
    // Run on load and scroll
    updateActiveSection();
    $(window).on('scroll', updateActiveSection);
   
    // ===========================================
    // 4. Toggle Events
    // ===========================================
    $('#menuHeader').click(function() {
        $('#floatingMenu').toggleClass('collapsed');
    });
   
    $('#menuToggle').click(function() {
        $('#floatingMenu').toggleClass('mobile-visible');
        // Close menu when clicking outside
        $(document).one('click', function(e) {
            if (!$(e.target).closest('#floatingMenu, #menuToggle').length) {
                $('#floatingMenu').removeClass('mobile-visible');
            }
        });
    });
   
    // Close menu with Escape key
    $(document).keyup(function(e) {
        if (e.key === "Escape") {
            $('#floatingMenu').removeClass('mobile-visible');
        }
    });
   
    // ===========================================
    // 5. Back to Top Button
    // ===========================================
    $('.back-to-top-fixed a').click(function(e) {
         e.preventDefault();
         e.preventDefault();
         $('html, body').animate({scrollTop: 0}, 400, 'swing');
         $('html, body').animate({
            scrollTop: 0
        }, 600, 'swing');
       
        // Button feedback
        $(this).css('transform', 'scale(0.9)');
        setTimeout(() => {
            $(this).css('transform', 'scale(1)');
        }, 200);
     });
     });
      
      
     // ===========================================
     // ===========================================
     // 6. Smooth Scrolling for Anchor Links
     // 5. Hover Effects
    // ===========================================
    if (CONFIG.smoothScroll) {
        $('a[href^="#"]').not('.no-smooth').on('click', function(e) {
            e.preventDefault();
            var target = $(this.hash);
            if (target.length) {
                $('html, body').animate({
                    scrollTop: target.offset().top - 60
                }, 500, 'swing', function() {
                    // Update URL hash without jumping
                    if (history.pushState) {
                        history.pushState(null, null, this.hash);
                    }
                });
            }
        });
    }
   
    // ===========================================
    // 7. Reading Time Estimator
     // ===========================================
     // ===========================================
     if (CONFIG.readingTime) {
     $(document).on('mouseenter', '#back-to-top-btn a', function() {
        var content = $('#mw-content-text').text();
         $(this).css({
        var wordCount = content.split(/\s+/).length;
             'background': 'linear-gradient(135deg, #FF7A20, #F4B400)',
        var readingTime = Math.ceil(wordCount / 200);
            'transform': 'translateY(-5px) scale(1.1)',
       
             'box-shadow': '0 6px 20px rgba(0,0,0,0.4)'
        if (wordCount > 50) { // Only show for longer articles
            $('#firstHeading').append(
                '<span class="reading-time-badge" style="' +
                'font-size: 14px; ' +
                'font-weight: normal; ' +
                'margin-left: 15px; ' +
                'padding: 3px 10px; ' +
                'background: linear-gradient(135deg, #FF6A00, #D93800); ' +
                'color: white; ' +
                'border-radius: 20px; ' +
                'display: inline-block;' +
                '">📖 ' + readingTime + ' min read</span>'
            );
        }
    }
   
    // ===========================================
    // 8. Table of Contents Improvements
    // ===========================================
    if (CONFIG.tableOfContents && $('#toc').length) {
        // Add sticky TOC toggle
         $('#toc').before(
             '<div id="toc-controls" style="margin-bottom: 10px;">' +
                '<button id="toggle-toc" class="btn" style="' +
                'background: linear-gradient(135deg, #FF6A00, #D93800); ' +
                'color: white; ' +
                'border: none; ' +
                'padding: 5px 15px; ' +
                'border-radius: 5px; ' +
                'cursor: pointer;' +
                '">Hide Table of Contents</button>' +
            '</div>'
        );
       
        $('#toggle-toc').click(function() {
             $('#toc ul').slideToggle(200);
            $(this).text(function(i, text) {
                return text === 'Hide Table of Contents' ?
                    'Show Table of Contents' : 'Hide Table of Contents';
            });
         });
         });
       
    }).on('mouseleave', '#back-to-top-btn a', function() {
        // Add "Back to Top" links after each section
        $(this).css({
        $('h2, h3').each(function() {
            'background': 'linear-gradient(135deg, #FF6A00, #D93800)',
            if ($(this).attr('id')) {
            'transform': 'translateY(0) scale(1)',
                $(this).append(
            'box-shadow': '0 4px 15px rgba(0,0,0,0.3)'
                    ' <a href="#top" class="section-top-link" style="' +
                    'font-size: 12px; ' +
                    'margin-left: 10px; ' +
                    'text-decoration: none; ' +
                    'opacity: 0.5; ' +
                    'transition: opacity 0.3s;' +
                    '">↑ top</a>'
                );
               
                $(this).find('.section-top-link').hover(
                    function() { $(this).css('opacity', '1'); },
                    function() { $(this).css('opacity', '0.5'); }
                );
            }
         });
         });
    }
   
    // ===========================================
    // 9. Keyboard Navigation
    // ===========================================
    $(document).on('keydown', function(e) {
        // Alt + Q - Toggle floating menu
        if (e.altKey && e.key === 'q') {
            e.preventDefault();
            $('#floatingMenu').toggleClass('collapsed');
        }
       
        // Alt + T - Back to top
        if (e.altKey && e.key === 't') {
            e.preventDefault();
            $('html, body').animate({scrollTop: 0}, 400);
        }
       
        // Alt + H - Go to Main Page
        if (e.altKey && e.key === 'h') {
            e.preventDefault();
            window.location.href = '/index.php/Main_Page';
        }
     });
     });
      
      
     // ===========================================
     // ===========================================
     // 10. Responsive Handling
     // 6. Responsive Adjustments
     // ===========================================
     // ===========================================
     $(window).resize(function() {
     $(window).resize(function() {
         if ($(window).width() > CONFIG.desktopBreakpoint) {
         if ($(window).width() <= 768) {
             $('#floatingMenu').removeClass('mobile-visible');
             $('#back-to-top-btn').css({
             $('#floatingMenu').css('right', '20px');
                'bottom': '20px',
                'right': '20px'
            });
             $('#back-to-top-btn a').css({
                'width': '50px',
                'height': '50px',
                'font-size': '14px'
            });
         } else {
         } else {
             $('#floatingMenu').css('right', '');
             $('#back-to-top-btn').css({
                'bottom': '30px',
                'right': '30px'
            });
            $('#back-to-top-btn a').css({
                'width': '60px',
                'height': '60px',
                'font-size': '16px'
            });
         }
         }
     });
     }).resize(); // Trigger on load
      
      
     // ===========================================
     // ===========================================
     // 11. Page Load Performance
     // 7. Check if button is visible on page load
     // ===========================================
     // ===========================================
     // Lazy load images if any
     setTimeout(function() {
    $('img').each(function() {
         var btn = $('#back-to-top-btn');
         var img = $(this);
         console.log('Button exists:', btn.length > 0);
         var src = img.attr('src');
         console.log('Button display:', btn.css('display'));
         if (src && !img.hasClass('loaded')) {
        console.log('Button visibility:', btn.css('visibility'));
            img.on('load', function() {
        console.log('Button opacity:', btn.css('opacity'));
                $(this).addClass('loaded');
        console.log('Button position:', btn.offset());
            });
       
         }
        // Force show for testing (remove after confirming it works)
     });
         // btn.css({'display': 'block', 'opacity': '1', 'visibility': 'visible'});
     }, 2000);
      
      
     // ===========================================
     // ===========================================
     // 12. Error Handling
     // 8. Alternative: Add to existing menu
     // ===========================================
     // ===========================================
     $(window).on('error', function(e) {
     // Add to floating menu as alternative
        console.log('JavaScript error:', e.message);
    setTimeout(function() {
     });
        if ($('.floating-menu-items').length && $('#back-to-top-btn').css('display') === 'none') {
            $('.floating-menu-items').append(
                '<a href="#" id="menu-back-to-top" style="border-top: 1px solid #eee; margin-top: 10px;">⬆️ Back to Top</a>'
            );
           
            $('#menu-back-to-top').click(function(e) {
                e.preventDefault();
                $('html, body').animate({scrollTop: 0}, 600);
            });
        }
     }, 1000);
});
});
// ===========================================
// 13. Sticky Header CSS (add to your CSS file)
// ===========================================
/*
.sticky-header {
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    right: 0 !important;
    z-index: 1000 !important;
    background: white !important;
    box-shadow: 0 2px 10px rgba(0,0,0,0.1) !important;
    animation: slideDown 0.3s ease;
}
@keyframes slideDown {
    from { transform: translateY(-100%); }
    to { transform: translateY(0); }
}
*/

Revision as of 10:59, 21 February 2026

$(document).ready(function() {
    // ===========================================
    // 1. Back to Top Button - FIXED VERSION
    // ===========================================
    
    // Remove any existing back-to-top buttons first
    $('.back-to-top-fixed').remove();
    
    // Create the button with explicit inline styles
    var topButtonHTML = `
        <div class="back-to-top-fixed" id="back-to-top-btn" style="
            position: fixed;
            bottom: 30px;
            right: 30px;
            z-index: 9999;
            display: none;
            opacity: 0;
            visibility: hidden;
            transition: opacity 0.3s ease, visibility 0.3s ease;
        ">
            <a href="#" style="
                background: linear-gradient(135deg, #FF6A00, #D93800);
                color: white;
                width: 60px;
                height: 60px;
                border-radius: 50%;
                display: flex;
                align-items: center;
                justify-content: center;
                font-weight: bold;
                font-size: 16px;
                text-decoration: none;
                box-shadow: 0 4px 15px rgba(0,0,0,0.3);
                border: 3px solid white;
                transition: all 0.3s ease;
                cursor: pointer;
            ">↑ TOP</a>
        </div>
    `;
    
    // Append to body
    $('body').append(topButtonHTML);
    
    // Debug: Check if button was added
    console.log('Back to top button added:', $('#back-to-top-btn').length);
    
    // ===========================================
    // 2. Alternative Button (if first one fails)
    // ===========================================
    setTimeout(function() {
        if ($('#back-to-top-btn').length === 0) {
            console.log('First button failed, adding alternative...');
            $('body').append('<div style="position:fixed;bottom:30px;right:30px;z-index:99999;background:red;color:white;padding:10px;" id="emergency-top">TOP</div>');
        }
    }, 1000);
    
    // ===========================================
    // 3. Scroll Handler for Button Visibility
    // ===========================================
    var lastScrollTop = 0;
    var scrollTimeout;
    
    $(window).scroll(function() {
        var st = $(this).scrollTop();
        
        // Clear previous timeout
        if (scrollTimeout) {
            clearTimeout(scrollTimeout);
        }
        
        scrollTimeout = setTimeout(function() {
            // Show/hide based on scroll position
            if (st > 300) {
                $('#back-to-top-btn').css({
                    'display': 'block',
                    'opacity': '1',
                    'visibility': 'visible'
                }).fadeIn(300);
            } else {
                $('#back-to-top-btn').fadeOut(300, function() {
                    $(this).css({
                        'display': 'none',
                        'opacity': '0',
                        'visibility': 'hidden'
                    });
                });
            }
            
            // Hide on scroll down (optional)
            if ($(window).width() > 768) {
                if (st > lastScrollTop && st > 500) {
                    $('#back-to-top-btn').css('right', '-100px');
                } else {
                    $('#back-to-top-btn').css('right', '30px');
                }
            }
            
            lastScrollTop = st;
        }, 50);
    });
    
    // ===========================================
    // 4. Click Handler
    // ===========================================
    $(document).on('click', '#back-to-top-btn a, #emergency-top', function(e) {
        e.preventDefault();
        $('html, body').animate({
            scrollTop: 0
        }, 600, 'swing');
        
        // Button feedback
        $(this).css('transform', 'scale(0.9)');
        setTimeout(() => {
            $(this).css('transform', 'scale(1)');
        }, 200);
    });
    
    // ===========================================
    // 5. Hover Effects
    // ===========================================
    $(document).on('mouseenter', '#back-to-top-btn a', function() {
        $(this).css({
            'background': 'linear-gradient(135deg, #FF7A20, #F4B400)',
            'transform': 'translateY(-5px) scale(1.1)',
            'box-shadow': '0 6px 20px rgba(0,0,0,0.4)'
        });
    }).on('mouseleave', '#back-to-top-btn a', function() {
        $(this).css({
            'background': 'linear-gradient(135deg, #FF6A00, #D93800)',
            'transform': 'translateY(0) scale(1)',
            'box-shadow': '0 4px 15px rgba(0,0,0,0.3)'
        });
    });
    
    // ===========================================
    // 6. Responsive Adjustments
    // ===========================================
    $(window).resize(function() {
        if ($(window).width() <= 768) {
            $('#back-to-top-btn').css({
                'bottom': '20px',
                'right': '20px'
            });
            $('#back-to-top-btn a').css({
                'width': '50px',
                'height': '50px',
                'font-size': '14px'
            });
        } else {
            $('#back-to-top-btn').css({
                'bottom': '30px',
                'right': '30px'
            });
            $('#back-to-top-btn a').css({
                'width': '60px',
                'height': '60px',
                'font-size': '16px'
            });
        }
    }).resize(); // Trigger on load
    
    // ===========================================
    // 7. Check if button is visible on page load
    // ===========================================
    setTimeout(function() {
        var btn = $('#back-to-top-btn');
        console.log('Button exists:', btn.length > 0);
        console.log('Button display:', btn.css('display'));
        console.log('Button visibility:', btn.css('visibility'));
        console.log('Button opacity:', btn.css('opacity'));
        console.log('Button position:', btn.offset());
        
        // Force show for testing (remove after confirming it works)
        // btn.css({'display': 'block', 'opacity': '1', 'visibility': 'visible'});
    }, 2000);
    
    // ===========================================
    // 8. Alternative: Add to existing menu
    // ===========================================
    // Add to floating menu as alternative
    setTimeout(function() {
        if ($('.floating-menu-items').length && $('#back-to-top-btn').css('display') === 'none') {
            $('.floating-menu-items').append(
                '<a href="#" id="menu-back-to-top" style="border-top: 1px solid #eee; margin-top: 10px;">⬆️ Back to Top</a>'
            );
            
            $('#menu-back-to-top').click(function(e) {
                e.preventDefault();
                $('html, body').animate({scrollTop: 0}, 600);
            });
        }
    }, 1000);
});