MediaWiki:Category Sorter/code.js

// __NOWYSIWYG__ '; };   Table.getAlphabet = function (titles) { var alphabet = []; for (var i = 0; i < titles.length; i++) { if ($.inArray(titles[i][0].toUpperCase, alphabet) === -1) { alphabet.push(titles[i][0].toUpperCase); }       }        return alphabet; };   Table.create = function (titles) { var title, table, alphabet = Table.getAlphabet(titles); table = new Table(           //Math.floor((alphabet.length + titles.length) / 3) + 1,            Math.max(1, Math.floor((alphabet.length * 1.25 + titles.length) / 3)),            'alpha'        ); if (titles.length > 200) { createSideNav(alphabet, 'alpha'); }       while (titles.length) { title = titles.shift; table.addLink(title, '/wiki/' + encodeURIComponent(title), title[0].toUpperCase); }       return table.toString; };   Table.rebuild = function (titles) { var title, table, alphabet = [], total = 0; if (!alphabet.length) { $.each(titles, function (first, list) {               alphabet.push(first);                total += list.length;            }); }       createSideNav(alphabet, 'sortkey'); table = new Table(Math.max(1, Math.floor((alphabet.length * 1.25 + total) / 3)), 'sortkey'); $.each(titles, function (first, list) {           while (list.length) {                title = list.shift;                table.addLink(title, '/wiki/' + encodeURIComponent(title), first);            }        }); while (titles.length) { title = titles.shift; table.addLink(title, '/wiki/' + encodeURIComponent(title), title[0].toUpperCase); }       return table.toString; };   function createSideNav (alphabet, cssClass) { var color = $('.WikiaPage').css('border-left-color'), links = []; for (var i = 0; i < alphabet.length; i++) { links.push('' + alphabet[i] + ''); }       $('head').append('.letters{width: 10px; padding: 5px; minHeight: 50px; left: 0; top: 0; border-top-left-radius: 4px; border-bottom-left-radius: 4px; border-top: 1px solid ' + color + '; border-left: 1px solid ' + color + '; border-bottom: 1px solid ' + color + '; border-right: none; position: fixed; background: ' + window.sassParams['color-body'] + '; z-index: 2; line-height: 140%; white-space: pre; text-align: center} '); var letters = $(' ') .appendTo(document.body) .addClass(cssClass) .html(links.join("\n")); var $win = $(window), pageWidth = $('#WikiaPage').width, lettersHeight = letters.height, setPosition = function { letters .css({                   top: Math.floor(($win.height - lettersHeight) / 2) + 'px',                    left: Math.floor(($win.width + $win.scrollLeft - pageWidth) / 2) - 20 + 'px'                }); };       setPosition; $win.resize(setPosition); }   function getCatMembers (cat, cmcontinue) { var defer = $.Deferred, url = '/api.php?format=json&action=query&list=categorymembers&cmprop=title|sortkeyprefix&cmtype=page&cmlimit=500'; url += '&cmtitle=Category:' + cat; if (cmcontinue) { url += '&cmcontinue=' + cmcontinue; }       $.getJSON(url) .done(function (data) {           if (data.query && data.query.categorymembers && $.isArray(data.query.categorymembers)) {                var lists = {                        sortkey: {},                        alpha: []                    },                    m, key,                    members = data.query.categorymembers;                for (var i = 0; i < members.length; i++) {                    m = members[i];                    key = (m.sortkeyprefix || m.title)[0].toUpperCase;                    lists.sortkey[key] = lists.sortkey[key] || [];                    lists.sortkey[key].push(m.title);                    lists.alpha.push(m.title);                }                if (data['query-continue'] && data['query-continue'].categorymembers && data['query-continue'].categorymembers.cmcontinue) {                    cmcontinue = data['query-continue'].categorymembers.cmcontinue; getCatMembers(cat, cmcontinue) .done(function (nextList) {                       $.each(lists.sortkey, function (i, v) { if (!nextList.sortkey[i]) return; lists.sortkey[i] = v.concat(nextList.sortkey[i]); delete nextList.sortkey[i]; });                       lists.sortkey = $.extend(lists.sortkey, nextList.sortkey);                        lists.alpha = lists.alpha.concat(nextList.alpha);                        defer.resolve(lists);                    }); } else { defer.resolve(lists); }           }        });        return defer.promise;    }    function getCat (cat) {        var defer = $.Deferred;        getCatMembers(cat || mw.config.get('wgTitle'))        .done(function (lists) { lists.alpha.sort; defer.resolve(lists); });       return defer.promise;    }    function addRadioButtons (createAlphaSort) {        var $p = pages.find('p').first;        $p.html($p.html + ' Sort by: '+ ' '+ ' Last Name ' + ' '+ ' First Name ' );       pages.find('input[name="sort"]')        .click(function  { $('.alpha').add('.sortkey').css('display', 'none'); $('.' + $(this).val).css('display', 'block'); })       .eq(1).one('click', createAlphaSort);    }    var table = pages.find('table').first.addClass('sortkey');    if (pages.children('a').length) { // category is oversized        var h2 = pages.children('h2').clone,            m = pages.children('p').text.match(/(\d+)\D+$/m),            total = m[1] || 'NaN';        pages.empty        .append(h2)        .append( ' This category contains ' + total + ' pages. ' +           ' '        );        getCat        .done(function (lists) { $('#cat-placeholder').remove; table = $(Table.rebuild(lists.sortkey)) .appendTo(pages); addRadioButtons(function {               table.after(Table.create(lists.alpha));            }); });   } else {        addRadioButtons(function  { var titles = []; table.find('a').each(function {                titles.push($(this).text);            }); table.after(Table.create(titles.sort)); });   } }(mediaWiki, jQuery)); //