Разлика между версии на „МедияУики:PageNumbers.js“
(Нова страница: /********************************* * page numbers for proofreadpage * by [wikisource:user:ThomasV] 31 Aug 2012 * https://en.wikisource.org/wiki/MediaWiki:PageNumbers.js...) |
|||
Ред 1: | Ред 1: | ||
/********************************* | /********************************* | ||
− | |||
− | |||
* https://en.wikisource.org/wiki/MediaWiki:PageNumbers.js | * https://en.wikisource.org/wiki/MediaWiki:PageNumbers.js | ||
**********************************/ | **********************************/ | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
function get_elements_by_classname(classname, tagname) { | function get_elements_by_classname(classname, tagname) { | ||
Ред 114: | Ред 105: | ||
if(document.getElementById("text-wrap")) { | if(document.getElementById("text-wrap")) { | ||
− | var optlist = get_optlist(); | + | //var optlist = get_optlist(); |
− | mw.util.addPortletLink ('p-displayOptions', 'javascript:toggle_layout();', ws_msg('layout'), 'option-textLayout', '' ); | + | //mw.util.addPortletLink ('p-displayOptions', 'javascript:toggle_layout();', ws_msg('layout'), 'option-textLayout', '' ); |
layout_num = 0; | layout_num = 0; | ||
Ред 210: | Ред 201: | ||
if( !self.proofreadpage_source_href || self.pagenum_ml.length==0) return; | if( !self.proofreadpage_source_href || self.pagenum_ml.length==0) return; | ||
− | var optlist = get_optlist(); | + | //var optlist = get_optlist(); |
− | mw.util.addPortletLink ('p-displayOptions', 'javascript:hide_pagenumbers();', ws_msg('hide_page_numbers'), 'option-pageNumbers', '', 'n' ); | + | //mw.util.addPortletLink ('p-displayOptions', 'javascript:hide_pagenumbers();', ws_msg('hide_page_numbers'), 'option-pageNumbers', '', 'n' ); |
var cs = self.pagenum_ml[0].parentNode.parentNode; | var cs = self.pagenum_ml[0].parentNode.parentNode; | ||
Ред 280: | Ред 271: | ||
for(var i=self.pagenum_ml.length-1; i>=0; i--) { | for(var i=self.pagenum_ml.length-1; i>=0; i--) { | ||
var a = self.pagenum_ml[i]; | var a = self.pagenum_ml[i]; | ||
− | var num = | + | |
+ | var num = a.id; | ||
if(num.match(/^.CE.9E/)) num="Ξ"; | if(num.match(/^.CE.9E/)) num="Ξ"; | ||
if(num.match(/^.E2.80.93/)) num="—"; | if(num.match(/^.E2.80.93/)) num="—"; | ||
if(num.match(/^.E2.80.94/)) num="—"; | if(num.match(/^.E2.80.94/)) num="—"; | ||
var page = a.title; | var page = a.title; | ||
− | var pagetitle = | + | var pagetitle = page; |
var page_url = wgArticlePath.replace("$1", encodeURIComponent( pagetitle.replace(/ /g,"_") ) ); | var page_url = wgArticlePath.replace("$1", encodeURIComponent( pagetitle.replace(/ /g,"_") ) ); | ||
// encodeURIComponent encodes '/', which breaks subpages | // encodeURIComponent encodes '/', which breaks subpages | ||
Ред 291: | Ред 283: | ||
var ll = a.parentNode.nextSibling; | var ll = a.parentNode.nextSibling; | ||
if(ll && ll.tagName=="A" && ll.className=="new") class_str=" class=\"new\" "; else class_str=""; | if(ll && ll.tagName=="A" && ll.className=="new") class_str=" class=\"new\" "; else class_str=""; | ||
− | var link_str = "<a href=\""+page_url+"\"" + class_str + " title=\""+ | + | |
+ | var link_str = "<a href=\""+page_url+"\"" + class_str + " title=\""+pagetitle+"\">"+num+"</a>"; | ||
+ | |||
if(self.proofreadpage_numbers_inline){ | if(self.proofreadpage_numbers_inline){ | ||
a.innerHTML = " <span class=\"pagenumber noprint\" style=\"color:#666666; display:inline; margin:0px; padding:0px;\">[<b>"+link_str+"</b>]</span> "; | a.innerHTML = " <span class=\"pagenumber noprint\" style=\"color:#666666; display:inline; margin:0px; padding:0px;\">[<b>"+link_str+"</b>]</span> "; |
Текуща версия към 13:51, 19 декември 2013
/********************************* * https://en.wikisource.org/wiki/MediaWiki:PageNumbers.js **********************************/ function get_elements_by_classname(classname, tagname) { if(document.getElementsByClassName) return document.getElementsByClassName(classname); var ml = new Array(); var l = document.getElementsByTagName(tagname); for (var i=0; i< l.length; i++) { var a = l[i]; if( a.className.indexOf(classname) !=-1 ) ml[ml.length] = l[i]; } return ml; } function get_optlist() { var optlist = document.getElementById("optlist"); if(!optlist) { var displayOptions = document.createElement("div"); if (self.skin=='vector' || self.skin=='workroom') displayOptions.className = "portal collapsed"; else displayOptions.className = "portlet"; if (self.skin=='vector' || self.skin=='workroom') cl="body"; else cl="pBody"; displayOptions.innerHTML = '<h5>' + ws_msg('optlist') + '<\/h5><div class="'+cl+'"><ul id="optlist"></ul><\/div>'; var ptb = document.getElementById("p-tb"); ptb.parentNode.insertBefore(displayOptions,ptb); displayOptions.setAttribute("id","p-displayOptions"); displayOptions.id="p-displayOptions"; /* */ optlist = document.getElementById("optlist"); } return optlist; } var layout_num = 0; function toggle_layout() { var n=0; for( key in self.ws_layouts ) n++; layout_num = (layout_num+1) % n; $.cookie("layout",""+layout_num); set_layout(layout_num); } if(!self.ws_layouts) self.ws_layouts = {} function set_layout(layout_num) { var i=0; for( key in self.ws_layouts ) { if(i==layout_num) layout_name = key; i++; } d = self.ws_layouts[layout_name]; for( key in d ) { if(key.substring(0,1)=='.') { var t = get_elements_by_classname( key.substring(1,key.length) ,"span"); for(var i=0; i<t.length; i++) t[i].style.cssText = d[key]; } else if(key.substring(0,1)=='#') { var item = document.getElementById(key.substring(1,key.length)); if(item) item.style.cssText = d[key]; } } o_a = document.getElementById("option-textLayout"); if(o_a) o_a.innerHTML = "<a href='javascript:toggle_layout();'>"+layout_name+"</a>"; refresh_pagenumbers(); } function add_page_container(){ if(wgNamespaceNumber!=0 && wgNamespaceNumber!=2 ) return; var id = null; if(wgAction=="view"||wgAction=="purge") id = "#bodyContent"; if(wgAction=="submit") id = "#wikiPreview"; if (!id) return; if( document.getElementById("text-wrap") ) return; if( !self.proofreadpage_source_href && get_elements_by_classname("pagenum","span").length==0 ) return; var t = get_elements_by_classname("text","div"); for(var i=0; i<t.length; i++) t[i].className =""; t = get_elements_by_classname("lefttext","div"); for(var i=0; i<t.length; i++) t[i].className =""; t = get_elements_by_classname("centertext","div"); for(var i=0; i<t.length; i++) t[i].className =""; t = get_elements_by_classname("indented-page","div"); for(var i=0; i<t.length; i++) t[i].className =""; t = get_elements_by_classname("prose","div"); for(var i=0; i<t.length; i++) t[i].className =""; $(id).prepend($('div#contentSub')); $('div#contentSub').nextUntil('div.printfooter').wrapAll("<div id='text-wrap'><div id='text-container'><div id='text'></div></div></div><div style='clear:both'></div>"); } function set_layout_by_name(name){ if (self.ws_layouts[name]==undefined) return; //doesn't exists var i=0; for( key in self.ws_layouts ) { if(key==name) break; i++; } layout_num = i; set_layout(i) } function init_page_layout() { if(self.debug_page_layout) return; if(document.URL.indexOf("match=") > 0 ) return; if(document.URL.indexOf("diff=") > 0 ) return; var k=false; for( key in self.ws_layouts ) { k = true;} if(!k) return; add_page_container(); if(document.getElementById("text-wrap")) { //var optlist = get_optlist(); //mw.util.addPortletLink ('p-displayOptions', 'javascript:toggle_layout();', ws_msg('layout'), 'option-textLayout', '' ); layout_num = 0; if ($('#dynamic_layout_overrider').size()) { var layout_name = $('#dynamic_layout_overrider').text(); set_layout_by_name(layout_name); } else { /* FIXME: the cookie also must store the selected layout by name, but it must be backward compatible with the storing by integer */ layout = $.cookie("layout") ; if (layout) layout_num = parseInt( layout ); if(!layout_num || layout_num == NaN) layout_num = 0; set_layout(layout_num); } } } function hide_pagenumbers(){ var ml = get_elements_by_classname( 'pagenumber', 'span' ); for(var i=0; i<ml.length; i++) { ml[i].style.display="none"; } o_a = document.getElementById("option-pageNumbers"); if(o_a) { o_a.innerHTML = "<a href='javascript:show_pagenumbers();'>"+ws_msg('show_page_numbers')+"</a>"; } $.cookie("pagenum","0") ; } function show_pagenumbers(){ var ml = get_elements_by_classname( 'pagenumber', 'span' ); for(var i=0; i<ml.length; i++) { ml[i].style.display=""; } o_a = document.getElementById("option-pageNumbers"); if(o_a){ o_a.innerHTML = "<a href='javascript:hide_pagenumbers();'>"+ws_msg('hide_page_numbers')+"</a>"; } $.cookie("pagenum","1") ; } // see http://stackoverflow.com/questions/1472842/firefox-and-chrome-give-different-values-for-offsettop function get_offset(item) { var ox=0; var oy=0; var zme = item; for(zmi=0;zmi<50;zmi++) { if(zme+1 == 1 ) { break; } else { ox+=zme.offsetLeft; oy+=zme.offsetTop; } zme=zme.offsetParent; } return [ox,oy, item.offsetTop]; } function pagenum_over(ox,oy,prev_ox,prev_oy,h,w) { if(self.proofreadpage_disable_highlighting) return true; var ct = document.getElementById("ct-popup"); if(!ct) return true; var dd = h/10; ct.style.top = (oy-h+dd)+"px"; ct.style.height= ""; ct.firstChild.style.width = (w-ox)+"px"; ct.firstChild.nextSibling.style.height = (prev_oy-oy-h)+"px"; ct.firstChild.nextSibling.nextSibling.style.width = prev_ox+"px"; return true; } function pagenum_out(){ if(self.proofreadpage_disable_highlighting) return true; var ct = document.getElementById("ct-popup"); if(!ct) return true; ct.style.height= "0px"; ct.firstChild.style.width = "0px"; ct.firstChild.nextSibling.style.height = "0px"; ct.firstChild.nextSibling.nextSibling.style.width = "0px"; return true; } function init_page_numbers() { if(document.URL.indexOf("match=") > 0 ) return; self.pagenum_ml = get_elements_by_classname( 'pagenum', 'span' ); if( !self.proofreadpage_source_href || self.pagenum_ml.length==0) return; //var optlist = get_optlist(); //mw.util.addPortletLink ('p-displayOptions', 'javascript:hide_pagenumbers();', ws_msg('hide_page_numbers'), 'option-pageNumbers', '', 'n' ); var cs = self.pagenum_ml[0].parentNode.parentNode; //this happens for empty pages ; check if there are other cases if(cs.tagName=="P") cs = cs.parentNode; /* Measure the line height and the height of an empty span */ var ct = document.createElement("div"); ct.setAttribute("id","my-ct"); cs.insertBefore(ct,cs.firstChild); var ss = document.createElement("div"); /*we need a div, not a span*/ ss.innerHTML=" <span></span>"; /*empty span following some text */ ss.setAttribute("id","my-ss"); cs.appendChild(ss); /* container for page numbers */ var div_pagenums = document.createElement("div"); div_pagenums.setAttribute("id","ct-pagenums"); /* insert the container in the grandparent node, or parent, or self */ var mcs = document.getElementById("text-wrap"); if(!mcs) { if(cs.parentNode.parentNode.style.position=="relative") mcs = cs.parentNode.parentNode; else if(cs.parentNode.style.position=="relative") mcs = cs.parentNode; else { mcs = cs; mcs.style.position = "relative"; } } mcs.appendChild(div_pagenums); /* container for the highlight */ var div_popup = document.createElement("div"); var opacity="background-color:#CC6600;opacity:0.1;-ms-filter:alpha(opacity=20);filter:alpha(opacity=20);"; div_popup.setAttribute("id","ct-popup"); div_popup.style.cssText = "position:absolute;width:100%;"; div_popup.innerHTML = "<div style=\""+opacity+"float:right;width:0px;\" > </div>" +"<div style=\""+opacity+"width:100%;height:0px;clear:both;\"></div>" +"<div style=\""+opacity+"width:0px;\"> </div>"; cs.appendChild(div_popup); refresh_pagenumbers(); } function refresh_pagenumbers() { var ct = document.getElementById("my-ct"); if(!ct) return; //create a dummy pagenumber at the end to provide the end position //because this is often not found correctly, especially with gaps in the page ranges $('<span id="dummy-pagenumber" class="pagenum" style="display:none;"></span>').appendTo('div#mw-content-text'); var ss = document.getElementById("my-ss"); var cs = ct.parentNode; /* set it to relative because the highlight is of width 100% */ cs.style.position = "relative"; /* fixme : this interacts with layouts */ var lineheight = ss.offsetHeight; var offset_h = ss.lastChild.offsetHeight; var linewidth = cs.offsetWidth; var ct_o = get_offset(ct); var ct_ox = ct_o[0]; var ct_oy = ct_o[1]; var oo = get_offset(ss); var ox = oo[0] - ct_ox; var oy = oo[1] - ct_oy; var oh =""; for(var i=self.pagenum_ml.length-1; i>=0; i--) { var a = self.pagenum_ml[i]; var num = a.id; if(num.match(/^.CE.9E/)) num="Ξ"; if(num.match(/^.E2.80.93/)) num="—"; if(num.match(/^.E2.80.94/)) num="—"; var page = a.title; var pagetitle = page; var page_url = wgArticlePath.replace("$1", encodeURIComponent( pagetitle.replace(/ /g,"_") ) ); // encodeURIComponent encodes '/', which breaks subpages page_url = page_url.replace(/%2F/g, '/'); var ll = a.parentNode.nextSibling; if(ll && ll.tagName=="A" && ll.className=="new") class_str=" class=\"new\" "; else class_str=""; var link_str = "<a href=\""+page_url+"\"" + class_str + " title=\""+pagetitle+"\">"+num+"</a>"; if(self.proofreadpage_numbers_inline){ a.innerHTML = " <span class=\"pagenumber noprint\" style=\"color:#666666; display:inline; margin:0px; padding:0px;\">[<b>"+link_str+"</b>]</span> "; } else { if(a.offsetTop==0 && i!=0) { //if(self.proofreadpage_debug) alert("zero2 "+a.id+" "+a.parentNode.offsetTop); continue; } prev_ox = ox; prev_oy = oy; a_o = get_offset(a); ox = a_o[0] - ct_ox; oy = a_o[1] - ct_oy + offset_h; if(prev_oy - oy > 5) { oh = oh + "<div class=\"pagenumber noprint\" onmouseover=\"pagenum_over("+ ox + "," + oy + "," + prev_ox + "," + prev_oy + "," + lineheight + "," + linewidth + ");\" onmouseout=\"pagenum_out();\" style=\"position:absolute; left:-4em; top:"+(oy+cs.offsetTop-lineheight)+"px; text-indent:0em; text-align:left; font-size:80%; font-weight:normal; font-style: normal;\">["+link_str+"]</div>"; } } } if(!self.proofreadpage_numbers_inline) { var ct_pagenums = document.getElementById("ct-pagenums"); ct_pagenums.innerHTML = oh; } pagenum_out(); if( parseInt($.cookie("pagenum")) == 0 ) hide_pagenumbers(); $('#dummy-pagenumber').remove(); //don't leave this hanging around to confuse other scripts } if ($.inArray(mw.config.get('wgAction'), [ 'view', 'submit' ]) != -1) { //fixme : this is sensitive to order (detection of containers with "relative" position) addOnloadHook(init_page_layout); addOnloadHook(init_page_numbers); }