Difference between revisions of "MediaWiki:Common.js"

m
m (Step 2 of imageheight change.)
 
(48 intermediate revisions by the same user not shown)
Line 2: Line 2:
  
 
/**
 
/**
  * Import utility functions.
+
  * Determines if a particular element possesses a class.
 
  */
 
  */
importScript('MediaWiki:Common.js/Utility.js');
+
var hasClass = (function () {
 
+
     var reCache = {};
/**
+
     return function (element, className) {
* Import more specific scripts, if necessary.
+
         return (reCache[className] ? reCache[className] : (reCache[className] = new RegExp('(?:\\s|^)' + className + '(?:\\s|$)'))).test(element.className);
*/
+
if (wgAction == 'edit' || wgAction == 'submit')
+
{
+
    importScript('MediaWiki:Common.js/Editbar.js');
+
}
+
 
+
/**
+
* Partial function application for passing a function, that itself has parameters, as a parameter.
+
*/
+
function passFunction(func) {
+
     var args = Array.prototype.slice.call(arguments).splice(1);
+
     return function() {
+
         var allArguments = args.concat(Array.prototype.slice.call(arguments));
+
        return func.apply(this, allArguments);
+
 
     };
 
     };
}
+
})();
  
 
/**
 
/**
  * Executes a specific action when the Enter key is pressed in a specific element.
+
  * Determines if a given value is a number.
 
  */
 
  */
/*function listenForEnterKey(keyEvent, action) {
+
function isNumeric(value) {
     var keyID = window.event ? event.keyCode : keyEvent.keyCode;
+
     return new RegExp(/(^\d+$)|(^\d+\.\d+$)/).test(value);
    if (keyID == 13) action();
+
}*/
+
 
+
/**
+
* 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);
 
  
 
/**
 
/**
  * Classical inheritance implementation.
+
  * Removes all children from an element.
 
  */
 
  */
