User:Ras52/mountainlist.js

function mountainlist_init { var idnum = 0; // Find all tables with class sortable and make them sortable var tables = getElementsByClassName(document, "table", "mountainlist"); for (var ti = 0; ti < tables.length ; ti++) { if (!tables[ti].id) { tables[ti].setAttribute('id','mountainlist_table_id_'+idnum); ++idnum; }		mountainlist_initTable(tables[ti]); } } function mountainlist_initTable(table) { var firstRow; if (table.rows && table.rows.length > 0) { if (table.tHead && table.tHead.rows.length > 0) { firstRow = table.tHead.rows[table.tHead.rows.length-1]; } else { firstRow = table.rows[0]; }	}	if (!firstRow) return; // We have a first row: assume it's the header var prom, height; for (var i = 0; i < firstRow.cells.length; i++) { var cell = firstRow.cells[i]; if ((" "+cell.className+" ").indexOf(" prominence ") != -1) prom = cell; if ((" "+cell.className+" ").indexOf(" height ") != -1) height = cell; }	if (prom && height && mountainlist_display_eminence) { mountainlist_addEminence(table, firstRow, prom.cellIndex, height.cellIndex); }	for (var i = 0; i < firstRow.cells.length; i++) { var cell = firstRow.cells[i]; if ((" "+cell.className+" ").indexOf(" metres ") != -1 && mountainlist_convert_metres_to_feet) { mountainlist_doConv(table, cell.cellIndex, 3.28084); cell.className.replace( /\bmetres\b/, "feet" ); // Heuristics to edit the title mountainlist_editCellTitle( cell, ["metres", "feet", "metre", "foot", "meters", "feet", "meter", "foot", "m", "ft"] ); }		else if ((" "+cell.className+" ").indexOf(" feet ") != -1 && mountainlist_convert_feet_to_metres) { mountainlist_doConv(table, cell.cellIndex, 1/3.28084); cell.className = cell.className.replace( /\bmetres\b/, "feet" ); // Heuristics to edit the title mountainlist_editCellTitle( cell, ["feet", "metres", "foot", "metre", "feet", "meters", "foot", "meter", "ft", "m"] ); }		if ((" "+cell.className+" ").indexOf(" prominence ") != -1) prom = cell; if ((" "+cell.className+" ").indexOf(" height ") != -1) height = cell; } } function mountainlist_addEminence(table, firstRow, prom, height) { while (table && !(table.tagName && table.tagName.toLowerCase == 'table')) table = table.parentNode; if (!table) return; // Work out a type for the column if (table.rows.length <= 1) return; // Skip the first row if that's where the headings are var rowStart = (table.tHead && table.tHead.rows.length > 0 ? 0 : 1); for (var i = rowStart; i < table.rows.length; i++) { if (table.rows[i].cells.length > prom && table.rows[i].cells.length > height) { var h = mountainlist_parseCellAsInt(table.rows[i].cells[height]); var p = mountainlist_parseCellAsInt(table.rows[i].cells[prom]); var val = mountainlist_addCommas( Math.round( Math.sqrt(h*p) ) ); table.rows[i].innerHTML += ' '+val+' '; }	}	var classname = , unit = ; if ((" "+firstRow.cells[prom].className+" ").indexOf(" metres ") != -1 && (" "+firstRow.cells[prom].className+" ").indexOf(" metres ") != -1) { classname = 'metres'; unit = ' (m)'; }	else if ((" "+firstRow.cells[prom].className+" ").indexOf(" metres ") != -1 && (" "+firstRow.cells[prom].className+" ").indexOf(" feet ") != -1) { classname = 'feet'; unit = ' (ft)'; }

// Code copied from sortable table code. // NOTE: This isn't a robust implementation strategy: what if the internal workings of that code change? // We shouldn't be relying on its internals, but equally we can't just use the public interface. var sortbutton = ''; if ((" "+table.className+" ").indexOf(" sortable ") != -1) { sortbutton = '   '; }

firstRow.innerHTML += 'Eminence' + unit + sortbutton + ' '; } function mountainlist_doConv(table, column, factor) { while (table && !(table.tagName && table.tagName.toLowerCase == 'table')) table = table.parentNode; if (!table) return; // Work out a type for the column if (table.rows.length <= 1) return; // Skip the first row if that's where the headings are var rowStart = (table.tHead && table.tHead.rows.length > 0 ? 0 : 1); for (var i = rowStart; i < table.rows.length; i++) { if (table.rows[i].cells.length > column) { var val = Math.round(mountainlist_parseCellAsInt(table.rows[i].cells[column]) * factor); table.rows[i].cells[column].innerHTML = mountainlist_addCommas(val); }	} } function mountainlist_addCommas(str) { str += ''; x = str.split('.'); x1 = x[0]; x2 = x.length > 1 ? '.' + x[1] : ''; var rgx = /(\d+)(\d{3})/; while (rgx.test(x1)) { x1 = x1.replace(rgx, '$1' + ',' + '$2'); }	return x1 + x2; }

function mountainlist_parseCellAsInt(cell) { return parseInt( cell.innerHTML.replace( /^\s+([0-9,.]+).*$/, '$1' ).replace( /,/g, '' ) ); } function mountainlist_editCellTitle(cell, vals) { if (vals.length % 2) return; for (var i = 0; i < vals.length; i+=2) cell.innerHTML = cell.innerHTML.replace(new RegExp("\\b" + vals[i] + "\\b"), vals[i+1]); } addOnloadHook(mountainlist_init);