mediawiki-extensions-Popups/src/previewBehavior.js
Jon Robson 049729626c IP masked users use localStorage for settings
Bug: T330517
Change-Id: Ib283e37f379e8cccc7f49ab0ce9aadd6535ea668
2023-06-14 08:16:50 -07:00

59 lines
1.7 KiB
JavaScript

/**
* @module previewBehaviour
*/
const canSaveToUserPreferences = require( './canSaveToUserPreferences.js' );
/**
* A collection of event handlers specific to how the user interacts with all
* previews. The event handlers are are agnostic to how/when they are bound
* //but not to what they are bound//, i.e. the showSettings event handler is
* written to be bound to either an `<a>` or `<button>` element.
*
* @typedef {Object} ext.popups.PreviewBehavior
* @property {string} settingsUrl
* @property {Function} showSettings
* @property {Function} previewDwell
* @property {Function} previewAbandon
* @property {Function} previewShow
* @property {Function} click handler for the entire preview
*/
/**
* Creates an instance of `ext.popups.PreviewBehavior`.
*
* If the user is logged out, then clicking the cog should show the settings
* modal.
*
* If the user is logged in, then clicking the cog should send them to the
* the "Appearance" tab otherwise.
*
* @param {mw.User} user
* @param {Object} actions The action creators bound to the Redux store
* @return {ext.popups.PreviewBehavior}
*/
export default function createPreviewBehavior( user, actions ) {
let settingsUrl, showSettings = () => {};
if ( !canSaveToUserPreferences( user ) ) {
showSettings = ( event ) => {
event.preventDefault();
actions.showSettings();
};
} else {
const rawTitle = 'Special:Preferences#mw-prefsection-rendering';
settingsUrl = mw.Title.newFromText( rawTitle )
.getUrl();
}
return {
settingsUrl,
showSettings,
previewDwell: actions.previewDwell,
previewAbandon: actions.abandon,
previewShow: actions.previewShow,
click: actions.linkClick
};
}