User:PerfektesChaos/js/fragmentAnchors/r.js

This is an old revision of this page, as edited by PerfektesChaos (talk | contribs) at 07:15, 19 April 2016 (1.1). 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.
/// Gadget/fragmentAnchors/r.js
/// 2016-04-17 PerfektesChaos@de.wikipedia
/// Fingerprint:#0#294272B8#
///<nowiki>
(function(mw,$){
"use strict";
var Version=1.1,FANCY="fragmentAnchors",FASC={cfg:{},siblings:"error-repeated-id",source0:"6/62/Anchor_pictogram.svg",source1:"d/d6/Anchor_pictogram_red.svg",vsn:Version
};
if(typeof mw.libs[FANCY]!=="object"||!mw.libs[FANCY]){
mw.libs[FANCY]={};}
mw.libs[FANCY].type=FANCY;
FANCY=mw.libs[FANCY];
FANCY.fetch=function(){
var dup,r,s;
if(FASC.cfg.live){
FASC.furnish(false,true);
if(FASC.dup){
dup={};
for(s in FASC.dup){
dup[s]=FASC.dup[s].length;}
}else{
dup=false;}
r=[FASC.total,dup,FASC.want];
}else{
r=false;}
return r;
};
function feed(){
mw.hook("wikipage.content").add(FASC.furnish);}
function fiat(){
FASC.cfg.$portlet.hide();
mw.loader.using(["jquery.tipsy"],feed);}
function fire(){
var env=mw.config.get(["wgAction","wgPageContentModel"]);
FANCY.vsn=Version;
FANCY.doc="[[w:en:User:PerfektesChaos/js/"+FANCY.type+"]]";
mw.loader.state("ext.gadget."+FANCY.type,"ready");
if(env.wgPageContentModel==="wikitext"){
FASC.locked=(env.wgAction==="view");
FASC.sign=FANCY.type;
FASC.cfg.live=false;
mw.loader.using(["user","mediawiki.util"],FASC.fire);}}
FASC.cfg.fair=function(anchor){
var r=anchor.replace(/_/g," "),s;
if(typeof FASC.wikiAnchor!=="object"){
s="[0-9A-Fa-f]";
s="\\.("+s+s+")";
FASC.wikiAnchor=new RegExp(s,"g");}
try{
s=r.replace(FASC.wikiAnchor,"%$1");
r=decodeURIComponent(s);
}catch(e){
}
return r;
};
FASC.cfg.fancy=function(anchor){
return "[["+anchor+"]]";
};
FASC.cfg.filter=function(anchor){
return(anchor.substr(0,7)!=="#Anker:");
};
FASC.cfg.fire=function(){
var portlet=mw.util.addPortletLink("p-tb","#"," ","t-"+FASC.sign);
if(typeof FANCY.large==="boolean"){
FASC.large=FANCY.large;}
FASC.cfg.$portlet=$(portlet);
FASC.cfg.$portlet.append(FASC.$facet());
FASC.cfg.$portlet.click(fiat);
FASC.cfg.$portlet.attr({title:FASC.sign+" "+FASC.vsn});
mw.hook(FANCY.type+".ready").fire(FANCY);
FASC.cfg.live=true;
};
FASC.cfg.$flag=function(){
return $("h1").eq(0);
};
FASC.$facet=function(alert,augment){
var m=(alert||augment?1:0),n=(alert?20:12),s=FASC["source"+m],$r=$("<img />");
$r.attr({alt:"id?",src:"https://upload.wikimedia.org/wikipedia/commons/thumb/"+s+"/"+n+"px-"+s.substr(5)+".png"});
$r.css({"height":n,"width":n});
if(alert){
$r.css({"background-color":"#FFFF00","border":"#FF0000 2px solid"});}
return $r;
};
FASC.$factory=function(anchor,alert){
var sign=anchor.substr(1),lapse=(alert||!FASC.fine(sign)),mode=(alert?1:(lapse?2:0)),$r=FASC.badges[mode],show,$i,$s;
if(!$r){
$i=FASC.$facet(alert,lapse);
$i.addClass(FASC.show);
if(alert){
$i.addClass(FASC.siblings);}
$i.attr({rel:"tipsy"});
$s=$("<span>");
$s.css({"width":"1px"});
$s.text(" ");
$r=$("<a>");
$r.attr({href:"#"});
$r.append($s);
$r.append($i);
$r.append($s.clone());
FASC.badges[mode]=$r;}
$r=$r.clone();
$i=$r.children();
if(lapse){
show=mw.html.element("span",{style:"background-color:#FFFF00;color:#FF0000;font-weight:bold;padding:0.4em;"},sign);
}else{
show=mw.html.escape(sign);}
$i.attr({"data-id":sign,"title":show});
$i.tipsy({gravity:"s",html:true,opacity:0.8});
$r.click(FASC.flip);
return $r;
};
FASC.$failure=function(anchor){
var $r=$("<span>");
$r.addClass(FASC.single);
$r.css({"background-color":"#FFFF00","border":"#FF0000 2px solid",
"color":"#FF0000","font-size":"120%",
"font-weight":"bold","padding-left":"0.2em",
"padding-right":"0.2em"});
$r.attr({title:anchor});
$r.text("#");
return $r;
};
FASC.filter=function(all){
var r={},s,seek,$e;
for(s in all){
if(all[s]){
if(FASC.$content){
seek="[id=\""+s.substr(1)+"\"]";
$e=FASC.$content.find(seek);
if($e.length){
r[s]=$e;}
}else{
r[s]=all[s];}}}
return r;
};
FASC.fine=function(anchor){
var r=true,i;
if(typeof FASC.reFine!=="object"){
FASC.reFine=[new RegExp("^\\s"),new RegExp("\\s$"),new RegExp("^\\d"),new RegExp("[<>'\"#%]")];}
for(i=0;i<FASC.reFine.length;i++){
if(FASC.reFine[i].test(anchor)){
r=false;
break;}}
return r;
};
FASC.fire=function(){
FASC.badges=[false,false,false];
if(typeof FASC.large!=="boolean"){
FASC.large=false;}
FASC.reFine=false;
FASC.scope=FASC.sign+"-inner";
FASC.show=FASC.sign+"-mark";
FASC.single=FASC.show+" "+FASC.sign+"-broken";
$(FASC.cfg.fire);
};
FASC.flag=function(){
var $before=FASC.cfg.$flag(),dup,s,$box,$li,$ul;
if($before){
$box=$("<div>");
$ul=$("<ul>");
for(s in FASC.dup){
dup=FASC.dup[s];
$li=$("<li>");
$li.text(dup.length+"× "+s);
$ul.append($li);}
for(s in FASC.want){
$li=$("<li>");
$li.text("−"+FASC.want[s]+"× "+s);
$ul.append($li);}
$box.css({"border":"#FF0000 2px solid","color":"#FF0000",
"margin-bottom":"1em","margin-top":"1em",
"padding":"0.5em"});
$box.attr({id:FASC.sign+"-errors"});
$box.addClass(FASC.show+" error");
$box.append($ul);
$before.before($box);}
};
FASC.flip=function(){
var $that=$(this),sel=$that.attr("original-title"),$img;
if(sel){
$that.attr({"rel":null,"original-title":null});
sel="hide";
}else{
$img=$that.children().eq(1);
$img.trigger($.Event("mouseleave"));
sel=$img.attr("data-id");
sel=FASC.cfg.fancy("#"+sel);
$that.attr({"rel":"tipsy","title":sel});
$that.tipsy({gravity:"n",opacity:0.9,trigger:"manual"});
sel="show";}
$that.tipsy(sel);
return false;
};
FASC.furnish=function($area,absent){
var learn=(!absent),total={},$total=$("[id]"),dup,i,lapsus,s,$e,$inner,$m;
FASC.dup={};
FASC.total=[];
FASC.want=false;
$("."+FASC.show).remove();
if(!FASC.large&&$area){
FASC.$content=$area;
}else{
FASC.$content=false;}
for(i=0;i<$total.length;i++){
$e=$total.eq(i);
s="#"+$e.attr("id");
if(FASC.cfg.filter(s)){
if(typeof total[s]==="object"){
if(typeof FASC.dup[s]!=="object"){
FASC.dup[s]=[total[s]];
total[s]=true;}
FASC.dup[s].push($e);
}else{
total[s]=$e;}}}
$total=false;
if(typeof total["#"]==="object"){
FASC.dup["#"]=[total["#"]];
total["#"]=true;}
for(s in FASC.dup){
lapsus=true;
FASC.total.push(s);
dup=FASC.dup[s];
for(i=0;i<dup.length;i++){
$e=dup[i];
if($e){
$m=FASC.$factory(s,true);
$e.before($m);}}
total[s]=false;}
if(!lapsus){
FASC.dup=false;}
if(FASC.locked){
$inner=$("a[href^='#']");
for(i=0;i<$inner.length;i++){
$e=$inner.eq(i);
s=$e.attr("href");
if(s!=="#"&&typeof total[s]!=="object"){
$e.addClass(FASC.scope);
$e.before(FASC.$failure(s));
if(!FASC.want){
FASC.want={};}
if(typeof FASC.want[s]==="number"){
FASC.want[s]=FASC.want[s]+1;
}else{
FASC.want[s]=1;}}}}
if(FASC.dup||FASC.want){
FASC.flag();}
total=FASC.filter(total);
for(s in total){
if(typeof total[s]==="object"){
FASC.total.push(s);
if(learn){
$e=total[s];
$m=FASC.$factory(FASC.cfg.fair(s),false);
$e.before($m);}}}
};
fire();
}(window.mediaWiki,window.jQuery));
/// EOF</nowiki>fragmentAnchors/r.js