Debian Codegraph @

skins/Vector/resources/skins.vector.js/popupNotification.js

raw symbols SCIP
1 /** @module PopupNotification */
2 // Store active notifications to only show one at a time, for use inside clearHints and showHint
3 const /** @type {Record<string,OoUiPopupWidget>} */ activeNotification = {};
4
5 /**
6 * Adds and show a popup to the user to point them to the new location of the element
7 *
8 * @param {HTMLElement} container
9 * @param {string} message
10 * @param {string} id
11 * @param {string[]} [classes]
12 * @param {number|false} [timeout]
13 * @param {Function} [onDismiss]
14 * @return {JQuery.Promise<OoUiPopupWidget|undefined>}
15 */
16 function add( container, message, id, classes = [], timeout = 4000, onDismiss = () => {} ) {
17 /**
18 * @type {OoUiPopupWidget}
19 */
20 let popupWidget;
21 // load oojs-ui if it's not already loaded
22 // FIXME: This should be replaced with Codex.
23 return mw.loader.using( 'oojs-ui-core' ).then( () => {
24 // use existing hint.
25 if ( id && activeNotification[ id ] ) {
26 return activeNotification[ id ];
27 }
28 const content = document.createElement( 'p' );
29 content.textContent = message;
30 popupWidget = new OO.ui.PopupWidget( {
31 // eslint-disable-next-line no-jquery/no-jquery-constructor
32 $content: $( content ),
33 padded: true,
34 autoClose: timeout !== false,
35 head: timeout === false,
36 anchor: true,
37 align: 'center',
38 position: 'below',
39 classes: [ 'vector-popup-notification' ].concat( classes ),
40 container
41 } );
42 // eslint-disable-next-line no-jquery/no-other-methods
43 popupWidget.$element.appendTo( container );
44 popupWidget.on( 'closing', () => {
45 onDismiss();
46 } );
47 if ( popupWidget && id ) {
48 activeNotification[ id ] = popupWidget;
49 }
50 return popupWidget;
51 } );
52 }
53 /**
54 * Hides the popup widget
55 *
56 * @param {OoUiPopupWidget} popupWidget popupWidget from oojs-ui
57 * cannot use type because it's not loaded yet
58 */
59 function hide( popupWidget ) {
60 popupWidget.toggle( false );
61 }
62 /**
63 * Shows the popup widget
64 *
65 * @param {OoUiPopupWidget} popupWidget popupWidget from oojs-ui
66 * cannot use type because it's not loaded yet
67 * @param {number|false} [timeout] use false if user must dismiss it themselves.
68 */
69 function show( popupWidget, timeout = 4000 ) {
70 popupWidget.toggle( true );
71 popupWidget.toggleClipping( true );
72 // hide the popup after timeout ms
73 if ( timeout === false ) {
74 return;
75 }
76 setTimeout( () => {
77 hide( popupWidget );
78 }, timeout );
79 }
80
81 /**
82 * Hides all popups
83 *
84 */
85 function hideAll() {
86 for ( const key in activeNotification ) {
87 const popupWidget = activeNotification[ key ];
88 hide( popupWidget );
89 }
90 }
91
92 module.exports = {
93 add,
94 hide,
95 hideAll,
96 show
97 };