(function(){
+
function removeChildren(element) {
     var initializing = false, fnTest = /xyz/.test(function() { xyz; }) ? /\b_super\b/ : /.*/;
+
     while (element.hasChildNodes()) {
 
+
         element.removeChild(element.firstChild);
    this.Class = function() { };
+
+
    Class.extend = function(prop) {
+
         var _super = this.prototype;
+
 
+
        initializing = true;
+
        var prototype = new this();
+
        initializing = false;
+
 
+
        for (var name in prop) {
+
            prototype[name] = typeof prop[name] == "function" && typeof _super[name] == "function" && fnTest.test(prop[name]) ? (function(name, fn) {
+
                return function() {
+
                    var tmp = this._super;
+
                    this._super = _super[name];
+
                    var ret = fn.apply(this, arguments);     
+
                    this._super = tmp;
+
                    return ret;
+
                };
+
            })(name, prop[name]) : prop[name];
+
        }
+
 
+
        function Class() {
+
            if (!initializing && this.init)
+
                this.init.apply(this, arguments);
+
        }
+
 
+
        Class.prototype = prototype;
+
        Class.constructor = Class;
+
        Class.extend = arguments.callee;
+
 
+
        return Class;
+
    };
+
})();
+
 
+
/**
+
* Code for calculators.
+
*/
+
var Calculator = Class.extend({
+
    init: function(content, id, title, columns) {
+
        var location = document.getElementById(id);
+
 
+
        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 = columns;
+
        calcTableHeader.appendChild(document.createTextNode(title));
+
        calcTableRow.appendChild(calcTableHeader);
+
 
+
        calcTableRow = content;
+
        calcTableBody.appendChild(calcTableRow);
+
 
+
        location.appendChild(table);
+
    },
+
    listenForEnterKey: function(keyEvent, action) {
+
        var keyID = window.event ? event.keyCode : keyEvent.keyCode;
+
        if (keyID == 13)
+
            action();
+
    }
+
});
+
 
+
/**
+
* Calculator that calculates your percentage chance for more and better loot given a specified Luck value.
+
*/
+
var LuckToChanceCalculator = Calculator.extend({
+
    init: function() {
+
        var calculate = function(input, label) {
+
            if (input.value) {
+
                if (isNumeric(input.value)) {
+
                    var percentChance = Math.pow(input.value, (5 / 9));
+
                    if (percentChance > 100) {
+
                        percentChance = 100;
+
                    }
+
                    percentChance = percentChance.toFixed(3);
+
                    removeChildren(label);
+
                    label.appendChild(document.createTextNode(percentChance + '% Chance'));
+
                }
+
            }
+
        };
+
        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');
+
 
+
        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';
+
        luckInput.onkeyup = function(keyEvent) {
+
            var keyID = window.event ? event.keyCode : keyEvent.keyCode;
+
            if (keyID == 13)
+
                calculate(luckInput, percentChanceLabel);
+
        };
+
calcTableCell = document.createElement('td');
+
        calcTableCell.appendChild(luckInput);
+
        calcTableRow.appendChild(calcTableCell);
+
 
+
        calculateButton.type = 'button';
+
        calculateButton.value = 'Calculate >>';
+
        calculateButton.onclick = function() {
+
            calculate(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);
+
 
+
        this._super(calcTableRow, 'LuckToChance', 'Luck to Chance', 4);
+
    }
+
});
+
 
+
function searchForCalculators() {
+
    var calculators = document.getElementsByTagName('div');
+
 
+
    for (var i in calculators) {
+
        if (calculators[i].id == 'LuckToChance') {
+
            var luckToChance = new LuckToChanceCalculator();
+
        } else if (calculators[i].id == 'ChanceToLuck') {
+
            Calculator(ChanceToLuckCalculator);
+
        }
+
 
     }
 
     }
 
}
 
}
addOnloadHook(searchForCalculators);
 
 
/*function LuckToChanceCalculator() {
 
    this.id = 'LuckToChance';
 
    this.title = 'Luck to Chance';
 
    this.columns = '4';
 
    this.buildCalculator = function() {
 
        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');
 
 
        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';
 
        luckInput.onkeyup = function(event) {
 
            listenForEnterKey(event, passFunction(calculate, luckInput, percentChanceLabel));
 
        };
 
calcTableCell = document.createElement('td');
 
        calcTableCell.appendChild(luckInput);
 
        calcTableRow.appendChild(calcTableCell);
 
 
        calculateButton.type = 'button';
 
        calculateButton.value = 'Calculate >>';
 
        calculateButton.onclick = function() {
 
            calculate(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 calculate(luckInput, percentChanceLabel) {
 
        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'));
 
            }
 
        }
 
    }
 
}*/
 
  
 
/**
 
/**
  * Calculator that calculates the amount of Luck required to achieve a specified percentage chance for more and better loot.
+
  * Import more specific scripts.
 
  */
 
  */
function ChanceToLuckCalculator() {
+
importScript('MediaWiki:Collapsible.js');
    this.id = 'ChanceToLuck';
+
importScript('MediaWiki:Calculator.js');
    this.title = 'Chance to Luck';
+
    this.columns = '4';
+
    this.buildCalculator = function() {
+
        var calcTableRow = document.createElement('tr');
+
        var calcTableCell = document.createElement('td');
+
        var chanceInput = document.createElement('input');
+
        var calculateButton = document.createElement('input');
+
        var luckAmountLabel = document.createElement('div');
+
  
        calcTableCell.style.textAlign = 'right';
+
if (wgAction == 'edit' || wgAction == 'submit')
        calcTableCell.appendChild(document.createTextNode('Chance:'));
+
{
        calcTableRow.appendChild(calcTableCell);
+
     importScript('MediaWiki:Editbar.js');
 
+
        chanceInput.id = 'chanceInput';
+
        chanceInput.maxLength = '3';
+
        chanceInput.type = 'text';
+
        chanceInput.style.width = '25px';
+
        chanceInput.onkeyup = function(event) {
+
            listenForEnterKey(event, passFunction(calculate, chanceInput, luckAmountLabel));
+
        };
+
calcTableCell = document.createElement('td');
+
        calcTableCell.appendChild(chanceInput);
+
        calcTableRow.appendChild(calcTableCell);
+
 
+
        calculateButton.type = 'button';
+
        calculateButton.value = 'Calculate >>';
+
        calculateButton.onclick = function() {
+
            calculate(chanceInput, luckAmountLabel);
+
        };
+
calcTableCell = document.createElement('td');
+
        calcTableCell.appendChild(calculateButton);
+
        calcTableRow.appendChild(calcTableCell);
+
 
+
        luckAmountLabel.appendChild(document.createTextNode('0 Luck'));
+
        luckAmountLabel.style.textAlign = 'right';
+
        luckAmountLabel.style.width = '60px';
+
calcTableCell = document.createElement('td');
+
        calcTableCell.appendChild(luckAmountLabel);
+
        calcTableRow.appendChild(calcTableCell);
+
 
+
        return calcTableRow;
+
     };
+
 
+
    function calculate(chanceInput, luckAmountLabel) {
+
        if (chanceInput.value) {
+
            if (chanceInput.value > 100) {
+
                chanceInput.value = '100';
+
            }
+
            if (isNumeric(chanceInput.value)) {
+
                var luckAmount = Math.ceil(Math.pow(chanceInput.value, (9 / 5)));
+
                removeChildren(luckAmountLabel);
+
                luckAmountLabel.appendChild(document.createTextNode(luckAmount + ' Luck'));
+
            }
+
        }
+
    }
+
 
}
 
}

Latest revision as of 13: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');
}