User:Cwek/myTOCv2.js
外观
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google Chrome、Firefox、Microsoft Edge及Safari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
mw.loader.using( 'jquery.ui' ).then( function () {
(function(document,window,$,mw,undefined){
window.lftoc=window.lftoc||{};
var mytoc=window.lftoc;
var config=(mytoc.config=(mytoc.config||{}));
mytoc.init=function(){
if(mytoc.isBuilded===undefined)mytoc.isBuilded=false;
else if(mytoc.isBuilded)return;
mytoc.addStyle();
mytoc.bodyWidth=$("#content").css("margin-left");//$("#mw-panel").outerWidth();
mytoc.bodyHeight=document.body.clientHeight;
mytoc.oldid=mw.config.get("wgRevisionId");
mytoc.fullpagename=mw.config.get("wgPageName");
mytoc.namespaceid=mw.config.get("wgNamespaceNumber");
mytoc.action=mw.config.get("wgAction");
mytoc.isMP =(mw.config.get("wgIsMainPage")===true);
mytoc.isFlow=(mw.config.get("wgPageContentModel")==="flow-board");
mytoc.isJS =(mw.config.get("wgPageContentModel")==="javascript");
mytoc.isCSS =(mw.config.get("wgPageContentModel")==="css");
if(mytoc.needBuildIt()){
mytoc.buildFrame(mytoc.needOriginalTOC()?mytoc.getOriginalTOC():mytoc.buildTOC());
}
mytoc.isBuilded=true;
if(config.closeOriginalTOC)$("#toc.toc").addClass("toc_closed");
if(config.closeTOCAtFirst)mytoc.switchButtonClickEventHandle(false);
};
mytoc.addStyle=function(){
mw.loader.addStyleTag("/*#myTOC>#left.toc_closed,*/#toc.toc.toc_closed{display:none;}");
};
mytoc.needBuildIt=function(){
if(mytoc.action=="view"){
if(mytoc.isMP||mytoc.isFlow||mytoc.isJS||mytoc.isCSS)return false;
switch(mytoc.namespaceid){
case -1:{
if(mytoc.needOriginalTOC())return true;
}
case 14: //cate
case 828: //模块
case 2300: //GA
case 2302: //GAD
case 2600: //topic
{return false;}
//
default:
{return true;}
}
}
return false;
};
mytoc.nOT=undefined;
mytoc.needOriginalTOC=function(){
if(mytoc.nOT===undefined){
mytoc.nOT=mytoc.namespaceid==-1&&$("div#toc.toc").length==1&&
-1!=mytoc.fullpagename.indexOf("Special:网络书源");
}
return mytoc.nOT;
};
mytoc.getOriginalTOC=function(){
return $("div#toc.toc>ul").clone();
};
mytoc.OriginalTOCswitchButtonClickEventHandle=function(){
return function(){mytoc.OriginalTOCswitchButtonClickEvent()};
};
mytoc.OriginalTOCswitchButtonClickEvent=function(_on){
};
mytoc.fun_line_parse_body=function(text,ele_span){
return function(){
new mw.Api({async:false,cache:true})
.get({action:"parse","text":text.replace(/{{/g,"%7b%7b").replace(/}}/g,"%7d%7d"),
prop:"text",contentmodel:"wikitext",uselang:"user",
disablelimitreport:1,wrapoutputclass: "",
//maxage:600,servedby:1,curtimestamp:1
})
.done(function(data2){
ele_span.html(decodeURIComponent($(data2.parse.text["*"]).text()));
});
};
};
mytoc.fun_line_parse=function(text,ele_span){
setTimeout(mytoc.fun_line_parse_body(text,ele_span),1000);
};
mytoc.buildTOC=function(){
var rootul=$("<ul>");
new mw.Api({async:false})
.get({
action:"parse",
//page:fullpagename,
oldid:mytoc.oldid,
prop:"sections"
})
.done(function(data){
$(data.parse.sections).each(function(_x_,ele){
//console.debug(ele);
var anchor=ele.anchor;
var index=ele.index;
var toclevel=ele.toclevel;
var line=ele.line;
var number=ele.number;
//var level=ele.level;
var workul=rootul;
for(var l=1;l<=toclevel-1;l++){
var last_li=workul.find(">li:last-child");
workul=last_li.is(":has(>ul)")?last_li.children("ul"):($("<ul>").appendTo(last_li));
}
var ele_toctext=$("<span>").addClass("toctext");
$("<li>").addClass("toclevel-"+toclevel).addClass("tocsection-"+index).append(
$("<a>").attr("href","#"+anchor)
.append($("<span>").addClass("tocnumber").text(number))
.append(ele_toctext)
).appendTo(workul);
mytoc.fun_line_parse(line,ele_toctext);
});
if(data.parse.sections.length===0){
rootul.append($("<li>").text("没有目录数据"));
mytoc.switchButtonClickEventHandle(false);
}
});
return rootul;
};
mytoc.switchButtonClickEventHandle=function(_on){
var ele=$("#myTOC>#left");
var check=ele.hasClass("toc_closed");
if(check===_on){
ele.toggle({
effect:"slide",
duration:"slow",
direction:"left",
done:function(){ele.toggleClass("toc_closed");},
});
}
};
mytoc.buildFrame=function($rootul){
var outdiv=
$("<div>").attr("id","myTOC").addClass("noprint")
.css({ "position":"fixed", "top":"0px", "left":"0px", "z-index":"100", "padding":"0", });
//
var right=
$("<div>").attr("id","right")
.css({ "float":"right", "padding":"0", "margin":"0", "height":mytoc.bodyHeight, "width":"12px", });
var left=
$("<div>").attr("id","left")
.css({
"float":"left",
//"padding":"0",
"margin":"0",
"height":mytoc.bodyHeight,
"width":mytoc.bodyWidth,
});
//
var head=$("<div>").attr("id","toc_head")
.css({ "padding":"0", "margin":"0", "background-color":"white", "border":"1px solid #99B3FF", "text-align":"center", })
.append($("<a>").attr("href","#top").text("回到顶部"))
.append("·")
.append($("<a>").attr("href","#footer").text("直至底部"))
.append("<br/>")
.append($("<a>").attr("href","#").text("原生目录开关").click((function(){
return function(){$("#toc.toc").toggleClass("toc_closed");};
})()));
//
var body=$("<div>").attr("id","toc_body")
.css({
"width":"100%",
"background-color":"white",
"overflow-y":"auto",
"overflow-x":"scroll",
});
//
var toc_div=$("<div>").attr("id","toc_container").addClass("toc")
.css({
"margin":"0",
"padding-top":"0px",
"padding-bottom":"0px",
"padding-right":"0px",
"border-top":"0px",
"border-bottom":"0px",
"height":"100%",
"width":"100%",
"word-break":"keep-all",
}).append($rootul).appendTo(body);
var switchButton=
$("<div>").attr("id","button")
.css({ "border":"1px solid #99B3FF", "width":"1em", "padding":"0", "margin":"0", "position":"absolute", "top":"50%", "height":"3em", "margin-top":"-1em", "cursor":"pointer", })
.text("开关")
.click((function(){return function(eve){
mytoc.switchButtonClickEventHandle($("#myTOC>#left").hasClass("toc_closed"));
};})());
$("body").append(outdiv.append(left.append(head).append(body)).append(right.append(switchButton)));
body.css("height",left.outerHeight()-head.outerHeight());
};
$(mytoc.init);
})(document,window,jQuery,mediaWiki);
});