Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

MediaWiki:Common.js

MediaWiki interface page
Revision as of 05:09, 30 June 2026 by Aurelian (talk | contribs)

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)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/* Bottom collapse controls */
mw.loader.using('jquery.makeCollapsible').then(function () {
	var exampleTableQuery = window.matchMedia('(max-width: 899px)');

	function updateExampleTableState($table) {
		$table
			.children('caption')
			.attr('aria-expanded', $table.hasClass('mw-collapsed') ? 'false' : 'true');
	}

	function initialiseExampleTables() {
		$('.wizlords-example-table.mw-collapsible').each(function () {
			var $table = $(this);
			var $caption = $table.children('caption');

			if (exampleTableQuery.matches) {
				$caption
					.attr('role', 'button')
					.attr('tabindex', '0');

				updateExampleTableState($table);
			} else {
				$caption
					.removeAttr('role')
					.removeAttr('tabindex')
					.removeAttr('aria-expanded');
			}
		});
	}

	$(initialiseExampleTables);

	if (exampleTableQuery.addEventListener) {
		exampleTableQuery.addEventListener('change', initialiseExampleTables);
	} else if (exampleTableQuery.addListener) {
		exampleTableQuery.addListener(initialiseExampleTables);
	}

	$(document).on('click', '.wizlords-example-table.mw-collapsible > caption', function (event) {
		if (!exampleTableQuery.matches) {
			return;
		}

		if ($(event.target).closest('.mw-collapsible-toggle').length) {
			return;
		}

		var $table = $(this).closest('.wizlords-example-table');
		var $toggle = $table.find('> caption .mw-collapsible-toggle').first();

		if ($toggle.length) {
			$toggle.trigger('click');
			window.setTimeout(function () {
				updateExampleTableState($table);
			}, 0);
		}
	});

	$(document).on('keydown', '.wizlords-example-table.mw-collapsible > caption', function (event) {
		if (!exampleTableQuery.matches) {
			return;
		}

		if (event.key === 'Enter' || event.key === ' ') {
			event.preventDefault();
			$(this).trigger('click');
		}
	});

	function collapseFromBottom(toggleClass, collapsibleId) {
		$(document).on('click', toggleClass, function (event) {
			event.preventDefault();

			var $collapsible = $('#' + collapsibleId);
			var $topToggle = $collapsible.children('.mw-collapsible-toggle').first();

			if ($topToggle.length) {
				$topToggle.trigger('click');
			}
		});
	}

	collapseFromBottom('.wizlords-spellbook-collapse-arcane', 'wizlords-arcane-spellbook');
	collapseFromBottom('.wizlords-spellbook-collapse-divine', 'wizlords-divine-spellbook');

	$(document).on('click', '.wizlords-collapsible-section-bottom-toggle', function (event) {
		event.preventDefault();

		var $collapsible = $(this).closest('.wizlords-collapsible-section');
		var $topToggle = $collapsible.children('.mw-collapsible-toggle').first();

		if ($topToggle.length) {
			$topToggle.trigger('click');
		}
	});
});

/* Mobile class-card collapse controls */
( function () {
	var mobileQuery = window.matchMedia('(max-width: 599px)');

	function initialiseClassCards() {
		$('.wizlords-class-card').each(function () {
			var $card = $(this);

			if (mobileQuery.matches) {
				if (!$card.attr('data-wizlords-mobile-class-card')) {
					$card
						.attr('data-wizlords-mobile-class-card', '1')
						.attr('role', 'button')
						.attr('tabindex', '0');
				}

				if (!$card.hasClass('is-open')) {
					$card.attr('aria-expanded', 'false');
				}
			} else {
				$card
					.removeAttr('data-wizlords-mobile-class-card')
					.removeAttr('role')
					.removeAttr('tabindex')
					.removeAttr('aria-expanded')
					.removeClass('is-open');
			}
		});
	}

	function toggleClassCard(card) {
		if (!mobileQuery.matches) {
			return;
		}

		var $card = $(card);
		var isOpen = $card.hasClass('is-open');

		$card
			.toggleClass('is-open', !isOpen)
			.attr('aria-expanded', isOpen ? 'false' : 'true');
	}

	$(initialiseClassCards);

	if (mobileQuery.addEventListener) {
		mobileQuery.addEventListener('change', initialiseClassCards);
	} else if (mobileQuery.addListener) {
		mobileQuery.addListener(initialiseClassCards);
	}

	$(document).on('click', '.wizlords-class-card', function (event) {
		if ($(event.target).closest('a').length) {
			return;
		}

		toggleClassCard(this);
	});

	$(document).on('keydown', '.wizlords-class-card', function (event) {
		if (event.key === 'Enter' || event.key === ' ') {
			event.preventDefault();
			toggleClassCard(this);
		}
	});
}() );

