2023-09-20 23:36:57 +00:00
|
|
|
/**
|
|
|
|
* @typedef {Object} ClientPreference
|
|
|
|
* @property {string[]} options that are valid for this client preference
|
|
|
|
* @property {string} preferenceKey for registered users.
|
2024-01-18 00:59:41 +00:00
|
|
|
* @property {string} [type] defaults to radio. Supported: radio, switch
|
2024-01-30 20:31:43 +00:00
|
|
|
* @property {function} [callback] callback executed after a client preference has been modified.
|
2023-09-20 23:36:57 +00:00
|
|
|
*/
|
|
|
|
let /** @type {MwApi} */ api;
|
2023-09-14 15:36:54 +00:00
|
|
|
/**
|
|
|
|
* @typedef {Object} PreferenceOption
|
|
|
|
* @property {string} label
|
|
|
|
* @property {string} value
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2023-11-30 22:44:11 +00:00
|
|
|
* Get the list of client preferences that are active on the page, including hidden.
|
|
|
|
*
|
2023-09-14 15:36:54 +00:00
|
|
|
* @return {string[]} of active client preferences
|
|
|
|
*/
|
|
|
|
function getClientPreferences() {
|
|
|
|
return Array.from( document.documentElement.classList ).filter(
|
|
|
|
( className ) => className.match( /-clientpref-/ )
|
|
|
|
).map( ( className ) => className.split( '-clientpref-' )[ 0 ] );
|
|
|
|
}
|
|
|
|
|
2024-04-11 04:14:29 +00:00
|
|
|
/**
|
|
|
|
* Check if the feature is excluded from the current page.
|
|
|
|
* @param {string} featureName
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
function isFeatureExcluded( featureName ) {
|
|
|
|
return document.documentElement.classList.contains( featureName + '-clientpref-excluded' );
|
|
|
|
}
|
|
|
|
|
2023-11-30 22:44:11 +00:00
|
|
|
/**
|
|
|
|
* Get the list of client preferences that are active on the page and not hidden.
|
|
|
|
*
|
2024-01-12 00:07:33 +00:00
|
|
|
* @param {Record<string,ClientPreference>} config
|
2023-11-30 22:44:11 +00:00
|
|
|
* @return {string[]} of user facing client preferences
|
|
|
|
*/
|
2024-01-12 00:07:33 +00:00
|
|
|
function getVisibleClientPreferences( config ) {
|
2023-11-30 22:44:11 +00:00
|
|
|
const active = getClientPreferences();
|
|
|
|
// Order should be based on key in config.json
|
|
|
|
return Object.keys( config ).filter( ( key ) => active.indexOf( key ) > -1 );
|
|
|
|
}
|
|
|
|
|
2023-11-20 22:55:41 +00:00
|
|
|
/**
|
|
|
|
* @param {string} featureName
|
|
|
|
* @param {string} value
|
2024-01-12 00:07:33 +00:00
|
|
|
* @param {Record<string,ClientPreference>} config
|
2023-11-20 22:55:41 +00:00
|
|
|
*/
|
2024-01-12 00:07:33 +00:00
|
|
|
function toggleDocClassAndSave( featureName, value, config ) {
|
2023-11-20 22:55:41 +00:00
|
|
|
const pref = config[ featureName ];
|
2024-01-30 20:31:43 +00:00
|
|
|
const callback = pref.callback || ( () => {} );
|
2023-11-20 22:55:41 +00:00
|
|
|
if ( mw.user.isNamed() ) {
|
|
|
|
// FIXME: Ideally this would be done in mw.user.clientprefs API.
|
|
|
|
// mw.user.clientPrefs.get is marked as being only stable for anonymous and temporary users.
|
|
|
|
// So instead we have to keep track of all the different possible values and remove them
|
|
|
|
// before adding the new class.
|
|
|
|
config[ featureName ].options.forEach( ( possibleValue ) => {
|
2024-01-11 19:08:25 +00:00
|
|
|
document.documentElement.classList.remove( `${ featureName }-clientpref-${ possibleValue }` );
|
2023-11-20 22:55:41 +00:00
|
|
|
} );
|
2024-01-11 19:08:25 +00:00
|
|
|
document.documentElement.classList.add( `${ featureName }-clientpref-${ value }` );
|
2023-11-20 22:55:41 +00:00
|
|
|
// Ideally this should be taken care of via a single core helper function.
|
|
|
|
mw.util.debounce( function () {
|
|
|
|
api = api || new mw.Api();
|
2024-02-12 16:52:46 +00:00
|
|
|
api.saveOption( pref.preferenceKey, value ).then( () => {
|
|
|
|
callback();
|
|
|
|
} );
|
2023-11-20 22:55:41 +00:00
|
|
|
}, 100 )();
|
|
|
|
// END FIXME.
|
|
|
|
} else {
|
2024-01-24 20:23:29 +00:00
|
|
|
// This case is much simpler, the API transparently takes care of classes as well as storage.
|
2023-11-20 22:55:41 +00:00
|
|
|
mw.user.clientPrefs.set( featureName, value );
|
2024-01-30 20:31:43 +00:00
|
|
|
callback();
|
2023-11-20 22:55:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-14 15:36:54 +00:00
|
|
|
/**
|
|
|
|
* @param {string} featureName
|
|
|
|
* @param {string} value
|
2024-01-18 00:59:41 +00:00
|
|
|
* @return {string}
|
2023-09-14 15:36:54 +00:00
|
|
|
*/
|
2024-01-18 00:59:41 +00:00
|
|
|
const getInputId = ( featureName, value ) => `skin-client-pref-${ featureName }-value-${ value }`;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} type
|
|
|
|
* @param {string} featureName
|
|
|
|
* @param {string} value
|
|
|
|
* @return {HTMLInputElement}
|
|
|
|
*/
|
|
|
|
function makeInputElement( type, featureName, value ) {
|
2023-09-14 15:36:54 +00:00
|
|
|
const input = document.createElement( 'input' );
|
2024-01-12 00:07:33 +00:00
|
|
|
const name = `skin-client-pref-${ featureName }-group`;
|
2024-01-18 00:59:41 +00:00
|
|
|
const id = getInputId( featureName, value );
|
2023-09-14 15:36:54 +00:00
|
|
|
input.name = name;
|
|
|
|
input.id = id;
|
2024-01-18 00:59:41 +00:00
|
|
|
input.type = type;
|
|
|
|
if ( type === 'checkbox' ) {
|
|
|
|
input.checked = value === '1';
|
|
|
|
} else {
|
|
|
|
input.value = value;
|
|
|
|
}
|
|
|
|
input.setAttribute( 'data-event-name', id );
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} featureName
|
|
|
|
* @param {string} value
|
|
|
|
* @return {HTMLLabelElement}
|
|
|
|
*/
|
|
|
|
function makeLabelElement( featureName, value ) {
|
|
|
|
const label = document.createElement( 'label' );
|
|
|
|
// eslint-disable-next-line mediawiki/msg-doc
|
|
|
|
label.textContent = mw.msg( `${ featureName }-${ value }-label` );
|
|
|
|
label.setAttribute( 'for', getInputId( featureName, value ) );
|
|
|
|
return label;
|
|
|
|
}
|
|
|
|
|
2024-04-11 04:14:29 +00:00
|
|
|
/**
|
|
|
|
* Create an element that informs users that a feature is not functional
|
|
|
|
* on a given page. This message is hidden by default and made visible in
|
|
|
|
* CSS if a specific exclusion class exists.
|
|
|
|
*
|
|
|
|
* @param {string} featureName
|
|
|
|
* @return {HTMLElement}
|
|
|
|
*/
|
|
|
|
function makeExclusionNotice( featureName ) {
|
|
|
|
const p = document.createElement( 'p' );
|
|
|
|
// eslint-disable-next-line mediawiki/msg-doc
|
|
|
|
const noticeMessage = mw.message( `${ featureName }-exclusion-notice` );
|
|
|
|
p.classList.add( 'exclusion-notice', `${ featureName }-exclusion-notice` );
|
|
|
|
p.textContent = noticeMessage.text();
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2024-01-18 00:59:41 +00:00
|
|
|
/**
|
|
|
|
* @param {Element} parent
|
|
|
|
* @param {string} featureName
|
|
|
|
* @param {string} value
|
|
|
|
* @param {string} currentValue
|
|
|
|
* @param {Record<string,ClientPreference>} config
|
|
|
|
*/
|
|
|
|
function appendRadioToggle( parent, featureName, value, currentValue, config ) {
|
|
|
|
const input = makeInputElement( 'radio', featureName, value );
|
2023-11-07 23:35:26 +00:00
|
|
|
input.classList.add( 'cdx-radio__input' );
|
2023-09-14 15:36:54 +00:00
|
|
|
if ( currentValue === value ) {
|
|
|
|
input.checked = true;
|
|
|
|
}
|
2024-04-11 04:14:29 +00:00
|
|
|
|
|
|
|
if ( isFeatureExcluded( featureName ) ) {
|
|
|
|
input.disabled = true;
|
|
|
|
}
|
|
|
|
|
2023-11-07 23:35:26 +00:00
|
|
|
const icon = document.createElement( 'span' );
|
|
|
|
icon.classList.add( 'cdx-radio__icon' );
|
2024-01-18 00:59:41 +00:00
|
|
|
const label = makeLabelElement( featureName, value );
|
2023-11-07 23:35:26 +00:00
|
|
|
label.classList.add( 'cdx-radio__label' );
|
2023-09-14 15:36:54 +00:00
|
|
|
const container = document.createElement( 'div' );
|
2023-11-07 23:35:26 +00:00
|
|
|
container.classList.add( 'cdx-radio' );
|
2023-09-14 15:36:54 +00:00
|
|
|
container.appendChild( input );
|
2023-11-07 23:35:26 +00:00
|
|
|
container.appendChild( icon );
|
2023-09-14 15:36:54 +00:00
|
|
|
container.appendChild( label );
|
|
|
|
parent.appendChild( container );
|
|
|
|
input.addEventListener( 'change', () => {
|
2024-01-12 00:07:33 +00:00
|
|
|
toggleDocClassAndSave( featureName, value, config );
|
2023-09-14 15:36:54 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2024-01-18 00:59:41 +00:00
|
|
|
/**
|
|
|
|
* @param {Element} form
|
|
|
|
* @param {string} featureName
|
|
|
|
* @param {HTMLElement} labelElement
|
|
|
|
* @param {string} currentValue
|
|
|
|
* @param {Record<string,ClientPreference>} config
|
|
|
|
*/
|
|
|
|
function appendToggleSwitch( form, featureName, labelElement, currentValue, config ) {
|
|
|
|
const input = makeInputElement( 'checkbox', featureName, currentValue );
|
|
|
|
input.classList.add( 'cdx-toggle-switch__input' );
|
|
|
|
const switcher = document.createElement( 'span' );
|
|
|
|
switcher.classList.add( 'cdx-toggle-switch__switch' );
|
|
|
|
const grip = document.createElement( 'span' );
|
|
|
|
grip.classList.add( 'cdx-toggle-switch__switch__grip' );
|
|
|
|
switcher.appendChild( grip );
|
|
|
|
const label = labelElement || makeLabelElement( featureName, currentValue );
|
|
|
|
label.classList.add( 'cdx-toggle-switch__label' );
|
|
|
|
const toggleSwitch = document.createElement( 'span' );
|
|
|
|
toggleSwitch.classList.add( 'cdx-toggle-switch' );
|
|
|
|
toggleSwitch.appendChild( input );
|
|
|
|
toggleSwitch.appendChild( switcher );
|
|
|
|
toggleSwitch.appendChild( label );
|
|
|
|
input.addEventListener( 'change', () => {
|
|
|
|
toggleDocClassAndSave( featureName, input.checked ? '1' : '0', config );
|
|
|
|
} );
|
|
|
|
form.appendChild( toggleSwitch );
|
|
|
|
}
|
|
|
|
|
2023-09-14 15:36:54 +00:00
|
|
|
/**
|
|
|
|
* @param {string} className
|
|
|
|
* @return {Element}
|
|
|
|
*/
|
|
|
|
function createRow( className ) {
|
|
|
|
const row = document.createElement( 'div' );
|
|
|
|
row.setAttribute( 'class', className );
|
|
|
|
return row;
|
|
|
|
}
|
|
|
|
|
2024-01-18 00:59:41 +00:00
|
|
|
/**
|
|
|
|
* Get the label for the feature.
|
|
|
|
*
|
|
|
|
* @param {string} featureName
|
|
|
|
* @return {MwMessage}
|
|
|
|
*/
|
|
|
|
const getFeatureLabelMsg = ( featureName ) =>
|
|
|
|
// eslint-disable-next-line mediawiki/msg-doc
|
|
|
|
mw.message( `${ featureName }-name` );
|
|
|
|
|
2023-09-14 15:36:54 +00:00
|
|
|
/**
|
|
|
|
* adds a toggle button
|
|
|
|
*
|
|
|
|
* @param {string} featureName
|
2024-01-12 00:07:33 +00:00
|
|
|
* @param {Record<string,ClientPreference>} config
|
2023-09-14 15:36:54 +00:00
|
|
|
* @return {Element|null}
|
|
|
|
*/
|
2024-01-18 00:59:41 +00:00
|
|
|
function makeControl( featureName, config ) {
|
2023-09-20 23:36:57 +00:00
|
|
|
const pref = config[ featureName ];
|
|
|
|
if ( !pref ) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-09-14 15:36:54 +00:00
|
|
|
const currentValue = mw.user.clientPrefs.get( featureName );
|
|
|
|
// The client preference was invalid. This shouldn't happen unless a gadget
|
|
|
|
// or script has modified the documentElement.
|
2023-11-20 22:55:41 +00:00
|
|
|
if ( typeof currentValue === 'boolean' ) {
|
2023-09-14 15:36:54 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const row = createRow( '' );
|
|
|
|
const form = document.createElement( 'form' );
|
2024-01-18 00:59:41 +00:00
|
|
|
const type = pref.type || 'radio';
|
|
|
|
switch ( type ) {
|
|
|
|
case 'radio':
|
|
|
|
pref.options.forEach( ( value ) => {
|
|
|
|
appendRadioToggle( form, featureName, value, currentValue, config );
|
|
|
|
} );
|
|
|
|
break;
|
|
|
|
case 'switch': {
|
|
|
|
const labelElement = document.createElement( 'label' );
|
|
|
|
labelElement.textContent = getFeatureLabelMsg( featureName ).text();
|
|
|
|
appendToggleSwitch( form, featureName, labelElement, currentValue, config );
|
|
|
|
break;
|
|
|
|
} default:
|
|
|
|
throw new Error( 'Unknown client preference! Only switch or radio are supported.' );
|
|
|
|
}
|
2023-09-14 15:36:54 +00:00
|
|
|
row.appendChild( form );
|
2024-04-11 04:14:29 +00:00
|
|
|
|
|
|
|
if ( isFeatureExcluded( featureName ) ) {
|
|
|
|
const exclusionNotice = makeExclusionNotice( featureName );
|
|
|
|
row.appendChild( exclusionNotice );
|
|
|
|
}
|
2023-09-14 15:36:54 +00:00
|
|
|
return row;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-11-07 23:35:26 +00:00
|
|
|
* @param {Element} parent
|
2023-09-14 15:36:54 +00:00
|
|
|
* @param {string} featureName
|
2024-01-12 00:07:33 +00:00
|
|
|
* @param {Record<string,ClientPreference>} config
|
2023-09-14 15:36:54 +00:00
|
|
|
*/
|
2024-01-12 00:07:33 +00:00
|
|
|
function makeClientPreference( parent, featureName, config ) {
|
2024-01-18 00:59:41 +00:00
|
|
|
const labelMsg = getFeatureLabelMsg( featureName );
|
2024-01-24 20:23:29 +00:00
|
|
|
// If the user is not debugging messages and no language exists,
|
|
|
|
// exit as its a hidden client preference.
|
2023-09-14 15:36:54 +00:00
|
|
|
if ( !labelMsg.exists() && mw.config.get( 'wgUserLanguage' ) !== 'qqx' ) {
|
2023-11-07 23:35:26 +00:00
|
|
|
return;
|
2023-09-14 15:36:54 +00:00
|
|
|
} else {
|
2024-01-12 00:07:33 +00:00
|
|
|
const id = `skin-client-prefs-${ featureName }`;
|
2023-11-07 23:35:26 +00:00
|
|
|
// @ts-ignore TODO: upstream patch URL
|
|
|
|
const portlet = mw.util.addPortlet( id, labelMsg.text() );
|
2024-01-18 00:59:41 +00:00
|
|
|
const labelElement = portlet.querySelector( 'label' );
|
2023-11-07 23:35:26 +00:00
|
|
|
// eslint-disable-next-line mediawiki/msg-doc
|
2024-01-11 19:08:25 +00:00
|
|
|
const descriptionMsg = mw.message( `${ featureName }-description` );
|
2023-11-07 23:35:26 +00:00
|
|
|
if ( descriptionMsg.exists() ) {
|
2024-01-18 00:59:41 +00:00
|
|
|
const desc = document.createElement( 'span' );
|
|
|
|
desc.classList.add( 'skin-client-pref-description' );
|
2023-11-07 23:35:26 +00:00
|
|
|
desc.textContent = descriptionMsg.text();
|
2024-01-18 00:59:41 +00:00
|
|
|
if ( labelElement && labelElement.parentNode ) {
|
|
|
|
labelElement.appendChild( desc );
|
2023-11-07 23:35:26 +00:00
|
|
|
}
|
|
|
|
}
|
2024-01-18 00:59:41 +00:00
|
|
|
const row = makeControl( featureName, config );
|
2023-11-07 23:35:26 +00:00
|
|
|
parent.appendChild( portlet );
|
|
|
|
if ( row ) {
|
|
|
|
const tmp = mw.util.addPortletLink( id, '', '' );
|
|
|
|
// create a dummy link
|
|
|
|
if ( tmp ) {
|
|
|
|
const link = tmp.querySelector( 'a' );
|
|
|
|
if ( link ) {
|
|
|
|
link.replaceWith( row );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-14 15:36:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fills the client side preference dropdown with controls.
|
2023-11-07 23:35:26 +00:00
|
|
|
* @param {string} selector of element to fill with client preferences
|
2024-01-12 00:07:33 +00:00
|
|
|
* @param {Record<string,ClientPreference>} config
|
|
|
|
* @return {Promise<Node>}
|
2023-09-14 15:36:54 +00:00
|
|
|
*/
|
2024-01-12 00:07:33 +00:00
|
|
|
function render( selector, config ) {
|
2023-11-07 23:35:26 +00:00
|
|
|
const node = document.querySelector( selector );
|
|
|
|
if ( !node ) {
|
2024-01-12 00:07:33 +00:00
|
|
|
return Promise.reject();
|
2023-11-07 23:35:26 +00:00
|
|
|
}
|
2024-01-12 00:07:33 +00:00
|
|
|
return new Promise( ( resolve ) => {
|
2024-01-18 00:59:41 +00:00
|
|
|
getVisibleClientPreferences( config ).forEach( ( pref ) => {
|
|
|
|
makeClientPreference( node, pref, config );
|
|
|
|
} );
|
|
|
|
mw.requestIdleCallback( () => {
|
|
|
|
resolve( node );
|
2023-11-07 23:35:26 +00:00
|
|
|
} );
|
2023-09-14 15:36:54 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2023-11-07 23:35:26 +00:00
|
|
|
/**
|
|
|
|
* @param {string} clickSelector what to click
|
|
|
|
* @param {string} renderSelector where to render
|
2024-01-12 00:07:33 +00:00
|
|
|
* @param {Record<string,ClientPreference>} config
|
2023-11-07 23:35:26 +00:00
|
|
|
*/
|
2024-01-12 00:07:33 +00:00
|
|
|
function bind( clickSelector, renderSelector, config ) {
|
2023-11-07 23:35:26 +00:00
|
|
|
let enhanced = false;
|
|
|
|
const chk = /** @type {HTMLInputElement} */ (
|
|
|
|
document.querySelector( clickSelector )
|
|
|
|
);
|
|
|
|
if ( !chk ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( chk.checked ) {
|
2024-01-12 00:07:33 +00:00
|
|
|
render( renderSelector, config );
|
2023-11-07 23:35:26 +00:00
|
|
|
enhanced = true;
|
|
|
|
} else {
|
|
|
|
chk.addEventListener( 'input', () => {
|
|
|
|
if ( enhanced ) {
|
|
|
|
return;
|
|
|
|
}
|
2024-01-12 00:07:33 +00:00
|
|
|
render( renderSelector, config );
|
2023-11-07 23:35:26 +00:00
|
|
|
enhanced = true;
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = {
|
|
|
|
bind,
|
2023-11-20 22:55:41 +00:00
|
|
|
toggleDocClassAndSave,
|
2023-11-07 23:35:26 +00:00
|
|
|
render
|
|
|
|
};
|