MediaWiki:Common.js: Difference between revisions

From Ultimate Dragon Ball Online Wiki

Ravenalien (talk | contribs)
No edit summary
Ravenalien (talk | contribs)
No edit summary
Line 5: Line 5:
     // Wait for DOM to be ready using MediaWiki hook
     // Wait for DOM to be ready using MediaWiki hook
     mw.hook('dom.ready').add(function() {
     mw.hook('dom.ready').add(function() {
         // Tab Navigation System
         console.log('Tab navigation script loaded');
        (function() {
 
            // Main Race Tab Navigation
        function initializeTabs() {
            $(document).on('click', '.tab-navigation > .tab-nav > .tab-button', function(e) {
            try {
                e.preventDefault();
                 // Main tabs (Race selection)
                var $this = $(this);
                 $('.tab-button').on('click', function(e) {
                var targetId = $this.data('target');
                    e.preventDefault();
               
                     console.log('Tab clicked:', $(this).data('target'));
                 // Remove active class from all main tab buttons
                $this.closest('.tab-nav').find('.tab-button').removeClass('active');
                // Add active class to clicked button
                $this.addClass('active');
               
                // Remove active class from all main tab panes
                 $this.closest('.tab-navigation').next('.tab-content').find('> .tab-pane').removeClass('active');
                // Add active class to target pane
                $('#' + targetId).addClass('active');
               
                // Reset sub-tabs to first tab when changing main tab
                var $subTabNav = $('#' + targetId + ' .sub-tab-navigation');
                if ($subTabNav.length) {
                     // Reset sub-tab buttons
                    var $firstSubButton = $subTabNav.find('> .tab-nav > .tab-button').first();
                    $subTabNav.find('> .tab-nav > .tab-button').removeClass('active');
                    $firstSubButton.addClass('active');
                      
                      
                    // Reset sub-tab panes
                     var targetId = $(this).data('target');
                     var firstSubTabId = $firstSubButton.data('target');
                     if (!targetId) {
                     $subTabNav.find('> .tab-content > .tab-pane').removeClass('active');
                        console.error('No target ID found for tab');
                    $('#' + firstSubTabId).addClass('active');
                        return;
                }
                    }
            });
 
           
                    // Handle main tab navigation
            // Sub-Tab Navigation
                    if ($(this).closest('.tab-navigation').length) {
            $(document).on('click', '.sub-tab-navigation > .tab-nav > .tab-button', function(e) {
                        // Remove active class from all main tabs and contents
                e.preventDefault();
                        $('.tab-navigation .tab-button').removeClass('active');
                var $this = $(this);
                        $('.tab-navigation').siblings('.tab-content').children('.tab-pane').removeClass('active');
                var targetId = $this.data('target');
                       
               
                        // Activate clicked tab and its content
                // Remove active class from all sub-tab buttons and add to clicked
                        $(this).addClass('active');
                $this.closest('.tab-nav').find('.tab-button').removeClass('active');
                        $('#' + targetId).addClass('active');
                $this.addClass('active');
                     }
               
                     // Handle sub-tab navigation
                // Remove active class from all sub-tab panes and add to target
                     else if ($(this).closest('.sub-tab-navigation').length) {
                $this.closest('.sub-tab-navigation').find('.tab-content > .tab-pane').removeClass('active');
                         var $subNav = $(this).closest('.sub-tab-navigation');
                $('#' + targetId).addClass('active');
            });
           
            // Ensure initial tab states are set
            $('.tab-navigation').each(function() {
                var $mainNav = $(this);
                var $mainButtons = $mainNav.find('> .tab-nav > .tab-button');
               
                // If no main tab is active, activate the first one
                if (!$mainButtons.filter('.active').length) {
                    $mainButtons.first().addClass('active');
                }
               
                // Get active main tab's target
                var activeMainTabId = $mainButtons.filter('.active').data('target');
                if (activeMainTabId) {
                    // Show active main tab's content
                    $('#' + activeMainTabId).addClass('active');
                      
                     // Handle sub-tabs within active main tab
                     var $subNav = $('#' + activeMainTabId + ' .sub-tab-navigation');
                    if ($subNav.length) {
                         var $subButtons = $subNav.find('> .tab-nav > .tab-button');
                          
                          
                         // If no sub-tab is active, activate the first one
                         // Remove active class from sibling tabs and contents
                         if (!$subButtons.filter('.active').length) {
                         $subNav.find('.tab-button').removeClass('active');
                            $subButtons.first().addClass('active');
                        $subNav.find('.tab-pane').removeClass('active');
                        }
                          
                          
                         // Show active sub-tab's content
                         // Activate clicked tab and its content
                        var activeSubTabId = $subButtons.filter('.active').data('target');
                        $(this).addClass('active');
                         if (activeSubTabId) {
                        $('#' + targetId).addClass('active');
                             $('#' + activeSubTabId).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
         // Add Tooltip functionality for map links
         (function() {
         try {
             $('span.material-button a').each(function() {
             $('span.material-button a').each(function() {
                 var href = $(this).attr('href');
                 var href = $(this).attr('href');
Line 98: Line 82:
                 }
                 }
             });
             });
         })();
         } catch (error) {
            console.error('Error setting up tooltips:', error);
        }


         // Enhanced Image Previews
         // Enhanced Image Previews
         (function() {
         try {
             $('.thumbimage').on('mouseenter', function() {
             $('.thumbimage').on('mouseenter', function() {
                 $(this).css({
                 $(this).css({
Line 114: Line 100:
                 });
                 });
             });
             });
         })();
         } catch (error) {
            console.error('Error setting up image previews:', error);
        }


         // Improve Mobile Experience
         // Improve Mobile Experience
         (function() {
         try {
             if (window.matchMedia("only screen and (max-width: 768px)").matches) {
             if (window.matchMedia("only screen and (max-width: 768px)").matches) {
                 $('.tab-nav').css({
                 $('.tab-nav').css({
Line 148: Line 136:
                 });
                 });
             }
             }
         })();
         } catch (error) {
            console.error('Error setting up mobile experience:', error);
        }
     });
     });
});
});

Revision as of 06:34, 26 February 2025

/* Any JavaScript here will be loaded for all users on every page load. */
mw.loader.using(['jquery'], function ($) {
    'use strict';
    
    // Wait for DOM to be ready using MediaWiki hook
    mw.hook('dom.ready').add(function() {
        console.log('Tab navigation script loaded');

        function initializeTabs() {
            try {
                // Main tabs (Race selection)
                $('.tab-button').on('click', function(e) {
                    e.preventDefault();
                    console.log('Tab clicked:', $(this).data('target'));
                    
                    var targetId = $(this).data('target');
                    if (!targetId) {
                        console.error('No target ID found for tab');
                        return;
                    }

                    // Handle main tab navigation
                    if ($(this).closest('.tab-navigation').length) {
                        // Remove active class from all main tabs and contents
                        $('.tab-navigation .tab-button').removeClass('active');
                        $('.tab-navigation').siblings('.tab-content').children('.tab-pane').removeClass('active');
                        
                        // 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
        try {
            $('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>');
                }
            });
        } catch (error) {
            console.error('Error setting up tooltips:', error);
        }

        // Enhanced Image Previews
        try {
            $('.thumbimage').on('mouseenter', function() {
                $(this).css({
                    'transform': 'scale(1.05)',
                    'transition': 'transform 0.3s ease',
                    'box-shadow': '0 8px 16px rgba(0,0,0,0.2)'
                });
            }).on('mouseleave', function() {
                $(this).css({
                    'transform': '',
                    'box-shadow': ''
                });
            });
        } catch (error) {
            console.error('Error setting up image previews:', error);
        }

        // Improve Mobile Experience
        try {
            if (window.matchMedia("only screen and (max-width: 768px)").matches) {
                $('.tab-nav').css({
                    'overflow-x': 'auto',
                    'white-space': 'nowrap',
                    '-webkit-overflow-scrolling': 'touch'
                });
                
                $('.tab-button').css({
                    'display': 'inline-block',
                    'float': 'none'
                });
                
                $('.thumbimage').each(function() {
                    var $this = $(this);
                    $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);
        }
    });
});