Jump to content

User:Yair rand/ReferenceTooltips.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Yair rand (talk | contribs) at 00:30, 7 December 2011 (??). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// See [[mw:Reference Tooltips]]

$(document).ready( function($) {

    // Make sure we are in article namespace
    if ( wgCanonicalNamespace === '' ) {
    
        // Load CSS
        importStylesheet('User:Yair rand/ReferenceTooltips.css');
        
        $(".reference").each( function() {
            var tooltipNode, timer;
            function findRef( h ){
                    h = h.firstChild.getAttribute("href"); h = h && h.substr(1);
                    h = h && document.getElementById( h );
                    return h;
            }
            function hide(){
                    if( tooltipNode && tooltipNode.parentNode ) {
                            timer = setTimeout( function() {
                                    $(tooltipNode).animate({opacity: 0}, 100, function(){ document.body.removeChild( tooltipNode ) })
                            }, 100)
                    } else {
                            var h = findRef( this );
                            h && (h.style.border = "");
                    }
            }
            function show(){
                    if( !tooltipNode.parentNode ){
                            document.body.appendChild( tooltipNode );
                    }
                    $(tooltipNode).stop().animate({opacity: 1}, 100)
                    clearTimeout( timer );
            }
            $(this).hover(function(){
                    var h = findRef( this );
                    if( !h ){return};
                    if( document.body.scrollTop + screen.availHeight > $( h ).offset().top ) {
                            h.style.border = "#080086 2px solid";
                            return;
                    }
                    if(!tooltipNode){
                            tooltipNode = document.createElement("ul");
                            tooltipNode.className = "referencetooltip";
                            var c = tooltipNode.appendChild( h.cloneNode( true ) );
                            try {
                                    if( c.firstChild.nodeName != "A" ) {
                                            while( c.childNodes[1].nodeName == "A" && c.childNodes[1].getAttribute( "href" ).indexOf("#cite_ref-") == 0 ) {
                                                    do { c.removeChild( c.childNodes[1] ) } while ( c.childNodes[1].nodeValue == " " );
                                            }
                                    }
                            } catch (e) { mw.log(e) }
                            c.removeChild( c.firstChild );
                            tooltipNode.appendChild( document.createElement( "li" ) );
                            $(tooltipNode).hover(show, hide);
                    }
                    show();
                    var o = $(this).offset();
                    $(tooltipNode).css({top: o.top - tooltipNode.offsetHeight, left: o.left - 7 });
            }, hide)
            
        } );
        
    }

} );