MediaWiki:Common.js: Difference between revisions

From AC Community Wiki
imported>Tlosk
mNo edit summary
imported>Tlosk's Bot
No edit summary
 
(3 intermediate revisions by 3 users not shown)
Line 1: Line 1:
/* Any JavaScript here will be loaded for all users on every page load. */
/* Any JavaScript here will be loaded for all users on every page load. */


function xp_at_level(level)
{
    if (level < 2)
        return 0;
    if (level == 2)
        return 1000;
    if (level <= 126)
        return Math.ceil((Math.pow((level + 5), 5) - Math.pow(6, 5)) / 9.0);
    return Math.round((Math.pow((level + 5), 5) - Math.pow(6, 5)) / 9.0);
}
function xp_to_next_level(level)
{
    if (level < 0 || level > 274)
        return 0;
    return xp_at_level(level + 1) - xp_at_level(level);
}


/** Collapsible tables *********************************************************
function percent_xp_to_next_level(level, percent)
*
*/
var autoCollapse = 2;
var collapseCaption = "hide";
var expandCaption = "show";
function collapseTable( tableIndex )
{
{
     var Button = document.getElementById( "collapseButton" + tableIndex );
     return Math.floor(xp_to_next_level(level) * percent / 100.0);
    var Table = document.getElementById( "collapsibleTable" + tableIndex );
}
 
    if ( !Table || !Button ) {
function format_xp(mystring)
        return false;
{
     }
    var result;
     var pos;
     var Rows = Table.rows;
 
     mystring += '';
     if ( Button.firstChild.data == collapseCaption ) {
     if (mystring.length <= 3) {
        for ( var i = 1; i < Rows.length; i++ ) {
         result = mystring;
            Rows[i].style.display = "none";
         }
        Button.firstChild.data = expandCaption;
     } else {
     } else {
         for ( var i = 1; i < Rows.length; i++ ) {
        var count = 0;
             Rows[i].style.display = Rows[0].style.display;
        result = "";
         for (pos = mystring.length - 3; pos >= 0 && count < 20; pos -= 3, count++) {
             if (result.length == 0)
                result = mystring.substr(pos, 3);
            else
                result = mystring.substr(pos, 3) + "," + result;
         }
         }
         Button.firstChild.data = collapseCaption;
         if (pos > -3)
            result = mystring.substr(0, 3+pos) + "," + result;
     }
     }
    return result;
}
}
 
function createCollapseButtons()
function calc_and_display_pxp(index)
{
{
     var tableIndex = 0;
     var level = parseInt(document.getElementById("pxp_level_input_" + index).value);
     var NavigationBoxes = new Object();
     var percent = parseFloat(document.getElementById("pxp_percent_input_" + index).value);
     var Tables = document.getElementsByTagName( "table" );
 
    document.getElementById("pxp_result_" + index).innerHTML = " = " + format_xp(percent_xp_to_next_level(level, percent));
     for ( var i = 0; i < Tables.length; i++ ) {
 
         if ( hasClass( Tables[i], "collapsible" ) ) {
    return false;
}
            /* only add button and increment count if there is a header row to work with */
 
             var HeaderRow = Tables[i].getElementsByTagName( "tr" )[0];
function hasClass(element, className)
            if (!HeaderRow) continue;
{
             var Header = HeaderRow.getElementsByTagName( "th" )[0];
    return element.className.indexOf(className) != -1;
             if (!Header) continue;
}
 
            NavigationBoxes[ tableIndex ] = Tables[i];
function createPXPCalculationForms()
            Tables[i].setAttribute( "id", "collapsibleTable" + tableIndex );
{
     var paras = document.getElementsByTagName("p");
             var Button    = document.createElement( "span" );
    var offset = 0;
             var ButtonLink = document.createElement( "a" );
 
             var ButtonText = document.createTextNode( collapseCaption );
     for (var index = 0; index < paras.length; index++) {
         if (hasClass(paras[index], "calc-pxp")) {
             Button.style.styleFloat = "right";   //
             var form = document.createElement("form");
             Button.style.cssFloat = "right";     // REMOVE THESE LINES
             var input1 = document.createElement("input");
             Button.style.fontWeight = "normal";   // ON 10 FEBRUARY 2009
             var input2 = document.createElement("input");
             Button.style.textAlign = "right";     //
             var input3 = document.createElement("input");
             Button.style.width = "6em";           //
             var span = document.createElement("span");
 
             Button.className = "collapseButton"; //Styles are declared in Common.css
             form.setAttribute("onSubmit", "return calc_and_display_pxp(" + offset + ");");
             input1.setAttribute("size", "6");
             ButtonLink.style.color = Header.style.color;
             input1.setAttribute("value", "200");
             ButtonLink.setAttribute( "id", "collapseButton" + tableIndex );
             input1.setAttribute("id", "pxp_level_input_" + offset);
             ButtonLink.setAttribute( "href", "javascript:collapseTable(" + tableIndex + ");" );
 
             ButtonLink.appendChild( ButtonText );
             input2.setAttribute("size", "6");
             input2.setAttribute("value", "10");
             Button.appendChild( document.createTextNode( "[" ) );
             input2.setAttribute("id", "pxp_percent_input_" + offset);
             Button.appendChild( ButtonLink );
 
             Button.appendChild( document.createTextNode( "]" ) );
             input3.setAttribute("type", "button");
             input3.setAttribute("value", "Calculate PXP");
             Header.insertBefore( Button, Header.childNodes[0] );
             input3.setAttribute("onclick", "javascript:calc_and_display_pxp(" + offset + ");");
             tableIndex++;
 
        }
             span.setAttribute("id", "pxp_result_" + offset);
    }
            span.innerHTML = "??? xp";
 
    for ( var i = 0;  i < tableIndex; i++ ) {
             form.appendChild(document.createTextNode("Level: "));
        if ( hasClass( NavigationBoxes[i], "collapsed" ) || ( tableIndex >= autoCollapse && hasClass( NavigationBoxes[i], "autocollapse" ) ) ) {
             form.appendChild(input1);
             collapseTable( i );
             form.appendChild(document.createTextNode(" Percent: "));
        }
             form.appendChild(input2);
        else if ( hasClass( NavigationBoxes[i], "innercollapse" ) ) {
             form.appendChild(input3);
             var element = NavigationBoxes[i];
             form.appendChild(span);
            while (element = element.parentNode) {
 
                if ( hasClass( element, "outercollapse" ) ) {
             paras[index].appendChild(form);
                    collapseTable ( i );
            offset++;
                    break;
                }
            }
         }
         }
     }
     }
}
}
 
