Jdlrobson has uploaded a new change for review. https://gerrit.wikimedia.org/r/312426
Change subject: Hygiene: Add set and get methods for active link ...................................................................... Hygiene: Add set and get methods for active link Named functions help explain to a reader and reviewer what the code is actually doing. Change-Id: I1d059c9270fd2298285fa5e4e52e403a06f35503 --- M resources/ext.popups.renderer/desktopRenderer.js 1 file changed, 38 insertions(+), 19 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Popups refs/changes/26/312426/1 diff --git a/resources/ext.popups.renderer/desktopRenderer.js b/resources/ext.popups.renderer/desktopRenderer.js index 45ab725..b5ae9f6 100644 --- a/resources/ext.popups.renderer/desktopRenderer.js +++ b/resources/ext.popups.renderer/desktopRenderer.js @@ -1,8 +1,28 @@ /*global popupDelay: true, popupHideDelay: true*/ ( function ( $, mw ) { - var closeTimer, openTimer, + var closeTimer, openTimer, $activeLink, logData = {}; + + /** + * Sets the link that the currently shown popup relates to + * + * @ignore + * @param {jQuery} [$link] if undefined there is no active link + */ + function setActiveLink( $link ) { + $activeLink = $link; + } + + /** + * Gets the link that the currently shown popup relates to + * + * @ignore + * @return {jQuery|undefined} if undefined there is no active link + */ + function getActiveLink( $link ) { + return $activeLink; + } /** * Logs the click on link or popup @@ -65,12 +85,6 @@ mw.popups.render.cache = {}; /** - * The link the currently has a popup - * @property {jQuery} currentLink - */ - mw.popups.render.currentLink = undefined; - - /** * Object to store all renderers * @property {Object} renderers */ @@ -87,13 +101,14 @@ * @param {string} linkInteractionToken random token representing the current interaction with the link */ mw.popups.render.render = function ( $link, event, dwellStartTime, linkInteractionToken ) { - var linkHref = $link.attr( 'href' ); + var linkHref = $link.attr( 'href' ), + $activeLink = getActiveLink(); // This will happen when the mouse goes from the popup box back to the // anchor tag. In such a case, the timer to close the box is cleared. if ( - mw.popups.render.currentLink && - mw.popups.render.currentLink[ 0 ] === $link[ 0 ] + $activeLink && + $activeLink[ 0 ] === $link[ 0 ] ) { if ( closeTimer ) { closeTimer.abort(); @@ -103,7 +118,7 @@ // If the mouse moves to another link (we already check if its the same // link in the previous condition), then close the popup. - if ( mw.popups.render.currentLink ) { + if ( $activeLink ) { mw.popups.render.closePopup(); } @@ -113,7 +128,7 @@ return; } - mw.popups.render.currentLink = $link; + setActiveLink( $link ); // Set the log data only after the current link is set, otherwise, functions like // closePopup will use the new log data when closing an old popup. logData = { @@ -224,12 +239,13 @@ * @param {Object} event */ mw.popups.render.clickHandler = function ( event ) { - var action = mw.popups.getAction( event ); + var action = mw.popups.getAction( event ), + $activeLink = getActiveLink(); logClickAction( event ); if ( action === 'opened in same tab' ) { - window.location.href = mw.popups.render.currentLink.attr( 'href' ); + window.location.href = $activeLink.attr( 'href' ); } }; @@ -240,13 +256,14 @@ * @method closePopup */ mw.popups.render.closePopup = function () { - var fadeInClass, fadeOutClass; + var fadeInClass, fadeOutClass, + $activeLink = getActiveLink(); - if ( mw.popups.render.currentLink === undefined ) { + if ( !$activeLink ) { return false; } - $( mw.popups.render.currentLink ).off( 'mouseleave blur', mw.popups.render.leaveActive ); + $activeLink.off( 'mouseleave blur', mw.popups.render.leaveActive ); fadeInClass = ( mw.popups.$popup.hasClass( 'mwe-popups-fade-in-up' ) ) ? 'mwe-popups-fade-in-up' : @@ -338,6 +355,8 @@ * @method leaveInactive */ mw.popups.render.leaveInactive = function () { + var $activeLink = getActiveLink(); + if ( logData.dwellStartTime && logData.linkInteractionToken && mw.now() - logData.dwellStartTime >= mw.popups.render.DWELL_EVENTS_MIN_INTERACTION_TIME @@ -348,7 +367,7 @@ } ) ); } // TODO: should `blur` also be here? - $( mw.popups.render.currentLink ).off( 'mouseleave', mw.popups.render.leaveInactive ); + $activeLink.off( 'mouseleave', mw.popups.render.leaveInactive ); if ( openTimer ) { openTimer.abort(); } @@ -364,7 +383,7 @@ */ mw.popups.render.reset = function () { logData = {}; - mw.popups.render.currentLink = undefined; + setActiveLink(); mw.popups.render.abortCurrentRequest(); openTimer = undefined; closeTimer = undefined; -- To view, visit https://gerrit.wikimedia.org/r/312426 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I1d059c9270fd2298285fa5e4e52e403a06f35503 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