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: Difference between revisions

MediaWiki interface page
No edit summary
No edit summary
 
(15 intermediate revisions by the same user not shown)
Line 1: Line 1:
/* Return to a box header after using its bottom collapse control. */
function wizlordsScrollToCollapsedHeader($box) {
if (!$box.length) { return; }
$box.children('.mw-collapsible-content').promise().done(function () {
window.requestAnimationFrame(function () {
window.requestAnimationFrame(function () {
if (!document.documentElement.contains($box[0])) { return; }
var offset = 16;
document.querySelectorAll('.citizen-header, .vector-sticky-header').forEach(function (header) {
var style = window.getComputedStyle(header);
var rect = header.getBoundingClientRect();
if ((style.position === 'fixed' || style.position === 'sticky') &&
rect.height > 0 && rect.top <= 0 && rect.bottom > 0) {
offset = Math.max(offset, rect.bottom + 16);
}
});
$box.children('[aria-expanded]').addBack('[aria-expanded]').attr('aria-expanded', 'false');
var previousMargin = $box[0].style.scrollMarginTop;
$box[0].style.scrollMarginTop = offset + 'px';
$box[0].scrollIntoView({ block: 'start', inline: 'nearest', behavior: 'instant' });
$box[0].style.scrollMarginTop = previousMargin;
});
});
});
}
/* Bottom collapse controls */
/* Bottom collapse controls */
mw.loader.using('jquery.makeCollapsible').then(function () {
mw.loader.using('jquery.makeCollapsible').then(function () {
Line 77: Line 102:
if ($topToggle.length) {
if ($topToggle.length) {
$topToggle.trigger('click');
$topToggle.trigger('click');
wizlordsScrollToCollapsedHeader($collapsible);
}
}
});
});
Line 84: Line 110:
collapseFromBottom('.wizlords-spellbook-collapse-divine', 'wizlords-divine-spellbook');
collapseFromBottom('.wizlords-spellbook-collapse-divine', 'wizlords-divine-spellbook');


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


Line 92: Line 118:
if ($topToggle.length) {
if ($topToggle.length) {
$topToggle.trigger('click');
$topToggle.trigger('click');
wizlordsScrollToCollapsedHeader($collapsible);
}
}
});
});
Line 137: Line 164:
.toggleClass('is-open', !isOpen)
.toggleClass('is-open', !isOpen)
.attr('aria-expanded', isOpen ? 'false' : 'true');
.attr('aria-expanded', isOpen ? 'false' : 'true');
if (isOpen) { wizlordsScrollToCollapsedHeader($card); }
}
}


Line 163: Line 191:
}() );
}() );