addOnloadHook( createCollapseButtons );
addOnloadHook(createPXPCalculationForms);

Latest revision as of 18:35, 2 November 2010

/* Any JavaScript here will be loaded for all users on every page load. */

function xp_at_level(level)
{
    if (level < 2)
        return 0;

    if (level == 2)
        return 1000;

    if (level <= 126)
        return Math.ceil((Math.pow((level + 5), 5) - Math.pow(6, 5)) / 9.0);

    return Math.round((Math.pow((level + 5), 5) - Math.pow(6, 5)) / 9.0);
}

function xp_to_next_level(level)
{
    if (level < 0 || level > 274)
        return 0;

    return xp_at_level(level + 1) - xp_at_level(level);
}

function percent_xp_to_next_level(level, percent)
{
    return Math.floor(xp_to_next_level(level) * percent / 100.0);
}

function format_xp(mystring)
{
    var result;
    var pos;

    mystring += '';
    if (mystring.length <= 3) {
        result = mystring;
    } else {
        var count = 0;
        result = "";
        for (pos = mystring.length - 3; pos >= 0 && count < 20; pos -= 3, count++) {
            if (result.length == 0)
                result = mystring.substr(pos, 3);
            else
                result = mystring.substr(pos, 3) + "," + result;
        }
        if (pos > -3)
            result = mystring.substr(0, 3+pos) + "," + result;
    }

    return result;
}

function calc_and_display_pxp(index)
{
    var level = parseInt(document.getElementById("pxp_level_input_" + index).value);
    var percent = parseFloat(document.getElementById("pxp_percent_input_" + index).value);

    document.getElementById("pxp_result_" + index).innerHTML = " = " + format_xp(percent_xp_to_next_level(level, percent));

    return false;
}

function hasClass(element, className)
{
    return element.className.indexOf(className) != -1;
}

function createPXPCalculationForms()
{
    var paras = document.getElementsByTagName("p");
    var offset = 0;

    for (var index = 0; index < paras.length; index++) {
        if (hasClass(paras[index], "calc-pxp")) {
            var form = document.createElement("form");
            var input1 = document.createElement("input");
            var input2 = document.createElement("input");
            var input3 = document.createElement("input");
            var span = document.createElement("span");

            form.setAttribute("onSubmit", "return calc_and_display_pxp(" + offset + ");");
            input1.setAttribute("size", "6");
            input1.setAttribute("value", "200");
            input1.setAttribute("id", "pxp_level_input_" + offset);

            input2.setAttribute("size", "6");
            input2.setAttribute("value", "10");
            input2.setAttribute("id", "pxp_percent_input_" + offset);

            input3.setAttribute("type", "button");
            input3.setAttribute("value", "Calculate PXP");
            input3.setAttribute("onclick", "javascript:calc_and_display_pxp(" + offset + ");");

            span.setAttribute("id", "pxp_result_" + offset);
            span.innerHTML = "??? xp";

            form.appendChild(document.createTextNode("Level: "));
            form.appendChild(input1);
            form.appendChild(document.createTextNode(" Percent: "));
            form.appendChild(input2);
            form.appendChild(input3);
            form.appendChild(span);

            paras[index].appendChild(form);
            offset++;
        }
    }
}

addOnloadHook(createPXPCalculationForms);