mirror of
https://gerrit.wikimedia.org/r/mediawiki/skins/Vector.git
synced 2024-11-25 16:15:28 +00:00
836a58d120
Bug: T322772 Change-Id: I3b03225beff7638d243312c843f132225b55d76c
72 lines
1.7 KiB
JavaScript
72 lines
1.7 KiB
JavaScript
/** @interface MwApi */
|
|
|
|
var /** @type {MwApi} */api,
|
|
debounce = require( /** @type {string} */ ( 'mediawiki.util' ) ).debounce;
|
|
|
|
/**
|
|
* Saves preference to user preferences and/or localStorage.
|
|
*
|
|
* @param {string} feature
|
|
* @param {boolean} enabled
|
|
*/
|
|
function save( feature, enabled ) {
|
|
var featuresJSON,
|
|
// @ts-ignore
|
|
features = mw.storage.get( 'VectorFeatures' ) || '{}';
|
|
|
|
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 )();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param {string} name
|
|
* @throws {Error} if unknown feature toggled.
|
|
*/
|
|
function toggle( name ) {
|
|
var featureClassEnabled = 'vector-feature-' + name + '-enabled',
|
|
classList = document.body.classList,
|
|
featureClassDisabled = 'vector-feature-' + name + '-disabled';
|
|
|
|
if ( classList.contains( featureClassDisabled ) ) {
|
|
classList.remove( featureClassDisabled );
|
|
classList.add( featureClassEnabled );
|
|
save( name, true );
|
|
} else if ( classList.contains( featureClassEnabled ) ) {
|
|
classList.add( featureClassDisabled );
|
|
classList.remove( featureClassEnabled );
|
|
save( name, false );
|
|
} else {
|
|
throw new Error( 'Attempt to toggle unknown feature: ' + name );
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Checks if the feature is enabled.
|
|
*
|
|
* @param {string} name
|
|
* @return {boolean}
|
|
*/
|
|
function isEnabled( name ) {
|
|
return document.body.classList.contains(
|
|
'vector-feature-' + name + '-enabled'
|
|
);
|
|
}
|
|
|
|
module.exports = {
|
|
isEnabled: isEnabled,
|
|
toggle: toggle
|
|
};
|