MediaWiki:Toc Toggle.js

/* src: https://irowiki.org/ */ var hid = 'https://playground.amazingwikis.org/wiki/images/general/Playground/a/ad/Arr_r.png'; var show = 'https://playground.amazingwikis.org/wiki/images/general/Playground/a/ad/Arr_d.png'; function tocTreeToggle(id) { var a = document.getElementById('toctog-a-' + id); var img = document.getElementById('toctog-i-' + id); var cul = a.parentNode.getElementsByTagName('ul'); var disp; if(a.getAttribute('class') == 'toctogShown') { disp = 'none'; a.setAttribute('class','toctogHidden'); img.setAttribute('src',hid); } else { disp = 'block'; a.setAttribute('class','toctogShown'); img.setAttribute('src',show); } for(var j=0;j<cul.length;j++) { if(cul[j].parentNode == a.parentNode) cul[j].style.display = disp } }

function tocTree { mw.util.addCSS('a.toctogHidden img, a.toctogShown img, a.toctogNull img {width:12px;height:12px;} #toc li a {padding-right:3px;} a.toctogNull img {visibility:hidden;}');

var toc = document.getElementById('toc'); if(!toc) return var li = toc.getElementsByTagName('li'); for (var i=0; i < li.length; i++) { var cul = li[i].getElementsByTagName('ul'); var a = document.createElement('a'); if(cul.length == 0) { a.setAttribute('class','toctogNull'); }    else { a.setAttribute('class','toctogShown'); a.setAttribute('id','toctog-a-' + i ); a.setAttribute('href','javascript:tocTreeToggle("' + i + '");'); }    var img = document.createElement('img'); //img.setAttribute('src',hid); img.setAttribute('id','toctog-i-' + i ); a.appendChild(img); li[i].insertBefore(a,li[i].firstChild); if(cul.length != 0) tocTreeToggle(i); } }

if (mw.config.get("wgContentNamespaces") != -1 && mw.config.get("wgArticleId") != 0) { tocTree; }