/* Mobile/tablet class category heading enhancements */
/* Mobile/tablet class category accordions */
( function () {
( function () {
var classCategoryHeadingQuery = window.matchMedia('(max-width: 899px)');
var classCategoryQuery = window.matchMedia('(max-width: 899px)');
var classCategoryHeadings = [
var classCategoryIcons = [
{
{
title: 'Martial Classes',
selector: '.wizlords-class-category-accordion-martial',
variant: 'martial',
fileName: 'MartialIcon.png'
icon: 'MartialIcon.png'
},
},
{
{
title: 'Arcane Classes',
selector: '.wizlords-class-category-accordion-arcane',
variant: 'arcane',
fileName: 'ArcaneIcon.png'
icon: 'ArcaneIcon.png'
},
},
{
{
title: 'Divine Classes',
selector: '.wizlords-class-category-accordion-divine',
variant: 'divine',
fileName: 'DivineIcon.png'
icon: 'DivineIcon.png'
}
}
];
];


function getClassCategoryIcon(fileName) {
function getClassCategoryAccordionIcon(fileName) {
var scriptPath = mw.config.get('wgScriptPath') || '';
var scriptPath = mw.config.get('wgScriptPath') || '';


Line 194: Line 219:
}
}


function getHeadingLabel($heading) {
function initialiseClassCategoryAccordionIcons() {
var $clone = $heading.clone();
classCategoryIcons.forEach(function (category) {
$(category.selector).each(function () {
var $icon = $(this).find('.wizlords-class-category-accordion-icon').first();
 
if ($icon.length && !$icon.children('img').length) {
$icon.empty().append(getClassCategoryAccordionIcon(category.fileName));
}
});
});
}
 
function initialiseClassCategoryAccordions() {
initialiseClassCategoryAccordionIcons();
 
$('.wizlords-class-category-accordion').each(function () {
var $accordion = $(this);
var $heading = $accordion.children('.wizlords-class-category-accordion-heading').first();
var isMobileCitizen = classCategoryQuery.matches && $('body').hasClass('skin-citizen');
var isOpen = $accordion.hasClass('is-open');
 
if (isMobileCitizen) {
$heading
.attr('role', 'button')
.attr('tabindex', '0')
.attr('aria-expanded', isOpen ? 'true' : 'false');
} else {
$accordion.removeClass('is-open');
$heading
.removeAttr('role')
.removeAttr('tabindex')
.removeAttr('aria-expanded');
}
});
}
 
function toggleClassCategoryAccordion(heading) {
if (!classCategoryQuery.matches || !$('body').hasClass('skin-citizen')) {
return;
}
 
var $heading = $(heading);
var $accordion = $heading.closest('.wizlords-class-category-accordion');
var isOpen = $accordion.hasClass('is-open');
 
$accordion.toggleClass('is-open', !isOpen);
$heading.attr('aria-expanded', isOpen ? 'false' : 'true');
}


$clone
$(initialiseClassCategoryAccordions);
.find('.mw-editsection, .indicator, .wizlords-mobile-class-category-heading-icon')
.remove();


return $.trim($clone.text()).replace(/\s+/g, ' ');
if (classCategoryQuery.addEventListener) {
classCategoryQuery.addEventListener('change', initialiseClassCategoryAccordions);
} else if (classCategoryQuery.addListener) {
classCategoryQuery.addListener(initialiseClassCategoryAccordions);
}
}


function enhanceClassCategoryHeadings() {
$(document).on('click', '.wizlords-class-category-accordion-heading', function () {
classCategoryHeadings.forEach(function (category) {
toggleClassCategoryAccordion(this);
$('h2').filter(function () {
});
return getHeadingLabel($(this)) === category.title;
}).each(function () {
var $heading = $(this);


if (classCategoryHeadingQuery.matches && $('body').hasClass('skin-citizen')) {
$(document).on('keydown', '.wizlords-class-category-accordion-heading', function (event) {
$heading
if (event.key === 'Enter' || event.key === ' ') {
.addClass('wizlords-mobile-class-category-heading')
event.preventDefault();
.addClass('wizlords-mobile-class-category-heading-' + category.variant);
toggleClassCategoryAccordion(this);
}
});
}() );
 
/* Mobile/tablet race culture cards */
( function () {
var cultureCardQuery = window.matchMedia('(max-width: 899px)');
 
function isMobileCitizenCultureLayout() {
return cultureCardQuery.matches && $('body').hasClass('skin-citizen');
}


if (!$heading.children('.wizlords-mobile-class-category-heading-icon').length) {
function initialiseCultureCards() {
var $icon = $('<span>')
var isActive = isMobileCitizenCultureLayout();
.addClass('wizlords-mobile-class-category-heading-icon')
.append(getClassCategoryIcon(category.icon));
var $editSection = $heading.children('.mw-editsection').first();


if ($editSection.length) {
$('.wizlords-culture-card').each(function () {
$icon.insertBefore($editSection);
var $card = $(this);
} else {
var isOpen = $card.hasClass('is-open');
$heading.append($icon);
 
}
if (isActive) {
}
$card
} else {
.attr('data-wizlords-mobile-culture-card', '1')
$heading
.attr('role', 'button')
.removeClass('wizlords-mobile-class-category-heading')
.attr('tabindex', '0')
.removeClass('wizlords-mobile-class-category-heading-' + category.variant)
.attr('aria-expanded', isOpen ? 'true' : 'false');
.children('.wizlords-mobile-class-category-heading-icon')
} else {
.remove();
$card
}
.removeClass('is-open')
});
.removeAttr('data-wizlords-mobile-culture-card')
.removeAttr('role')
.removeAttr('tabindex')
.removeAttr('aria-expanded');
}
});
});
}
}


$(enhanceClassCategoryHeadings);
function toggleCultureCard(card) {
if (!isMobileCitizenCultureLayout()) {
return;
}
 
var $card = $(card);
var isOpen = $card.hasClass('is-open');
 
$card
.toggleClass('is-open', !isOpen)
.attr('aria-expanded', isOpen ? 'false' : 'true');
}
 
$(initialiseCultureCards);
 
if (cultureCardQuery.addEventListener) {
cultureCardQuery.addEventListener('change', initialiseCultureCards);
} else if (cultureCardQuery.addListener) {
cultureCardQuery.addListener(initialiseCultureCards);
}
 
$(document).on('click', '.wizlords-culture-card', function (event) {
var isCultureHeaderLink = $(event.target).closest('.wizlords-culture-card-header a, .wizlords-culture-card-title a').length;
 
if (isMobileCitizenCultureLayout() && isCultureHeaderLink) {
event.preventDefault();
event.stopPropagation();
toggleCultureCard(this);
return;
}
 
if ($(event.target).closest('a').length) {
return;
}
 
toggleCultureCard(this);
});
 
$(document).on('keydown', '.wizlords-culture-card', function (event) {
if (event.key === 'Enter' || event.key === ' ') {
event.preventDefault();
toggleCultureCard(this);
}
});
}() );


if (classCategoryHeadingQuery.addEventListener) {
/* Responsive race Available Classes lists */
classCategoryHeadingQuery.addEventListener('change', enhanceClassCategoryHeadings);
( function () {
} else if (classCategoryHeadingQuery.addListener) {
function initialiseRaceAvailableClassLists() {
classCategoryHeadingQuery.addListener(enhanceClassCategoryHeadings);
$('.wizlords-culture-classes + ul, .wizlords-shared-classes ul').each(function () {
var $list = $(this);
var itemCount = $list.children('li').length;
var $sharedClasses = $list.closest('.wizlords-shared-classes');
var isDrakelingList = $sharedClasses
.children('.wizlords-shared-classes-title')
.first()
.text()
.indexOf('Drakelings') !== -1;
 
$list
.removeClass(function (index, className) {
return (className.match(/(^|\s)count-\d+/g) || []).join(' ');
})
.addClass('wizlords-available-classes-list count-' + itemCount)
.toggleClass('is-multicolumn', itemCount > 4)
.toggleClass('is-short', itemCount <= 4)
.toggleClass('is-drakeling-list', isDrakelingList);
 
$list
.closest('.wizlords-culture-card-body, .wizlords-shared-classes')
.addClass('wizlords-available-classes');
});
}
}
$(initialiseRaceAvailableClassLists);
}() );
}() );


