2022-10-21 18:14:54 +00:00
|
|
|
/** @interface MwApi */
|
|
|
|
|
2022-12-07 22:10:09 +00:00
|
|
|
let /** @type {MwApi} */ api;
|
|
|
|
const debounce = require( /** @type {string} */ ( 'mediawiki.util' ) ).debounce;
|
2022-10-21 18:14:54 +00:00
|
|
|
|
|
|
|
/**
|
2023-01-24 19:14:00 +00:00
|
|
|
* Saves preference to user preferences and/or cookies.
|
2022-10-21 18:14:54 +00:00
|
|
|
*
|
|
|
|
* @param {string} feature
|
|
|
|
* @param {boolean} enabled
|
|
|
|
*/
|
|
|
|
function save( feature, enabled ) {
|
2023-01-24 19:14:00 +00:00
|
|
|
if ( mw.user.isAnon() ) {
|
|
|
|
switch ( feature ) {
|
|
|
|
case 'limited-width':
|
|
|
|
if ( enabled ) {
|
|
|
|
// @ts-ignore
|
|
|
|
mw.cookie.set( 'mwclientprefs', null );
|
|
|
|
} else {
|
|
|
|
// @ts-ignore
|
|
|
|
mw.cookie.set( 'mwclientprefs', 'vector-feature-limited-width' );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// not a supported anonymous preference
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
2022-10-21 18:14:54 +00:00
|
|
|
debounce( function () {
|
|
|
|
api = api || new mw.Api();
|
|
|
|
api.saveOption( 'vector-' + feature, enabled ? 1 : 0 );
|
|
|
|
}, 500 )();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-21 18:37:26 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {string} name feature name
|
|
|
|
* @param {boolean} [override] option to force enabled or disabled state.
|
|
|
|
*
|
|
|
|
* @return {boolean} The new feature state (false=disabled, true=enabled).
|
|
|
|
* @throws {Error} if unknown feature toggled.
|
|
|
|
*/
|
|
|
|
function toggleDocClasses( name, override ) {
|
|
|
|
const featureClassEnabled = `vector-feature-${name}-enabled`,
|
|
|
|
classList = document.documentElement.classList,
|
|
|
|
featureClassDisabled = `vector-feature-${name}-disabled`;
|
|
|
|
|
|
|
|
if ( classList.contains( featureClassDisabled ) || override === true ) {
|
|
|
|
classList.remove( featureClassDisabled );
|
|
|
|
classList.add( featureClassEnabled );
|
|
|
|
return true;
|
|
|
|
} else if ( classList.contains( featureClassEnabled ) || override === false ) {
|
|
|
|
classList.add( featureClassDisabled );
|
|
|
|
classList.remove( featureClassEnabled );
|
|
|
|
return false;
|
|
|
|
} else {
|
2023-02-07 20:23:19 +00:00
|
|
|
throw new Error( `Attempt to toggle unknown feature: ${name}` );
|
2022-10-21 18:37:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:52:45 +00:00
|
|
|
/**
|
|
|
|
* @param {string} name
|
|
|
|
* @throws {Error} if unknown feature toggled.
|
|
|
|
*/
|
|
|
|
function toggle( name ) {
|
2022-10-21 18:37:26 +00:00
|
|
|
const featureState = toggleDocClasses( name );
|
2023-01-11 14:52:45 +00:00
|
|
|
save( name, featureState );
|
|
|
|
}
|
|
|
|
|
2022-11-17 21:51:11 +00:00
|
|
|
/**
|
|
|
|
* Checks if the feature is enabled.
|
|
|
|
*
|
|
|
|
* @param {string} name
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
function isEnabled( name ) {
|
2023-02-07 20:23:19 +00:00
|
|
|
return document.documentElement.classList.contains( `vector-feature-${name}-enabled` );
|
2022-11-17 21:51:11 +00:00
|
|
|
}
|
|
|
|
|
2022-10-21 18:37:26 +00:00
|
|
|
module.exports = { isEnabled, toggle, toggleDocClasses };
|