User:Bradv/wikiBrowser.js

// WikiBrowser //     Category:Physics                -- sort by item //     Special:Contributions/Bradv     -- sort by timestamp - not implemented //     Special:NewPages                -- sort by timestamp - not implemented

wikiBrowser = { browsing: null }

wikiBrowser.init = function { wikiBrowser.loadCookies; if (mw.config.get('wgPageName').indexOf('Category:')==0) { addPortletLink ('p-cactions', '#', 'Browse category', 'wbStart'); document.getElementById('wbStart').setAttribute('onclick', 'wikiBrowser.startBrowsing;return false;'); };   wikiBrowser.drawBar(mw.config.get('wgPageName')); };

wikiBrowser.loadCookies=function { wikiBrowser.browsing = (wikiBrowser.readCookie('wikiBrowser_browsing')); };

wikiBrowser.saveCookies=function { var cend = "; expires=Tue, 31-Dec-2030 23:59:59 GMT; path=/"; document.cookie = 'wikiBrowser_browsing=' + wikiBrowser.browsing.toString + cend; };

wikiBrowser.readCookie=function(name) { var nameEQ = name + "="; var ca = document.cookie.split(';'); for(var i=0;i < ca.length;i++) { var c = ca[i]; while (c.charAt(0)==' ') { c = c.substring(1,c.length); } if (c.indexOf(nameEQ) === 0) { return c.substring(nameEQ.length,c.length); } }   return ''; };

wikiBrowser.startBrowsing=function { wikiBrowser.browsing = mw.config.get('wgPageName'); wikiBrowser.saveCookies; wikiBrowser.drawBar(null); };

wikiBrowser.drawBar=function(current) { if (wikiBrowser.browsing) { var wb = document.getElementById('wikiBrowser'); if (wb) { wb.parentNode.removeChild(wb) } wb = document.createElement('div'); wb.id='wikiBrowser'; with (wb.style) { position='fixed'; top='0px'; left='0px'; width='100%'; padding='.5em 1em'; height='2em'; lineHeight='2em'; backgroundColor=document.defaultView.getComputedStyle(document.body, null).backgroundColor; borderBottom='1px solid #bbbbbb'; zIndex=10; }       document.body.appendChild(wb); var h = wb.offsetHeight;

document.body.style.marginTop=h + 'px'; document.body.style.backgroundPosition='0 ' + h + 'px'; document.getElementById('p-logo').style.marginTop=h + 'px'; document.getElementById('p-cactions').style.marginTop=h + 'px'; document.getElementById('p-personal').style.marginTop=h + 'px'; if (document.getElementById('sprint')) { document.getElementById('sprint').style.marginTop=h + 'px'; }

var hdr = document.createElement('div'); hdr.id='wikiBrowser_header'; hdr.appendChild(document.createTextNode('Browsing ')); hdr.style.fontWeight='bold'; hdr.style.fontSize='1.2em'; hdr.style.position='absolute'; hdr.style.left='50%'; wb.appendChild(hdr); var aHdr = document.createElement('a'); aHdr.href = mw.config.get('wgArticlePath').replace('$1', wikiBrowser.browsing); aHdr.appendChild(document.createTextNode(wikiBrowser.browsing)); hdr.appendChild(aHdr); hdr.style.marginLeft= '-' + parseInt(hdr.offsetWidth)/2 + 'px';

var prev = document.createElement('div'); prev.id='wikiBrowser_prev'; prev.style.position='absolute'; prev.style.width='50%'; prev.style.left='0px'; prev.style.marginLeft = '-' + (parseInt(hdr.offsetWidth)/2 + 10) + 'px'; prev.style.textAlign='right'; wb.appendChild(prev); var aPrev = document.createElement('a'); prev.appendChild(aPrev);

var next = document.createElement('div'); next.id='wikiBrowser_next'; next.style.position='absolute'; next.style.width='50%'; next.style.left='50%'; next.style.marginLeft = (parseInt(hdr.offsetWidth)/2 + 10) + 'px'; next.style.textAlign='left'; wb.appendChild(next);

var aNext = document.createElement('a'); next.appendChild(aNext);

wikiBrowser.drawLinks(current, aPrev, aNext); } };

wikiBrowser.drawLinks=function(current, aPrev, aNext) { var ajaxPrev, ajaxNext; var url, tag, attr; if (wikiBrowser.browsing.indexOf('Category:')==0) { url = '/w/api.php?action=query&format=xml&list=categorymembers&cmlimit=1&cmtitle=' + wikiBrowser.browsing; if (current) { url += '&cmstartsortkey=' + current; }       url += '&cmdir='; tag = 'cm'; attr = 'title'; } else { return; }

ajaxPrev = sajax_init_object; if (ajaxPrev) { ajaxPrev.onreadystatechange = function { if (ajaxPrev.readyState == 4 && ajaxPrev.status == 200) { try { var el = ajaxPrev.responseXML.documentElement.getElementsByTagName(tag)[0]; aPrev.appendChild(document.createTextNode('← ' + el.getAttribute(attr))); aPrev.href = mw.config.get('wgArticlePath').replace('$1', el.getAttribute(attr)); } catch (e) { }           }        }    }

ajaxPrev.open("GET", url + 'desc', true); ajaxPrev.send(null);

ajaxNext = sajax_init_object; if (ajaxNext) { ajaxNext.onreadystatechange = function { if (ajaxNext.readyState == 4 && ajaxNext.status == 200) { try { var el = ajaxNext.responseXML.documentElement.getElementsByTagName(tag)[0]; aNext.appendChild(document.createTextNode(el.getAttribute(attr) + ' →')); aNext.href = mw.config.get('wgArticlePath').replace('$1', el.getAttribute(attr)); } catch (e) { }           }        }    }

ajaxNext.open("GET", url + 'asc', true); ajaxNext.send(null); };

$(wikiBrowser.init);