mediawiki-extensions-Popups/resources/ext.popups.experiment.js
jdlrobson 765aa40cc1 Replace use of jStorage with mw.storage
Use the standardised MediaWiki storage system for the simple use
case of disabling/enabling Popups. The jStorage library is 12kb
uncompressed and cannot be justified for this usage (and if it becomes
the standardised library mw.storage will begin using it)

This means that browsers with full localStorage or no localStorage
support will not be able to disable Popups. Given the current ecosystem
of the web - localStorage is widely supported - and the fact that grade
A browsers enjoy localStorage support - this is not a problem.
See https://github.com/wikimedia/mediawiki/blob/REL1_27/resources/src/startup.js#L59

Changes:
* Stop using jStorage
* Cleanup and migrate previous values in jStorage with plan for removing this
in a month.

Bug: T136241
Change-Id: I6dac2911e84d6cb20731be34add01576cf407c46
2016-05-30 12:30:38 -07:00

91 lines
2.1 KiB
JavaScript

( function ( mw ) {
/**
* @ignore
*/
function getToken() {
var key = 'PopupsExperimentID',
id = mw.storage.get( key );
if ( !id ) {
id = mw.user.generateRandomSessionId();
mw.storage.set( key, id );
}
return id;
}
/**
* Has the user previously enabled Popups by clicking "Enable previews" in the
* footer menu?
*
* @return {boolean}
* @ignore
*/
function hasUserEnabledFeature() {
var value = mw.storage.get( 'mwe-popups-enabled' );
return Boolean( value ) && value !== '0';
}
/**
* Has the user previously disabled Popups by clicking "Disable previews" in the settings
* overlay?
*
* @return {boolean}
* @ignore
*/
function hasUserDisabledFeature() {
return mw.storage.get( 'mwe-popups-enabled' ) === '0';
}
/**
* @class mw.popups.experiment
* @singleton
*/
mw.popups.experiment = {};
/**
* Gets whether or not the user has Popups enabled, i.e. whether they are in the experiment
* condition.
*
* The user is in the experiment condition if:
* * they've enabled Popups by click "Enable previews" in the footer menu, or
* * they've enabled Popups as a beta feature, or
* * they aren't in the control bucket of the experiment
*
* N.B. that the user isn't entered into the experiment, i.e. they aren't assigned or a bucket,
* if the experiment isn't configured.
*
* @return {boolean}
*/
mw.popups.experiment.isUserInCondition = function isUserInCondition() {
var config = mw.config.get( 'wgPopupsExperimentConfig' );
// The first two tests deal with whether the user has /explicitly/ enable or disabled via its
// settings.
if ( hasUserEnabledFeature() ) {
return true;
}
if ( hasUserDisabledFeature() ) {
return false;
}
// Users with the beta feature enabled are already in the experimental condition.
if ( mw.config.get( 'wgPopupsExperimentIsBetaFeatureEnabled', false ) ) {
return true;
}
if ( !config ) {
return false;
}
// FIXME: mw.experiments should expose the CONTROL_BUCKET constant, e.g.
// `mw.experiments.CONTROL_BUCKET`.
return mw.experiments.getBucket( config, getToken() ) !== 'control';
};
}( mediaWiki ) );