Jump to content

User:Kaldari/ReferenceTooltips.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Kaldari (talk | contribs) at 20:17, 5 May 2012. 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]]

window.pg || $(document).ready( function($) {

    // Make sure we are in article, project, or help namespace
    if ( wgCanonicalNamespace === '' || wgCanonicalNamespace === 'Project' || wgCanonicalNamespace === 'Help' ) {
    
        $(".reference").each( function() {
            var tooltipNode, hideTimer, showTimer;
            function findRef( h ){
                h = h.firstChild.getAttribute("href"); h = h && h.split("#"); h = h && h[1];
                h = h && document.getElementById( h );
                return h;
            }
            function hide(){
                if( tooltipNode && tooltipNode.parentNode == document.body ) {
                    hideTimer = 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 || tooltipNode.parentNode.nodeType === 11 ){
                    document.body.appendChild( tooltipNode );
                }
                $(tooltipNode).stop().animate({opacity: 1}, 100)
                clearTimeout( hideTimer );
            }
            $(this).hover(function(){
                var _this = this;
                if( showTimer ) {
                    clearTimeout( showTimer );
                }
                showTimer = setTimeout(function() {
                    var h = findRef( _this );
                    if( !h ){return};
                    if( ( window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0 ) + $(window).height() > $( 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-") !== -1 ) {
                                    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(), oH = tooltipNode.offsetHeight;
                    $(tooltipNode).css({top: o.top - oH, left: o.left - 7 });
                    if( tooltipNode.offsetHeight > oH ) { // is it squished against the right side of the page?
                        $(tooltipNode).css({left:'auto',right:0});
                        tooltipNode.lastChild.style.marginLeft = (o.left - tooltipNode.offsetLeft) + "px";
                    }
                }, 1000);
            },
            function(){
console.debug(this);
                clearTimeout( showTimer );
                hide();
            } );
        } );
        
    }

} );