MediaWiki:Common.js: Difference between revisions

From Ultimate Dragon Ball Online Wiki

Ravenalien (talk | contribs)
No edit summary
Ravenalien (talk | contribs)
No edit summary
 
(11 intermediate revisions by the same user not shown)
Line 1: Line 1:
/* Any JavaScript here will be loaded for all users on every page load. */
/* Any JavaScript here will be loaded for all users on every page load. */
mw.loader.using(['jquery'], function ($) {
 
$(document).ready(function() {
     'use strict';
     'use strict';
      
      
     // Wait for DOM to be ready using MediaWiki hook
     // Main race tab click handler (works for both regular and image buttons)
     mw.hook('dom.ready').add(function() {
     $('.tab-navigation .tab-button, .tab-navigation .tab-button-image').click(function(e) {
        console.log('Tab navigation script loaded');
        e.preventDefault();
 
       
        function initializeTabs() {
        var targetId = $(this).data('target');
            try {
       
                // Main tabs (Race selection)
        // Update main race tabs (for both button types)
                $('.tab-button').on('click', function(e) {
        $('.tab-navigation .tab-button, .tab-navigation .tab-button-image').removeClass('active');
                    e.preventDefault();
        $(this).addClass('active');
                    console.log('Tab clicked:', $(this).data('target'));
       
                   
        // Update main content panes
                    var targetId = $(this).data('target');
        $('.tab-content > .tab-pane').removeClass('active');
                    if (!targetId) {
        $('#' + targetId).addClass('active');
                        console.error('No target ID found for tab');
       
                        return;
        // Always activate the overview tab for the selected race
                    }
        var $subNav = $('#' + targetId + ' .sub-tab-navigation');
 
        var $overviewTab;
                    // Handle main tab navigation
       
                    if ($(this).closest('.tab-navigation').length) {
        // Check if using image tabs or regular tabs
                        // Remove active class from all main tabs and contents
        if ($subNav.find('.tab-button-image').length) {
                        $('.tab-navigation .tab-button').removeClass('active');
            $overviewTab = $subNav.find('.tab-button-image').first();
                        $('.tab-navigation').siblings('.tab-content').children('.tab-pane').removeClass('active');
        } else {
                       
            $overviewTab = $subNav.find('.tab-button').first();
                        // Activate clicked tab and its content
                        $(this).addClass('active');
                        $('#' + targetId).addClass('active');
                    }
                    // Handle sub-tab navigation
                    else if ($(this).closest('.sub-tab-navigation').length) {
                        var $subNav = $(this).closest('.sub-tab-navigation');
                       
                        // Remove active class from sibling tabs and contents
                        $subNav.find('.tab-button').removeClass('active');
                        $subNav.find('.tab-pane').removeClass('active');
                       
                        // Activate clicked tab and its content
                        $(this).addClass('active');
                        $('#' + targetId).addClass('active');
                    }
                });
 
                // Set initial active states if not already set
                $('.tab-navigation, .sub-tab-navigation').each(function() {
                    var $nav = $(this);
                    var $buttons = $nav.find('.tab-button');
                    var $activeButton = $buttons.filter('.active');
                   
                    // If no active button, set first one active
                    if ($activeButton.length === 0) {
                        $activeButton = $buttons.first().addClass('active');
                    }
                   
                    // Activate corresponding content
                    if ($activeButton.length) {
                        var targetId = $activeButton.data('target');
                        if (targetId) {
                            $('#' + targetId).addClass('active');
                        }
                    }
                });
               
                console.log('Tab initialization complete');
            } catch (error) {
                console.error('Error initializing tabs:', error);
            }
         }
         }
        // Initialize tabs
        initializeTabs();
          
          
         // Add Tooltip functionality for map links
        var overviewTargetId = $overviewTab.data('target');
         try {
       
            $('span.material-button a').each(function() {
         // Set overview tab and content as active
                var href = $(this).attr('href');
         $subNav.find('.tab-button, .tab-button-image').removeClass('active');
                if (href && href.indexOf('world-map') !== -1) {
        $overviewTab.addClass('active');
                    $(this).parent().attr('title', 'Click to view location on map');
       
                    $(this).prepend('<span class="map-icon">🗺️ </span>');
        $subNav.find('.tab-content .tab-pane').removeClass('active');
                }
        $('#' + overviewTargetId).addClass('active');
             });
    });
         } catch (error) {
   
             console.error('Error setting up tooltips:', error);
    // Create collapsible sections
    $('.collapsible-header').click(function() {
        $(this).toggleClass('collapsed');
        $(this).next('.collapsible-content').slideToggle(300);
       
        // Toggle the icon
        var $icon = $(this).find('.collapse-icon');
        if ($icon.text() === '▼') {
             $icon.text('►');
         } else {
             $icon.text('');
         }
         }
 
    });
        // Enhanced Image Previews
   
        try {
    // Initialize all sections as expanded
            $('.thumbimage').on('mouseenter', function() {
    $('.collapsible-header').each(function() {
                $(this).css({
        $(this).find('.collapse-icon').text('');
                    'transform': 'scale(1.05)',
    });
                    'transition': 'transform 0.3s ease',
   
                    'box-shadow': '0 8px 16px rgba(0,0,0,0.2)'
    // Sub-class tab click handler (works for both regular and image buttons)
                 });
    $('.sub-tab-navigation .tab-button, .sub-tab-navigation .tab-button-image').click(function(e) {
             }).on('mouseleave', function() {
        e.preventDefault();
                 $(this).css({
        e.stopPropagation();
                    'transform': '',
       
                    'box-shadow': ''
        var $this = $(this);
                 });
        var $subNav = $this.closest('.sub-tab-navigation');
             });
        var targetId = $this.data('target');
        } catch (error) {
       
             console.error('Error setting up image previews:', error);
        // Only update the buttons in this sub-tab navigation
        $subNav.find('.tab-button, .tab-button-image').removeClass('active');
        $this.addClass('active');
       
        // Find and update only the content panes within this sub-tab section
        $subNav.find('.tab-content .tab-pane').removeClass('active');
        $('#' + targetId).addClass('active');
       
        // Keep the parent tab-pane active
        $this.closest('.tab-pane').addClass('active');
    });
   
    // Initial setup
    function initializeTabs() {
        // Set first race tab active if none are
        var $mainNav = $('.tab-navigation');
        if (!$mainNav.find('.tab-button.active, .tab-button-image.active').length) {
            if ($mainNav.find('.tab-button-image').length) {
                var $firstMainTab = $mainNav.find('.tab-button-image').first();
                 $firstMainTab.addClass('active');
             } else {
                var $firstMainTab = $mainNav.find('.tab-button').first();
                 $firstMainTab.addClass('active');
            }
           
            var mainTargetId = $firstMainTab.data('target');
            $('#' + mainTargetId).addClass('active');
           
            // Set its overview tab active
            var $subNav = $('#' + mainTargetId + ' .sub-tab-navigation');
            var $overviewTab;
           
            // Check if using image tabs or regular tabs
            if ($subNav.find('.tab-button-image').length) {
                 $overviewTab = $subNav.find('.tab-button-image').first();
             } else {
                $overviewTab = $subNav.find('.tab-button').first();
            }
           
            $overviewTab.addClass('active');
             $('#' + $overviewTab.data('target')).addClass('active');
         }
         }
 
       
         // Improve Mobile Experience
         // For each race tab that's active
         try {
         $('.tab-navigation .tab-button.active, .tab-navigation .tab-button-image.active').each(function() {
            if (window.matchMedia("only screen and (max-width: 768px)").matches) {
            var mainTargetId = $(this).data('target');
                $('.tab-nav').css({
            var $mainContent = $('#' + mainTargetId);
                    'overflow-x': 'auto',
           
                    'white-space': 'nowrap',
            // Ensure overview tab is active if no sub-tab is active
                    '-webkit-overflow-scrolling': 'touch'
            var $subNav = $mainContent.find('.sub-tab-navigation');
                 });
            if ($subNav.length && !$subNav.find('.tab-button.active, .tab-button-image.active').length) {
                 var $overviewTab;
                  
                  
                 $('.tab-button').css({
                 // Check if using image tabs or regular tabs
                     'display': 'inline-block',
                if ($subNav.find('.tab-button-image').length) {
                     'float': 'none'
                     $overviewTab = $subNav.find('.tab-button-image').first();
                 });
                } else {
                     $overviewTab = $subNav.find('.tab-button').first();
                 }
                  
                  
                 $('.thumbimage').each(function() {
                 $overviewTab.addClass('active');
                    var $this = $(this);
                $('#' + $overviewTab.data('target')).addClass('active');
                    $this.on('touchstart', function() {
                        $this.css({
                            'transform': 'scale(1.05)',
                            'transition': 'transform 0.3s ease',
                            'box-shadow': '0 8px 16px rgba(0,0,0,0.2)'
                        });
                       
                        setTimeout(function() {
                            $this.css({
                                'transform': '',
                                'box-shadow': ''
                            });
                        }, 1000);
                    });
                });
             }
             }
         } catch (error) {
         });
             console.error('Error setting up mobile experience:', error);
    }
   
    // Run initial setup
    initializeTabs();
   
    // Add animation effect when switching tabs
    $('.tab-button, .tab-button-image').click(function() {
        $('.tab-pane.active').css('animation', 'tabFadeIn 0.3s');
        setTimeout(function() {
             $('.tab-pane.active').css('animation', '');
        }, 300);
    });
   
    // Add image button hover effect
    $('.tab-button-image').hover(function() {
        if (!$(this).hasClass('active')) {
            $(this).find('img').css('opacity', '0.8');
        }
    }, function() {
        $(this).find('img').css('opacity', '1');
    });
   
    // Add map tooltips
    $('span.material-button a').each(function() {
        var href = $(this).attr('href');
        if (href && href.indexOf('world-map') !== -1) {
            $(this).parent().attr('title', 'Click to view location on map');
            $(this).prepend('<span class="map-icon">🗺️ </span>');
         }
         }
     });
     });
   
    // Enhanced Image Previews
    $('.thumbimage').hover(function() {
        $(this).css({
            'transform': 'scale(1.05)',
            'transition': 'transform 0.3s ease',
            'box-shadow': '0 8px 16px rgba(0,0,0,0.2)'
        });
    }, function() {
        $(this).css({
            'transform': '',
            'box-shadow': ''
        });
    });
   
    // Mobile Experience Optimization
    if (window.matchMedia("only screen and (max-width: 768px)").matches) {
        // Make tab navigation scrollable
        $('.tab-nav').css({
            'overflow-x': 'auto',
            'white-space': 'nowrap',
            '-webkit-overflow-scrolling': 'touch'
        });
       
        // Ensure tab buttons don't wrap
        $('.tab-button, .tab-button-image').css({
            'display': 'inline-block',
            'float': 'none'
        });
       
        // Adjust image button sizes for mobile
        $('.tab-button-image').css({
            'width': '100px',
            'height': '70px'
        });
       
        // Touch-friendly image previews
        $('.thumbimage, .tab-button-image').on('touchstart', function() {
            var $this = $(this);
            $this.css({
                'transform': 'scale(1.05)',
                'transition': 'transform 0.3s ease',
                'box-shadow': '0 8px 16px rgba(0,0,0,0.2)'
            });
           
            setTimeout(function() {
                $this.css({
                    'transform': '',
                    'box-shadow': ''
                });
            }, 1000);
        });
    }
   
    // Add preload for tab images to prevent flickering on first load
    function preloadTabImages() {
        $('.tab-button-image img').each(function() {
            var img = new Image();
            img.src = $(this).attr('src');
        });
    }
   
    // Run preload
    preloadTabImages();
});
});

