Debian Codegraph @

resources/src/mediawiki.notification.convertmessagebox.js

raw symbols SCIP
1 /**
2 * Exposes a method for converting a messagebox to a notification.
3 *
4 * @module mediawiki.notification.convertmessagebox
5 */
6
7 /**
8 * Convert a messagebox to a notification.
9 *
10 * @example
11 * const convertmessagebox = require( 'mediawiki.notification.convertmessagebox' );
12 *
13 * @method (require("mediawiki.notification.convertmessagebox"))
14 * @param {Object} [options] Options
15 * @param {mw.Message} [options.msg] Message key (must be loaded already)
16 */
17 module.exports = function ( options ) {
18 const $successBox = $( '.mw-notify-success' ),
19 $warningBox = $( '.mw-notify-warning' ),
20 $errorBox = $( '.mw-notify-error' );
21
22 // If there is a message box and javascript is enabled, use a slick notification instead!
23 let $msgBox, type;
24 if ( $successBox.length ) {
25 $msgBox = $successBox;
26 type = 'info';
27 } else if ( $warningBox.length ) {
28 $msgBox = $warningBox;
29 type = 'warn';
30 } else if ( $errorBox.length ) {
31 $msgBox = $errorBox;
32 type = 'error';
33 } else {
34 return;
35 }
36
37 const autoHide = $msgBox.attr( 'data-mw-autohide' ) === 'true';
38
39 // If the msg param is given, use it, otherwise use the text of the successbox
40 const msg = options && options.msg || $msgBox.text();
41 $msgBox.detach();
42
43 let notif = mw.notification.notify( msg, { autoHide: autoHide, type: type } );
44 if ( !autoHide ) {
45 // 'change' event not reliable!
46 $( document ).one( 'keydown mousedown', () => {
47 if ( notif ) {
48 notif.close();
49 notif = null;
50 }
51 } );
52 }
53 };