Florianschmidtwelzow has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/246274

Change subject: Use mw.notification when User rights was saved successfully
......................................................................

Use mw.notification when User rights was saved successfully

Split out Special:Preferences handling of successbox into it's own
mediawiki.successboxToNotify module and make it useable for other
places, too. Use this module on Special:UserRights.

Bug: T115463
Change-Id: I87054b55053d209835d6fdea1f6e3e67f10e3ac8
---
M includes/specials/SpecialUserrights.php
M resources/Resources.php
M resources/src/mediawiki.special/mediawiki.special.preferences.js
A resources/src/mediawiki.special/mediawiki.special.userrights.js
A resources/src/mediawiki/mediawiki.searchboxToNotify.js
5 files changed, 84 insertions(+), 21 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/74/246274/1

diff --git a/includes/specials/SpecialUserrights.php 
b/includes/specials/SpecialUserrights.php
index a6fe1b5..6726e03 100644
--- a/includes/specials/SpecialUserrights.php
+++ b/includes/specials/SpecialUserrights.php
@@ -138,6 +138,7 @@
 
                // show a successbox, if the user rights was saved successfully
                if ( $request->getCheck( 'success' ) && $this->mFetchedUser !== 
null ) {
+                       $out->addModules( array( 'mediawiki.special.userrights' 
) );
                        $out->wrapWikiMsg(
                                "<div class=\"successbox\">\n$1\n</div>",
                                array( 'savedrights', 
$this->mFetchedUser->getName() )
diff --git a/resources/Resources.php b/resources/Resources.php
index fee1e7c..007db97 100644
--- a/resources/Resources.php
+++ b/resources/Resources.php
@@ -1088,6 +1088,13 @@
                'scripts' => 'resources/src/mediawiki/mediawiki.notify.js',
                'targets' => array( 'desktop', 'mobile' ),
        ),
+       'mediawiki.searchboxToNotify' => array(
+               'dependencies' => array(
+                       'mediawiki.notification',
+               ),
+               'scripts' => 
'resources/src/mediawiki/mediawiki.searchboxToNotify.js',
+               'targets' => array( 'desktop', 'mobile' ),
+       ),
        'mediawiki.RegExp' => array(
                'scripts' => 'resources/src/mediawiki/mediawiki.RegExp.js',
                'targets' => array( 'desktop', 'mobile' ),
@@ -1720,7 +1727,13 @@
                'dependencies' => array(
                        'mediawiki.language',
                        'mediawiki.confirmCloseWindow',
-                       'mediawiki.notification',
+                       'mediawiki.searchboxToNotify',
+               ),
+       ),
+       'mediawiki.special.userrights' => array(
+               'scripts' => 
'resources/src/mediawiki.special/mediawiki.special.userrights.js',
+               'dependencies' => array(
+                       'mediawiki.searchboxToNotify',
                ),
        ),
        'mediawiki.special.recentchanges' => array(
diff --git a/resources/src/mediawiki.special/mediawiki.special.preferences.js 
b/resources/src/mediawiki.special/mediawiki.special.preferences.js
index 9b790e0..d3f7b49 100644
--- a/resources/src/mediawiki.special/mediawiki.special.preferences.js
+++ b/resources/src/mediawiki.special/mediawiki.special.preferences.js
@@ -5,8 +5,7 @@
        var $preftoc, $preferences, $fieldsets, $legends,
                hash, labelFunc,
                $tzSelect, $tzTextbox, $localtimeHolder, servertime,
-               $checkBoxes, allowCloseWindow,
-               notif;
+               $checkBoxes, allowCloseWindow;
 
        labelFunc = function () {
                return this.id.replace( /^mw-prefsection/g, 'preftab' );
@@ -86,24 +85,11 @@
        }
 
        // Check for messageboxes (.successbox, .warningbox, .errorbox) to 
replace with notifications
-       if ( $( '.mw-preferences-messagebox' ).length ) {
-               // If there is a #mw-preferences-success box and javascript is 
enabled, use a slick notification instead!
-               if ( $( '#mw-preferences-success' ).length ) {
-                       notif = mediaWiki.notification.notify( 
mediaWiki.message( 'savedprefs' ), { autoHide: false } );
-                       // 'change' event not reliable!
-                       $( '#preftoc, .prefsection' ).one( 'change keydown 
mousedown', function () {
-                               if ( notif ) {
-                                       notif.close();
-                                       notif = null;
-                               }
-                       } );
-
-                       // Remove now-unnecessary success=1 querystring to 
prevent reappearance of notification on reload
-                       if ( history.replaceState ) {
-                               history.replaceState( {}, document.title, 
location.href.replace( /&?success=1/, '' ) );
-                       }
-               }
-       }
+       mw.searchboxToNotify( {
+               messageBoxSelector: '.mw-preferences-messagebox',
+               msg: 'savedprefs',
+               closeSelector: '#preftoc, .prefsection'
+       } );
 
        // Populate the prefToc
        $legends.each( function ( i, legend ) {
diff --git a/resources/src/mediawiki.special/mediawiki.special.userrights.js 
b/resources/src/mediawiki.special/mediawiki.special.userrights.js
new file mode 100644
index 0000000..14f18d4
--- /dev/null
+++ b/resources/src/mediawiki.special/mediawiki.special.userrights.js
@@ -0,0 +1,10 @@
+/*!
+ * JavaScript for Special:Preferences
+ */
+jQuery( function ( $ ) {
+       // Check for messageboxes (.successbox, .warningbox, .errorbox) to 
replace with notifications
+       mw.searchboxToNotify( {
+               messageBoxSelector: '.successbox',
+               closeSelector: '#mw-content-text'
+       } );
+} );
diff --git a/resources/src/mediawiki/mediawiki.searchboxToNotify.js 
b/resources/src/mediawiki/mediawiki.searchboxToNotify.js
new file mode 100644
index 0000000..a2310c9
--- /dev/null
+++ b/resources/src/mediawiki/mediawiki.searchboxToNotify.js
@@ -0,0 +1,53 @@
+/**
+ * @class mw.searchboxToNotify
+ */
+( function ( mw ) {
+       'use strict';
+
+       /**
+        * Checks, if the successbox has content and converts it into a 
MediaWiki notification with the
+        * text of the successbox or a given message key. If no further params 
are set, the notification
+        * will automatically hit after 5 seconds, otherwise when the user 
clicks the element with the
+        * closeSelector.
+        *
+        * @cfg {Object} options Options for searchboxToNotify
+        * @cfg {String} options.messageBoxSelector The selector for the 
successbox.
+        * @cfg {String} [options.msg] Message key for the notification (needs 
to be already loaded in JS).
+        * @cfg {String} [options.closeSelector] The element which, when 
clicked/changed, closes the notification.
+        * @cfg {String} [options.closeSeconds] How many seconds to wait, 
before to close the notification. (Only
+        *  observed, if options.closeSelector isn't set)
+        */
+       mw.searchboxToNotify = function ( options ) {
+               var notif, msg, autoHide, $msgBox = $( 
options.messageBoxSelector );
+
+               $.extend( options, {
+                       closeSeconds: 5
+               } );
+
+               // If there is a success box and javascript is enabled, use a 
slick notification instead!
+               if ( $msgBox.length ) {
+                       autoHide = options.closeSelector === undefined;
+
+                       // if the msg param is given, use it, instead use the 
text of the successbox
+                       msg = options.msg ? mw.msg( options.msg ) : 
$msgBox.text();
+
+                       notif = mediaWiki.notification.notify( msg, { autoHide: 
autoHide, autoHideSeconds: options.closeSeconds } );
+                       if ( !autoHide ) {
+                               // 'change' event not reliable!
+                               $( options.closeSelector ).one( 'change keydown 
mousedown', function () {
+                                       if ( notif ) {
+                                               notif.close();
+                                               notif = null;
+                                       }
+                               } );
+                       }
+
+                       // Remove now-unnecessary success=1 querystring to 
prevent reappearance of notification on reload
+                       if ( history.replaceState ) {
+                               history.replaceState( {}, document.title, 
location.href.replace( /&?success=1/, '' ) );
+                       }
+                       $msgBox.remove();
+               }
+       };
+
+}( mediaWiki ) );

-- 
To view, visit https://gerrit.wikimedia.org/r/246274
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I87054b55053d209835d6fdea1f6e3e67f10e3ac8
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Florianschmidtwelzow <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to