Latest revision as of 20:37, 1 March 2025

/* Any JavaScript here will be loaded for all users on every page load. */

$(document).ready(function() {
    'use strict';
    
    // Main race tab click handler (works for both regular and image buttons)
    $('.tab-navigation .tab-button, .tab-navigation .tab-button-image').click(function(e) {
        e.preventDefault();
        
        var targetId = $(this).data('target');
        
        // Update main race tabs (for both button types)
        $('.tab-navigation .tab-button, .tab-navigation .tab-button-image').removeClass('active');
        $(this).addClass('active');
        
        // Update main content panes
        $('.tab-content > .tab-pane').removeClass('active');
        $('#' + targetId).addClass('active');
        
        // Always activate the overview tab for the selected race
        var $subNav = $('#' + targetId + ' .sub-tab-navigation');
        var $overviewTab;
        
        // Check if using image tabs or regular tabs
        if ($subNav.find('.tab-button-image').length) {
            $overviewTab = $subNav.find('.tab-button-image').first();
        } else {
            $overviewTab = $subNav.find('.tab-button').first();
        }
        
        var overviewTargetId = $overviewTab.data('target');
        
        // Set overview tab and content as active
        $subNav.find('.tab-button, .tab-button-image').removeClass('active');
        $overviewTab.addClass('active');
        
        $subNav.find('.tab-content .tab-pane').removeClass('active');
        $('#' + overviewTargetId).addClass('active');
    });
    
    // Create collapsible sections
    $('.collapsible-header').click(function() {
        $(this).toggleClass('collapsed');
        $(this).next('.collapsible-content').slideToggle(300);
        
        // Toggle the icon
        var $icon = $(this).find('.collapse-icon');
        if ($icon.text() === '▼') {
            $icon.text('►');
        } else {
            $icon.text('▼');
        }
    });
    
    // Initialize all sections as expanded
    $('.collapsible-header').each(function() {
        $(this).find('.collapse-icon').text('▼');
    });
    
    // Sub-class tab click handler (works for both regular and image buttons)
    $('.sub-tab-navigation .tab-button, .sub-tab-navigation .tab-button-image').click(function(e) {
        e.preventDefault();
        e.stopPropagation();
        
        var $this = $(this);
        var $subNav = $this.closest('.sub-tab-navigation');
        var targetId = $this.data('target');
        
        // Only update the buttons in this sub-tab navigation
        $subNav.find('.tab-button, .tab-button-image').removeClass('active');
        $this.addClass('active');
        
        // Find and update only the content panes within this sub-tab section
        $subNav.find('.tab-content .tab-pane').removeClass('active');
        $('#' + targetId).addClass('active');
        
        // Keep the parent tab-pane active
        $this.closest('.tab-pane').addClass('active');
    });
    
    // Initial setup
    function initializeTabs() {
        // Set first race tab active if none are
        var $mainNav = $('.tab-navigation');
        if (!$mainNav.find('.tab-button.active, .tab-button-image.active').length) {
            if ($mainNav.find('.tab-button-image').length) {
                var $firstMainTab = $mainNav.find('.tab-button-image').first();
                $firstMainTab.addClass('active');
            } else {
                var $firstMainTab = $mainNav.find('.tab-button').first();
                $firstMainTab.addClass('active');
            }
            
            var mainTargetId = $firstMainTab.data('target');
            $('#' + mainTargetId).addClass('active');
            
            // Set its overview tab active
            var $subNav = $('#' + mainTargetId + ' .sub-tab-navigation');
            var $overviewTab;
            
            // Check if using image tabs or regular tabs
            if ($subNav.find('.tab-button-image').length) {
                $overviewTab = $subNav.find('.tab-button-image').first();
            } else {
                $overviewTab = $subNav.find('.tab-button').first();
            }
            
            $overviewTab.addClass('active');
            $('#' + $overviewTab.data('target')).addClass('active');
        }
        
        // For each race tab that's active
        $('.tab-navigation .tab-button.active, .tab-navigation .tab-button-image.active').each(function() {
            var mainTargetId = $(this).data('target');
            var $mainContent = $('#' + mainTargetId);
            
            // Ensure overview tab is active if no sub-tab is active
            var $subNav = $mainContent.find('.sub-tab-navigation');
            if ($subNav.length && !$subNav.find('.tab-button.active, .tab-button-image.active').length) {
                var $overviewTab;
                
                // Check if using image tabs or regular tabs
                if ($subNav.find('.tab-button-image').length) {
                    $overviewTab = $subNav.find('.tab-button-image').first();
                } else {
                    $overviewTab = $subNav.find('.tab-button').first();
                }
                
                $overviewTab.addClass('active');
                $('#' + $overviewTab.data('target')).addClass('active');
            }
        });
    }
    
    // Run initial setup
    initializeTabs();
    
    // Add animation effect when switching tabs
    $('.tab-button, .tab-button-image').click(function() {
        $('.tab-pane.active').css('animation', 'tabFadeIn 0.3s');
        setTimeout(function() {
            $('.tab-pane.active').css('animation', '');
        }, 300);
    });
    
    // Add image button hover effect
    $('.tab-button-image').hover(function() {
        if (!$(this).hasClass('active')) {
            $(this).find('img').css('opacity', '0.8');
        }
    }, function() {
        $(this).find('img').css('opacity', '1');
    });
    
    // Add map tooltips
    $('span.material-button a').each(function() {
        var href = $(this).attr('href');
        if (href && href.indexOf('world-map') !== -1) {
            $(this).parent().attr('title', 'Click to view location on map');
            $(this).prepend('<span class="map-icon">🗺️ </span>');
        }
    });
    
    // Enhanced Image Previews
    $('.thumbimage').hover(function() {
        $(this).css({
            'transform': 'scale(1.05)',
            'transition': 'transform 0.3s ease',
            'box-shadow': '0 8px 16px rgba(0,0,0,0.2)'
        });
    }, function() {
        $(this).css({
            'transform': '',
            'box-shadow': ''
        });
    });
    
    // Mobile Experience Optimization
    if (window.matchMedia("only screen and (max-width: 768px)").matches) {
        // Make tab navigation scrollable
        $('.tab-nav').css({
            'overflow-x': 'auto',
            'white-space': 'nowrap',
            '-webkit-overflow-scrolling': 'touch'
        });
        
        // Ensure tab buttons don't wrap
        $('.tab-button, .tab-button-image').css({
            'display': 'inline-block',
            'float': 'none'
        });
        
        // Adjust image button sizes for mobile
        $('.tab-button-image').css({
            'width': '100px',
            'height': '70px'
        });
        
        // Touch-friendly image previews
        $('.thumbimage, .tab-button-image').on('touchstart', function() {
            var $this = $(this);
            $this.css({
                'transform': 'scale(1.05)',
                'transition': 'transform 0.3s ease',
                'box-shadow': '0 8px 16px rgba(0,0,0,0.2)'
            });
            
            setTimeout(function() {
                $this.css({
                    'transform': '',
                    'box-shadow': ''
                });
            }, 1000);
        });
    }
    
    // Add preload for tab images to prevent flickering on first load
    function preloadTabImages() {
        $('.tab-button-image img').each(function() {
            var img = new Image();
            img.src = $(this).attr('src');
        });
    }
    
    // Run preload
    preloadTabImages();
});