MediaWiki:Common.js: Difference between revisions

From Path of Exile Wiki
Jump to navigation Jump to search
(Use gadget for responsive tables instead. See MediaWiki:Gadget-responsive-tables.js)
(Set default file upload license to unknown instead of none)
Line 5: Line 5:
'use strict';
'use strict';
/* DO NOT ADD CODE ABOVE THIS LINE */
/* DO NOT ADD CODE ABOVE THIS LINE */
/* Translation strings */
var i18n = {
    defaultLicense: 'Copyright unlicensed'
};


/**
/**
Line 146: Line 151:
         $('.quality .quality-' + index).addClass('quality-selected');
         $('.quality .quality-' + index).addClass('quality-selected');
     })
     })
}
function setDefaultFileUploadLicense() {
    if ( mw.config.get('wgCanonicalSpecialPageName') === 'Upload' ) {
        mw.loader.using( 'mediawiki.special.upload', function() {
            var licenseField = document.getElementById('wpLicense');
            if ( licenseField ) {
                const changeEvent = new Event('change');
                if ( licenseField.value === '' ) {
                    licenseField.value = i18n.defaultLicense;
                }
                licenseField.dispatchEvent(changeEvent);
            }
        } );
    }
}
}


/* Fires when DOM is ready */
/* Fires when DOM is ready */
$(function() {
$( function() {
 
     /* For adding expand/collapse all buttons for mw-collapsible */
     /* For adding expand/collapse all buttons for mw-collapsible */
     $(".mw-collapsible-collapse-all").on("click", function () {
     $(".mw-collapsible-collapse-all").on("click", function () {
Line 176: Line 197:
     // Change 1 & 0 to checkmarks in tables  
     // Change 1 & 0 to checkmarks in tables  
     boolean_table();
     boolean_table();
   


});
    // Set default file upload license to unknown instead of none
    setDefaultFileUploadLicense();
 
} );
/* End DOM ready */
/* End DOM ready */


/* DO NOT ADD CODE BELOW THIS LINE */
/* DO NOT ADD CODE BELOW THIS LINE */
}() );
}() );

Revision as of 04:21, 24 December 2021

/* global mw, $ */
/* jshint strict:false, browser:true */

