MediaWiki:Common.js
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.
/**
* For language wikis, use
* <code> mw.loader.load("https://herosiege.wiki.gg/load.php?lang=en&modules=site&only=scripts&skin=vector"); </code>
* as the content of MediaWiki:Common.js to import from this script.
*
* See [https://herosiege.wiki.gg/zh/wiki/MediaWiki:Common.js zh wiki] for an example.
*
*/
// load various utilities from the commons wiki, including l10nFactory and isEditorActive
mw.loader.getScript( 'https://commons.wiki.gg/index.php?title=MediaWiki:Common-base.js&action=raw&ctype=text/javascript' );
mw.loader.load('https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js');
/**
* select2
*/
// Load Select2 JavaScript
mw.loader.load('https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js');
// Ensure jQuery UI is also loaded if needed
mw.loader.using(['jquery.ui'], function () {
// Now you can safely use jQuery UI and Select2 in your code
});
mw.loader.using('jquery', function () {
$(function () {
$('select.form-select2-init').select2({ width: '275px' });
if( $('select.form-select2').val() ){
$('select.form-select2').select2({ width: '275px' });
}
var observer = new MutationObserver(function (mutations) {
mutations.forEach(function (mutation) {
if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
$(mutation.addedNodes).find('select.form-select2').each(function () {
if (!$(this).data('select2')) {
console.log('Applying Select2 to newly added dropdown');
$(this).select2({width: '275px'});
}
});
}
});
});
// Configure and start the observer
observer.observe(document.body, {
childList: true,
subtree: true
});
});
});
$(document).on('select2:open', function(){
document.querySelector('.select2-search__field').focus();
});
mw.loader.using('jquery', function () {
$(document).ready(function() {
function toggleExtraOptions() {
var $checkbox = $(this.firstChild);
var isChecked = $checkbox.is(':checked');
var $statBlock = $checkbox.closest('.stat-block');
if ($statBlock.length) {
var $extraOptionsDiv = $statBlock.find('.extra-options');
if ($extraOptionsDiv.length) {
if (isChecked) {
$extraOptionsDiv.show();
} else {
$extraOptionsDiv.hide();
}
} else {
console.log("Extra options div not found");
}
} else {
console.log("Stat block not found");
}
}
function assignEventListeners() {
$('.extra-options-checkbox').off('change').on('change', toggleExtraOptions);
}
assignEventListeners();
var observer = new MutationObserver(function(mutationsList) {
mutationsList.forEach(function(mutation) {
if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
assignEventListeners();
}
});
});
observer.observe(document.body, { childList: true, subtree: true });
});
});
mw.loader.using('jquery', function () {
var statsSpells = [
'cast_when_attacking',
'chance_after_blocking',
'chance_after_each_kill',
'cast_after_kill',
'chance_when_attacking',
'chance_when_casting',
'chance_when_spellhit',
'chance_when_striking',
'chance_when_struck',
'grant_spell',
'grant_aura',
'grant_aura_holder_only',
];
var statsSkills = ['all_skills_flat', 'physical_skills_flat', 'magic_skills_flat', 'arcane_skills_flat', 'poison_skills_flat', 'lightning_skills_flat', 'cold_skills_flat', 'fire_skills_flat', 'summon_skills_flat', 'random_skill_flat'];
$(document).on('change', '.stat_id_dropdown', function() {
var selectedValue = $(this).val();
var statBlock = $(this).closest('.stat-block');
if (statsSpells.includes(selectedValue)) {
statBlock.find('.extra-options').show();
statBlock.find('.class-option').show();
} else if (statsSkills.includes(selectedValue)) {
statBlock.find('.extra-options').hide();
statBlock.find('.class-option').show();
} else {
statBlock.find('.extra-options').hide();
statBlock.find('.class-option').hide();
}
});
// Trigger the change event on page load to set the initial state
$('.stat_id_dropdown').trigger('change');
});
/**
* content width toggle
*/
$(function(){
$body = $('body');
$('<div id="nav-content-size-toggle"><span></span></div>')
.prependTo($('#mw-head'))
.on('click', function(){
$body.toggleClass('content-size-expanded');
$(window).trigger('resize');
});
});
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* sidebar nav menu toggle for mobile view
*/
$(function(){
$('<div class="menu-toggle"/>').insertBefore($('#p-logo')).on("click", function(event){
event.stopPropagation();
$(this).toggleClass('expanded');
});
});
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* mobile floating fix
*/
$(function(){
var $contentBox = $('#mw-content-text .mw-parser-output');
var $elements = $contentBox.children();
var handle = function(){
var fullWidth = $contentBox.width();
if(!fullWidth){
return;
}
var offset = $contentBox.offset().left;
$elements.removeClass('mobile-floating-fix mobile-fullwidth');
if(fullWidth > 720){
return;
}
var maxLeft = 0;
for(var i=$elements.length; i>0; i--){
var $el = $($elements[i-1]);
if($el.css('float') == 'right'){
var left = $el.offset().left;
if(left - offset < 300 || (maxLeft && left < maxLeft + 12) ){
$el.addClass('mobile-floating-fix');
maxLeft = Math.max(maxLeft, left + $el.outerWidth());
continue;
}
}
maxLeft = 0;
}
var threshold = Math.min(90, fullWidth*0.25);
$('#mw-content-text .infobox, #mw-content-text .portable-infobox').each(function(){
var $el = $(this);
if(fullWidth - $el.outerWidth() < threshold){
$el.addClass('mobile-fullwidth');
}
});
};
handle();
$(window).on('resize', mw.util.debounce( handle, 200) );
});
///////////////////////////////////////////////////////////////////////////////////////////////////////////////