2024-05-27 19:46:49 +00:00
|
|
|
const config = require( './config.json' );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class representing an OverflowElement.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
*/
|
|
|
|
class OverflowElement {
|
2024-06-11 22:00:45 +00:00
|
|
|
constructor( element, isPointerDevice ) {
|
2024-05-27 19:46:49 +00:00
|
|
|
this.element = element;
|
2024-06-11 22:00:45 +00:00
|
|
|
this.isPointerDevice = isPointerDevice;
|
2024-05-27 19:46:49 +00:00
|
|
|
this.elementWidth = 0;
|
2024-06-11 22:00:45 +00:00
|
|
|
this.contentScrollLeft = 0;
|
|
|
|
this.contentWidth = 0;
|
2024-05-27 21:04:28 +00:00
|
|
|
this.onScroll = mw.util.throttle( this.onScroll.bind( this ), 250 );
|
2024-05-27 19:46:49 +00:00
|
|
|
this.updateState = this.updateState.bind( this );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Toggles classes on the wrapper element based on the provided conditions.
|
|
|
|
*
|
|
|
|
* @param {Array} classes - An array of conditions and class names to toggle.
|
|
|
|
* Each element in the array should be a tuple where the first element is a boolean condition
|
|
|
|
* and the second element is the class name to toggle.
|
|
|
|
*
|
|
|
|
* @return {void}
|
|
|
|
*/
|
|
|
|
toggleClasses( classes ) {
|
|
|
|
classes.forEach( ( [ condition, className ] ) => {
|
|
|
|
const hasClass = this.wrapper.classList.contains( className );
|
|
|
|
if ( condition && !hasClass ) {
|
|
|
|
this.wrapper.classList.add( className );
|
|
|
|
} else if ( !condition && hasClass ) {
|
|
|
|
this.wrapper.classList.remove( className );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2024-05-27 21:04:28 +00:00
|
|
|
/**
|
2024-06-11 22:00:45 +00:00
|
|
|
* Checks if the state of the overflow element has changed by comparing the current element width, content scroll left,
|
|
|
|
* and content width with the cached values. Returns true if any of the values have changed, otherwise returns false.
|
2024-05-27 21:04:28 +00:00
|
|
|
*
|
|
|
|
* @return {boolean} - True if the state has changed, false otherwise.
|
|
|
|
*/
|
|
|
|
hasStateChanged() {
|
|
|
|
return (
|
|
|
|
this.element.scrollWidth !== this.elementWidth ||
|
2024-06-11 22:00:45 +00:00
|
|
|
Math.round( this.content.scrollLeft ) !== this.contentScrollLeft ||
|
|
|
|
this.content.offsetWidth !== this.contentWidth
|
2024-05-27 21:04:28 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-06-11 22:00:45 +00:00
|
|
|
* Checks if the element has overflowed horizontally by comparing the element width with the content width.
|
2024-05-27 21:04:28 +00:00
|
|
|
*
|
|
|
|
* @return {boolean} - True if the element has overflowed, false otherwise.
|
|
|
|
*/
|
|
|
|
hasOverflowed() {
|
2024-06-11 22:00:45 +00:00
|
|
|
return this.elementWidth > this.contentWidth;
|
2024-05-27 21:04:28 +00:00
|
|
|
}
|
|
|
|
|
2024-05-27 19:46:49 +00:00
|
|
|
/**
|
2024-06-11 22:00:45 +00:00
|
|
|
* Updates the state of the overflow element by calculating the element width, content scroll left, and content width.
|
2024-05-27 19:46:49 +00:00
|
|
|
* If the width values are invalid, logs an error and returns.
|
|
|
|
* Compares the current state with the previous state and updates the cache if there is a change.
|
2024-06-11 22:00:45 +00:00
|
|
|
* Toggles classes on the content element based on the overflow state (left or right).
|
2024-05-27 19:46:49 +00:00
|
|
|
*
|
|
|
|
* @return {void}
|
|
|
|
*/
|
|
|
|
updateState() {
|
|
|
|
const elementWidth = this.element.scrollWidth;
|
2024-06-11 22:00:45 +00:00
|
|
|
const contentScrollLeft = Math.round( this.content.scrollLeft );
|
|
|
|
const contentWidth = this.content.offsetWidth;
|
2024-05-27 19:46:49 +00:00
|
|
|
|
2024-05-27 21:04:28 +00:00
|
|
|
if ( !this.hasStateChanged() ) {
|
2024-05-27 19:46:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.elementWidth = elementWidth;
|
2024-06-11 22:00:45 +00:00
|
|
|
this.contentScrollLeft = contentScrollLeft;
|
|
|
|
this.contentWidth = contentWidth;
|
2024-05-27 19:46:49 +00:00
|
|
|
|
2024-05-27 21:58:04 +00:00
|
|
|
let isLeftOverflowing, isRightOverflowing;
|
|
|
|
|
2024-05-27 21:04:28 +00:00
|
|
|
if ( !this.hasOverflowed() ) {
|
2024-05-27 21:58:04 +00:00
|
|
|
isLeftOverflowing = false;
|
|
|
|
isRightOverflowing = false;
|
|
|
|
} else {
|
2024-06-11 22:00:45 +00:00
|
|
|
isLeftOverflowing = this.contentScrollLeft > 0;
|
|
|
|
isRightOverflowing =
|
|
|
|
this.contentScrollLeft + this.contentWidth < this.elementWidth;
|
2024-05-27 21:04:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
window.requestAnimationFrame( () => {
|
|
|
|
const updateClasses = [
|
|
|
|
[ isLeftOverflowing, 'citizen-overflow--left' ],
|
|
|
|
[ isRightOverflowing, 'citizen-overflow--right' ]
|
|
|
|
];
|
|
|
|
this.toggleClasses( updateClasses );
|
|
|
|
} );
|
2024-05-27 19:46:49 +00:00
|
|
|
}
|
|
|
|
|
2024-05-27 21:10:04 +00:00
|
|
|
/**
|
|
|
|
* Filters and adds inherited classes to the wrapper element.
|
|
|
|
*
|
|
|
|
* @return {void}
|
|
|
|
*/
|
2024-05-27 21:16:23 +00:00
|
|
|
handleInheritedClasses() {
|
|
|
|
const inheritedClasses = config.wgCitizenOverflowInheritedClasses;
|
2024-06-11 22:00:45 +00:00
|
|
|
const filteredClasses = inheritedClasses.filter( ( cls ) => this.element.classList.contains( cls )
|
|
|
|
);
|
2024-05-27 21:10:04 +00:00
|
|
|
|
|
|
|
filteredClasses.forEach( ( cls ) => {
|
|
|
|
if ( !this.wrapper.classList.contains( cls ) ) {
|
|
|
|
this.wrapper.classList.add( cls );
|
|
|
|
}
|
|
|
|
if ( this.element.classList.contains( cls ) ) {
|
|
|
|
this.element.classList.remove( cls );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2024-05-27 19:46:49 +00:00
|
|
|
/**
|
2024-05-27 20:11:55 +00:00
|
|
|
* Wraps the element in a div container with the class 'citizen-overflow-wrapper'.
|
2024-05-27 21:10:04 +00:00
|
|
|
* Checks if the element or its parent node is null or undefined, and logs an error if so.
|
|
|
|
* Verifies the existence of the necessary configuration classes for wrapping and logs an error if missing.
|
|
|
|
* Creates a new div wrapper element, adds the class 'citizen-overflow-wrapper', and appends it to the parent node before the element.
|
|
|
|
* Moves the element inside the wrapper.
|
|
|
|
* Handles inherited classes such as 'floatleft' and 'floatright' by adding them to the wrapper and removing them from the element.
|
|
|
|
* Logs any errors that occur during the wrapping process.
|
2024-05-27 19:46:49 +00:00
|
|
|
*
|
|
|
|
* @return {void}
|
|
|
|
*/
|
|
|
|
wrap() {
|
|
|
|
if ( !this.element || !this.element.parentNode ) {
|
2024-06-11 22:00:45 +00:00
|
|
|
mw.log.error(
|
|
|
|
'[Citizen] Element or element.parentNode is null or undefined. Please check if the element or element.parentNode is null or undefined.'
|
|
|
|
);
|
2024-05-27 19:46:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
2024-06-11 22:00:45 +00:00
|
|
|
const fragment = document.createDocumentFragment();
|
|
|
|
|
2024-05-27 19:46:49 +00:00
|
|
|
const wrapper = document.createElement( 'div' );
|
2024-05-27 20:11:55 +00:00
|
|
|
wrapper.className = 'citizen-overflow-wrapper';
|
2024-05-27 21:16:23 +00:00
|
|
|
this.handleInheritedClasses();
|
2024-06-11 22:00:45 +00:00
|
|
|
fragment.appendChild( wrapper );
|
|
|
|
|
|
|
|
if ( this.isPointerDevice ) {
|
2024-06-11 23:07:38 +00:00
|
|
|
const elementStyles = window.getComputedStyle( this.element );
|
|
|
|
const topMargin = parseInt( elementStyles.marginTop );
|
|
|
|
const bottomMargin = parseInt( elementStyles.marginBottom );
|
2024-06-11 23:15:42 +00:00
|
|
|
const createButton = ( type ) => {
|
|
|
|
const button = document.createElement( 'button' );
|
|
|
|
button.className = `citizen-overflow-navButton citizen-overflow-navButton-${ type } citizen-ui-icon mw-ui-icon-wikimedia-collapse`;
|
|
|
|
button.setAttribute( 'aria-hidden', 'true' );
|
|
|
|
button.setAttribute( 'tabindex', '-1' );
|
|
|
|
return button;
|
|
|
|
};
|
2024-06-11 23:07:38 +00:00
|
|
|
|
2024-06-11 22:00:45 +00:00
|
|
|
const nav = document.createElement( 'div' );
|
|
|
|
nav.className = 'citizen-overflow-nav';
|
2024-06-11 23:07:38 +00:00
|
|
|
nav.style.marginTop = `${ topMargin }px`;
|
|
|
|
nav.style.marginBottom = `${ bottomMargin }px`;
|
2024-06-11 22:00:45 +00:00
|
|
|
|
2024-06-11 23:15:42 +00:00
|
|
|
nav.appendChild( createButton( 'left' ) );
|
|
|
|
nav.appendChild( createButton( 'right' ) );
|
2024-06-11 22:00:45 +00:00
|
|
|
|
|
|
|
wrapper.appendChild( nav );
|
|
|
|
this.nav = nav;
|
|
|
|
}
|
2024-06-12 16:51:50 +00:00
|
|
|
|
|
|
|
const content = document.createElement( 'div' );
|
|
|
|
content.className = 'citizen-overflow-content';
|
|
|
|
wrapper.appendChild( content );
|
|
|
|
|
|
|
|
const parentNode = this.element.parentNode;
|
|
|
|
parentNode.insertBefore( fragment, this.element );
|
|
|
|
content.appendChild( this.element );
|
|
|
|
|
|
|
|
this.wrapper = wrapper;
|
|
|
|
this.content = content;
|
2024-05-27 19:46:49 +00:00
|
|
|
} catch ( error ) {
|
2024-06-11 22:00:45 +00:00
|
|
|
mw.log.error(
|
|
|
|
`[Citizen] Error occurred while wrapping element: ${ error.message }`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scrolls the content element by the specified offset.
|
|
|
|
*
|
|
|
|
* @param {number} offset - The amount by which to scroll the content element.
|
|
|
|
* @return {void}
|
|
|
|
*/
|
|
|
|
scrollContent( offset ) {
|
|
|
|
const delta = this.content.scrollWidth - this.content.offsetWidth;
|
2024-06-11 22:56:54 +00:00
|
|
|
const scrollLeft = Math.floor( this.content.scrollLeft ) + offset;
|
2024-06-11 22:00:45 +00:00
|
|
|
|
|
|
|
window.requestAnimationFrame( () => {
|
|
|
|
this.content.scrollLeft = Math.min( Math.max( scrollLeft, 0 ), delta );
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles the click event on the navigation buttons.
|
|
|
|
* Scrolls the content element left or right based on the button clicked.
|
|
|
|
*
|
|
|
|
* @param {Event} event - The click event object.
|
|
|
|
* @return {void}
|
|
|
|
*/
|
|
|
|
onClick( event ) {
|
|
|
|
const target = event.target;
|
|
|
|
if ( !target.classList.contains( 'citizen-overflow-navButton' ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const offset = this.wrapper.offsetWidth / 2;
|
|
|
|
if ( target.classList.contains( 'citizen-overflow-navButton-left' ) ) {
|
|
|
|
this.scrollContent( -offset );
|
|
|
|
} else if (
|
|
|
|
target.classList.contains( 'citizen-overflow-navButton-right' )
|
|
|
|
) {
|
|
|
|
this.scrollContent( offset );
|
2024-05-27 19:46:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles the scroll event by requesting an animation frame to update the state of the overflow element.
|
|
|
|
*
|
|
|
|
* @return {void}
|
|
|
|
*/
|
|
|
|
onScroll() {
|
2024-05-27 21:04:28 +00:00
|
|
|
this.updateState();
|
2024-05-27 19:46:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resumes the functionality of the overflow element by updating its state, adding a scroll event listener, and observing element resize.
|
|
|
|
* Calls the 'updateState' method to update the state of the overflow element.
|
2024-06-11 22:00:45 +00:00
|
|
|
* Adds a scroll event listener to the content element to handle scroll events by calling the 'onScroll' method.
|
2024-05-27 19:46:49 +00:00
|
|
|
* Observes the element for resize changes using the 'resizeObserver'.
|
|
|
|
*
|
|
|
|
* @return {void}
|
|
|
|
*/
|
|
|
|
resume() {
|
|
|
|
this.updateState();
|
2024-06-11 22:00:45 +00:00
|
|
|
this.content.addEventListener( 'scroll', this.onScroll );
|
2024-05-27 19:46:49 +00:00
|
|
|
this.resizeObserver.observe( this.element );
|
2024-06-11 22:00:45 +00:00
|
|
|
if ( this.isPointerDevice ) {
|
|
|
|
this.nav.addEventListener( 'click', this.onClick.bind( this ) );
|
|
|
|
}
|
2024-05-27 19:46:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pauses the functionality of the overflow element by removing the scroll event listener and stopping observation of element resize.
|
2024-06-11 22:00:45 +00:00
|
|
|
* Removes the scroll event listener from the content element that triggers the 'onScroll' method.
|
2024-05-27 19:46:49 +00:00
|
|
|
* Stops observing resize changes of the element using the 'resizeObserver'.
|
|
|
|
*
|
|
|
|
* @return {void}
|
|
|
|
*/
|
|
|
|
pause() {
|
2024-06-11 22:00:45 +00:00
|
|
|
this.content.removeEventListener( 'scroll', this.onScroll );
|
2024-05-27 19:46:49 +00:00
|
|
|
this.resizeObserver.unobserve( this.element );
|
2024-06-11 22:00:45 +00:00
|
|
|
if ( this.isPointerDevice ) {
|
|
|
|
this.nav.removeEventListener( 'click', this.onClick );
|
|
|
|
}
|
2024-05-27 19:46:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets up an IntersectionObserver to handle intersection changes for the overflow element.
|
|
|
|
* When the element intersects with the viewport, resumes the functionality by calling the 'resume' method.
|
|
|
|
* When the element is not intersecting with the viewport, pauses the functionality by calling the 'pause' method.
|
|
|
|
* Observes the intersection changes for the element using the IntersectionObserver.
|
|
|
|
*
|
|
|
|
* @return {void}
|
|
|
|
*/
|
|
|
|
setupIntersectionObserver() {
|
|
|
|
// eslint-disable-next-line compat/compat
|
|
|
|
this.intersectionObserver = new IntersectionObserver( ( entries ) => {
|
|
|
|
entries.forEach( ( entry ) => {
|
|
|
|
if ( entry.isIntersecting ) {
|
|
|
|
this.resume();
|
|
|
|
} else {
|
|
|
|
this.pause();
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
this.intersectionObserver.observe( this.element );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets up a ResizeObserver to monitor changes in the size of the element and triggers the 'updateState' method accordingly.
|
|
|
|
*
|
|
|
|
* @return {void}
|
|
|
|
*/
|
|
|
|
setupResizeObserver() {
|
|
|
|
// eslint-disable-next-line compat/compat
|
|
|
|
this.resizeObserver = new ResizeObserver( this.updateState );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes the OverflowElement by wrapping the element, setting up a ResizeObserver to monitor size changes,
|
|
|
|
* setting up an IntersectionObserver to handle intersection changes, and resuming the functionality of the overflow element.
|
|
|
|
*
|
|
|
|
* @return {void}
|
|
|
|
*/
|
|
|
|
init() {
|
|
|
|
this.wrap();
|
|
|
|
this.setupResizeObserver();
|
|
|
|
this.setupIntersectionObserver();
|
|
|
|
this.resume();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes the process of wrapping overflow elements within the given body content.
|
|
|
|
*
|
2024-05-27 20:11:55 +00:00
|
|
|
* @param {HTMLElement} bodyContent - The body content element containing elements to be wrapped.
|
2024-05-27 19:46:49 +00:00
|
|
|
* @return {void}
|
|
|
|
*/
|
|
|
|
function init( bodyContent ) {
|
2024-05-29 18:02:57 +00:00
|
|
|
const nowrapClasses = config.wgCitizenOverflowNowrapClasses;
|
|
|
|
if ( !nowrapClasses || !Array.isArray( nowrapClasses ) ) {
|
2024-06-11 22:00:45 +00:00
|
|
|
mw.log.error(
|
|
|
|
'[Citizen] Invalid or missing $wgCitizenOverflowNowrapClasses. Cannot proceed with wrapping element.'
|
|
|
|
);
|
2024-05-29 18:02:57 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-06-11 22:00:45 +00:00
|
|
|
const overflowElements = bodyContent.querySelectorAll(
|
|
|
|
'.citizen-overflow, .wikitable:not( .wikitable .wikitable )'
|
|
|
|
);
|
2024-05-29 18:02:57 +00:00
|
|
|
if ( !overflowElements.length ) {
|
|
|
|
return;
|
2024-05-27 19:46:49 +00:00
|
|
|
}
|
2024-05-29 18:02:57 +00:00
|
|
|
|
2024-06-11 22:00:45 +00:00
|
|
|
const isPointerDevice = window.matchMedia( '(hover: hover) and (pointer: fine)' ).matches;
|
|
|
|
|
2024-05-29 18:02:57 +00:00
|
|
|
overflowElements.forEach( ( el ) => {
|
|
|
|
if ( nowrapClasses.some( ( cls ) => el.classList.contains( cls ) ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-06-11 22:00:45 +00:00
|
|
|
const overflowElement = new OverflowElement( el, isPointerDevice );
|
2024-05-29 18:02:57 +00:00
|
|
|
overflowElement.init();
|
|
|
|
} );
|
2024-05-27 19:46:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
init: init
|
|
|
|
};
|