MediaWiki:Gadget-CustomButtons.js

From JoJo's Bizarre Encyclopedia - JoJo Wiki
Revision as of 13:29, 26 July 2024 by Vish (talk | contribs)
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
$(document).ready(function() {
	/* Quote Template Buttons */
    $('.quote-buttons').each(function() {
        var $quoteBox = $(this).closest('.quoteBox');

        $(this).find('#showAllButton').click(function() {
            $quoteBox.find('.nihongo-english').css('display', 'inline');
            $quoteBox.find('.nihongo-japanese').css('display', 'inline');
            $quoteBox.find('.nihongo-bracket').css('display', 'inline');
            $quoteBox.find('.nihongo-romaji').remove(); // Remove any appended romaji spans
        });

        $(this).find('#showEnglishButton').click(function() {
            $quoteBox.find('.nihongo-english').css('display', 'inline');
            $quoteBox.find('.nihongo-japanese').css('display', 'none');
            $quoteBox.find('.nihongo-romaji').css('display', 'none');
            $quoteBox.find('.nihongo-bracket').css('display', 'none');
        });

        $(this).find('#showJapaneseButton').click(function() {
            $quoteBox.find('.nihongo-english').css('display', 'none');
            $quoteBox.find('.nihongo-japanese').css('display', 'inline');
            $quoteBox.find('.nihongo-romaji').css('display', 'none');
            $quoteBox.find('.nihongo-bracket').css('display', 'none');
        });

        $(this).find('#showRomajiButton').click(function() {
            $quoteBox.find('.nihongo-english').css('display', 'none');
            $quoteBox.find('.nihongo-bracket').css('display', 'none');

            $quoteBox.find('.nihongo-japanese').each(function() {
                var $japaneseElement = $(this);
                var romaji = $japaneseElement.data('content');

                if (romaji) {
                    if ($japaneseElement.next('.nihongo-romaji').length === 0) {
                        $japaneseElement.after('<span class="nihongo-romaji" style="display:inline; font-family:Meiryo">' + romaji + '</span>');
                    } else {
                        $japaneseElement.next('.nihongo-romaji').css('display', 'inline');
                    }
                    $japaneseElement.css('display', 'none');
                } else {
                    $japaneseElement.closest('.popup-wrapper').find('.nihongo-english').css('display', 'inline');
                    $japaneseElement.css('display', 'none');
                }
            });

            $quoteBox.find('.popup-box').css('display', 'none');
        });
    });
    
    /* DiffBox Template Buttons */
    // Function to toggle images within a specific container
    function toggleImages(container, showImage1) {
        if (showImage1) {
            container.find('.volDiffImage1').css('display', 'block');
            container.find('.volDiffImage2').css('display', 'none');
        } else {
            container.find('.volDiffImage1').css('display', 'none');
            container.find('.volDiffImage2').css('display', 'block');
        }
    }

    // Function to toggle the part class for buttons
    function toggleButtonPartClass(container, tab1Selected) {
        var part = container.find('.volDiffButtons').data('part');
        if (tab1Selected) {
            container.find('.volDiffTab1').addClass(part);
            container.find('.volDiffTab2').removeClass(part);
        } else {
            container.find('.volDiffTab1').removeClass(part);
            container.find('.volDiffTab2').addClass(part);
        }
    }

    // Event listener for individual tab buttons
    $(document).on('click', '.volDiffTab1', function() {
        var container = $(this).closest('.volDiffBox');
        toggleImages(container, true);
        toggleButtonPartClass(container, true);
    });

    $(document).on('click', '.volDiffTab2', function() {
        var container = $(this).closest('.volDiffBox');
        toggleImages(container, false);
        toggleButtonPartClass(container, false);
    });

    // Event listener for global buttons
    $('.volDiffBtn1').click(function() {
        $('.volDiffBox').each(function() {
            toggleImages($(this), true);
            toggleButtonPartClass($(this), true);
        });
    });

    $('.volDiffBtn2').click(function() {
        $('.volDiffBox').each(function() {
            toggleImages($(this), false);
            toggleButtonPartClass($(this), false);
        });
    });
    
	// Function to toggle expand/collapse
	function toggleExpandCollapse() {
	    var $containerBody = $(this).closest('.volDiffContainer').find('.volDiffContainerBody');
	    var isExpanded = $containerBody.css('max-height') === 'none';
	
	    if (isExpanded) {
	        $containerBody.css('max-height', '375px');
	        $(this).text('Expand');
	    } else {
	        $containerBody.css('max-height', 'none');
	        $(this).text('Collapse');
	    }
	}

	// Function to check if the content is overflowing
	function checkOverflow() {
	    $('.volDiffContainer').each(function() {
	        var $containerBody = $(this).find('.volDiffContainerBody');
	        var $expandBtn = $(this).find('.volDiffExpandBtn');
	
	        // Check if content is overflowing
	        if ($containerBody[0].scrollHeight > $containerBody[0].clientHeight) {
	            $expandBtn.show();
	        } else {
	            $expandBtn.hide();
	        }
	    });
	}
	
	// Attach click event to the expand/collapse button
	$('.volDiffExpandBtn').click(toggleExpandCollapse);
	
	// Check for overflow when the document is ready
	$(document).ready(function() {
	    checkOverflow();
	    
	    // Re-check for overflow on window resize to ensure responsiveness
	    $(window).resize(function() {
	        checkOverflow();
	    });
	});

});