User:Evad37/CatVision.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:Evad37/CatVision. |
//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> <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();
}
}