MediaWiki:Gadget-ReferenceTooltips.js

提供:KANOTYPE WIKI
ナビゲーションに移動 検索に移動

注意: 保存後、変更を確認するにはブラウザーのキャッシュを消去する必要がある場合があります。

  • Firefox / Safari: Shift を押しながら 再読み込み をクリックするか、Ctrl-F5 または Ctrl-R を押してください (Mac では ⌘-R)
  • Google Chrome: Ctrl-Shift-R を押してください (Mac では ⌘-Shift-R)
  • Internet Explore/Edger: Ctrl を押しながら 最新の情報に更新 をクリックするか、Ctrl-F5 を押してください
  • Opera: Ctrl-F5を押してください
  1 // See [[mw:Reference Tooltips]]
  2 
  3 window.pg || $(document).ready( function($) {
  4 
  5     // Make sure we are in article, project, or help namespace
  6     if ( wgCanonicalNamespace === '' || wgCanonicalNamespace === 'Project' || wgCanonicalNamespace === 'Help' ) {
  7         function toggleRT(o){
  8             mw.loader.using("jquery.cookie",function(){
  9                     $.cookie("RTsettings",o+"|"+ settings[1] + "|" + settings[2], {path:"/",expires:90});
 10                     location.reload();
 11             })
 12         }
 13         var settings = document.cookie.split("RTsettings=")[1];
 14         settings = settings ? settings.split(";")[0].split("%7C") : [1, 200, +("ontouchstart" in document.documentElement)];
 15         if( settings[0] == 0 ) {
 16             var footer = $("#footer-places, #f-list");
 17             if( footer.length === 0 ) {
 18                     footer = $("#footer li").parent();
 19             }
 20             footer.append($("<li>").append($("<a>").text("Enable Reference Tooltips").attr("href","javascript:(function(){})()").click(function(){toggleRT(1)})));
 21             return;
 22         }
 23         var isTouchscreen = +settings[2],
 24             timerLength = isTouchscreen ? 0 : +settings[1],
 25             settingsMenu;
 26         $(".reference").each( function() {
 27             var tooltipNode, hideTimer, showTimer, checkFlip = false;
 28             function findRef( h ){
 29                     h = h.firstChild.getAttribute("href"); h = h && h.split("#"); h = h && h[1];
 30                     h = h && document.getElementById( h );
 31                     h = h && h.nodeName == "LI" && h;
 32                     return h;
 33             }
 34             function hide( refLink ){
 35                     if( tooltipNode && tooltipNode.parentNode == document.body ) {
 36                             hideTimer = setTimeout( function() {
 37                                     $(tooltipNode).animate({opacity: 0}, 100, function(){ document.body.removeChild( tooltipNode ) })
 38                             }, isTouchscreen ? 16 : 100)
 39                     } else {
 40                             var h = findRef( refLink );
 41                             h && (h.style.border = "");
 42                     }
 43             }
 44             function show(){
 45                     if( !tooltipNode.parentNode || tooltipNode.parentNode.nodeType === 11 ){
 46                             document.body.appendChild( tooltipNode );
 47                             checkFlip = true;
 48                     }
 49                     $(tooltipNode).stop().animate({opacity: 1}, 100)
 50                     clearTimeout( hideTimer );
 51             }
 52             function openSettingsMenu(){
 53                     if( settingsMenu ) {
 54                             settingsMenu.dialog( "open" );
 55                     } else {
 56                             settingsMenu = $("<form>").append(
 57                                     $("<button>").css("width","100%").text("Disable Reference Tooltips").button().click(function(){toggleRT(0)}),
 58                                     $("<br>"),
 59                                     $("<small>").text("Once disabled, Reference Tooltips can be re-enabled using a link in the footer of the page."),
 60                                     $("<hr>"),
 61                                     $("<label>").text("Delay before the tooltip appears (in milliseconds): ").append($("<input>").attr({"type":"number","value":settings[1],step:50,min:0,max:5000})),
 62                                     $("<br>"),
 63                                     $("<span>").text("Tooltip is activated by:"),
 64                                     $("<label>").append(
 65                                             $("<input>").attr({"type":"radio", "name":"RTActivate", "checked":settings[2]==0&&"checked", "disabled":"ontouchstart" in document.documentElement&&"disabled"}),
 66                                             "hovering"
 67                                     ),
 68                                     $("<label>").append(
 69                                             $("<input>").attr({"type":"radio", "name":"RTActivate", "checked":settings[2]==1&&"checked"}),
 70                                             "clicking"
 71                                     )
 72                             ).submit(function(e){e.preventDefault()}).dialog({modal:true,width:500,title:"Reference Tooltips options",buttons:{"Save settings":function(){
 73                                     var a = this.getElementsByTagName("input"),
 74                                             b = +a[0].value;
 75                                     $.cookie("RTsettings","1|"+ (b > -1 && b < 5001 ? b : settings[1]) + (a[1].checked ? "|0" : "|1"), {path:"/",expires:90});
 76                                     location.reload();
 77                             }}});
 78                     }
 79             }
 80             $(this)[ isTouchscreen ? 'click' : 'hover' ](function( e ){
 81                     var _this = this;
 82                     if( isTouchscreen ) {
 83                             e.preventDefault();
 84                             (tooltipNode && tooltipNode.parentNode == document.body) || setTimeout( function(){
 85                                     $( document.body ).on("click touchstart", function( e ) {
 86                                             e = e || event;
 87                                             e = e.target || e.srcElement;
 88                                             for( ; e && !$( e ).hasClass( "referencetooltip" ) ; )
 89                                                     e = e.parentNode;
 90                                             if( !e ){
 91                                                     clearTimeout( showTimer );
 92                                                     hide( _this );
 93                                                     $(document.body).off("click touchstart", arguments.callee)
 94                                             }
 95                                     })
 96                             }, 0);
 97                     }
 98                     showTimer && clearTimeout( showTimer );
 99                     showTimer = setTimeout( function() {
100                             var h = findRef( _this );
101                             if( !h ){return};
102                             if( !isTouchscreen && ( window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0 ) + $(window).height() > $( h ).offset().top + h.offsetHeight ) {
103                                     h.style.border = "#080086 2px solid";
104                                     return;
105                             }
106                             if(!tooltipNode){
107                                     tooltipNode = document.createElement("ul");
108                                     tooltipNode.className = "referencetooltip";
109                                     var c = tooltipNode.appendChild( h.cloneNode( true ) );
110                                     try {
111                                             if( c.firstChild.nodeName != "A" ) {
112                                                     while( c.childNodes[1].nodeName == "A" && c.childNodes[1].getAttribute( "href" ).indexOf("#cite_ref-") !== -1 ) {
113                                                             do { c.removeChild( c.childNodes[1] ) } while ( c.childNodes[1].nodeValue == " " );
114                                                     }
115                                             }
116                                     } catch (e) { mw.log(e) }
117                                     c.removeChild( c.firstChild );
118                                     $( tooltipNode.firstChild.insertBefore( document.createElement( "span" ), tooltipNode.firstChild.firstChild ) ).addClass("RTsettings").attr("title", "Tooltip settings").click(function(){
119                                             mw.loader.using(["jquery.cookie","jquery.ui.dialog"], openSettingsMenu);
120                                     })
121                                     tooltipNode.appendChild( document.createElement( "li" ) );
122                                     isTouchscreen || $(tooltipNode).hover(show, hide);
123                             }
124                             show();
125                             var o = $(_this).offset(), oH = tooltipNode.offsetHeight;
126                             $(tooltipNode).css({top: o.top - oH, left: o.left - 7 });
127                             if( tooltipNode.offsetHeight > oH ) { // is it squished against the right side of the page?
128                                     $(tooltipNode).css({left:'auto',right:0});
129                                     tooltipNode.lastChild.style.marginLeft = (o.left - tooltipNode.offsetLeft) + "px";
130                             }
131                             if( checkFlip ) {
132                                     if( o.top < tooltipNode.offsetHeight + ( window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0 ) ) { // is part of it above the top of the screen?
133                                             $(tooltipNode).addClass("RTflipped").css({top: o.top + 12});
134                                     } else if( tooltipNode.className === "referencetooltip RTflipped" ) { // cancel previous
135                                             $(tooltipNode).removeClass("RTflipped");
136                                     }
137                                     checkFlip = false;
138                             }
139                     }, timerLength);
140             }, isTouchscreen ? undefined : function(){clearTimeout(showTimer); hide(this); } )
141  
142         } );
143         
144     }
145 
146 } );