Difference between revisions of "MediaWiki:Common.js"

m (That didn't work.)
m (Step 2 of imageheight change.)
 
(139 intermediate revisions by the same user not shown)
Line 2: Line 2:
  
 
/**
 
/**
  * Tests if a particular element possesses a class.
+
  * Determines if a particular element possesses a class.
 
  */
 
  */
 
var hasClass = (function () {
 
var hasClass = (function () {
Line 12: Line 12:
  
 
/**
 
/**
  * Code for collapsible tables.
+
  * Determines if a given value is a number.
 
  */
 
  */
var collapseCaption = 'hide';
+
function isNumeric(value) {
var expandCaption = 'show';
+
     return new RegExp(/(^\d+$)|(^\d+\.\d+$)/).test(value);
 
+
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;
+
* Removes all children from an element.
    var navigationBoxes = new Object();
+
*/
    var tables = getElementsByClassName(document, 'table', 'collapsible');
+
function removeChildren(element) {
 
+
    while (element.hasChildNodes()) {
    for (var i = 0; i < tables.length; i++) {
+
         element.removeChild(element.firstChild);
        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);
 
  
 
/**
 
/**
  * Calculator that calculates your percentage chance for more and better loot given a Luck value.
+
  * Import more specific scripts.
 
  */
 
  */
function luckToChanceCalculator() {
+
importScript('MediaWiki:Collapsible.js');
    var location = document.getElementById('luckToChance');
+
importScript('MediaWiki:Calculator.js');
  
    if (location) {
+
if (wgAction == 'edit' || wgAction == 'submit')
        var luckLabel = document.createElement('div');
+
{
        luckLabel.appendChild(document.createTextNode('Luck:'));
+
    importScript('MediaWiki:Editbar.js');
        luckLabel.style.cssFloat = 'left';
+
        luckLabel.style.height = '22px';
+
        luckLabel.style.lineHeight = '22px';
+
        luckLabel.style.styleFloat = 'left';
+
        luckLabel.style.width = '40px';
+
        location.appendChild(luckLabel);
+
 
+
        var luckInput = document.createElement('input');
+
        luckInput.id = 'luckInput';
+
        luckInput.maxLength = '4';
+
        luckInput.type = 'text';
+
        luckInput.style.cssFloat = 'left';
+
        luckInput.style.styleFloat = 'left';
+
        luckInput.style.width = '35px';
+
        location.appendChild(luckInput);
+
 
+
        var percentChanceLabel = document.createElement('div');
+
        percentChanceLabel.appendChild(document.createTextNode('0.000% Chance'));
+
        percentChanceLabel.style.cssFloat = 'left';
+
        percentChanceLabel.style.height = '22px';
+
        percentChanceLabel.style.lineHeight = '22px';
+
        percentChanceLabel.style.styleFloat = 'left';
+
        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) {
+
                var percentChance = Math.pow(luckInput.value, (5 / 9));
+
                if (percentChance > 100) {
+
                    percentChance = 100;
+
                }
+
                percentChance = percentChance.toFixed(3);
+
                while (percentChanceLabel.hasChildNodes()) {
+
                    percentChanceLabel.removeChild(percentChanceLabel.lastChild);
+
                }
+
                percentChanceLabel.appendChild(document.createTextNode(percentChance + '% Chance'));
+
            }
+
        };
+
        calculateButton.style.cssFloat = 'left';
+
        calculateButton.style.marginLeft = '3px';
+
        calculateButton.style.styleFloat = 'left';
+
        location.appendChild(calculateButton);
+
        location.appendChild(percentChanceLabel);
+
    }
+
 
}
 
}
 
addOnloadHook(luckToChanceCalculator);
 

Latest revision as of 12:30, 24 August 2011

/* 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);
    }
}

/**
 * Import more specific scripts.
 */
importScript('MediaWiki:Collapsible.js');
importScript('MediaWiki:Calculator.js');

if (wgAction == 'edit' || wgAction == 'submit')
{
    importScript('MediaWiki:Editbar.js');
}