( function() {
'use strict';
/* DO NOT ADD CODE ABOVE THIS LINE */

/* Translation strings */
var i18n = {
    defaultLicense: 'Copyright unlicensed'
};

/**
 * Hoverbox
 * @param  config  Object containing configuration
 */
function hoverbox(config) {
    const defaults = {
        mainClass: 'hoverbox',
        activatorClass: 'hoverbox__activator',
        displayClass: 'hoverbox__display'
    };
    config = Object.assign(defaults, config);
    var timestamp = Date.now(),
        $container = $('#hoverbox-displays-' + timestamp);
    if ( $container.length === 0 ) {
        $container = $('<div id="hoverbox-displays-' + timestamp + '" class="hoverbox-display-container"></div>');
    }
    $('body').append($container);
    var $hoverbox = $('.' + config.mainClass),
        idCounter = 0;
    $hoverbox.each(function() {
        var $this = $(this),
            $activator = $this.find('.' + config.activatorClass).first(),
            $display = $this.find('.' + config.displayClass).first(),
            id = $this.data('hoverbox-id') || idCounter++,
            $target = $container.find('[data-hoverbox-target="' + id + '"]');
        if ( $target.length === 0 ) {
            $container.append($display);
            $display.attr('data-hoverbox-target', id);
        } else {
            $display.remove();
            $display = $target;
        }
        $activator.hover(function() {
            var viewport = {},
                activator = {},
                display = {},
                position, // position relative to the activator
                location; // location relative to the viewport
            viewport.width = document.documentElement.clientWidth;
            viewport.height = document.documentElement.clientHeight;
            viewport.top = document.documentElement.scrollTop;
            viewport.left = document.documentElement.scrollLeft;
            viewport.bottom = viewport.top + viewport.height;
            viewport.right = viewport.left + viewport.width;
            activator.width = $activator.outerWidth();
            activator.height = $activator.outerHeight();
            activator.top = $activator.offset().top;
            activator.left = $activator.offset().left;
            activator.bottom = activator.top + activator.height;
            activator.right = activator.left + activator.width;
            display.width = $display.outerWidth();
            display.height = $display.outerHeight();
            if (viewport.width < display.width) { // Don't bother showing the hoverbox at all if the viewport is too small
                return false;
            }
            if (activator.left > viewport.width - activator.right) {
                location = 'right';
            } else {
                location = 'left';
            }
            if (activator.top - display.height > viewport.top) {
                position = 'above';
                display.top = activator.top - display.height;
                display.left = activator.left + (activator.width / 2) - (display.width / 2);
            } else if (activator.right + display.width < viewport.right) {
                position = 'right-of';
                display.top = activator.top + (activator.height / 2) - (display.height / 2);
                display.left = activator.right;
            } else if (activator.left - display.width > viewport.left) {
                position = 'left-of';
                display.top = activator.top + (activator.height / 2) - (display.height / 2);
                display.left = activator.left - display.width;
            } else {
                position = 'below';
                display.top = activator.bottom;
                display.left = activator.left + (activator.width / 2) - (display.width / 2);
            }
            display.top = Math.max(viewport.top, display.top);
            display.left = Math.max(viewport.left, Math.min(viewport.right - display.width, display.left));
            $display.addClass('is-visible is-' + position + '-activator is-' + location + '-side-of-viewport').offset(display);
        }, function() {
            $display.removeClass('is-visible is-above-activator is-below-activator is-left-of-activator is-right-of-activator is-left-side-of-viewport is-right-side-of-viewport');
        });
    });
}

/*
 * Veiled modifiers
 */
function veiledModifier() {
    var mainClass = 'veiled'; // Class name of veiled modifier elements
    var affixCount = 6; // Number of veiled prefixes and veiled suffixes
    var affixClass = '-m0$1'; // $1 is replaced with a random number between 1 and affixCount
    var elements = document.getElementsByClassName(mainClass);
    if ( elements.length > 0 ) {
        var last, random;
        for ( var i = 0; i < elements.length; i++ ) {
            var element = elements[i];
            if ( last === undefined ) { // Choose with a random integer between 1 and affixCount
                random = Math.floor(Math.random() * affixCount) + 1;
            } else { // Continue choosing random integers in the same range while ensuring no repeats
                random = Math.floor(Math.random() * (affixCount - 1)) + 1;
                if ( random >= last ) {
                    random = random + 1;
                }
            }
            last = random;
            element.classList.add(affixClass.replace('$1', random));
        }
    }
}

function boolean_table() {
    $('.boolean-table').find('table').find('td').each(function () {
        var text = $(this).html();
        if (text == '0') {
            $(this).html('&#x2717;');
            $(this).addClass('table-cell-xmark');
        } else if (text == '1') {
            $(this).html('&#x2713;');
            $(this).addClass('table-cell-checkmark');
        }
    })
}

function quality_selector() {
    $('.quality-section-select').click(function () {
        var index;
        var classes = $(this).attr('class').split(/\s+/);
        for (var i = 0; i < classes.length; i++) {
            var match = classes[i].match('(?:quality\-)([0-9]+)');
            if (match) {
                index = Number(match[1]);
                break;
            }
        }
        $('.quality-section-select').removeClass('quality-selected');
        $('.quality-box').removeClass('quality-selected');
        $('.quality .quality-' + index).addClass('quality-selected');
    })
}

function setDefaultFileUploadLicense() {
    if ( mw.config.get('wgCanonicalSpecialPageName') === 'Upload' ) {
        mw.loader.using( 'mediawiki.special.upload', function() {
            var licenseField = document.getElementById('wpLicense');
            if ( licenseField ) {
                const changeEvent = new Event('change');
                if ( licenseField.value === '' ) {
                    licenseField.value = i18n.defaultLicense;
                }
                licenseField.dispatchEvent(changeEvent);
            }
        } );
    }
}

/* Fires when DOM is ready */
$( function() {

    /* For adding expand/collapse all buttons for mw-collapsible */
    $(".mw-collapsible-collapse-all").on("click", function () {
        $('.mw-collapsible-toggle-expanded a').trigger('click');
    });
    $(".mw-collapsible-expand-all").on("click", function () {
        $(".mw-collapsible-toggle-collapsed a").trigger('click');
    });

    // Hoverbox
    hoverbox();

    // Item hoverbox
    hoverbox({
        mainClass: 'c-item-hoverbox',
        activatorClass: 'c-item-hoverbox__activator',
        displayClass: 'c-item-hoverbox__display'
    });

    // Veiled modifiers
    veiledModifier();
    
    // Quality selector in item box
    quality_selector();
    
    // Change 1 & 0 to checkmarks in tables 
    boolean_table();

    // Set default file upload license to unknown instead of none
    setDefaultFileUploadLicense();

} );
/* End DOM ready */

/* DO NOT ADD CODE BELOW THIS LINE */
}() );