/* Code adapted from Yair Rand's NavPopupsRestyled.js
* https://en.wikipedia.org/wiki/User:Yair_rand/NavPopupsRestyled.js
*/
( function ( $, mw ) {
var closeTimer, // The timer use to delay `closeBox`
openTimer, // The timer used to delay sending the API request/opening the popup form cache
scrolled = false, // true if user scrolled the page but haven't moved mouse cursor
elTime, // EL: UNIX timestamp of when the popup was rendered
elDuration, // EL: How long was the popup open in milliseconds
elAction, // EL: Was the popup clicked or middle clicked or dismissed
elSessionId, // EL: Get defined after the getSessionId method is created
currentLink, // DOM element of the current anchor tag
cache = {},
curRequest, // Current API request
supportsSVG = document.implementation.hasFeature( 'http://www.w3.org/TR/SVG11/feature#Image', '1.1' ),
api = new mw.Api(),
SIZES = {
portraitImage: {
h: 250, // Exact height
w: 203 // Max width
},
landscapeImage: {
h: 200, // Max height
w: 300 // Exact Width
},
landscapePopupWidth: 450, // Exact width of a landscape popup
portraitPopupWidth: 300 // Exact width of a portrait popup
},
POPUP_DELAY = 150, // Time to wait in ms before showing a popup on hover
POPUP_CLOSE_DELAY = 100, // Time to wait in ms before closing a popup on de-hover
API_DELAY = 50, // Time to wait in ms before starting the API queries on hover, must be <= POPUP_DELAY
$svg, $box; // defined at the end of the file
/**
* Return a promise corresponding to a `setTimeout()` call. Call `.abort()` on the return value
* to perform the equivalent of `clearTimeout()`.
*
* @param {number} ms Milliseconds to wait
* @return {jQuery.Promise}
*/
function timeoutPromise( ms ) {
var deferred, promise, timeout;
deferred = $.Deferred();
timeout = setTimeout( function () {
deferred.resolve();
}, ms );
promise = deferred.promise( { abort: function () {
clearTimeout( timeout );
deferred.reject();
} } );
return promise;
}
/**
* @method sendRequest
* Send an API request, create DOM elements and
* put them in the cache. Returns a promise.
* @param {String} href
* @param {String} title
* @return {jQuery.Promise}
*/
function sendRequest( href, title ) {
var deferred = $.Deferred();
curRequest = api.get( {
action: 'query',
prop: 'extracts|pageimages|revisions|info',
redirects: 'true',
exintro: 'true',
exsentences: 2,
// there is an added geometric limit on .mwe-popups-extract
// so that text does not overflow from the card
explaintext: 'true',
piprop: 'thumbnail',
pithumbsize: 300,
rvprop: 'timestamp',
inprop: 'watched',
indexpageids: true,
titles: title
} );
curRequest.done( function ( re ) {
curRequest = undefined;
if ( re.query.pages[ re.query.pageids[ 0 ] ].extract === '' ){
return false;
}
var $a,
page = re.query.pages[ re.query.pageids[ 0 ] ],
$contentbox = $( '