User:Ignatus/iwpopups.js
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/*
Gadget for displaying popups for pages of yr local (or another specified
in Ignatus.homewiki) wiki in another wikis.
NB: May be not compatible with [[mw:Extension:Popups]] which code was
partially used here (from git.wikimedia.org/blob/mediawiki%2Fextensions%2FPopups/8707f8ec526130be01be7ad033770552b553ad79/resources%2Fext.popups.core.js
and other parts of code).
*/
// Initializing Ignatus object
if(typeof Ignatus != 'object')Ignatus={homewiki:null};
// Getting home wiki
var api = new mw.Api();
if(!Ignatus.homewiki)api.get( {
action: 'query',
meta: 'globaluserinfo'
} ).done( function ( re ) { Ignatus.homewiki = re.query.globaluserinfo.home } );
// We must work if it's Wikipedia, not the homewiki
// (set local Ignatus.homewiki='xxwiki' in [[xx:Special:myPage/common.js]]
// if you don't want it also in xxwiki)
if( (mw.config.get('wgDBname') != Ignatus.homewiki)
&& (mw.config.get('wgNoticeProject') == 'wikipedia') )
( function ( $, mw ) {
var scrolled=false, cache={}, apipath=(/^(.+)wik/.exec(Ignatus.homewiki)[1]||'ru')+'.wikipedia.org/w/api.php';
var openTimer, currentLink, currentRequest;
var shower = $('<div>');// the "popup"
// Waiting function
var 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;
};
//Creates a new "popup"
var createPopup = function ( re, href ) {
//TODO:DOIT
};
//Opens a popup from cache
var openPopup = function(link,event){
//TODO:DOIT
};
//Gets the extraction. I won't load an image since it's often stupid.
var init = function(link){
var href = link.attr( 'href' ),
title = link.attr( 'title' ),//If remove titles, attr→data
deferred = $.Deferred();
if ( !title ) {
return deferred.reject().promise();
}
currentRequest = $.ajax( {
type:"GET",
url: apipath,
data:{ action: 'query',
prop: 'extracts',
redirects: 'true',
exintro: 'true',
exsentences: 2,
explaintext: 'true',
titles: title,
format: 'json'
},
dataType:'json',
error: deferred.reject,
success: function ( re ) {
currentRequest = undefined;
if (
!re.query ||
!re.query.pages ||
!re.query.pages[ re.query.pageids[ 0 ] ].extract ||
re.query.pages[ re.query.pageids[ 0 ] ].extract === ''
) {
deferred.reject();
return;
}
cache[ href ] = createPopup( re, href );//There can be sth more complicated, but here just text
deferred.resolve();
}
} );
};
// rendering function. Instead of popup, currently positions element in a rectangle on the left
var render=function(link, event){
// Ignore if its meant to call a function
// TODO: Remove this when adding reference popups
if ( link.attr( 'href' ) === '#' ) {
return;
}
currentLink = link;
if ( cache[ link.attr( 'href' ) ] ) {
openTimer = wait( 500 )//POPUP_DELAY
.done( function () {
openPopup( link, event );
} );
} else {
// TODO: check for link type and call correct renderer
var cachePopup = init(link);
openTimer = wait( 50 )//API_DELAY
.done( function () {
openTimer = wait( 450 );//POPUP_DELAY - API_DELAY
$.when( openTimer, cachePopup ).done( function () {
openPopup( link, event );
} );
} );
}
};//render()
$( document ).ready( function () {
mw.hook( 'wikipage.content').add( function ( $content ) {
// Select elements for creating popups (local hrefs out of specific classes)
var elements = $content
.find( 'a[href][title]:not(.extiw, .image, .new, .internal, .external, .oo-ui-buttonedElement-button)' )
.filter( function () {
return ( this.href.replace(/^https?:\/\//,'//') ===
( mw.config.get( 'wgServer' ) + mw.util.getUrl( this.title ) ).replace(/^https?:\/\//,'//') );
} );
// Here could come tooltip remover like mw.popups.removeTooltips, but I don't want
// Setting popup show event for the links
elements.on( 'mouseenter focus', function ( event ) {
if ( scrolled ) {
return;
}
render( $( this ), event );
} );
} );//function mw.hook
//TODO: add scrollkeeper
} );//function document.ready
}( jQuery, mediaWiki ) );