Difference between revisions of "MediaWiki:Common.js"
| Line 1: | Line 1: | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
(function () { | (function () { | ||
var iconSelector = | var iconSelector = | ||
| Line 39: | Line 11: | ||
var iconLeft = icon.getBoundingClientRect().left; | var iconLeft = icon.getBoundingClientRect().left; | ||
var availableWidth = viewportWidth - iconLeft - 16; | var availableWidth = viewportWidth - iconLeft - 16; | ||
| + | var popupWidth = Math.min(viewportWidth * 0.7, availableWidth); | ||
icon.style.setProperty( | icon.style.setProperty( | ||
'--popup-width', | '--popup-width', | ||
| − | Math.max(0, | + | Math.max(0, popupWidth) + 'px' |
); | ); | ||
} | } | ||
function arrangePopups() { | function arrangePopups() { | ||
| − | var openIcons = Array. | + | var openIcons = Array.prototype.filter.call( |
| − | document.querySelectorAll(iconSelector) | + | document.querySelectorAll(iconSelector), |
| − | + | function (icon) { | |
| − | + | return icon.classList.contains('open'); | |
| − | + | } | |
| + | ); | ||
openIcons.forEach(function (icon) { | openIcons.forEach(function (icon) { | ||
| Line 67: | Line 41: | ||
} | } | ||
| + | var panelRect = panel.getBoundingClientRect(); | ||
var offset = 0; | var offset = 0; | ||
| − | |||
placedPanels.forEach(function (previousRect) { | placedPanels.forEach(function (previousRect) { | ||
| Line 87: | Line 61: | ||
}); | }); | ||
| − | icon.style.setProperty( | + | icon.style.setProperty('--popup-offset', offset + 'px'); |
| − | |||
| − | |||
| − | |||
placedPanels.push(panel.getBoundingClientRect()); | placedPanels.push(panel.getBoundingClientRect()); | ||
| Line 111: | Line 82: | ||
icon.classList.toggle('open'); | icon.classList.toggle('open'); | ||
| − | requestAnimationFrame(arrangePopups); | + | window.requestAnimationFrame(arrangePopups); |
}); | }); | ||
window.addEventListener('resize', function () { | window.addEventListener('resize', function () { | ||
| − | requestAnimationFrame(arrangePopups); | + | window.requestAnimationFrame(arrangePopups); |
}); | }); | ||
})(); | })(); | ||
Revision as of 17:09, 24 July 2026
(function () {
var iconSelector =
'.meter, .parallel, .commentary, .pictures, .outline, .comment, .word';
var panelSelector =
'.metertext, .paralleltext, .commentarytext, ' +
'.picturestext, .outlinetext, .commenttext, .wordtext';
function updateWidth(icon) {
var viewportWidth = document.documentElement.clientWidth;
var iconLeft = icon.getBoundingClientRect().left;
var availableWidth = viewportWidth - iconLeft - 16;
var popupWidth = Math.min(viewportWidth * 0.7, availableWidth);
icon.style.setProperty(
'--popup-width',
Math.max(0, popupWidth) + 'px'
);
}
function arrangePopups() {
var openIcons = Array.prototype.filter.call(
document.querySelectorAll(iconSelector),
function (icon) {
return icon.classList.contains('open');
}
);
openIcons.forEach(function (icon) {
icon.style.setProperty('--popup-offset', '0px');
updateWidth(icon);
});
var placedPanels = [];
openIcons.forEach(function (icon) {
var panel = icon.querySelector(panelSelector);
if (!panel) {
return;
}
var panelRect = panel.getBoundingClientRect();
var offset = 0;
placedPanels.forEach(function (previousRect) {
var overlapsHorizontally =
panelRect.left < previousRect.right &&
panelRect.right > previousRect.left;
var overlapsVertically =
panelRect.top < previousRect.bottom &&
panelRect.bottom > previousRect.top;
if (overlapsHorizontally && overlapsVertically) {
offset = Math.max(
offset,
previousRect.bottom - panelRect.top + 8
);
}
});
icon.style.setProperty('--popup-offset', offset + 'px');
placedPanels.push(panel.getBoundingClientRect());
});
}
document.addEventListener('click', function (event) {
var icon = event.target.closest(iconSelector);
if (!icon) {
return;
}
var panel = icon.querySelector(panelSelector);
if (panel && panel.contains(event.target)) {
return;
}
icon.classList.toggle('open');
window.requestAnimationFrame(arrangePopups);
});
window.addEventListener('resize', function () {
window.requestAnimationFrame(arrangePopups);
});
})();