2014-02-06 10:49:28 +00:00
|
|
|
/* Code adapted from Yair Rand's NavPopupsRestyled.js
|
|
|
|
* https://en.wikipedia.org/wiki/User:Yair_rand/NavPopupsRestyled.js
|
|
|
|
*/
|
|
|
|
|
2014-04-02 09:37:13 +00:00
|
|
|
( function ( $, mw ) {
|
|
|
|
$( document ).ready( function () {
|
2014-02-06 10:49:28 +00:00
|
|
|
|
|
|
|
var closeTimer, // The timer use to delay `closeBox`
|
|
|
|
openTimer, // The timer used to delay sending the API request/opening the popup form cache
|
2014-04-03 21:54:56 +00:00
|
|
|
scrolled = false, // true if user scrolled the page but haven't moved mouse cursor
|
2014-02-11 09:48:00 +00:00
|
|
|
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
|
2014-02-06 10:49:28 +00:00
|
|
|
currentLink, // DOM element of the current anchor tag
|
|
|
|
cache = {},
|
|
|
|
curRequest, // Current API request
|
2014-04-09 11:57:29 +00:00
|
|
|
supportsSVG = document.implementation.hasFeature( 'http://www.w3.org/TR/SVG11/feature#Image', '1.1' ),
|
2014-02-06 10:49:28 +00:00
|
|
|
api = new mw.Api(),
|
2014-02-24 05:09:03 +00:00
|
|
|
SIZES = {
|
2014-02-24 08:12:21 +00:00
|
|
|
portraitImage: {
|
|
|
|
h: 250, // Exact height
|
2014-02-28 04:06:30 +00:00
|
|
|
w: 203 // Max width
|
2014-02-24 08:12:21 +00:00
|
|
|
},
|
2014-02-24 05:09:03 +00:00
|
|
|
landscapeImage: {
|
|
|
|
h: 200, // Max height
|
|
|
|
w: 300 // Exact Width
|
2014-02-24 08:08:54 +00:00
|
|
|
},
|
|
|
|
landscapePopupWidth: 450, // Exact width of a landscape popup
|
|
|
|
portraitPopupWidth: 300 // Exact width of a portrait popup
|
2014-02-24 05:09:03 +00:00
|
|
|
},
|
2014-02-18 13:28:12 +00:00
|
|
|
$svg, $box; // defined at the end of the file
|
2014-02-06 10:49:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @method sendRequest
|
|
|
|
* Send an API request, create DOM elements and
|
|
|
|
* put them in the cache. Call `createBox`.
|
|
|
|
* @param {String} href
|
|
|
|
* @param {String} title
|
|
|
|
* @param {Object} $el
|
|
|
|
*/
|
2014-04-02 09:37:13 +00:00
|
|
|
function sendRequest( href, title, $el ) {
|
2014-02-06 10:49:28 +00:00
|
|
|
|
|
|
|
curRequest = api.get( {
|
|
|
|
action: 'query',
|
|
|
|
prop: 'extracts|pageimages|revisions|info',
|
|
|
|
redirects: 'true',
|
|
|
|
exintro: 'true',
|
|
|
|
exsentences: 2,
|
2014-04-13 04:58:57 +00:00
|
|
|
// there is an added geometric limit on .mwe-popups-extract
|
|
|
|
// so that text does not overflow from the card
|
2014-02-06 10:49:28 +00:00
|
|
|
explaintext: 'true',
|
|
|
|
piprop: 'thumbnail',
|
|
|
|
pithumbsize: 300,
|
|
|
|
rvprop: 'timestamp',
|
|
|
|
inprop: 'watched',
|
|
|
|
indexpageids: true,
|
|
|
|
titles: decodeURI( title )
|
|
|
|
} );
|
|
|
|
|
|
|
|
curRequest.done( function ( re ) {
|
|
|
|
curRequest = undefined;
|
|
|
|
|
2014-02-17 10:48:31 +00:00
|
|
|
var $a,
|
2014-04-02 09:37:13 +00:00
|
|
|
page = re.query.pages[ re.query.pageids[ 0 ] ],
|
|
|
|
$contentbox = $( '<div>' )
|
|
|
|
.addClass( 'mwe-popups-extract' )
|
|
|
|
.text( page.extract ),
|
2014-02-06 10:49:28 +00:00
|
|
|
thumbnail = page.thumbnail,
|
|
|
|
tall = thumbnail && thumbnail.height > thumbnail.width,
|
2014-02-17 10:48:31 +00:00
|
|
|
$thumbnail = createThumbnail( thumbnail, tall ),
|
2014-02-06 10:49:28 +00:00
|
|
|
timestamp = new Date( page.revisions[ 0 ].timestamp ),
|
|
|
|
timediff = new Date() - timestamp,
|
|
|
|
oneDay = 1000 * 60 * 60 * 24,
|
|
|
|
timestampclass = ( timediff < oneDay ) ?
|
|
|
|
'mwe-popups-timestamp-recent' :
|
|
|
|
'mwe-popups-timestamp-older',
|
|
|
|
$timestamp = $( '<div>' )
|
|
|
|
.addClass( timestampclass )
|
|
|
|
.append(
|
2014-04-02 09:37:13 +00:00
|
|
|
$( '<span>' ).text( mw.message( 'popups-last-edited',
|
|
|
|
moment( timestamp ).fromNow() ).text() )
|
|
|
|
);
|
2014-02-06 10:49:28 +00:00
|
|
|
|
2014-02-11 09:48:00 +00:00
|
|
|
$a = $( '<a>' )
|
2014-04-02 09:37:13 +00:00
|
|
|
.append( $thumbnail, $contentbox, $timestamp )
|
2014-02-11 09:48:00 +00:00
|
|
|
.attr( 'href', href )
|
|
|
|
.on( 'click', logClick );
|
|
|
|
|
2014-04-02 09:37:13 +00:00
|
|
|
cache[ href ] = {
|
|
|
|
box: $a,
|
|
|
|
thumbnail: thumbnail,
|
|
|
|
tall: tall
|
|
|
|
};
|
2014-02-06 10:49:28 +00:00
|
|
|
createBox( href, $el );
|
2014-04-02 09:37:13 +00:00
|
|
|
} );
|
2014-02-06 10:49:28 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-03-31 11:26:50 +00:00
|
|
|
/**
|
|
|
|
* @method createSVGTag
|
|
|
|
* Use createElementNS to create the svg:image tag as jQuery
|
|
|
|
* uses createElement instead. Some browsers map the `image` tag
|
|
|
|
* to `img` tag, thus an `svg:image` is required.
|
|
|
|
* @param {String} tag
|
|
|
|
* @return {Object}
|
|
|
|
*/
|
|
|
|
function createSVGTag( tag ) {
|
|
|
|
return document.createElementNS( 'http://www.w3.org/2000/svg', tag );
|
|
|
|
}
|
|
|
|
|
2014-02-17 10:48:31 +00:00
|
|
|
/**
|
|
|
|
* @method createThumbnail
|
|
|
|
* Returns a thumbnail object based on the ratio of the image
|
|
|
|
* @param {Object} thumbnail
|
|
|
|
* @param {boolean} tall
|
|
|
|
* @return {Object} jQuery DOM element of the thumbnail
|
|
|
|
*/
|
2014-04-02 09:37:13 +00:00
|
|
|
function createThumbnail( thumbnail, tall ) {
|
2014-02-17 10:48:31 +00:00
|
|
|
if ( !thumbnail ) {
|
|
|
|
return $( '<span>' );
|
|
|
|
}
|
|
|
|
|
2014-04-09 14:05:10 +00:00
|
|
|
var $thumbnailSVGImage, $thumbnail;
|
2014-02-17 10:57:05 +00:00
|
|
|
|
|
|
|
if ( tall ) {
|
2014-04-09 11:57:29 +00:00
|
|
|
if ( supportsSVG ) {
|
2014-04-09 14:05:10 +00:00
|
|
|
$thumbnailSVGImage = $( createSVGTag( 'image' ) );
|
|
|
|
$thumbnailSVGImage
|
2014-02-24 08:12:21 +00:00
|
|
|
.addClass( 'mwe-popups-is-not-tall' )
|
|
|
|
.attr( {
|
|
|
|
'xlink:href': thumbnail.source,
|
2014-02-28 04:06:30 +00:00
|
|
|
x: ( thumbnail.width > SIZES.portraitImage.w ) ?
|
|
|
|
( ( thumbnail.width - SIZES.portraitImage.w ) / -2 ) :
|
|
|
|
( SIZES.portraitImage.w - thumbnail.width ),
|
2014-04-09 12:19:19 +00:00
|
|
|
y: ( thumbnail.height > SIZES.portraitImage.h) ?
|
|
|
|
( ( thumbnail.height - SIZES.portraitImage.h ) / -2 ) :
|
|
|
|
0,
|
2014-02-24 08:12:21 +00:00
|
|
|
width: thumbnail.width,
|
|
|
|
height: thumbnail.height
|
|
|
|
} );
|
|
|
|
|
|
|
|
$thumbnail = $( '<svg>' )
|
|
|
|
.attr( {
|
|
|
|
xmlns: 'http://www.w3.org/2000/svg',
|
2014-02-28 04:06:30 +00:00
|
|
|
viewBox: '0 0 ' + SIZES.portraitImage.w + ' ' + SIZES.portraitImage.h,
|
|
|
|
width: SIZES.portraitImage.w,
|
2014-02-24 08:12:21 +00:00
|
|
|
height: SIZES.portraitImage.h
|
|
|
|
} )
|
2014-04-09 14:05:10 +00:00
|
|
|
.append( $thumbnailSVGImage );
|
2014-02-24 08:12:21 +00:00
|
|
|
} else {
|
|
|
|
$thumbnail = $( '<div>' )
|
|
|
|
.addClass( 'mwe-popups-is-tall' )
|
2014-04-02 09:37:13 +00:00
|
|
|
.css( 'background-image', 'url(' + thumbnail.source + ')' );
|
2014-02-24 08:12:21 +00:00
|
|
|
}
|
2014-02-17 10:57:05 +00:00
|
|
|
} else {
|
2014-04-09 11:57:29 +00:00
|
|
|
if ( supportsSVG ) {
|
2014-04-09 14:05:10 +00:00
|
|
|
$thumbnailSVGImage = $( createSVGTag( 'image' ) );
|
|
|
|
$thumbnailSVGImage
|
2014-02-18 13:28:12 +00:00
|
|
|
.addClass( 'mwe-popups-is-not-tall' )
|
|
|
|
.attr( {
|
|
|
|
'xlink:href': thumbnail.source,
|
|
|
|
'clip-path': 'url(#mwe-popups-mask)',
|
|
|
|
x: 0,
|
2014-04-09 12:19:19 +00:00
|
|
|
y: ( thumbnail.height > SIZES.landscapeImage.h) ?
|
|
|
|
( ( thumbnail.height - SIZES.landscapeImage.h ) / -2 ) :
|
|
|
|
0,
|
2014-02-18 13:28:12 +00:00
|
|
|
width: thumbnail.width,
|
|
|
|
height: thumbnail.height
|
|
|
|
} );
|
|
|
|
|
|
|
|
$thumbnail = $( '<svg>' )
|
|
|
|
.attr( {
|
|
|
|
xmlns: 'http://www.w3.org/2000/svg',
|
2014-04-09 12:19:19 +00:00
|
|
|
viewBox: '0 0 ' + SIZES.landscapeImage.w + ' ' + ( thumbnail.height > SIZES.landscapeImage.h ) ?
|
|
|
|
SIZES.landscapeImage.h :
|
|
|
|
thumbnail.height,
|
2014-02-24 08:12:21 +00:00
|
|
|
width: SIZES.landscapeImage.w + 3,
|
2014-04-09 12:19:19 +00:00
|
|
|
height: ( thumbnail.height > SIZES.landscapeImage.h ) ?
|
|
|
|
SIZES.landscapeImage.h :
|
|
|
|
thumbnail.height
|
2014-02-18 13:28:12 +00:00
|
|
|
} )
|
2014-04-09 14:05:10 +00:00
|
|
|
.append( $thumbnailSVGImage );
|
2014-02-18 13:28:12 +00:00
|
|
|
} else {
|
2014-02-24 05:09:03 +00:00
|
|
|
$thumbnail = $( '<div>' )
|
|
|
|
.addClass( 'mwe-popups-is-not-tall' )
|
|
|
|
.css( 'background-image', 'url(' + thumbnail.source + ')' );
|
2014-02-18 13:28:12 +00:00
|
|
|
}
|
2014-02-17 10:57:05 +00:00
|
|
|
}
|
2014-02-17 10:48:31 +00:00
|
|
|
|
|
|
|
return $thumbnail;
|
|
|
|
}
|
|
|
|
|
2014-02-06 10:49:28 +00:00
|
|
|
/**
|
|
|
|
* @method createBox
|
|
|
|
* Looks into the `cache` and uses the href to render the popup
|
|
|
|
* and offsets it to the link. Rebinds the `mouseleave` event
|
|
|
|
* for the anchor element to `leaveActive`.
|
|
|
|
* @param {String} href
|
|
|
|
* @param {Object} $el
|
|
|
|
*/
|
2014-04-02 09:37:13 +00:00
|
|
|
function createBox( href, $el ) {
|
2014-02-06 10:49:28 +00:00
|
|
|
var bar = cache[ href ],
|
2014-02-18 13:28:12 +00:00
|
|
|
offsetTop = $el.offset().top + $el.height() + 9,
|
2014-02-24 08:12:21 +00:00
|
|
|
offsetLeft = $el.offset().left,
|
|
|
|
flipped = false;
|
2014-02-11 09:48:00 +00:00
|
|
|
|
|
|
|
elTime = mw.now();
|
|
|
|
elAction = 'dismissed';
|
|
|
|
|
2014-02-18 13:28:12 +00:00
|
|
|
if ( bar.thumbnail === undefined ) {
|
|
|
|
bar.thumbnail = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( bar.tall === undefined ) {
|
|
|
|
bar.tall = false;
|
|
|
|
}
|
|
|
|
|
2014-02-24 08:08:54 +00:00
|
|
|
if ( offsetLeft > ( $( window ).width() / 2 ) ) {
|
|
|
|
offsetLeft = offsetLeft + $el.width();
|
|
|
|
offsetLeft -= ( !bar.tall ) ? SIZES.portraitPopupWidth : SIZES.landscapePopupWidth;
|
2014-02-24 08:12:21 +00:00
|
|
|
flipped = true;
|
2014-02-24 08:08:54 +00:00
|
|
|
}
|
|
|
|
|
2014-02-06 10:49:28 +00:00
|
|
|
$box
|
|
|
|
.children()
|
|
|
|
.detach()
|
2014-02-18 13:28:12 +00:00
|
|
|
// avoid .empty() to keep event handlers
|
|
|
|
.end()
|
2014-02-24 08:12:21 +00:00
|
|
|
.removeClass( 'mwe-popups-is-tall mwe-popups-is-not-tall mwe-popups-no-image-tri mwe-popups-image-tri flipped' )
|
|
|
|
.toggleClass( 'flipped', flipped )
|
2014-02-18 13:28:12 +00:00
|
|
|
// Add border triangle if there is no image or its landscape
|
|
|
|
.toggleClass( 'mwe-popups-no-image-tri', ( !bar.thumbnail || bar.tall ) )
|
|
|
|
// If theres an image and the popup is portrait do the SVG stuff
|
|
|
|
.toggleClass( 'mwe-popups-image-tri', ( bar.thumbnail && !bar.tall ) )
|
2014-02-06 10:49:28 +00:00
|
|
|
.addClass( bar.tall ? 'mwe-popups-is-tall' : 'mwe-popups-is-not-tall' )
|
2014-04-02 09:37:13 +00:00
|
|
|
.css( {
|
2014-02-06 10:49:28 +00:00
|
|
|
top: offsetTop,
|
2014-02-13 01:58:31 +00:00
|
|
|
left: offsetLeft
|
2014-04-02 09:37:13 +00:00
|
|
|
} )
|
2014-02-06 10:49:28 +00:00
|
|
|
.append( bar.box )
|
2014-03-23 03:58:52 +00:00
|
|
|
.attr( 'aria-hidden', 'false' )
|
2014-02-06 11:11:29 +00:00
|
|
|
.show()
|
|
|
|
.removeClass( 'mwe-popups-fade-out mwe-popups-fade-in' )
|
2014-02-18 13:28:12 +00:00
|
|
|
.addClass( 'mwe-popups-fade-in' )
|
|
|
|
// 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
|
|
|
|
.html( $box.html() );
|
2014-02-24 08:12:21 +00:00
|
|
|
|
2014-04-02 09:37:13 +00:00
|
|
|
if ( flipped && bar.thumbnail ) {
|
2014-02-24 08:12:21 +00:00
|
|
|
if ( !bar.tall ) {
|
2014-04-02 09:37:13 +00:00
|
|
|
$box.find( 'image' )[ 0 ].setAttribute( 'clip-path', 'url(#mwe-popups-mask-flip)' );
|
2014-02-24 08:12:21 +00:00
|
|
|
} else {
|
|
|
|
$box
|
|
|
|
.removeClass( 'mwe-popups-no-image-tri' )
|
2014-04-02 09:37:13 +00:00
|
|
|
.find( 'image' )[ 0 ].setAttribute( 'clip-path', 'url(#mwe-popups-landscape-mask)' );
|
2014-02-24 08:12:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-06 10:49:28 +00:00
|
|
|
$el
|
2014-03-23 02:32:37 +00:00
|
|
|
.off( 'mouseleave blur', leaveInactive )
|
|
|
|
.on( 'mouseleave blur', leaveActive );
|
2014-03-23 04:23:34 +00:00
|
|
|
|
|
|
|
$( document ).on( 'keydown', closeOnEsc );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @method closeOnEsc
|
|
|
|
* Use escape to close popup
|
|
|
|
*/
|
|
|
|
function closeOnEsc( e ) {
|
|
|
|
if ( e.keyCode === 27 ) {
|
|
|
|
closeBox();
|
|
|
|
}
|
2014-02-06 10:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @method leaveActive
|
2014-02-06 11:11:29 +00:00
|
|
|
* Closes the box after a delay of 100ms
|
2014-02-06 10:49:28 +00:00
|
|
|
* Delay to give enough time for the use to move the pointer from
|
|
|
|
* the link to the popup box. Also avoids closing the popup by accident.
|
|
|
|
*/
|
2014-04-02 09:37:13 +00:00
|
|
|
function leaveActive() {
|
2014-02-06 11:11:29 +00:00
|
|
|
closeTimer = setTimeout( closeBox, 100 );
|
2014-02-06 10:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @method leaveInactive
|
|
|
|
* Unbinds events on the anchor tag and aborts AJAX request.
|
|
|
|
*/
|
2014-04-02 09:37:13 +00:00
|
|
|
function leaveInactive() {
|
2014-02-06 10:49:28 +00:00
|
|
|
$( currentLink ).off( 'mouseleave', leaveInactive );
|
|
|
|
clearTimeout( openTimer );
|
|
|
|
if ( curRequest ) {
|
|
|
|
curRequest.abort();
|
|
|
|
}
|
|
|
|
currentLink = openTimer = curRequest = undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @method closeBox
|
|
|
|
* Removes the hover class from the link and unbinds events
|
|
|
|
* Hides the popup, clears timers and sets it and the
|
|
|
|
* `currentLink` to undefined.
|
|
|
|
*/
|
2014-04-02 09:37:13 +00:00
|
|
|
function closeBox() {
|
2014-02-11 09:48:00 +00:00
|
|
|
elDuration = mw.now() - elTime;
|
|
|
|
|
2014-02-06 10:49:28 +00:00
|
|
|
$( currentLink ).removeClass( 'mwe-popups-anchor-hover' ).off( 'mouseleave', leaveActive );
|
2014-02-06 11:11:29 +00:00
|
|
|
|
|
|
|
$box
|
|
|
|
.removeClass( 'mwe-popups-fade-out mwe-popups-fade-in' )
|
|
|
|
.addClass( 'mwe-popups-fade-out' )
|
|
|
|
.on( 'webkitAnimationEnd oanimationend msAnimationEnd animationend', function () {
|
|
|
|
if ( $( this ).hasClass( 'mwe-popups-fade-out' ) ) {
|
|
|
|
$( this )
|
2014-04-02 09:37:13 +00:00
|
|
|
.off( 'webkitAnimationEnd oanimationend msAnimationEnd animationend' )
|
|
|
|
.removeClass( 'mwe-popups-fade-out' )
|
|
|
|
.attr( 'aria-hidden', 'true' )
|
|
|
|
.hide();
|
2014-02-06 11:11:29 +00:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2014-04-02 09:37:13 +00:00
|
|
|
if ( closeTimer ) {
|
2014-02-06 10:49:28 +00:00
|
|
|
clearTimeout( closeTimer );
|
|
|
|
}
|
2014-02-11 09:48:00 +00:00
|
|
|
|
2014-03-23 04:23:34 +00:00
|
|
|
$( document ).off( 'keydown', closeOnEsc );
|
|
|
|
|
2014-02-11 09:48:00 +00:00
|
|
|
logEvent();
|
2014-02-06 10:49:28 +00:00
|
|
|
currentLink = closeTimer = undefined;
|
|
|
|
}
|
|
|
|
|
2014-02-11 09:48:00 +00:00
|
|
|
/**
|
|
|
|
* @method logClick
|
|
|
|
* Logs different actions such as meta and shift click on the popup.
|
|
|
|
* Is bound to the `click` event.
|
|
|
|
* @param {Object} e
|
|
|
|
*/
|
2014-04-02 09:37:13 +00:00
|
|
|
function logClick( e ) {
|
|
|
|
if ( e.which === 2 ) { // middle click
|
2014-02-11 09:48:00 +00:00
|
|
|
elAction = 'opened in new tab';
|
2014-04-02 09:37:13 +00:00
|
|
|
} else if ( e.which === 1 ) {
|
2014-02-11 09:48:00 +00:00
|
|
|
if ( e.ctrlKey || e.metaKey ) {
|
|
|
|
elAction = 'opened in new tab';
|
2014-04-02 09:37:13 +00:00
|
|
|
} else if ( e.shiftKey ) {
|
2014-02-11 09:48:00 +00:00
|
|
|
elAction = 'opened in new window';
|
|
|
|
} else {
|
|
|
|
elAction = 'opened in same tab';
|
|
|
|
elDuration = mw.now() - elTime;
|
|
|
|
logEvent( this.href );
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @method logEvent
|
|
|
|
* Logs the Popup event as defined in the following schema -
|
|
|
|
* https://meta.wikimedia.org/wiki/Schema:Popups
|
|
|
|
* If `href` is passed it redirects to that location after the event is logged.
|
|
|
|
* @param {string} href
|
|
|
|
*/
|
2014-04-02 09:37:13 +00:00
|
|
|
function logEvent( href ) {
|
2014-03-25 06:14:24 +00:00
|
|
|
if ( typeof mw.eventLog !== 'function' ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-02-11 09:48:00 +00:00
|
|
|
var dfd = $.Deferred(),
|
|
|
|
event = {
|
2014-04-02 09:37:13 +00:00
|
|
|
duration: Math.round( elDuration ),
|
|
|
|
action: elAction
|
2014-02-11 09:48:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if ( elSessionId !== null ) {
|
|
|
|
event.sessionId = elSessionId;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( href ) {
|
|
|
|
dfd.always( function () {
|
|
|
|
location.href = href;
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
mw.eventLog.logEvent( 'Popups', event ).then( dfd.resolve, dfd.reject );
|
|
|
|
setTimeout( dfd.reject, 1000 );
|
|
|
|
elTime = elDuration = elAction = undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @method getSessionId
|
|
|
|
* Generates a unique sessionId or pulls an existing one from localStorage
|
|
|
|
* @return {string} sessionId
|
|
|
|
*/
|
2014-04-02 09:37:13 +00:00
|
|
|
function getSessionId() {
|
2014-02-11 09:48:00 +00:00
|
|
|
var sessionId = null;
|
|
|
|
try {
|
|
|
|
sessionId = localStorage.getItem( 'popupsSessionId' );
|
|
|
|
if ( sessionId === null ) {
|
|
|
|
sessionId = mw.user.generateRandomSessionId();
|
|
|
|
localStorage.setItem( 'popupsSessionId', sessionId );
|
|
|
|
}
|
|
|
|
} catch ( e ) {}
|
|
|
|
return sessionId;
|
|
|
|
}
|
|
|
|
elSessionId = getSessionId();
|
|
|
|
|
2014-04-03 21:54:56 +00:00
|
|
|
// Prevent popups from showing up when the mouse cursor accidentally
|
|
|
|
// ends up hovering a link after scrolling
|
|
|
|
$( window ).on( 'scroll', function () {
|
|
|
|
scrolled = true;
|
|
|
|
} );
|
|
|
|
$( window ).on( 'mousemove', function () {
|
|
|
|
scrolled = false;
|
|
|
|
} );
|
|
|
|
|
2014-02-06 11:15:05 +00:00
|
|
|
// Remove title attribute to remove the default yellow tooltip
|
|
|
|
// Put the title back after the hover
|
|
|
|
$( '#mw-content-text a' )
|
|
|
|
.not( '.extiw' )
|
|
|
|
.not( '.image' )
|
|
|
|
.not( '.new' )
|
|
|
|
.not( '[title=""]' )
|
2014-03-23 02:32:37 +00:00
|
|
|
.on( 'mouseenter focus', function () {
|
|
|
|
$( this )
|
|
|
|
.attr( 'data-original-title', $( this ).attr( 'title' ) )
|
2014-04-02 09:37:13 +00:00
|
|
|
.attr( 'title', '' );
|
2014-03-23 02:32:37 +00:00
|
|
|
} )
|
|
|
|
.on( 'mouseleave blur', function () {
|
|
|
|
$( this )
|
|
|
|
.attr( 'title', $( this ).attr( 'data-original-title' ) )
|
2014-04-02 09:37:13 +00:00
|
|
|
.attr( 'data-original-title', '' );
|
2014-03-23 02:32:37 +00:00
|
|
|
} );
|
2014-02-06 11:15:05 +00:00
|
|
|
|
2014-03-23 02:32:37 +00:00
|
|
|
$( '#mw-content-text a' ).on( 'mouseenter focus', function () {
|
2014-02-06 10:49:28 +00:00
|
|
|
var $this = $( this ),
|
|
|
|
href = $this.attr( 'href' ),
|
2014-02-06 11:15:05 +00:00
|
|
|
title = $this.attr( 'data-original-title' );
|
2014-02-06 10:49:28 +00:00
|
|
|
|
|
|
|
// If a popup for the following link can't be shown
|
2014-04-03 21:54:56 +00:00
|
|
|
if (
|
|
|
|
scrolled ||
|
|
|
|
!title ||
|
2014-02-06 10:49:28 +00:00
|
|
|
$this.hasClass( 'extiw' ) ||
|
|
|
|
$this.hasClass( 'image' ) ||
|
|
|
|
$this.hasClass( 'new' ) ||
|
|
|
|
href.indexOf( '?' ) !== -1 ||
|
|
|
|
href.indexOf( location.origin + location.pathname + '#' ) === 0
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 ( currentLink === this ) {
|
|
|
|
clearTimeout( closeTimer );
|
|
|
|
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 ( currentLink ) {
|
|
|
|
closeBox();
|
|
|
|
}
|
|
|
|
|
|
|
|
currentLink = this;
|
2014-03-23 02:32:37 +00:00
|
|
|
$this.on( 'mouseleave blur', leaveInactive );
|
2014-02-06 10:49:28 +00:00
|
|
|
|
2014-04-02 09:37:13 +00:00
|
|
|
if ( cache[ href ] ) {
|
2014-02-06 10:49:28 +00:00
|
|
|
openTimer = setTimeout( function () {
|
|
|
|
createBox( href, $this );
|
2014-02-12 00:05:57 +00:00
|
|
|
}, 150 );
|
2014-02-06 10:49:28 +00:00
|
|
|
} else {
|
|
|
|
openTimer = setTimeout( function () {
|
|
|
|
sendRequest( href, title, $this );
|
2014-02-12 00:05:57 +00:00
|
|
|
}, 50 ); // sendRequest sooner so that it *hopefully* shows up in 150ms
|
2014-02-06 10:49:28 +00:00
|
|
|
}
|
|
|
|
// Delay to avoid triggering the popup and AJAX requests on accidental
|
|
|
|
// hovers (likes ones during srcolling or moving the pointer elsewhere).
|
|
|
|
|
|
|
|
} );
|
|
|
|
|
|
|
|
// Container for the popup
|
|
|
|
$box = $( '<div>' )
|
2014-03-23 02:37:08 +00:00
|
|
|
.attr( 'role', 'tooltip' )
|
2014-02-06 10:49:28 +00:00
|
|
|
.addClass( 'mwe-popups' )
|
|
|
|
.on( {
|
|
|
|
mouseleave: leaveActive,
|
|
|
|
mouseenter: function () {
|
|
|
|
if ( closeTimer ) {
|
|
|
|
clearTimeout( closeTimer );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} )
|
|
|
|
.appendTo( document.body );
|
2014-02-07 07:35:34 +00:00
|
|
|
|
2014-02-18 13:28:12 +00:00
|
|
|
// SVG for masking and creating the triangle/pokey
|
2014-04-09 11:57:29 +00:00
|
|
|
if (supportsSVG ) {
|
2014-02-18 13:28:12 +00:00
|
|
|
$svg = $( '<div>' )
|
|
|
|
.attr( 'id', 'mwe-popups-svg' )
|
|
|
|
.appendTo( document.body )
|
2014-02-24 08:12:21 +00:00
|
|
|
.html( '<svg width=\"0\" height=\"0\">' +
|
|
|
|
'<defs><clippath id=\"mwe-popups-mask\">' +
|
|
|
|
'<polygon points=\"0 8, 10 8, 18 0, 26 8, 1000 8, 1000 1000, 0 1000\"/></clippath>' +
|
|
|
|
'</defs>' +
|
|
|
|
'<defs><clippath id=\"mwe-popups-mask-flip\">' +
|
|
|
|
'<polygon points=\"0 8, 274 8, 282 0, 290 8, 1000 8, 1000 1000, 0 1000\"/></clippath>' +
|
|
|
|
'</defs>' +
|
|
|
|
'<defs><clippath id=\"mwe-popups-landscape-mask\">' +
|
|
|
|
'<polygon points=\"0 8, 174 8, 182 0, 190 8, 1000 8, 1000 1000, 0 1000\"/></clippath>' +
|
|
|
|
'</defs>' +
|
|
|
|
'</svg>' );
|
2014-02-18 13:28:12 +00:00
|
|
|
}
|
|
|
|
|
2014-02-06 10:49:28 +00:00
|
|
|
} );
|
2014-02-07 07:35:34 +00:00
|
|
|
|
2014-04-02 09:37:13 +00:00
|
|
|
}( jQuery, mediaWiki ) );
|