2023-04-20 12:13:34 +00:00
|
|
|
/** @module PopupNotification */
|
2023-04-03 21:16:52 +00:00
|
|
|
// Store active notifications to only show one at a time, for use inside clearHints and showHint
|
|
|
|
const /** @type {Record<string,OoUiPopupWidget>} */ activeNotification = {};
|
|
|
|
|
2023-03-23 20:03:13 +00:00
|
|
|
/**
|
|
|
|
* Adds and show a popup to the user to point them to the new location of the element
|
|
|
|
*
|
2023-04-03 21:16:52 +00:00
|
|
|
* @param {HTMLElement} container
|
2023-03-23 20:03:13 +00:00
|
|
|
* @param {string} message
|
2023-04-03 21:16:52 +00:00
|
|
|
* @param {string} id
|
2023-03-23 20:03:13 +00:00
|
|
|
* @param {string[]} [classes]
|
2023-04-03 21:16:52 +00:00
|
|
|
* @param {number|false} [timeout]
|
|
|
|
* @param {Function} [onDismiss]
|
|
|
|
* @return {JQuery.Promise<OoUiPopupWidget|undefined>}
|
2023-03-23 20:03:13 +00:00
|
|
|
*/
|
2023-04-03 21:16:52 +00:00
|
|
|
function add( container, message, id, classes = [], timeout = 4000, onDismiss = () => {} ) {
|
2023-03-23 20:03:13 +00:00
|
|
|
/**
|
2023-04-03 21:16:52 +00:00
|
|
|
* @type {OoUiPopupWidget}
|
2023-03-23 20:03:13 +00:00
|
|
|
*/
|
|
|
|
let popupWidget;
|
|
|
|
// load oojs-ui if it's not already loaded
|
2023-04-12 22:37:36 +00:00
|
|
|
// FIXME: This should be replaced with Codex.
|
2023-04-03 21:16:52 +00:00
|
|
|
return mw.loader.using( 'oojs-ui-core' ).then( () => {
|
2023-04-24 12:26:48 +00:00
|
|
|
// use existing hint.
|
|
|
|
if ( id && activeNotification[ id ] ) {
|
|
|
|
return activeNotification[ id ];
|
|
|
|
}
|
2023-04-12 22:37:36 +00:00
|
|
|
const content = document.createElement( 'p' );
|
|
|
|
content.textContent = message;
|
2023-03-23 20:03:13 +00:00
|
|
|
popupWidget = new OO.ui.PopupWidget( {
|
2023-06-13 21:57:05 +00:00
|
|
|
// eslint-disable-next-line no-jquery/no-jquery-constructor
|
2023-04-12 22:37:36 +00:00
|
|
|
$content: $( content ),
|
2023-03-23 20:03:13 +00:00
|
|
|
padded: true,
|
2023-04-03 21:16:52 +00:00
|
|
|
autoClose: timeout !== false,
|
|
|
|
head: timeout === false,
|
2023-03-23 20:03:13 +00:00
|
|
|
anchor: true,
|
|
|
|
align: 'center',
|
|
|
|
position: 'below',
|
|
|
|
classes: [ 'vector-popup-notification' ].concat( classes ),
|
|
|
|
container
|
|
|
|
} );
|
2023-06-13 21:57:05 +00:00
|
|
|
// eslint-disable-next-line no-jquery/no-other-methods
|
2023-03-23 20:03:13 +00:00
|
|
|
popupWidget.$element.appendTo( container );
|
2023-04-03 21:16:52 +00:00
|
|
|
popupWidget.on( 'closing', () => {
|
|
|
|
onDismiss();
|
|
|
|
} );
|
2023-04-24 12:26:48 +00:00
|
|
|
if ( popupWidget && id ) {
|
|
|
|
activeNotification[ id ] = popupWidget;
|
|
|
|
}
|
2023-04-03 21:16:52 +00:00
|
|
|
return popupWidget;
|
2023-03-23 20:03:13 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
/**
|
2023-04-24 12:26:48 +00:00
|
|
|
* Hides the popup widget
|
2023-03-23 20:03:13 +00:00
|
|
|
*
|
2023-04-03 21:16:52 +00:00
|
|
|
* @param {OoUiPopupWidget} popupWidget popupWidget from oojs-ui
|
|
|
|
* cannot use type because it's not loaded yet
|
|
|
|
*/
|
2023-04-24 12:26:48 +00:00
|
|
|
function hide( popupWidget ) {
|
2023-04-03 21:16:52 +00:00
|
|
|
popupWidget.toggle( false );
|
|
|
|
}
|
|
|
|
/**
|
2023-04-24 12:26:48 +00:00
|
|
|
* Shows the popup widget
|
2023-04-03 21:16:52 +00:00
|
|
|
*
|
|
|
|
* @param {OoUiPopupWidget} popupWidget popupWidget from oojs-ui
|
2023-03-23 20:03:13 +00:00
|
|
|
* cannot use type because it's not loaded yet
|
2023-04-03 21:16:52 +00:00
|
|
|
* @param {number|false} [timeout] use false if user must dismiss it themselves.
|
2023-03-23 20:03:13 +00:00
|
|
|
*/
|
|
|
|
function show( popupWidget, timeout = 4000 ) {
|
|
|
|
popupWidget.toggle( true );
|
2023-04-03 21:16:52 +00:00
|
|
|
// @ts-ignore https://github.com/wikimedia/typescript-types/pull/40
|
2023-03-23 20:03:13 +00:00
|
|
|
popupWidget.toggleClipping( true );
|
|
|
|
// hide the popup after timeout ms
|
2023-04-03 21:16:52 +00:00
|
|
|
if ( timeout === false ) {
|
|
|
|
return;
|
|
|
|
}
|
2023-03-23 20:03:13 +00:00
|
|
|
setTimeout( () => {
|
2023-04-24 12:26:48 +00:00
|
|
|
hide( popupWidget );
|
2023-03-23 20:03:13 +00:00
|
|
|
}, timeout );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-04-24 12:26:48 +00:00
|
|
|
* Hides all popups
|
2023-03-23 20:03:13 +00:00
|
|
|
*
|
|
|
|
*/
|
2023-04-24 12:26:48 +00:00
|
|
|
function hideAll() {
|
|
|
|
for ( const key in activeNotification ) {
|
|
|
|
const popupWidget = activeNotification[ key ];
|
|
|
|
hide( popupWidget );
|
|
|
|
}
|
2023-03-23 20:03:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
add,
|
2023-04-24 12:26:48 +00:00
|
|
|
hide,
|
|
|
|
hideAll,
|
|
|
|
show
|
2023-03-23 20:03:13 +00:00
|
|
|
};
|