Jdlrobson has uploaded a new change for review.

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

Change subject: Hygiene: Pull out event handler for display of Hovercards
......................................................................

Hygiene: Pull out event handler for display of Hovercards

* Make it clearer what event is being bound, consistent with
onLinkClick and onLinkAbandon
* Don't create event data twice
* Refactor to reduce function size

Change-Id: Ie4368531612a2829ad191629410ba548eadb2007
---
M resources/ext.popups.targets/desktopTarget.js
1 file changed, 37 insertions(+), 37 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Popups 
refs/changes/57/315857/1

diff --git a/resources/ext.popups.targets/desktopTarget.js 
b/resources/ext.popups.targets/desktopTarget.js
index ceb4592..f8cb7cd 100644
--- a/resources/ext.popups.targets/desktopTarget.js
+++ b/resources/ext.popups.targets/desktopTarget.js
@@ -34,6 +34,41 @@
        }
 
        /**
+        * `mouseenter` and `focus` events handler for links that are eligible 
for
+        * popups. Handles the disply of a popup.
+        *
+        * @param {Object} event
+        */
+       function onLinkHover( event ) {
+               var $link = $( this ),
+                       // Cache the hover start time and link interaction 
token for a later use
+                       eventData = {
+                               dwellStartTime: mw.now(),
+                               linkInteractionToken: 
mw.popups.getRandomToken(),
+                               hovercardsSuppressedByGadget: 
isNavigationPopupsGadgetEnabled()
+                       };
+
+               // Only enable Popups when the Navigation popups gadget is not 
enabled
+               if ( !eventData.hovercardsSuppressedByGadget && 
mw.popups.enabled ) {
+                       if ( mw.popups.scrolled ) {
+                               return;
+                       }
+
+                       mw.popups.removeTooltips( $link );
+                       mw.popups.render.render( $link, event,
+                               eventData.dwellStartTime, 
eventData.linkInteractionToken );
+               } else {
+                       $link
+                               .off( 'mouseleave.popups blur.popups 
click.popups' )
+                               // We are passing the same data, rather than a 
shared object, into two different functions.
+                               // The reason is that we don't want one 
function to change the data and
+                               // have a side-effect on the other function's 
data.
+                               .on( 'mouseleave.popups blur.popups', 
eventData, onLinkAbandon )
+                               .on( 'click.popups', eventData, onLinkClick );
+               }
+       }
+
+       /**
         * `click` event handler for links that are eligible for popups, but 
when
         * Popups are disabled.
         *
@@ -133,44 +168,9 @@
         * @method setupMouseEvents
         */
        function setupMouseEvents( $content ) {
-               var $elements, dwellStartTime, linkInteractionToken;
-
                mw.popups.$content = $content;
-               $elements = mw.popups.selectPopupElements();
-
-               $elements.on( 'mouseenter focus', function ( event ) {
-                       var $link = $( this );
-
-                       // Only enable Popups when the Navigation popups gadget 
is not enabled
-                       if ( !isNavigationPopupsGadgetEnabled() && 
mw.popups.enabled ) {
-                               if ( mw.popups.scrolled ) {
-                                       return;
-                               }
-
-                               mw.popups.removeTooltips( $link );
-                               mw.popups.render.render( $link, event, 
mw.now(), mw.popups.getRandomToken() );
-                       } else {
-                               // Cache the hover start time and link 
interaction token for a later use
-                               dwellStartTime = mw.now();
-                               linkInteractionToken = 
mw.popups.getRandomToken();
-
-                               $link
-                                       .off( 'mouseleave.popups blur.popups 
click.popups' )
-                                       // We are passing the same data, rather 
than a shared object, into two different functions.
-                                       // The reason is that we don't want one 
function to change the data and
-                                       // have a side-effect on the other 
function's data.
-                                       .on( 'mouseleave.popups blur.popups', {
-                                               dwellStartTime: dwellStartTime,
-                                               linkInteractionToken: 
linkInteractionToken,
-                                               hovercardsSuppressedByGadget: 
isNavigationPopupsGadgetEnabled()
-                                       }, onLinkAbandon )
-                                       .on( 'click.popups', {
-                                               dwellStartTime: dwellStartTime,
-                                               linkInteractionToken: 
linkInteractionToken,
-                                               hovercardsSuppressedByGadget: 
isNavigationPopupsGadgetEnabled()
-                                       }, onLinkClick );
-                       }
-               } );
+               mw.popups.selectPopupElements()
+                       .on( 'mouseenter focus', onLinkHover );
        }
 
        mw.hook( 'wikipage.content' ).add( setupMouseEvents );

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie4368531612a2829ad191629410ba548eadb2007
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Popups
Gerrit-Branch: master
Gerrit-Owner: Jdlrobson <jrob...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to