]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blobdiff - resources/src/mediawiki/mediawiki.notification.convertmessagebox.js
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / resources / src / mediawiki / mediawiki.notification.convertmessagebox.js
diff --git a/resources/src/mediawiki/mediawiki.notification.convertmessagebox.js b/resources/src/mediawiki/mediawiki.notification.convertmessagebox.js
new file mode 100644 (file)
index 0000000..5d46de6
--- /dev/null
@@ -0,0 +1,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 ) );