2014-06-06 08:36:20 +00:00
|
|
|
/*global popupDelay: true, popupHideDelay: true*/
|
|
|
|
|
2014-04-25 11:43:42 +00:00
|
|
|
( function ( $, mw ) {
|
2016-05-16 20:32:11 +00:00
|
|
|
var logData = {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Logs the click on link or popup
|
|
|
|
*
|
|
|
|
* @param {Object} event
|
|
|
|
*/
|
|
|
|
function logClickAction( event ) {
|
|
|
|
mw.track( 'ext.popups.schemaPopups', $.extend( {}, logData, {
|
|
|
|
action: mw.popups.getAction( event ),
|
|
|
|
totalInteractionTime: Math.round( mw.now() - logData.dwellStartTime )
|
|
|
|
} ) );
|
|
|
|
}
|
2014-04-25 11:43:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @class mw.popups.render
|
|
|
|
* @singleton
|
|
|
|
*/
|
|
|
|
mw.popups.render = {};
|
|
|
|
|
|
|
|
/**
|
2014-06-06 08:36:20 +00:00
|
|
|
* Time to wait in ms before showing a popup on hover.
|
|
|
|
* Use the navigation popup delay if it has been set by the user.
|
|
|
|
* This isn't the official way of setting the delay
|
|
|
|
* TODO: Add setting to change delay
|
2014-04-25 11:43:42 +00:00
|
|
|
* @property POPUP_DELAY
|
|
|
|
*/
|
2014-06-06 08:36:20 +00:00
|
|
|
mw.popups.render.POPUP_DELAY = ( typeof popupDelay === 'undefined' ) ?
|
|
|
|
500 :
|
|
|
|
popupDelay * 1000;
|
2014-04-25 11:43:42 +00:00
|
|
|
|
|
|
|
/**
|
2014-06-06 08:36:20 +00:00
|
|
|
* Time to wait in ms before closing a popup on de-hover.
|
|
|
|
* Use the navigation popup delay if it has been set by the user
|
|
|
|
* This isn't the official way of setting the delay
|
|
|
|
* TODO: Add setting to change delay
|
2014-04-25 11:43:42 +00:00
|
|
|
* @property POPUP_CLOSE_DELAY
|
|
|
|
*/
|
2014-06-06 08:36:20 +00:00
|
|
|
mw.popups.render.POPUP_CLOSE_DELAY = ( typeof popupHideDelay === 'undefined' ) ?
|
|
|
|
300 :
|
|
|
|
popupHideDelay * 1000;
|
2014-04-25 11:43:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Time to wait in ms before starting the API queries on hover, must be <= POPUP_DELAY
|
|
|
|
* @property API_DELAY
|
|
|
|
*/
|
|
|
|
mw.popups.render.API_DELAY = 50;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cache of all the popups that were opened in this session
|
|
|
|
* @property {Object} cache
|
|
|
|
*/
|
|
|
|
mw.popups.render.cache = {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The timer used to delay `closePopups`
|
|
|
|
* @property {jQuery.Deferred} closeTimer
|
|
|
|
*/
|
|
|
|
mw.popups.render.closeTimer = undefined;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The timer used to delay sending the API request/opening the popup form cache
|
|
|
|
* @property {jQuery.Deferred} openTimer
|
|
|
|
*/
|
|
|
|
mw.popups.render.openTimer = undefined;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The link the currently has a popup
|
|
|
|
* @property {jQuery} currentLink
|
|
|
|
*/
|
|
|
|
mw.popups.render.currentLink = undefined;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Current API request
|
|
|
|
* @property {jQuery.Deferred} currentRequest
|
|
|
|
*/
|
|
|
|
mw.popups.render.currentRequest = undefined;
|
|
|
|
|
2015-08-11 13:45:24 +00:00
|
|
|
/**
|
|
|
|
* Object to store all renderers
|
|
|
|
* @property {Object} renderers
|
|
|
|
*/
|
|
|
|
mw.popups.render.renderers = {};
|
|
|
|
|
2014-04-25 11:43:42 +00:00
|
|
|
/**
|
|
|
|
* Close all other popups and render the new one from the cache
|
|
|
|
* or by finding and calling the correct renderer
|
|
|
|
*
|
|
|
|
* @method render
|
|
|
|
* @param {Object} link
|
|
|
|
* @param {Object} event
|
2016-05-16 20:32:11 +00:00
|
|
|
* @param {number} dwellStartTime the instant when the link is dwelled on
|
|
|
|
* @param {string} linkInteractionToken random token representing the current interaction with the link
|
2014-04-25 11:43:42 +00:00
|
|
|
*/
|
2016-05-16 20:32:11 +00:00
|
|
|
mw.popups.render.render = function ( link, event, dwellStartTime, linkInteractionToken ) {
|
|
|
|
var linkHref = link.attr( 'href' );
|
|
|
|
|
2014-04-25 11:43:42 +00:00
|
|
|
// 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.
|
2015-01-13 13:18:02 +00:00
|
|
|
if (
|
|
|
|
mw.popups.render.currentLink &&
|
2015-08-26 10:15:16 +00:00
|
|
|
mw.popups.render.currentLink[ 0 ] === link[ 0 ]
|
2015-01-13 13:18:02 +00:00
|
|
|
) {
|
|
|
|
if ( mw.popups.render.closeTimer ) {
|
|
|
|
mw.popups.render.closeTimer.abort();
|
|
|
|
}
|
2014-04-25 11:43:42 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 ) {
|
|
|
|
mw.popups.render.closePopup();
|
|
|
|
}
|
|
|
|
|
2014-05-19 16:47:15 +00:00
|
|
|
// Ignore if its meant to call a function
|
|
|
|
// TODO: Remove this when adding reference popups
|
2016-05-16 20:32:11 +00:00
|
|
|
if ( linkHref === '#' ) {
|
2014-05-19 16:47:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-06 10:15:24 +00:00
|
|
|
// Disable Navigration Popups
|
|
|
|
mw.popups.disableNavPopup();
|
2015-01-13 13:18:02 +00:00
|
|
|
|
2014-04-25 11:43:42 +00:00
|
|
|
mw.popups.render.currentLink = link;
|
2016-05-16 20:32:11 +00:00
|
|
|
|
|
|
|
logData = {
|
|
|
|
pageTitleHover: mw.popups.getTitle( linkHref ),
|
|
|
|
dwellStartTime: dwellStartTime,
|
|
|
|
linkInteractionToken: linkInteractionToken
|
|
|
|
};
|
|
|
|
|
2014-04-25 11:43:42 +00:00
|
|
|
link.on( 'mouseleave blur', mw.popups.render.leaveInactive );
|
2016-05-16 20:32:11 +00:00
|
|
|
link.off( 'click', logClickAction ).on( 'click', logClickAction );
|
2014-04-25 11:43:42 +00:00
|
|
|
|
|
|
|
if ( mw.popups.render.cache[ link.attr( 'href' ) ] ) {
|
|
|
|
mw.popups.render.openTimer = mw.popups.render.wait( mw.popups.render.POPUP_DELAY )
|
|
|
|
.done( function () {
|
|
|
|
mw.popups.render.openPopup( link, event );
|
|
|
|
} );
|
|
|
|
} else {
|
2015-03-26 14:03:38 +00:00
|
|
|
// Wait for timer before making API queries and showing hovercard
|
2014-04-25 11:43:42 +00:00
|
|
|
mw.popups.render.openTimer = mw.popups.render.wait( mw.popups.render.API_DELAY )
|
|
|
|
.done( function () {
|
2015-08-18 05:47:18 +00:00
|
|
|
var cachePopup, key,
|
|
|
|
renderers = mw.popups.render.renderers;
|
|
|
|
|
|
|
|
// Check run the matcher method of all renderers to find the right one
|
|
|
|
for ( key in renderers ) {
|
|
|
|
if ( renderers.hasOwnProperty( key ) && key !== 'article' ) {
|
|
|
|
if ( !!renderers[ key ].matcher( link.attr( 'href' ) ) ) {
|
2016-05-16 20:32:11 +00:00
|
|
|
cachePopup = renderers[ key ].init( link, $.extend( {}, logData ) );
|
2015-08-18 05:47:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use the article renderer if nothing else matches
|
|
|
|
if ( cachePopup === undefined ) {
|
2016-05-16 20:32:11 +00:00
|
|
|
cachePopup = mw.popups.render.renderers.article.init( link, $.extend( {}, logData ) );
|
2015-08-18 05:47:18 +00:00
|
|
|
}
|
2015-03-26 14:03:38 +00:00
|
|
|
|
2014-04-25 11:43:42 +00:00
|
|
|
mw.popups.render.openTimer = mw.popups.render.wait( mw.popups.render.POPUP_DELAY - mw.popups.render.API_DELAY );
|
|
|
|
|
|
|
|
$.when( mw.popups.render.openTimer, cachePopup ).done( function () {
|
|
|
|
mw.popups.render.openPopup( link, event );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves the popup from the cache, uses its offset function
|
|
|
|
* applied classes and calls the process function.
|
|
|
|
* Takes care of event logging and attaching other events.
|
|
|
|
*
|
|
|
|
* @method openPopup
|
|
|
|
* @param {Object} link
|
|
|
|
* @param {Object} event
|
|
|
|
*/
|
|
|
|
mw.popups.render.openPopup = function ( link, event ) {
|
|
|
|
var
|
|
|
|
cache = mw.popups.render.cache [ link.attr( 'href' ) ],
|
|
|
|
popup = cache.popup,
|
|
|
|
offset = cache.getOffset( link, event ),
|
|
|
|
classes = cache.getClasses( link );
|
|
|
|
|
|
|
|
mw.popups.$popup
|
|
|
|
.html( '' )
|
|
|
|
.attr( 'class', 'mwe-popups' )
|
|
|
|
.addClass( classes.join( ' ' ) )
|
|
|
|
.css( offset )
|
2016-05-12 23:41:04 +00:00
|
|
|
// Clone the element to avoid manipulating the cached object accidentally (see T68496)
|
|
|
|
.append( popup.clone() )
|
2014-04-25 11:43:42 +00:00
|
|
|
.show()
|
|
|
|
.attr( 'aria-hidden', 'false' )
|
|
|
|
.on( 'mouseleave', mw.popups.render.leaveActive )
|
|
|
|
.on( 'mouseenter', function () {
|
|
|
|
if ( mw.popups.render.closeTimer ) {
|
|
|
|
mw.popups.render.closeTimer.abort();
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
// Hack to 'refresh' the SVG and thus display it
|
|
|
|
// Elements get added to the DOM and not to the screen because of different namespaces of HTML and SVG
|
|
|
|
// More information and workarounds here - http://stackoverflow.com/a/13654655/366138
|
|
|
|
mw.popups.$popup.html( mw.popups.$popup.html() );
|
|
|
|
|
|
|
|
// Event logging
|
2016-05-16 20:32:11 +00:00
|
|
|
$.extend( logData, {
|
2016-05-12 22:37:19 +00:00
|
|
|
pageTitleHover: cache.settings.title,
|
2016-05-16 20:32:11 +00:00
|
|
|
namespaceIdHover: cache.settings.namespace,
|
|
|
|
perceivedWait: Math.round( mw.now() - logData.dwellStartTime )
|
|
|
|
} );
|
|
|
|
|
|
|
|
cache.process( link, $.extend( {}, logData ) );
|
|
|
|
|
2016-05-12 22:37:19 +00:00
|
|
|
mw.popups.$popup.find( 'a.mwe-popups-extract, a.mwe-popups-discreet' ).click( mw.popups.render.clickHandler );
|
2014-04-25 11:43:42 +00:00
|
|
|
|
|
|
|
link
|
|
|
|
.off( 'mouseleave blur', mw.popups.render.leaveInactive )
|
|
|
|
.on( 'mouseleave blur', mw.popups.render.leaveActive );
|
|
|
|
|
|
|
|
$( document ).on( 'keydown', mw.popups.render.closeOnEsc );
|
2016-05-16 20:32:11 +00:00
|
|
|
|
|
|
|
mw.popups.incrementPreviewCount();
|
2014-04-25 11:43:42 +00:00
|
|
|
};
|
|
|
|
|
2015-03-19 11:04:29 +00:00
|
|
|
/**
|
|
|
|
* Click handler for the hovercard
|
|
|
|
*
|
|
|
|
* @method clickHandler
|
|
|
|
* @param {Object} event
|
|
|
|
*/
|
|
|
|
mw.popups.render.clickHandler = function ( event ) {
|
2016-05-16 20:32:11 +00:00
|
|
|
var action = mw.popups.getAction( event );
|
|
|
|
|
|
|
|
logClickAction( event );
|
2016-05-12 22:37:19 +00:00
|
|
|
|
|
|
|
if ( action === 'opened in same tab' ) {
|
|
|
|
window.location.href = mw.popups.render.currentLink.attr( 'href' );
|
2015-03-19 11:04:29 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-25 11:43:42 +00:00
|
|
|
/**
|
|
|
|
* Removes the hover class from the link and unbinds events
|
|
|
|
* Hides the popup, clears timers and sets it and the resets the renderer
|
|
|
|
*
|
|
|
|
* @method closePopup
|
|
|
|
*/
|
|
|
|
mw.popups.render.closePopup = function () {
|
2016-05-16 20:32:11 +00:00
|
|
|
var fadeInClass, fadeOutClass;
|
2015-08-26 10:15:16 +00:00
|
|
|
|
2015-01-13 13:18:02 +00:00
|
|
|
if ( mw.popups.render.currentLink === undefined ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-04-25 11:43:42 +00:00
|
|
|
$( mw.popups.render.currentLink ).off( 'mouseleave blur', mw.popups.render.leaveActive );
|
|
|
|
|
2014-05-27 12:11:52 +00:00
|
|
|
fadeInClass = ( mw.popups.$popup.hasClass( 'mwe-popups-fade-in-up' ) ) ?
|
|
|
|
'mwe-popups-fade-in-up' :
|
|
|
|
'mwe-popups-fade-in-down';
|
|
|
|
|
|
|
|
fadeOutClass = ( fadeInClass === 'mwe-popups-fade-in-up' ) ?
|
|
|
|
'mwe-popups-fade-out-down' :
|
|
|
|
'mwe-popups-fade-out-up';
|
|
|
|
|
2014-04-25 11:43:42 +00:00
|
|
|
mw.popups.$popup
|
2016-01-08 04:57:00 +00:00
|
|
|
.off( 'mouseleave', mw.popups.render.leaveActive )
|
2014-05-27 12:11:52 +00:00
|
|
|
.removeClass( fadeInClass )
|
|
|
|
.addClass( fadeOutClass );
|
2014-04-25 11:43:42 +00:00
|
|
|
|
|
|
|
mw.popups.render.wait( 150 ).done( function () {
|
2014-05-27 12:11:52 +00:00
|
|
|
if ( mw.popups.$popup.hasClass( fadeOutClass ) ) {
|
2014-04-25 11:43:42 +00:00
|
|
|
mw.popups.$popup
|
|
|
|
.attr( 'aria-hidden', 'true' )
|
|
|
|
.hide()
|
2014-05-27 11:28:10 +00:00
|
|
|
.removeClass( 'mwe-popups-fade-out-down' );
|
2014-04-25 11:43:42 +00:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
if ( mw.popups.render.closeTimer ) {
|
|
|
|
mw.popups.render.closeTimer.abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
$( document ).off( 'keydown', mw.popups.render.closeOnEsc );
|
|
|
|
mw.popups.render.reset();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a promise corresponding to a `setTimeout()` call. Call `.abort()` on the return value
|
|
|
|
* to perform the equivalent of `clearTimeout()`
|
|
|
|
*
|
|
|
|
* @method wait
|
2015-08-26 10:15:16 +00:00
|
|
|
* @param {number} ms Milliseconds to wait
|
2014-04-25 11:43:42 +00:00
|
|
|
* @return {jQuery.Promise}
|
|
|
|
*/
|
|
|
|
mw.popups.render.wait = function ( ms ) {
|
|
|
|
var deferred, promise, timeout;
|
|
|
|
|
|
|
|
deferred = $.Deferred();
|
|
|
|
|
|
|
|
timeout = setTimeout( function () {
|
|
|
|
deferred.resolve();
|
|
|
|
}, ms );
|
|
|
|
|
|
|
|
promise = deferred.promise( { abort: function () {
|
|
|
|
clearTimeout( timeout );
|
|
|
|
deferred.reject();
|
|
|
|
} } );
|
|
|
|
|
|
|
|
return promise;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Use escape to close popup
|
|
|
|
*
|
|
|
|
* @method closeOnEsc
|
|
|
|
*/
|
|
|
|
mw.popups.render.closeOnEsc = function ( event ) {
|
|
|
|
if ( event.keyCode === 27 ) {
|
|
|
|
mw.popups.render.closePopup();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Closes the box after a delay
|
|
|
|
* Delay to give enough time for the user to move the pointer from
|
|
|
|
* the link to the popup box. Also avoids closing the popup by accident
|
|
|
|
*
|
|
|
|
* @method leaveActive
|
|
|
|
*/
|
|
|
|
mw.popups.render.leaveActive = function () {
|
|
|
|
mw.popups.render.closeTimer = mw.popups.render.wait( mw.popups.render.POPUP_CLOSE_DELAY ).done( function () {
|
2016-05-16 20:32:11 +00:00
|
|
|
mw.track( 'ext.popups.schemaPopups', $.extend( {}, logData, {
|
|
|
|
action: 'dismissed',
|
|
|
|
totalInteractionTime: Math.round( mw.now() - logData.dwellStartTime )
|
|
|
|
} ) );
|
|
|
|
|
2014-04-25 11:43:42 +00:00
|
|
|
mw.popups.render.closePopup();
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unbinds events on the anchor tag and aborts AJAX request.
|
|
|
|
*
|
|
|
|
* @method leaveInactive
|
|
|
|
*/
|
|
|
|
mw.popups.render.leaveInactive = function () {
|
2016-05-16 20:32:11 +00:00
|
|
|
if ( logData.dwellStartTime &&
|
|
|
|
logData.linkInteractionToken &&
|
|
|
|
mw.now() - logData.dwellStartTime >= 250
|
|
|
|
) {
|
|
|
|
mw.track( 'ext.popups.schemaPopups', $.extend( {}, logData, {
|
|
|
|
action: 'dwelledButAbandoned',
|
|
|
|
totalInteractionTime: Math.round( mw.now() - logData.dwellStartTime )
|
|
|
|
} ) );
|
|
|
|
}
|
|
|
|
// TODO: should `blur` also be here?
|
2014-04-25 11:43:42 +00:00
|
|
|
$( mw.popups.render.currentLink ).off( 'mouseleave', mw.popups.render.leaveInactive );
|
|
|
|
if ( mw.popups.render.openTimer ) {
|
|
|
|
mw.popups.render.openTimer.abort();
|
|
|
|
}
|
|
|
|
if ( mw.popups.render.currentRequest ) {
|
|
|
|
mw.popups.render.currentRequest.abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
mw.popups.render.reset();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resets the renderer
|
|
|
|
*
|
|
|
|
* @method reset
|
|
|
|
*/
|
|
|
|
mw.popups.render.reset = function () {
|
2016-05-16 20:32:11 +00:00
|
|
|
logData = {};
|
2014-04-25 11:43:42 +00:00
|
|
|
mw.popups.render.currentLink = undefined;
|
|
|
|
mw.popups.render.currentRequest = undefined;
|
|
|
|
mw.popups.render.openTimer = undefined;
|
|
|
|
mw.popups.render.closeTimer = undefined;
|
|
|
|
};
|
|
|
|
|
2015-08-26 10:15:16 +00:00
|
|
|
} )( jQuery, mediaWiki );
|