Merge "ApiSandbox: Move labels outside progress bars"
[lhc/web/wiklou.git] / resources / src / mediawiki.notification.convertmessagebox.js
1 /**
2 * Usage:
3 *
4 * var convertmessagebox = require( 'mediawiki.notification.convertmessagebox' );
5 *
6 * @class mw.plugin.convertmessagebox
7 * @singleton
8 */
9 ( function () {
10 'use strict';
11
12 /**
13 * Convert a messagebox to a notification.
14 *
15 * Checks if a message box with class `.mw-notify-success`, `.mw-notify-warning`, or `.mw-notify-error`
16 * exists and converts it into a mw.Notification with the text of the element or a given message key.
17 *
18 * By default the notification will automatically hide after 5s, or when the user clicks the element.
19 * This can be overridden by setting attribute `data-mw-autohide="true"`.
20 *
21 * @param {Object} [options] Options
22 * @param {mw.Message} [options.msg] Message key (must be loaded already)
23 */
24 function convertmessagebox( options ) {
25 var $msgBox, type, autoHide, msg, notif,
26 $successBox = $( '.mw-notify-success' ),
27 $warningBox = $( '.mw-notify-warning' ),
28 $errorBox = $( '.mw-notify-error' );
29
30 // If there is a message box and javascript is enabled, use a slick notification instead!
31 if ( $successBox.length ) {
32 $msgBox = $successBox;
33 type = 'info';
34 } else if ( $warningBox.length ) {
35 $msgBox = $warningBox;
36 type = 'warn';
37 } else if ( $errorBox.length ) {
38 $msgBox = $errorBox;
39 type = 'error';
40 } else {
41 return;
42 }
43
44 autoHide = $msgBox.attr( 'data-mw-autohide' ) === 'true';
45
46 // If the msg param is given, use it, otherwise use the text of the successbox
47 msg = options && options.msg || $msgBox.text();
48 $msgBox.detach();
49
50 notif = mw.notification.notify( msg, { autoHide: autoHide, type: type } );
51 if ( !autoHide ) {
52 // 'change' event not reliable!
53 $( document ).one( 'keydown mousedown', function () {
54 if ( notif ) {
55 notif.close();
56 notif = null;
57 }
58 } );
59 }
60 }
61
62 module.exports = convertmessagebox;
63
64 }() );