User:P1ayer/CatNav.js

// http://zh.wikipedia.org/w/index.php?title=User:Liangent/Scripts/CatNav.js

jQuery(function {   // ns:14 is category. avoid hard-coding?    if (mw.config.get('wgNamespaceNumber') != 14 || mw.config.get('wgArticleId') == 0 || (mw.config.get('wgAction') != 'view' && mw.config.get('wgAction') != 'purge')) return;    var CatNav = {};    CatNav.depthLimit = 8;    CatNav.pageName = mw.config.get('wgPageName').replace(/_/g, ' ');    CatNav.data = {};    CatNav.walked = {};    CatNav.path = [];    CatNav.pending = 0;    CatNav.categoryPrefix = new RegExp('^' + mw.config.get('wgFormattedNamespaces')[14] + ':');    CatNav.titlesLimit = ((mw.config.get('wgUserGroups').indexOf('sysop') != -1 || mw.config.get('wgUserGroups').indexOf('bot') != -1) ? 500 : 50);   CatNav.cllimitLimit = ((mw.config.get('wgUserGroups').indexOf('sysop') != -1 || mw.config.get('wgUserGroups').indexOf('bot') != -1) ? 5000 : 500);   CatNav.query = function(pages, clcontinue) {        if (pages.length > CatNav.titlesLimit) {            CatNav.query(pages.slice(0, CatNav.titlesLimit));            CatNav.query(pages.slice(CatNav.titlesLimit));        } else if (pages.length != 0) {            var titles = pages.join('|');            CatNav.pending++;            // since this will be repeated on every category page, i'd better filter hidden categories out            // rather than know which are hidden and hide them.            var data = {                action: 'query',                format: 'json',                rawcontinue: '',                titles: titles,                prop: 'categories',                clshow: '!hidden',                cllimit: CatNav.cllimitLimit            };            if (clcontinue) {                data.clcontinue = clcontinue;            }            jQuery.post(mw.config.get('wgScriptPath') + '/api.php', data, function(data) { CatNav.pending--; var toquery = []; for (var pageid in data.query.pages) { var page = data.query.pages[pageid]; var pagename = page.title; jQuery.each(page.categories ? page.categories : [], function {                       CatNav.data[pagename][this.title] = true;                        if (!CatNav.data[this.title]) {                            CatNav.data[this.title] = {};                            toquery.push(this.title);                        }                    }); }               if (data['query-continue']) { CatNav.query([titles], data['query-continue'].categories.clcontinue); }               CatNav.query(toquery); if (CatNav.pending == 0) { CatNav.walk(CatNav.pageName); }           }, 'json');        }    };    CatNav.walk = function(page) {        if (CatNav.path.length == CatNav.depthLimit) return;        if (CatNav.walked[page]) {            CatNav.path.push(page);            CatNav.display(true);            CatNav.path.pop;        } else {            CatNav.walked[page] = true;            CatNav.path.push(page);            var output = true;            for (var parent in CatNav.data[page]) {                output = false;                CatNav.walk(parent);            }            if (output) {                CatNav.display;            }            CatNav.path.pop;            CatNav.walked[page] = false;        }    };    CatNav.display = function(loop) {        var div = jQuery(' ').addClass('catnav');        var isfirst = true;        jQuery.each(CatNav.path, function { if (!isfirst) { div.prepend(' &gt; '); } else { isfirst = false; }           div.prepend(jQuery('').attr( 'href', encodeURIComponent(this).replace(/(.*)/, mw.config.get('wgArticlePath')) ).text(this.replace(CatNav.categoryPrefix, ''))); });       if (loop) {            div.prepend('[...] &gt; ');        }        div.insertBefore('.printfooter');    };    CatNav.data[CatNav.pageName] = {};    CatNav.query([CatNav.pageName]); });