2014-04-02 09:37:13 +00:00
|
|
|
( function ( $, mw ) {
|
2014-05-19 22:48:32 +00:00
|
|
|
'use strict';
|
2014-04-22 14:16:54 +00:00
|
|
|
|
|
|
|
/**
|
2014-04-25 11:43:42 +00:00
|
|
|
* @class mw.popups
|
|
|
|
* @singleton
|
2014-04-22 14:16:54 +00:00
|
|
|
*/
|
2014-04-25 11:43:42 +00:00
|
|
|
mw.popups = {};
|
2014-04-22 14:16:54 +00:00
|
|
|
|
2014-06-17 11:08:05 +00:00
|
|
|
mw.popups.enabled = $.jStorage.get( 'mwe-popups-enabled' ) !== 'false';
|
|
|
|
|
2014-04-22 14:16:54 +00:00
|
|
|
/**
|
2014-04-25 11:43:42 +00:00
|
|
|
* Checks SVG support on the browser
|
2014-06-26 09:30:18 +00:00
|
|
|
*
|
|
|
|
* Set to false on Internet Explorer because adding SVGs
|
|
|
|
* through JavaScript in IE is failing. Thus, falling back to PNGs
|
|
|
|
*
|
2014-04-25 11:43:42 +00:00
|
|
|
* @property {Boolean} supportsSVG
|
2014-04-22 14:16:54 +00:00
|
|
|
*/
|
2014-06-26 09:30:18 +00:00
|
|
|
mw.popups.supportsSVG = ( $.client.profile().name === 'msie' ) ?
|
|
|
|
false :
|
|
|
|
document.implementation.hasFeature( 'http://www.w3.org/TR/SVG11/feature#Image', '1.1' );
|
2014-02-11 09:48:00 +00:00
|
|
|
|
2014-04-22 14:16:54 +00:00
|
|
|
/**
|
2014-04-25 11:43:42 +00:00
|
|
|
* The API object used for all this extension's requests
|
|
|
|
* @property {Object} api
|
2014-04-22 14:16:54 +00:00
|
|
|
*/
|
2014-04-25 11:43:42 +00:00
|
|
|
mw.popups.api = new mw.Api();
|
2014-04-22 14:16:54 +00:00
|
|
|
|
|
|
|
/**
|
2014-04-25 11:43:42 +00:00
|
|
|
* Whether the page is being scrolled.
|
|
|
|
* @property {Boolean} scrolled
|
2014-04-22 14:16:54 +00:00
|
|
|
*/
|
2014-04-25 11:43:42 +00:00
|
|
|
mw.popups.scrolled = false;
|
2014-04-22 14:16:54 +00:00
|
|
|
|
|
|
|
/**
|
2014-04-25 11:43:42 +00:00
|
|
|
* List of classes of which links are ignored
|
|
|
|
* @property {Array} IGNORE_CLASSES
|
2014-04-22 14:16:54 +00:00
|
|
|
*/
|
2014-04-25 11:43:42 +00:00
|
|
|
mw.popups.IGNORE_CLASSES = [
|
2014-05-19 22:30:29 +00:00
|
|
|
'.extiw',
|
2014-04-25 11:43:42 +00:00
|
|
|
'.image',
|
|
|
|
'.new',
|
2014-06-03 08:13:40 +00:00
|
|
|
'.internal',
|
2014-06-04 04:13:50 +00:00
|
|
|
'.external',
|
2015-02-17 12:45:55 +00:00
|
|
|
'.oo-ui-buttonedElement-button'
|
2014-04-25 11:43:42 +00:00
|
|
|
];
|
2014-04-22 14:16:54 +00:00
|
|
|
|
|
|
|
/**
|
2014-04-25 11:43:42 +00:00
|
|
|
* If SVG is supported, creates the SVG mask used to create the
|
|
|
|
* the triangle pointer on popups with images
|
|
|
|
*
|
|
|
|
* @method createSVGMask
|
2014-04-22 14:16:54 +00:00
|
|
|
*/
|
2014-04-25 11:43:42 +00:00
|
|
|
mw.popups.createSVGMask = function () {
|
|
|
|
if ( !mw.popups.supportsSVG ) {
|
2014-04-22 14:16:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
2014-02-11 09:48:00 +00:00
|
|
|
|
2014-04-25 11:43:42 +00:00
|
|
|
$( '<div>' )
|
|
|
|
.attr( 'id', 'mwe-popups-svg' )
|
|
|
|
.appendTo( document.body )
|
|
|
|
.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>' +
|
|
|
|
'<clippath id="mwe-popups-mask-flip">' +
|
|
|
|
'<polygon points="0 8, 274 8, 282 0, 290 8, 1000 8, 1000 1000, 0 1000"/>' +
|
|
|
|
'</clippath>' +
|
|
|
|
'<clippath id="mwe-popups-landscape-mask">' +
|
|
|
|
'<polygon points="0 8, 174 8, 182 0, 190 8, 1000 8, 1000 1000, 0 1000"/>' +
|
|
|
|
'</clippath>' +
|
2014-04-30 11:13:05 +00:00
|
|
|
'<clippath id="mwe-popups-landscape-mask-flip">' +
|
|
|
|
'<polygon points="0 0, 1000 0, 1000 243, 190 243, 182 250, 174 243, 0 243"/>' +
|
|
|
|
'</clippath>' +
|
2014-04-25 11:43:42 +00:00
|
|
|
'</defs>' +
|
|
|
|
'</svg>'
|
|
|
|
);
|
|
|
|
return true;
|
|
|
|
};
|
2014-04-22 14:16:54 +00:00
|
|
|
|
|
|
|
/**
|
2014-04-25 11:43:42 +00:00
|
|
|
* Create the element that holds the popups
|
|
|
|
*
|
|
|
|
* @method createPopupElement
|
2014-04-22 14:16:54 +00:00
|
|
|
*/
|
2014-04-25 11:43:42 +00:00
|
|
|
mw.popups.createPopupElement = function () {
|
|
|
|
mw.popups.$popup = $( '<div>' )
|
2014-05-19 22:48:32 +00:00
|
|
|
.attr( {
|
|
|
|
'class': 'mwe-popups',
|
|
|
|
'role': 'tooltip',
|
|
|
|
'aria-hidden': 'true'
|
|
|
|
} )
|
2014-04-22 14:16:54 +00:00
|
|
|
.appendTo( document.body );
|
2014-04-25 11:43:42 +00:00
|
|
|
};
|
2014-02-11 09:48:00 +00:00
|
|
|
|
2014-04-25 11:43:42 +00:00
|
|
|
/**
|
|
|
|
* Temorarily remove tooltips from links on hover
|
|
|
|
*
|
|
|
|
* @method removeTooltips
|
|
|
|
*/
|
2014-05-19 22:35:58 +00:00
|
|
|
mw.popups.removeTooltips = function ( $elements ) {
|
|
|
|
$elements
|
2014-05-19 22:42:41 +00:00
|
|
|
.filter( '[title]:not([title=""])' )
|
2014-03-23 02:32:37 +00:00
|
|
|
.on( 'mouseenter focus', function () {
|
|
|
|
$( this )
|
2014-04-25 11:43:42 +00:00
|
|
|
.data( '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 )
|
2014-04-25 11:43:42 +00:00
|
|
|
.attr( 'title', $( this ).data( 'title' ) );
|
2014-03-23 02:32:37 +00:00
|
|
|
} );
|
2014-04-25 11:43:42 +00:00
|
|
|
};
|
2014-02-06 11:15:05 +00:00
|
|
|
|
2014-04-25 11:43:42 +00:00
|
|
|
/**
|
|
|
|
* Checks if the user is scrolling, sets to false on mousemove
|
|
|
|
*
|
|
|
|
* @method checkScroll
|
|
|
|
*/
|
|
|
|
mw.popups.checkScroll = function () {
|
|
|
|
$( window ).on( 'scroll', function () {
|
|
|
|
mw.popups.scrolled = true;
|
|
|
|
} );
|
|
|
|
|
|
|
|
$( window ).on( 'mousemove', function () {
|
|
|
|
mw.popups.scrolled = false;
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2014-07-23 02:18:13 +00:00
|
|
|
* Register a hover event that may render a popup on an appropriate link.
|
2014-04-25 11:43:42 +00:00
|
|
|
*
|
|
|
|
* @method setupTriggers
|
|
|
|
*/
|
2014-05-19 22:35:58 +00:00
|
|
|
mw.popups.setupTriggers = function ( $elements ) {
|
|
|
|
$elements.on( 'mouseenter focus', function ( event ) {
|
2014-07-15 15:41:58 +00:00
|
|
|
if ( mw.popups.scrolled ) {
|
2014-02-06 10:49:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-02-17 14:03:13 +00:00
|
|
|
mw.popups.render.render( $( this ), event );
|
2014-04-25 11:43:42 +00:00
|
|
|
} );
|
|
|
|
};
|
2014-02-06 10:49:28 +00:00
|
|
|
|
2014-05-19 22:35:58 +00:00
|
|
|
/**
|
|
|
|
* Returns links that can have Popups
|
|
|
|
*
|
|
|
|
* @method selectPopupElements
|
|
|
|
*/
|
|
|
|
mw.popups.selectPopupElements = function () {
|
2015-02-17 14:03:13 +00:00
|
|
|
return mw.popups.$content
|
2014-07-15 15:41:58 +00:00
|
|
|
.find( 'a[href][title]:not(' + mw.popups.IGNORE_CLASSES.join(', ') + ')' )
|
|
|
|
.filter( function () {
|
2015-02-24 17:19:43 +00:00
|
|
|
var linkHref = new mw.Uri( this.href ),
|
|
|
|
expectedHref = new mw.Uri( mw.util.getUrl( this.title ) );
|
|
|
|
|
|
|
|
// don't compare fragment to display popups on anchored page links
|
|
|
|
linkHref.fragment = undefined;
|
|
|
|
return linkHref.toString() === expectedHref.toString();
|
2014-07-15 15:41:58 +00:00
|
|
|
} );
|
2014-05-19 22:35:58 +00:00
|
|
|
};
|
|
|
|
|
2014-04-25 11:43:42 +00:00
|
|
|
mw.hook( 'wikipage.content').add( function ( $content ) {
|
2014-06-17 11:08:05 +00:00
|
|
|
if ( mw.popups.enabled ) {
|
|
|
|
mw.popups.$content = $content;
|
2014-05-19 22:35:58 +00:00
|
|
|
|
2014-06-17 11:08:05 +00:00
|
|
|
var $elements = mw.popups.selectPopupElements();
|
|
|
|
mw.popups.removeTooltips( $elements );
|
|
|
|
mw.popups.setupTriggers( $elements );
|
|
|
|
}
|
2014-04-25 11:43:42 +00:00
|
|
|
} );
|
2014-02-06 10:49:28 +00:00
|
|
|
|
2014-04-25 11:43:42 +00:00
|
|
|
$( function () {
|
2014-06-17 11:08:05 +00:00
|
|
|
if ( mw.popups.enabled ) {
|
|
|
|
mw.popups.checkScroll();
|
|
|
|
mw.popups.createSVGMask();
|
|
|
|
mw.popups.createPopupElement();
|
|
|
|
}
|
2014-02-06 10:49:28 +00:00
|
|
|
} );
|
2014-02-07 07:35:34 +00:00
|
|
|
|
2014-04-25 11:43:42 +00:00
|
|
|
} ) ( jQuery, mediaWiki );
|