Jump to content

User:Evad37/CatVision.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
//initial setup
var $cats_ul = $('#mw-normal-catlinks ul');                       //get list
var $cats_li = $cats_ul.children('li');                           //get list items
var catlist = '';                                                 //will contain a list of all cats on page   
$cats_li.each(function( index ) {                                 //add sequential ids to items so default order can be restored
    if (index < 10) {
        var position = '00' + index;
    } else if (index < 100) {  
        var position = '0' + index;
    } else {
        var position = index;
    }
    $( this ).prop( "id", position );
    var pipe_if_needed = (index == 0) ? '' : '%7C';              
    catlist = catlist + pipe_if_needed + 'Category:' + $(this).text(); //build up list of cats
});

$('#mw-normal-catlinks').wrap("<div id='catColWrap'></div>");     //wrap with div - columns will be turned on/off by setting style here

//main functions
function cat_sort_alpha() {
    $cats_li.sort(function(a,b) {
        if(a.children[0].textContent > b.children[0].textContent) return 1;
        return -1;
    });
    $cats_li.detach().prependTo($cats_ul);
}

function cat_sort_orig() {
    $cats_li.sort(function(a,b) {
        if(a.getAttribute("id") > b.getAttribute("id")) return 1;
        return -1;
    });
    $cats_li.detach().prependTo($cats_ul);
}

function cat_show_cols() {
    $('#catColWrap').attr("style", "-moz-column-width: 30em; -webkit-column-width: 30em; column-width: 30em;");
    $('#catColWrap > div > ul > li').attr("style", "display:block;");
}

function cat_no_cols() {
    $('#catColWrap').attr("style", "");
    $('#catColWrap > div > ul > li').attr("style", "");
}

//interface 
var catvision_links = "<input id='check_cols' type='checkbox' onclick='toggle_cols()'><label for='check_cols'>Show in columns</label>&nbsp; <input id='check_sort' type='checkbox' onclick='toggle_sort()'><label for='check_sort'>Sort alphabetical</label>";
$('#catlinks').prepend(catvision_links);

function toggle_cols() {
    if ($('#check_cols').prop( "checked" )) {
        cat_show_cols();
    } else {
        cat_no_cols();
    }
}

function toggle_sort() {
    if ($('#check_sort').prop( "checked" )) {
        cat_sort_alpha();
    } else {
        cat_sort_orig();
    }
}