Line 550: Line 699:
.toggleClass('is-open', !isOpen)
.toggleClass('is-open', !isOpen)
.attr('aria-expanded', isOpen ? 'false' : 'true');
.attr('aria-expanded', isOpen ? 'false' : 'true');
if (isOpen) { wizlordsScrollToCollapsedHeader($card); }
}
}


Line 609: Line 759:


$(document).on('click', '.wizlords-mobile-spellbook-collapse', function () {
$(document).on('click', '.wizlords-mobile-spellbook-collapse', function () {
toggleMobileSpellbook($(this).closest('.wizlords-mobile-spellbook').children('.wizlords-mobile-spellbook-header').first());
var $book = $(this).closest('.wizlords-mobile-spellbook');
toggleMobileSpellbook($book.children('.wizlords-mobile-spellbook-header').first());
wizlordsScrollToCollapsedHeader($book);
});
});


Line 640: Line 792:
});
});
}() );
}() );
/* Mobile lore profiles reuse MediaWiki's desktop collapse state. */
mw.loader.using('jquery.makeCollapsible').then(function () {
var query = window.matchMedia('(max-width: 599px)');
function prepareLoreProfiles() {
$('.wizlords-lore-profile > .wizlords-collapsible-section-title').each(function () {
var title = $(this);
if (query.matches) {
title.attr({ role: 'button', tabindex: '0',
'aria-expanded': title.parent().hasClass('mw-collapsed') ? 'false' : 'true' });
} else {
title.removeAttr('role tabindex aria-expanded');
}
});
}
$(prepareLoreProfiles);
if (query.addEventListener) { query.addEventListener('change', prepareLoreProfiles); }
$(document).on('click', '.wizlords-lore-profile > .wizlords-collapsible-section-title, .wizlords-lore-profile > .wizlords-lore-profile-mobile-icon', function () {
if (!query.matches) { return; }
var $profile = $(this).parent();
var wasOpen = !$profile.hasClass('mw-collapsed');
$profile.children('.mw-collapsible-toggle').first().trigger('click');
prepareLoreProfiles();
if (wasOpen) { wizlordsScrollToCollapsedHeader($profile); }
});
$(document).on('keydown', '.wizlords-lore-profile > .wizlords-collapsible-section-title, .wizlords-lore-profile > .wizlords-lore-profile-mobile-icon', function (event) {
if (query.matches && (event.key === 'Enter' || event.key === ' ')) {
event.preventDefault();
$(this).trigger('click');
}
});
});
$(document).on('keydown', '.wizlords-lore-collapse', function (event) {
if (event.key === 'Enter' || event.key === ' ') {
event.preventDefault();
$(this).trigger('click');
}
});