2022-12-07 22:10:09 +00:00
|
|
|
const features = require( './features.js' );
|
2023-04-03 21:16:52 +00:00
|
|
|
const popupNotification = require( './popupNotification.js' );
|
2022-12-07 22:10:09 +00:00
|
|
|
const LIMITED_WIDTH_FEATURE_NAME = 'limited-width';
|
2023-04-03 21:16:52 +00:00
|
|
|
const TOGGLE_ID = 'toggleWidth';
|
2022-10-21 18:14:54 +00:00
|
|
|
|
2022-11-17 21:51:11 +00:00
|
|
|
/**
|
|
|
|
* Sets data attribute for click tracking purposes.
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} toggleBtn
|
|
|
|
*/
|
|
|
|
function setDataAttribute( toggleBtn ) {
|
|
|
|
toggleBtn.dataset.eventName = features.isEnabled( LIMITED_WIDTH_FEATURE_NAME ) ?
|
|
|
|
'limited-width-toggle-off' : 'limited-width-toggle-on';
|
|
|
|
}
|
2023-04-12 20:36:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets appropriate popup text based off the limited width feature flag
|
|
|
|
*
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
function getPopupText() {
|
|
|
|
const label = features.isEnabled( LIMITED_WIDTH_FEATURE_NAME ) ?
|
|
|
|
'vector-limited-width-toggle-off-popup' : 'vector-limited-width-toggle-on-popup';
|
|
|
|
// possible messages:
|
|
|
|
// * vector-limited-width-toggle-off-popup
|
|
|
|
// * vector-limited-width-toggle-on-popup
|
|
|
|
return mw.msg( label );
|
|
|
|
}
|
|
|
|
|
2022-10-21 18:14:54 +00:00
|
|
|
/**
|
|
|
|
* adds a toggle button
|
|
|
|
*/
|
|
|
|
function init() {
|
2023-05-02 18:31:34 +00:00
|
|
|
const settings = /** @type {HTMLElement|null} */ ( document.querySelector( '.vector-settings' ) );
|
|
|
|
const toggle = /** @type {HTMLElement|null} */ ( document.querySelector( '.vector-limited-width-toggle' ) );
|
2023-06-12 22:22:33 +00:00
|
|
|
const toggleIcon = /** @type {HTMLElement|null} */ ( document.querySelector( '.vector-limited-width-toggle .vector-icon' ) );
|
2023-05-02 18:31:34 +00:00
|
|
|
|
|
|
|
if ( !settings || !toggle || !toggleIcon ) {
|
|
|
|
return;
|
|
|
|
}
|
2023-04-12 20:36:55 +00:00
|
|
|
|
2022-11-17 21:51:11 +00:00
|
|
|
setDataAttribute( toggle );
|
2023-04-12 20:36:55 +00:00
|
|
|
|
2023-04-03 21:16:52 +00:00
|
|
|
/**
|
|
|
|
* @param {string} id this allows us to group notifications making sure only one is visible
|
|
|
|
* at any given time. All existing popups associated with ID will be removed.
|
|
|
|
* @param {number|false} timeout
|
|
|
|
*/
|
|
|
|
const showPopup = ( id, timeout = 4000 ) => {
|
2023-05-22 18:26:29 +00:00
|
|
|
popupNotification.add( settings, getPopupText(), id, [], timeout )
|
2023-04-24 12:26:48 +00:00
|
|
|
.then( ( popupWidget ) => {
|
|
|
|
if ( popupWidget ) {
|
|
|
|
popupNotification.show( popupWidget, timeout );
|
|
|
|
}
|
|
|
|
} );
|
2023-04-03 21:16:52 +00:00
|
|
|
};
|
|
|
|
|
2022-10-21 18:14:54 +00:00
|
|
|
toggle.addEventListener( 'click', function () {
|
2023-05-02 18:31:34 +00:00
|
|
|
const isLimitedWidth = features.isEnabled( LIMITED_WIDTH_FEATURE_NAME );
|
|
|
|
const oldIcon = isLimitedWidth ? 'fullScreen' : 'exitFullscreen';
|
|
|
|
const newIcon = isLimitedWidth ? 'exitFullscreen' : 'fullScreen';
|
|
|
|
|
2022-11-17 21:51:11 +00:00
|
|
|
features.toggle( LIMITED_WIDTH_FEATURE_NAME );
|
|
|
|
setDataAttribute( toggle );
|
2024-01-11 19:08:25 +00:00
|
|
|
toggleIcon.classList.remove( `mw-ui-icon-wikimedia-${ oldIcon }` );
|
|
|
|
toggleIcon.classList.add( `mw-ui-icon-wikimedia-${ newIcon }` );
|
2023-04-12 22:27:28 +00:00
|
|
|
window.dispatchEvent( new Event( 'resize' ) );
|
2023-05-02 18:31:34 +00:00
|
|
|
if ( isLimitedWidth ) {
|
|
|
|
// Now is full width, show notification
|
2023-04-03 21:16:52 +00:00
|
|
|
showPopup( TOGGLE_ID );
|
|
|
|
}
|
2022-10-21 18:14:54 +00:00
|
|
|
} );
|
2023-04-12 20:36:55 +00:00
|
|
|
}
|
2022-10-21 18:14:54 +00:00
|
|
|
|
|
|
|
module.exports = init;
|