summaryrefslogtreecommitdiff
path: root/www/wiki/resources/src/mediawiki/mediawiki.notification.convertmessagebox.js
blob: 5d46de60bbbb1164b7e5f972274afb8586656125 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
 * Usage:
 *
 *     var convertmessagebox = require( 'mediawiki.notification.convertmessagebox' );
 *
 * @class mw.plugin.convertmessagebox
 * @singleton
 */
( function ( mw, $ ) {
	'use strict';

	/**
	 * Convert a messagebox to a notification.
	 *
	 * Checks if a message box with class `.mw-notify-success`, `.mw-notify-warning`, or `.mw-notify-error`
	 * exists and converts it into a mw.Notification with the text of the element or a given message key.
	 *
	 * By default the notification will automatically hide after 5s, or when the user clicks the element.
	 * This can be overridden by setting attribute `data-mw-autohide="true"`.
	 *
	 * @param {Object} [options] Options
	 * @param {mw.Message} [options.msg] Message key (must be loaded already)
	 */
	function convertmessagebox( options ) {
		var $msgBox, type, autoHide, msg, notif,
			$successBox = $( '.mw-notify-success' ),
			$warningBox = $( '.mw-notify-warning' ),
			$errorBox = $( '.mw-notify-error' );

		// If there is a message box and javascript is enabled, use a slick notification instead!
		if ( $successBox.length ) {
			$msgBox = $successBox;
			type = 'info';
		} else if ( $warningBox.length ) {
			$msgBox = $warningBox;
			type = 'warn';
		} else if ( $errorBox.length ) {
			$msgBox = $errorBox;
			type = 'error';
		} else {
			return;
		}

		autoHide = $msgBox.attr( 'data-mw-autohide' ) === 'true';

		// If the msg param is given, use it, otherwise use the text of the successbox
		msg = options && options.msg || $msgBox.text();
		$msgBox.detach();

		notif = mw.notification.notify( msg, { autoHide: autoHide, type: type } );
		if ( !autoHide ) {
			// 'change' event not reliable!
			$( document ).one( 'keydown mousedown', function () {
				if ( notif ) {
					notif.close();
					notif = null;
				}
			} );
		}
	}

	module.exports = convertmessagebox;

}( mediaWiki, jQuery ) );