User:Sannse/monobook.js

// ==== List Columnizer ==== // Mostly written by Splarka - // Version 0.0.1 beta // Breaks large lists of text in the wiki textarea into multicolumn tables using templates that you define (below). // Current limitations // * Number of columns      : 2-10 (easily changed) // * Min number of elements : 11 // * String space           : browser // * Number of template tags: 3 (start/middle/end) // * Parameterized first tag: not yet // Note: will currently only work with a full edit box contents, use carefully

addOnloadHook(columnbutton); var colStart = ''; var colMid = ''; var colEnd = '';

function columnbutton { if(!document.getElementById('wpTextbox1')) return; var tabs = document.getElementById('p-cactions').getElementsByTagName('ul')[0]; if(document.getElementById('wpTextbox1').value.indexOf(colStart) == -1) { addlilink(tabs, 'javascript:columns', 'columns', 'ca-columns'); } else { addlilink(tabs, 'javascript:columnsoff', 'undo columns', 'ca-columns'); } }

function columns { var lines = document.getElementById('wpTextbox1').value.split('\n'); if(lines.length < 11) { alert('Oh come on, there are only ' + lines.length + ' lines.\nYou can do that many yourself!'); return; }

var evendiv = ''; for(var i = 2; i < 11; i++) { if(lines.length % i == 0) evendiv = evendiv + ' /' + i   } if(evendiv.length != 0) evendiv = 'This divides evenly by: ' + evendiv + '\n'

var numcol = prompt('There are currently ' + lines.length + ' lines.\n' + evendiv + 'Enter a number of columns from 2 to 10:','3') if((isNaN(numcol))||(numcol > 10)||(numcol < 2)) { alert('Invalid number or cancellation.'); return; }

if(lines.length % numcol == 0) { collen = lines.length / numcol; } else { collen = Math.ceil(lines.length / numcol); }

var tag=colStart; for(var i = 0; i < numcol; i++) { elementinsert(lines,collen * i + i,tag); tag=colMid; }   lines[lines.length] = colEnd; document.getElementById('wpTextbox1').value = lines.join('\n'); document.getElementById('ca-columns').innerHTML = 'undo columns'; document.getElementById('ca-columns').href = 'javascript:columnsoff'; }

function columnsoff { document.getElementById('ca-columns').innerHTML = 'columns'; document.getElementById('ca-columns').href = 'javascript:columns';

var lines = document.getElementById('wpTextbox1').value.split('\n'); var totag = 0 for(var i = 0; i < lines.length; i++) { if((lines[i] == colStart)||(lines[i] == colMid)||(lines[i] == colEnd)) { elementremove(lines,i+1); totag++; }   }    alert(totag + ' column tags removed.'); document.getElementById('wpTextbox1').value = lines.join('\n') }

function elementinsert(arr,loc,str) { for(var j = arr.length; j > loc; j--) arr[j] = arr[j-1]; arr[loc] = str; }

function elementremove(arr,loc) { for(var j = loc; j < arr.length; j++) arr[j-1] = arr[j]; arr.length = arr.length - 1; }

function addlilink(tabs, url, name, id) { var na = document.createElement('a'); na.href = url; na.id = id; na.appendChild(document.createTextNode(name)); var li = document.createElement('li'); li.appendChild(na); tabs.appendChild(li); return li; }

// ==== End List Columnizer ====