/* Mobile/tablet spellbook builder and collapse controls */
( function () {
	var spellbookQuery = window.matchMedia('(max-width: 899px)');
	var hasBuiltMobileSpellbooks = false;

	var spellSummaries = {
		'Arcane Weapon': 'Imbues a one-handed weapon with arcane energy, allowing it to strike with magical force.',
		'Greater Arcane Weapon': 'Empowers a two-handed weapon with potent arcane magic, greatly enhancing its destructive power.',
		'Arcane Armour': 'Wraps an ally in protective arcane energy, increasing their resilience against harm.',
		'Arcane Ward': 'Enchants a shield with magical protection, allowing it to deflect hostile spells.',
		'Arcane Bolt': 'Channels raw arcane energy into a magical projectile that blasts through enemy defences.',
		'Arcane Force': 'Unleashes a burst of telekinetic power that hurls enemies away from the caster.',
		'Nullify': 'Dispels magical effects within the surrounding area, disrupting spells and summoned creatures alike.',
		'Mage Walk': 'Allows the caster to briefly walk across water as though it were solid ground.',
		'Arcane Bridge': 'Creates a temporary bridge of magical energy, allowing allies to safely cross dangerous terrain.',
		'Arcane Bridge Lv1': 'Creates a temporary bridge of magical energy, allowing allies to safely cross dangerous terrain.',
		'Arcane Bridge Lv2': 'Creates a temporary bridge of magical energy, allowing allies to safely cross dangerous terrain.',
		'Arcane Bind': 'Pins an enemy in place with invisible magical force, preventing them from escaping.',
		'Summon Minor Arcane Servant - Defend': 'Summons a lesser arcane spirit that remains close to its master, defending them from nearby threats.',
		'Summon Minor Arcane Servant - Attack': 'Summons a lesser arcane spirit that relentlessly pursues and attacks a chosen enemy.',
		'Summon Arcane Servant - Defend': 'Calls forth a powerful guardian spirit dedicated to protecting its summoner.',
		'Summon Arcane Servant - Attack': 'Summons a powerful arcane warrior that hunts down enemies with unwavering determination.',
		'Summon Greater Arcane Servant': 'Calls forth a sentient arcane servant capable of acting independently on the battlefield.',
		'Blessing': 'Fills an ally with divine favour, strengthening their body and shielding them from hostile magic.',
		'Blessed Weapon': 'Consecrates a one-handed weapon so it strikes with holy power.',
		'Greater Blessed Weapon': 'Infuses a two-handed weapon with divine might, allowing it to smite evil with every blow.',
		'Holy Ward': 'Blesses a shield with sacred protection, turning aside magical attacks.',
		'Sanctuary': 'Creates a holy sanctuary that wards away magical beings and protects the faithful.',
		'Heal': 'Channels divine energy to mend wounds and restore an ally\'s vitality.',
		'Full Heal': 'Completely restores a living ally, closing wounds and renewing their strength.',
		'Mass Heal': 'Calls upon divine power to restore multiple wounded allies at once.',
		'Resurrection': 'Returns a fallen ally to life through the power of divine intervention.',
		'Bind Person': 'Calls upon divine authority to hold a mortal in place, preventing their escape.',
		'Bind Person Lv1': 'Calls upon divine authority to hold a mortal in place, preventing their escape.',
		'Bind Person Lv2': 'Calls upon divine authority to hold a mortal in place, preventing their escape.',
		'Immobilise': 'Overwhelms a target with sacred power, leaving them completely unable to move or fight.',
		'Taunt': 'Compels magical creatures to focus their attention upon the caster through divine command.',
		'Taunt Lv1': 'Compels magical creatures to focus their attention upon the caster through divine command.',
		'Taunt Lv2': 'Compels magical creatures to focus their attention upon the caster through divine command.',
		'Banish': 'Drives magical creatures and summoned beings away with overwhelming divine authority.'
	};

	var mobileSpellTitles = {
		'Summon Arcane Servant - Attack': 'Servant - Attack',
		'Summon Arcane Servant - Defend': 'Servant - Defend',
		'Summon Minor Arcane Servant - Attack': 'Minor Servant - Attack',
		'Summon Minor Arcane Servant - Defend': 'Minor Servant - Defend',
		'Summon Greater Arcane Servant': 'Greater Servant'
	};

	function getSpellIcon(iconType) {
		var fileName = iconType === 'divine' ? 'DivineIcon.png' : 'ArcaneIcon.png';
		var scriptPath = mw.config.get('wgScriptPath') || '';

		return $('<img>')
			.attr('src', scriptPath + '/index.php/Special:FilePath/' + encodeURIComponent(fileName))
			.attr('alt', '')
			.attr('loading', 'lazy')
			.attr('decoding', 'async');
	}

	function getSpellbookIcon(iconType) {
		var fileName = iconType === 'divine' ? 'DivineSpellbookIcon.png' : 'ArcaneSpellbookIcon.png';
		var scriptPath = mw.config.get('wgScriptPath') || '';

		return $('<img>')
			.attr('src', scriptPath + '/index.php/Special:FilePath/' + encodeURIComponent(fileName))
			.attr('alt', '')
			.attr('loading', 'lazy')
			.attr('decoding', 'async');
	}

	function buildMobileSpellCard($sourceCard, iconType) {
		var title = $.trim($sourceCard.children('.wizlords-spell-card-title').first().text());
		var mobileTitle = mobileSpellTitles[title] || title;
		var $card = $('<div>').addClass('wizlords-mobile-spell-card');
		var $icon = $('<div>').addClass('wizlords-mobile-spell-card-icon').append(getSpellIcon(iconType));
		var $title = $('<div>').addClass('wizlords-mobile-spell-card-title').text(mobileTitle);
		var $summary = $('<div>').addClass('wizlords-mobile-spell-card-summary').text(spellSummaries[title] || '');
		var $rules = $('<div>').addClass('wizlords-mobile-spell-card-rules');

		$sourceCard.children().not('.wizlords-spell-card-title').each(function () {
			$rules.append($(this).clone());
		});

		return $card
			.append($icon)
			.append($title)
			.append($summary)
			.append($rules);
	}

	function buildMobileSpellbook(sourceSelector, title, variantClass, iconType) {
		var $source = $(sourceSelector);
		var $content = $source.children('.mw-collapsible-content').first();
		var $book = $('<div>').addClass('wizlords-mobile-spellbook ' + variantClass);
		var $header = $('<div>')
			.addClass('wizlords-mobile-spellbook-header')
			.attr('role', 'button')
			.attr('tabindex', '0')
			.attr('aria-expanded', 'false');
		var $body = $('<div>').addClass('wizlords-mobile-spellbook-body');
		var $currentCategory = null;

		$header
			.append($('<span>').addClass('wizlords-mobile-spellbook-title').text(title))
			.append($('<span>').addClass('wizlords-mobile-spellbook-icon').append(getSpellbookIcon(iconType)));

		$book.append($header).append($body);

		$content.children().each(function () {
			var $node = $(this);

			if ($node.hasClass('wizlords-spell-category-heading')) {
				$currentCategory = $('<div>').addClass('wizlords-mobile-spell-category');
				$currentCategory.append($('<div>').addClass('wizlords-mobile-spell-category-heading').text($.trim($node.text())));
				$body.append($currentCategory);
				return;
			}

			if ($node.hasClass('wizlords-spell-category-description') && $currentCategory) {
				$currentCategory.append($node.clone().removeClass('wizlords-spell-category-description').addClass('wizlords-mobile-spell-category-description'));
				return;
			}

			if ($node.hasClass('wizlords-spell-card-grid')) {
				if (!$currentCategory) {
					$currentCategory = $('<div>').addClass('wizlords-mobile-spell-category');
					$body.append($currentCategory);
				}

				$node.children('.wizlords-spell-card').each(function () {
					$currentCategory.append(buildMobileSpellCard($(this), iconType));
				});
			}
		});

		return $book;
	}

	function buildMobileSpellbooks() {
		var $target = $('.wizlords-spellbooks-mobile');

		if (hasBuiltMobileSpellbooks || !$target.length) {
			return;
		}

		$target
			.empty()
			.append(buildMobileSpellbook('#wizlords-arcane-spellbook', 'Arcane Spellbook', 'wizlords-mobile-spellbook-arcane', 'arcane'))
			.append(buildMobileSpellbook('#wizlords-divine-spellbook', 'Divine Spellbook', 'wizlords-mobile-spellbook-divine', 'divine'));

		hasBuiltMobileSpellbooks = true;
	}

	function initialiseMobileSpellCards() {
		buildMobileSpellbooks();

		$('.wizlords-spellbooks-mobile').attr('aria-hidden', spellbookQuery.matches ? 'false' : 'true');

		$('.wizlords-mobile-spellbook-header').each(function () {
			var $header = $(this);
			var isOpen = $header.closest('.wizlords-mobile-spellbook').hasClass('is-open');

			if (spellbookQuery.matches) {
				$header.attr('aria-expanded', isOpen ? 'true' : 'false');
			} else {
				$header.removeAttr('aria-expanded');
			}
		});

		$('.wizlords-mobile-spell-card').each(function () {
			var $card = $(this);

			if (spellbookQuery.matches) {
				if (!$card.attr('data-wizlords-mobile-spell-card')) {
					$card
						.attr('data-wizlords-mobile-spell-card', '1')
						.attr('role', 'button')
						.attr('tabindex', '0');
				}

				if (!$card.hasClass('is-open')) {
					$card.attr('aria-expanded', 'false');
				}
			} else {
				$card
					.removeAttr('data-wizlords-mobile-spell-card')
					.removeAttr('role')
					.removeAttr('tabindex')
					.removeAttr('aria-expanded')
					.removeClass('is-open');
			}
		});
	}

	function toggleMobileSpellCard(card) {
		if (!spellbookQuery.matches) {
			return;
		}

		var $card = $(card);
		var isOpen = $card.hasClass('is-open');

		$card
			.toggleClass('is-open', !isOpen)
			.attr('aria-expanded', isOpen ? 'false' : 'true');
	}

	function toggleMobileSpellbook(header) {
		if (!spellbookQuery.matches) {
			return;
		}

		var $header = $(header);
		var $book = $header.closest('.wizlords-mobile-spellbook');
		var isOpen = $book.hasClass('is-open');

		$book.toggleClass('is-open', !isOpen);
		$header.attr('aria-expanded', isOpen ? 'false' : 'true');
	}

	$(initialiseMobileSpellCards);

	if (spellbookQuery.addEventListener) {
		spellbookQuery.addEventListener('change', initialiseMobileSpellCards);
	} else if (spellbookQuery.addListener) {
		spellbookQuery.addListener(initialiseMobileSpellCards);
	}

	$(document).on('click', '.wizlords-mobile-spell-card', function (event) {
		if ($(event.target).closest('a').length) {
			return;
		}

		toggleMobileSpellCard(this);
	});

	$(document).on('click', '.wizlords-mobile-spellbook-header', function () {
		toggleMobileSpellbook(this);
	});

	$(document).on('keydown', '.wizlords-mobile-spell-card', function (event) {
		if (event.key === 'Enter' || event.key === ' ') {
			event.preventDefault();
			toggleMobileSpellCard(this);
		}
	});

	$(document).on('keydown', '.wizlords-mobile-spellbook-header', function (event) {
		if (event.key === 'Enter' || event.key === ' ') {
			event.preventDefault();
			toggleMobileSpellbook(this);
		}
	});
}() );