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
|
|
|
|
|
|
|
/**
|
|
|
|
* Saves preference to user preferences and/or localStorage.
|
|
|
|
*
|
|
|
|
* @param {string} feature
|
|
|
|
* @param {boolean} enabled
|
|
|
|
*/
|
|
|
|
function save( feature, enabled ) {
|
2022-12-07 22:10:09 +00:00
|
|
|
let featuresJSON;
|
|
|
|
// @ts-ignore
|
|
|
|
const features = mw.storage.get( 'VectorFeatures' ) || '{}';
|
2022-10-21 18:14:54 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
featuresJSON = JSON.parse( features );
|
|
|
|
} catch ( e ) {
|
|
|
|
featuresJSON = {};
|
|
|
|
}
|
|
|
|
featuresJSON[ feature ] = enabled;
|
|
|
|
// @ts-ignore
|
|
|
|
mw.storage.set( 'VectorFeatures', JSON.stringify( featuresJSON ) );
|
|
|
|
|
|
|
|
if ( !mw.user.isAnon() ) {
|
|
|
|
debounce( function () {
|
|
|
|
api = api || new mw.Api();
|
|
|
|
api.saveOption( 'vector-' + feature, enabled ? 1 : 0 );
|
|
|
|
}, 500 )();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-01-11 14:52:45 +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).
|
2022-10-21 18:14:54 +00:00
|
|
|
* @throws {Error} if unknown feature toggled.
|
|
|
|
*/
|
2023-01-11 14:52:45 +00:00
|
|
|
function toggleBodyClasses( name, override ) {
|
2022-12-07 22:10:09 +00:00
|
|
|
const featureClassEnabled = 'vector-feature-' + name + '-enabled',
|
2022-10-21 18:14:54 +00:00
|
|
|
classList = document.body.classList,
|
|
|
|
featureClassDisabled = 'vector-feature-' + name + '-disabled';
|
|
|
|
|
2023-01-11 14:52:45 +00:00
|
|
|
if ( classList.contains( featureClassDisabled ) || override === true ) {
|
2022-10-21 18:14:54 +00:00
|
|
|
classList.remove( featureClassDisabled );
|
|
|
|
classList.add( featureClassEnabled );
|
2023-01-11 14:52:45 +00:00
|
|
|
return true;
|
|
|
|
} else if ( classList.contains( featureClassEnabled ) || override === false ) {
|
2022-10-21 18:14:54 +00:00
|
|
|
classList.add( featureClassDisabled );
|
|
|
|
classList.remove( featureClassEnabled );
|
2023-01-11 14:52:45 +00:00
|
|
|
return false;
|
2022-10-21 18:14:54 +00:00
|
|
|
} else {
|
|
|
|
throw new Error( 'Attempt to toggle unknown feature: ' + name );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:52:45 +00:00
|
|
|
/**
|
|
|
|
* @param {string} name
|
|
|
|
* @throws {Error} if unknown feature toggled.
|
|
|
|
*/
|
|
|
|
function toggle( name ) {
|
|
|
|
const featureState = toggleBodyClasses( name );
|
|
|
|
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 ) {
|
|
|
|
return document.body.classList.contains(
|
|
|
|
'vector-feature-' + name + '-enabled'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:52:45 +00:00
|
|
|
module.exports = { isEnabled, toggle, toggleBodyClasses };
|