Difference between revisions of "MediaWiki:Gadget-ReferenceTooltips.css"

From Harridanic
Jump to navigation Jump to search
(Created page with "→‎See [[mw:Reference Tooltips]]: .referencetooltip { position: absolute; list-style: none; list-style-image: none; opacity: 0; font...")
 
Line 1: Line 1:
/* See [[mw:Reference Tooltips]] */
+
// See [[mw:Reference Tooltips]]
 
+
.referencetooltip {
+
window.pg || $(document).ready( function($) {
        position: absolute;
+
        list-style: none;
+
    // Make sure we are in article, project, or help namespace
         list-style-image: none;
+
    if ( wgCanonicalNamespace === '' || wgCanonicalNamespace === 'Project' || wgCanonicalNamespace === 'Help' ) {
         opacity: 0;
+
        function toggleRT(o){
         font-size: 10px;
+
            mw.loader.using("jquery.cookie",function(){
         margin: 0;
+
                    $.cookie("RTsettings",o+"|"+ settings[1] + "|" + settings[2], {path:"/",expires:90});
        z-index: 5;
+
                    location.reload();
        padding: 0;
+
            })
}
+
         }
.referencetooltip li {
+
         var settings = document.cookie.split("RTsettings=")[1];
        border: #080086 2px solid;
+
         settings = settings ? settings.split(";")[0].split("%7C") : [1, 200, +("ontouchstart" in document.documentElement)];
         max-width: 260px;
+
         if( settings[0] == 0 ) {
         padding: 10px 8px 13px 8px;
+
            var footer = $("#footer-places, #f-list");
        margin: 0px;
+
            if( footer.length === 0 ) {
        background-color: #F7F7F7;
+
                    footer = $("#footer li").parent();
         box-shadow: 2px 4px 2px rgba(0,0,0,0.3);
+
            }
        -moz-box-shadow: 2px 4px 2px rgba(0,0,0,0.3);
+
            footer.append($("<li>").append($("<a>").text("Enable Reference Tooltips").attr("href","javascript:(function(){})()").click(function(){toggleRT(1)})));
        -webkit-box-shadow: 2px 4px 2px rgba(0,0,0,0.3);
+
            return;
}
+
         }
.referencetooltip li+li {
+
         var isTouchscreen = +settings[2],
        margin-left: 7px;
+
            timerLength = isTouchscreen ? 0 : +settings[1],
        margin-top: -2px;
+
            settingsMenu;
        border: 0;
+
         $(".reference").each( function() {
        padding: 0;
+
            var tooltipNode, hideTimer, showTimer, checkFlip = false;
        height: 3px;
+
            function findRef( h ){
        width: 0px;
+
                    h = h.firstChild.getAttribute("href"); h = h && h.split("#"); h = h && h[1];
        background-color: transparent;
+
                    h = h && document.getElementById( h );
        box-shadow: none;
+
                    h = h && h.nodeName == "LI" && h;
        -moz-box-shadow: none;
+
                    return h;
        -webkit-box-shadow: none;
+
            }
        border-top: 12px #080086 solid;
+
            function hide( refLink ){
        border-right: 7px transparent solid;
+
                    if( tooltipNode && tooltipNode.parentNode == document.body ) {
        border-left: 7px transparent solid;
+
                            hideTimer = setTimeout( function() {
}
+
                                    $(tooltipNode).animate({opacity: 0}, 100, function(){ document.body.removeChild( tooltipNode ) })
.referencetooltip>li+li::after {
+
                            }, isTouchscreen ? 16 : 100)
        content: '';
+
                    } else {
        border-top: 8px #F7F7F7 solid;
+
                            var h = findRef( refLink );
        border-right: 5px transparent solid;
+
                            h && (h.style.border = "");
        border-left: 5px transparent solid;
+
                    }
        margin-top: -12px;
+
            }
        margin-left: -5px;
+
            function show(){
        z-index: 1;
+
                    if( !tooltipNode.parentNode || tooltipNode.parentNode.nodeType === 11 ){
        height: 0px;
+
                            document.body.appendChild( tooltipNode );
        width: 0px;
+
                            checkFlip = true;
        display: block;
+
                    }
}
+
                    $(tooltipNode).stop().animate({opacity: 1}, 100)
.client-js .referencetooltip li ul li {
+
                    clearTimeout( hideTimer );
        border: none;
+
            }
        box-shadow: none;
+
            function openSettingsMenu(){
        -moz-box-shadow: none;
+
                    if( settingsMenu ) {
        -webkit-box-shadow: none;
+
                            settingsMenu.dialog( "open" );
        height: auto;
+
                    } else {
        width: auto;
+
                            settingsMenu = $("<form>").append(
        margin: auto;
+
                                    $("<button>").css("width","100%").text("Disable Reference Tooltips").button().click(function(){toggleRT(0)}),
        padding: 0;
+
                                    $("<br>"),
        position: static;
+
                                    $("<small>").text("Once disabled, Reference Tooltips can be re-enabled using a link in the footer of the page."),
}
+
                                    $("<hr>"),
.RTflipped {
+
                                    $("<label>").text("Delay before the tooltip appears (in milliseconds): ").append($("<input>").attr({"type":"number","value":settings[1],step:50,min:0,max:5000})),
        padding-top: 13px;
+
                                    $("<br>"),
}
+
                                    $("<span>").text("Tooltip is activated by:"),
.referencetooltip.RTflipped li+li {
+
                                    $("<label>").append(
        position: absolute;
+
                                            $("<input>").attr({"type":"radio", "name":"RTActivate", "checked":settings[2]==0&&"checked", "disabled":"ontouchstart" in document.documentElement&&"disabled"}),
        top: 2px;
+
                                            "hovering"
        border-top: 0;
+
                                    ),
        border-bottom: 12px #080086 solid;
+
                                    $("<label>").append(
}
+
                                            $("<input>").attr({"type":"radio", "name":"RTActivate", "checked":settings[2]==1&&"checked"}),
.referencetooltip.RTflipped li+li::after {
+
                                            "clicking"
        border-top: 0;
+
                                    )
        border-bottom: 8px #F7F7F7 solid;
+
                            ).submit(function(e){e.preventDefault()}).dialog({modal:true,width:500,title:"Reference Tooltips options",buttons:{"Save settings":function(){
        position: absolute;
+
                                    var a = this.getElementsByTagName("input"),
        margin-top: 7px;
+
                                            b = +a[0].value;
}
+
                                    $.cookie("RTsettings","1|"+ (b > -1 && b < 5001 ? b : settings[1]) + (a[1].checked ? "|0" : "|1"), {path:"/",expires:90});
.RTsettings{
+
                                    location.reload();
        float: right;
+
                            }}});
        height: 16px;
+
                    }
        width: 16px;
+
            }
        cursor: pointer;
+
            $(this)[ isTouchscreen ? 'click' : 'hover' ](function( e ){
        background-image: url(//upload.wikimedia.org/wikipedia/commons/e/ed/Cog.png);
+
                    var _this = this;
        margin-top: -9px;
+
                    if( isTouchscreen ) {
        margin-right: -7px;
+
                            e.preventDefault();
        -webkit-transition: opacity 0.15s;
+
                            (tooltipNode && tooltipNode.parentNode == document.body) || setTimeout( function(){
        -moz-transition: opacity 0.15s;
+
                                    $( document.body ).on("click touchstart", function( e ) {
        -o-transition: opacity 0.15s;
+
                                            e = e || event;
        -ms-transition: opacity 0.15s;
+
                                            e = e.target || e.srcElement;
        transition: opacity 0.15s;
+
                                            for( ; e && !$( e ).hasClass( "referencetooltip" ) ; )
        opacity: 0.6;
+
                                                    e = e.parentNode;
        filter: alpha(opacity=60);
+
                                            if( !e ){
}
+
                                                    clearTimeout( showTimer );
.RTsettings:hover{
+
                                                    hide( _this );
        opacity: 1;
+
                                                    $(document.body).off("click touchstart", arguments.callee)
         filter: alpha(opacity=100);
+
                                            }
}
+
                                    })
 +
                            }, 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[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.firstChild.insertBefore( document.createElement( "span" ), tooltipNode.firstChild.firstChild ) ).addClass("RTsettings").attr("title", "Tooltip settings").click(function(){
 +
                                            mw.loader.using(["jquery.cookie","jquery.ui.dialog"], 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( checkFlip ) {
 +
                                    if( o.top < tooltipNode.offsetHeight + ( window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0 ) ) { // is part of it above the top of the screen?
 +
                                            $(tooltipNode).addClass("RTflipped").css({top: o.top + 12});
 +
                                    } else if( tooltipNode.className === "referencetooltip RTflipped" ) { // cancel previous
 +
                                            $(tooltipNode).removeClass("RTflipped");
 +
                                    }
 +
                                    checkFlip = false;
 +
                            }
 +
                    }, timerLength);
 +
            }, isTouchscreen ? undefined : function(){clearTimeout(showTimer); hide(this); } )
 +
 +
         } );
 +
 +
    }
 +
 +
} );

Revision as of 12:31, 5 October 2012

// 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' ) {
        function toggleRT(o){
            mw.loader.using("jquery.cookie",function(){
                    $.cookie("RTsettings",o+"|"+ settings[1] + "|" + settings[2], {path:"/",expires:90});
                    location.reload();
            })
        }
        var settings = document.cookie.split("RTsettings=")[1];
        settings = settings ? settings.split(";")[0].split("%7C") : [1, 200, +("ontouchstart" in document.documentElement)];
        if( settings[0] == 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[2],
            timerLength = isTouchscreen ? 0 : +settings[1],
            settingsMenu;
        $(".reference").each( function() {
            var tooltipNode, hideTimer, showTimer, checkFlip = false;
            function findRef( h ){
                    h = h.firstChild.getAttribute("href"); h = h && h.split("#"); h = h && h[1];
                    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 );
                            checkFlip = true;
                    }
                    $(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[1],step:50,min:0,max:5000})),
                                    $("<br>"),
                                    $("<span>").text("Tooltip is activated by:"),
                                    $("<label>").append(
                                            $("<input>").attr({"type":"radio", "name":"RTActivate", "checked":settings[2]==0&&"checked", "disabled":"ontouchstart" in document.documentElement&&"disabled"}),
                                            "hovering"
                                    ),
                                    $("<label>").append(
                                            $("<input>").attr({"type":"radio", "name":"RTActivate", "checked":settings[2]==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[0].value;
                                    $.cookie("RTsettings","1|"+ (b > -1 && b < 5001 ? b : settings[1]) + (a[1].checked ? "|0" : "|1"), {path:"/",expires:90});
                                    location.reload();
                            }}});
                    }
            }
            $(this)[ isTouchscreen ? 'click' : 'hover' ](function( e ){
                    var _this = this;
                    if( isTouchscreen ) {
                            e.preventDefault();
                            (tooltipNode && tooltipNode.parentNode == document.body) || setTimeout( function(){
                                    $( document.body ).on("click touchstart", function( e ) {
                                            e = e || event;
                                            e = e.target || e.srcElement;
                                            for( ; e && !$( e ).hasClass( "referencetooltip" ) ; )
                                                    e = e.parentNode;
                                            if( !e ){
                                                    clearTimeout( showTimer );
                                                    hide( _this );
                                                    $(document.body).off("click touchstart", 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[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.firstChild.insertBefore( document.createElement( "span" ), tooltipNode.firstChild.firstChild ) ).addClass("RTsettings").attr("title", "Tooltip settings").click(function(){
                                            mw.loader.using(["jquery.cookie","jquery.ui.dialog"], 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( checkFlip ) {
                                    if( o.top < tooltipNode.offsetHeight + ( window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0 ) ) { // is part of it above the top of the screen?
                                            $(tooltipNode).addClass("RTflipped").css({top: o.top + 12});
                                    } else if( tooltipNode.className === "referencetooltip RTflipped" ) { // cancel previous
                                            $(tooltipNode).removeClass("RTflipped");
                                    }
                                    checkFlip = false;
                            }
                    }, timerLength);
            }, isTouchscreen ? undefined : function(){clearTimeout(showTimer); hide(this); } )
 
        } );
 
    }
 
} );