Misplaced Pages

User:Yair rand/ReferenceTooltips.js: Difference between revisions

Article snapshot taken from[REDACTED] with creative commons attribution-sharealike license. Give it a read and then ask your questions in the chat. We can research this topic together.
< User:Yair rand Browse history interactively← Previous editNext edit →Content deleted Content added
Revision as of 00:01, 3 July 2012 view sourceYair rand (talk | contribs)Extended confirmed users3,255 edits oops!← Previous edit Revision as of 02:38, 5 July 2012 view source Kaldari (talk | contribs)Autopatrolled, Extended confirmed users, File movers, Pending changes reviewers, Rollbackers68,434 edits shorter code that does the same thingNext edit →
Line 54: Line 54:
} else { } else {
settingsMenu = $("<form>").append( settingsMenu = $("<form>").append(
$("<button>").css("width","100%").addClass("ui-button ui-state-default ui-button-text-only").append($("<span>").addClass("ui-button-text").text("Disable Reference Tooltips")).click(function(){toggleRT(0)}), $("<button>").css("width","100%").text("Disable Reference Tooltips")).button().click(function(){toggleRT(0)}),
"<br>", "<br>",
$("<small>").text("Once disabled, Reference Tooltips can be re-enabled using a link in the footer of the page."), $("<small>").text("Once disabled, Reference Tooltips can be re-enabled using a link in the footer of the page."),

Revision as of 02:38, 5 July 2012

// See ]

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

    // Make sure we are in article, project, or help namespace
    if ( wgCanonicalNamespace === '' || wgCanonicalNamespace === 'Project' || wgCanonicalNamespace === 'Help' ) {
        function toggleRT(o){
            mw.loader.using("jquery.cookie",function(){
                    $.cookie("RTsettings",o+"|"+ settings + "|" + settings, {path:"/",expires:90});
                    location.reload();
            })
        }
        var settings = document.cookie.split("RTsettings=");
        settings = settings ? settings.split(";").split("%7C") : ;
        if( settings == 0 ) {
            var footer = $("#footer-places, #f-list");
            if( footer.length === 0 ) {
                    footer = $("#footer li").parent();
            }
            footer.append($("<li>").append($("<a>").text("Enable Reference Tooltips").attr("href","javascript:(function(){})()").click(function(){toggleRT(1)})));
            return;
        }
        var isTouchscreen = +settings,
            timerLength = isTouchscreen ? 0 : +settings,
            settingsMenu;
        $(".reference").each( function() {
            var tooltipNode, hideTimer, showTimer;
            function findRef( h ){
                    h = h.firstChild.getAttribute("href"); h = h && h.split("#"); h = h && h;
                    h = h && document.getElementById( h );
                    h = h && h.nodeName == "LI" && h;
                    return h;
            }
            function hide( refLink ){
                    if( tooltipNode && tooltipNode.parentNode == document.body ) {
                            hideTimer = setTimeout( function() {
                                    $(tooltipNode).animate({opacity: 0}, 100, function(){ document.body.removeChild( tooltipNode ) })
                            }, isTouchscreen ? 16 : 100)
                    } else {
                            var h = findRef( refLink );
                            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 );
            }
            function openSettingsMenu(){
                    if( settingsMenu ) {
                            settingsMenu.dialog( "open" );
                    } else {
                            settingsMenu = $("<form>").append(
                                    $("<button>").css("width","100%").text("Disable Reference Tooltips")).button().click(function(){toggleRT(0)}),
                                    "<br>",
                                    $("<small>").text("Once disabled, Reference Tooltips can be re-enabled using a link in the footer of the page."),
                                    "<hr>",
                                    $("<label>").text("Delay before the tooltip appears (in milliseconds): ").append($("<input>").attr({"type":"number","value":settings,step:50,min:0,max:5000})),
                                    "<br>",
                                    $("<span>").text("Tooltip is activated by:"),
                                    $("<label>").append(
                                            $("<input>").attr({"type":"radio", "name":"RTActivate", "checked":settings==0&&"checked", "disabled":"ontouchstart" in document.documentElement&&"disabled"}),
                                            "hovering"
                                    ),
                                    $("<label>").append(
                                            $("<input>").attr({"type":"radio", "name":"RTActivate", "checked":settings==1&&"checked"}),
                                            "clicking"
                                    )
                            ).submit(function(e){e.preventDefault()}).dialog({modal:true,width:500,title:"Reference Tooltips options",buttons:{"Save settings":function(){
                                    var a = this.getElementsByTagName("input"),
                                            b = +a.value;
                                    $.cookie("RTsettings","1|"+ (b > -1 && b < 5001 ? b : settings) + (a.checked ? "|0" : "|1"), {path:"/",expires:90});
                                    location.reload();
                            }}});
                    }
            }
            $(this)(function( e ){
                    var _this = this;
                    if( isTouchscreen ) {
                            e.preventDefault();
                            (tooltipNode && tooltipNode.parentNode == document.body) || setTimeout( function(){
                                    $( document.body ).click( function( e ) {
                                            e = e || event;
                                            e = e.target || e.srcElement;
                                            for( ; e && e.className!="referencetooltip"; )
                                                    e = e.parentNode;
                                            if( !e ){
                                                    clearTimeout( showTimer );
                                                    hide( _this );
                                                    $(document.body).unbind("click", arguments.callee)
                                            }
                                    })
                            }, 0);
                    }
                    showTimer && clearTimeout( showTimer );
                    showTimer = setTimeout( function() {
                            var h = findRef( _this );
                            if( !h ){return};
                            if( !isTouchscreen && ( window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0 ) + $(window).height() > $( h ).offset().top + h.offsetHeight ) {
                                    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.nodeName == "A" && c.childNodes.getAttribute( "href" ).indexOf("#cite_ref-") !== -1 ) {
                                                            do { c.removeChild( c.childNodes ) } while ( c.childNodes.nodeValue == " " );
                                                    }
                                            }
                                    } catch (e) { mw.log(e) }
                                    c.removeChild( c.firstChild );
                                    $( tooltipNode.firstChild.insertBefore( document.createElement( "span" ), tooltipNode.firstChild.firstChild ) ).addClass("RTsettings").attr("title", "Tooltip settings").click(function(){
                                            mw.loader.using(, openSettingsMenu);
                                    })
                                    tooltipNode.appendChild( document.createElement( "li" ) );
                                    isTouchscreen || $(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";
                            }
                            if( o.top < tooltipNode.offsetHeight ) { // is it above the top of the page?
                                    $(tooltipNode).addClass("RTflipped").css({top: o.top + 12});
                            } else if( tooltipNode.className === "referencetooltip RTflipped" ) { // cancel previous
                                    $(tooltipNode).removeClass("RTflipped");
                            }
                    }, timerLength);
            }, isTouchscreen ? undefined : function(){clearTimeout(showTimer); hide(this); } )
 
        } );
        
    }

} );
User:Yair rand/ReferenceTooltips.js: Difference between revisions Add topic