Difference between revisions of "MediaWiki:Common.js"
m |
m |
||
Line 128: | Line 128: | ||
* Creates a generic calculator. | * Creates a generic calculator. | ||
*/ | */ | ||
− | function Calculator(id, title, content) { | + | function Calculator(id, title, content, contentLength) { |
var location = document.getElementById(id); | var location = document.getElementById(id); | ||
Line 142: | Line 142: | ||
var calcTableHeader = document.createElement('th'); | var calcTableHeader = document.createElement('th'); | ||
− | calcTableHeader.colSpan = | + | calcTableHeader.colSpan = contentLength; |
calcTableHeader.appendChild(document.createTextNode(title)); | calcTableHeader.appendChild(document.createTextNode(title)); | ||
calcTableRow.appendChild(calcTableHeader); | calcTableRow.appendChild(calcTableHeader); | ||
Line 153: | Line 153: | ||
} | } | ||
− | function | + | function LuckToChanceCalculator() { |
− | + | var calcTableRow = document.createElement('tr'); | |
− | + | var calcTableCell = document.createElement('td'); | |
+ | var luckInput = document.createElement('input'); | ||
+ | var calculateButton = document.createElement('input'); | ||
+ | var percentChanceLabel = document.createElement('div'); | ||
− | + | function buildCalculator() { | |
calcTableCell.style.textAlign = 'right'; | calcTableCell.style.textAlign = 'right'; | ||
− | calcTableCell.appendChild(document.createTextNode(' | + | calcTableCell.appendChild(document.createTextNode('Luck:')); |
+ | calcTableRow.appendChild(calcTableCell); | ||
+ | |||
+ | luckInput.id = 'luckInput'; | ||
+ | luckInput.maxLength = '4'; | ||
+ | luckInput.type = 'text'; | ||
+ | luckInput.style.width = '35px'; | ||
+ | calcTableCell = document.createElement('td'); | ||
+ | calcTableCell.appendChild(luckInput); | ||
+ | calcTableRow.appendChild(calcTableCell); | ||
+ | |||
+ | calculateButton.type = 'button'; | ||
+ | calculateButton.value = 'Calculate >>'; | ||
+ | calculateButton.onclick = calculation(luckInput, percentChanceLabel); | ||
+ | calcTableCell = document.createElement('td'); | ||
+ | calcTableCell.appendChild(calculateButton); | ||
+ | calcTableRow.appendChild(calcTableCell); | ||
+ | |||
+ | percentChanceLabel.appendChild(document.createTextNode('0.000% Chance')); | ||
+ | percentChanceLabel.style.textAlign = 'right'; | ||
+ | percentChanceLabel.style.width = '110px'; | ||
+ | calcTableCell = document.createElement('td'); | ||
+ | calcTableCell.appendChild(percentChanceLabel); | ||
calcTableRow.appendChild(calcTableCell); | calcTableRow.appendChild(calcTableCell); | ||
Line 165: | Line 190: | ||
} | } | ||
− | Calculator('luckToChance', 'Luck to Chance', | + | function calculation(source, destination) { |
+ | if (source.value) { | ||
+ | if (isNumeric(source.value)) { | ||
+ | var percentChance = Math.pow(source.value, (5 / 9)); | ||
+ | if (percentChance > 100) { | ||
+ | percentChance = 100; | ||
+ | } | ||
+ | percentChance = percentChance.toFixed(3); | ||
+ | removeChildren(destination); | ||
+ | destination.appendChild(document.createTextNode(percentChance + '% Chance')); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | Calculator('luckToChance', 'Luck to Chance', buildCalculator, '4'); | ||
} | } | ||
Line 237: | Line 276: | ||
} | } | ||
− | addOnloadHook( | + | addOnloadHook(LuckToChanceCalculator); |
Revision as of 13:30, 16 February 2010
/* Any JavaScript here will be loaded for all users on every page load. */ /** * Determines if a particular element possesses a class. */ var hasClass = (function () { var reCache = {}; return function (element, className) { return (reCache[className] ? reCache[className] : (reCache[className] = new RegExp('(?:\\s|^)' + className + '(?:\\s|$)'))).test(element.className); }; })(); /** * Determines if a given value is a number. */ function isNumeric(value) { return new RegExp(/(^\d+$)|(^\d+\.\d+$)/).test(value); } /** * Removes all children from an element. */ function removeChildren(element) { while (element.hasChildNodes()) { element.removeChild(element.firstChild); } } /** * Code for collapsible tables. */ var collapseCaption = 'hide'; var expandCaption = 'show'; function collapseTable(tableIndex) { var button = document.getElementById('collapseButton' + tableIndex); var table = document.getElementById('collapsibleTable' + tableIndex); if (!table || !button) { return false; } var rows = table.rows; if (button.firstChild.data == collapseCaption) { for (var i = 1; i < rows.length; i++) { rows[i].style.display = 'none'; } button.firstChild.data = expandCaption; } else { for (var i = 1; i < rows.length; i++) { rows[i].style.display = rows[0].style.display; } button.firstChild.data = collapseCaption; } } function createCollapseButtons() { var tableIndex = 0; var navigationBoxes = new Object(); var tables = getElementsByClassName(document, 'table', 'collapsible'); for (var i = 0; i < tables.length; i++) { var headerRow = tables[i].getElementsByTagName('tr')[0]; if (!headerRow) { continue; } var header = headerRow.getElementsByTagName('th')[0]; if (!header) { continue; } navigationBoxes[tableIndex] = tables[i]; tables[i].setAttribute('id', 'collapsibleTable' + tableIndex); var spacer = document.createElement('div'); var button = document.createElement('span'); var buttonLink = document.createElement('a'); var buttonText = document.createTextNode(collapseCaption); spacer.style.cssFloat = 'left'; spacer.style.styleFloat = 'left'; spacer.style.textAlign = 'left'; spacer.style.width = '35px'; spacer.appendChild(document.createTextNode('\u00a0')); header.insertBefore(spacer, header.childNodes[0]); button.style.cssFloat = 'right'; button.style.styleFloat = 'right'; button.style.fontSize = '11px'; button.style.fontWeight = 'normal'; button.style.textAlign = 'right'; button.style.width = '35px'; buttonLink.style.color = '#FFFFFF'; buttonLink.id = 'collapseButton' + tableIndex; buttonLink.href = 'javascript:collapseTable(' + tableIndex + ');'; buttonLink.appendChild(buttonText); button.appendChild(document.createTextNode('[')); button.appendChild(buttonLink); button.appendChild(document.createTextNode(']')); header.insertBefore(button, header.childNodes[0]); tableIndex++; } for (var i = 0; i < tableIndex; i++) { if (hasClass(navigationBoxes[i], 'collapsed') || (tableIndex >= 2 && hasClass(navigationBoxes[i], 'autocollapse'))) { collapseTable(i); } else if (hasClass(navigationBoxes[i], 'innercollapse')) { var element = navigationBoxes[i]; while (element = element.parentNode) { if (hasClass(element, 'outercollapse')) { collapseTable(i); break; } } } } } addOnloadHook(createCollapseButtons); /** * Creates a generic calculator. */ function Calculator(id, title, content, contentLength) { var location = document.getElementById(id); if (location) { var table = document.createElement('table'); table.className = 'uoguidetable open'; var calcTableBody = document.createElement('tbody'); table.appendChild(calcTableBody); var calcTableRow = document.createElement('tr'); calcTableBody.appendChild(calcTableRow); var calcTableHeader = document.createElement('th'); calcTableHeader.colSpan = contentLength; calcTableHeader.appendChild(document.createTextNode(title)); calcTableRow.appendChild(calcTableHeader); calcTableRow = content(); calcTableBody.appendChild(calcTableRow); location.appendChild(table); } } function LuckToChanceCalculator() { var calcTableRow = document.createElement('tr'); var calcTableCell = document.createElement('td'); var luckInput = document.createElement('input'); var calculateButton = document.createElement('input'); var percentChanceLabel = document.createElement('div'); function buildCalculator() { calcTableCell.style.textAlign = 'right'; calcTableCell.appendChild(document.createTextNode('Luck:')); calcTableRow.appendChild(calcTableCell); luckInput.id = 'luckInput'; luckInput.maxLength = '4'; luckInput.type = 'text'; luckInput.style.width = '35px'; calcTableCell = document.createElement('td'); calcTableCell.appendChild(luckInput); calcTableRow.appendChild(calcTableCell); calculateButton.type = 'button'; calculateButton.value = 'Calculate >>'; calculateButton.onclick = calculation(luckInput, percentChanceLabel); calcTableCell = document.createElement('td'); calcTableCell.appendChild(calculateButton); calcTableRow.appendChild(calcTableCell); percentChanceLabel.appendChild(document.createTextNode('0.000% Chance')); percentChanceLabel.style.textAlign = 'right'; percentChanceLabel.style.width = '110px'; calcTableCell = document.createElement('td'); calcTableCell.appendChild(percentChanceLabel); calcTableRow.appendChild(calcTableCell); return calcTableRow; } function calculation(source, destination) { if (source.value) { if (isNumeric(source.value)) { var percentChance = Math.pow(source.value, (5 / 9)); if (percentChance > 100) { percentChance = 100; } percentChance = percentChance.toFixed(3); removeChildren(destination); destination.appendChild(document.createTextNode(percentChance + '% Chance')); } } } Calculator('luckToChance', 'Luck to Chance', buildCalculator, '4'); } /** * Calculator that calculates your percentage chance for more and better loot given a Luck value. */ function luckToChanceCalculator() { var location = document.getElementById('luckToChance'); if (location) { var calculator = document.createElement('table'); calculator.className = 'uoguidetable open'; location.appendChild(calculator); var calcTableBody = document.createElement('tbody'); calculator.appendChild(calcTableBody); var calcTableRow = document.createElement('tr'); calcTableBody.appendChild(calcTableRow); var calcTableHeader = document.createElement('th'); calcTableHeader.colSpan = '4'; calcTableHeader.appendChild(document.createTextNode('Luck to Chance')); calcTableRow.appendChild(calcTableHeader); var calcTableCell = document.createElement('td'); calcTableCell.style.textAlign = 'right'; calcTableCell.appendChild(document.createTextNode('Luck:')); calcTableRow = document.createElement('tr'); calcTableRow.appendChild(calcTableCell); calcTableBody.appendChild(calcTableRow); var luckInput = document.createElement('input'); luckInput.id = 'luckInput'; luckInput.maxLength = '4'; luckInput.type = 'text'; luckInput.style.width = '35px'; calcTableCell = document.createElement('td'); calcTableCell.appendChild(luckInput); calcTableRow.appendChild(calcTableCell); var percentChanceLabel = document.createElement('div'); percentChanceLabel.appendChild(document.createTextNode('0.000% Chance')); percentChanceLabel.style.textAlign = 'right'; percentChanceLabel.style.width = '110px'; var calculateButton = document.createElement('input'); calculateButton.type = 'button'; calculateButton.value = 'Calculate >>'; calculateButton.onclick = function() { if (luckInput.value) { if (isNumeric(luckInput.value)) { var percentChance = Math.pow(luckInput.value, (5 / 9)); if (percentChance > 100) { percentChance = 100; } percentChance = percentChance.toFixed(3); removeChildren(percentChanceLabel); percentChanceLabel.appendChild(document.createTextNode(percentChance + '% Chance')); } } }; calcTableCell = document.createElement('td'); calcTableCell.appendChild(calculateButton); calcTableRow.appendChild(calcTableCell); calcTableCell = document.createElement('td'); calcTableCell.appendChild(percentChanceLabel); calcTableRow.appendChild(calcTableCell); } } addOnloadHook(LuckToChanceCalculator);