Benutzer:Sphinx/CFtables.js

aus FreewarWiki, der Referenz für Freewar
Zur Navigation springen Zur Suche springen
Hinweis
Nach dem Speichern sollte zunächst der Browser-Cache geleert werden, bevor man die Auswirkungen der Änderung sehen kann.
  • Mozilla/Firefox/Safari: Strg + R (Mactaste + R bei Macintosh) drücken
  • Konqueror: F5 drücken
  • Opera: kompletten Cache unter Extras → Internetspuren löschen → Individuelle Auswahl leeren
  • Internet Explorer: Strg + F5


/*global jQuery:false */
(function (window, $, undefined) {
    'use strict';
    var document = window.document,
        timestring = function (timespan) {
            var cd_array = [],
                i = 0,
                mod = 0,
                parts = [86400, 3600, 60];

            for (i; i < parts.length; i += 1) {
                cd_array.push('' + Math.floor((mod > 0 ? (timespan % mod) : timespan) / parts[i]));

                if (cd_array[i].length < 2) { // string padding
                    cd_array[i] = '0' + cd_array[i];
                }

                mod = parts[i];
            }
            
            return cd_array.join(':');
        };
        
    //* only for usage within a content_script/extension
    if (window.addOnloadHook === undefined) {
        var addOnloadHook = $(document).ready;
    }//*/
    
    if ($("#CFoptions").length) { // injected in a ability page
        console.info("running CFtables.js");
        
        addOnloadHook(function () {
            var color_level = "#FFFFCC",
                color_total = color_level,
                colfirst = null,
                colhead = null,
                cfmax = +$("#CFmax").text(),
                cftime,
                cfsum = {},
                container_cflevel = $("#CFlevel"),
                container_cftotal = $("#CFtotal"),
                row_cflevel = null,
                row_cftotal = null,
                stage = 0,
                stage_base = 0,
                table_cflevel = $("table", container_cflevel),
                table_cftotal = $("table", container_cftotal),
                timespan = 0;

            if (window.gw === undefined) { // script injected before MediaWiki:CFoptions.js
                cftime = +$("#CFcalc"); // doesnt work within a content-script
                //* dirty content-script workaround, can be deleted
                if (isNaN(cftime)) { 
                    var timespan_a = $("td", $("#CFlevel tr")[3])[1].innerHTML.split(':');
                    cftime = timespan_a[0] * 86400 + timespan_a[1] * 3600 + timespan_a[2] * 60;
                    if (('' + cftime).length > 4) {
                        cftime = Math.round(cftime / 1000) * 1000;
                    } else {
                        cftime = Math.round(cftime / 100) * 100;
                    }
                }//*/
            } else {
                cftime = +window.gw;
            }

            if (isNaN(cfmax) === false && isNaN(cftime) === false) {
                $("tr", table_cflevel).remove();
                $("tr", table_cftotal).remove();

                for (stage; stage <= cfmax && stage <= 1000; stage += 1) {
                    // create first collumn
                    colfirst = $("<td/>", {
                        html: "<b>" + (stage === 0 ? "Stufe" : stage) + "</b>",
                        style: "background-color: " + (stage === 0 ? "#FFEECB" : "#FFFFCC")
                    });

                    // create rows
                    row_cflevel = $("<tr/>").append(colfirst);
                    if (stage % 5 === 0) {
                        row_cftotal = $("<tr/>").append(colfirst.clone());
                    }

                    if (Math.ceil(stage / 5) % 2 === 1) {
                        color_level = "#FFEECB";
                    } else {
                        color_level = "#FFFFCC";
                    }

                    if (Math.ceil(stage / 25) % 2 === 1) {
                        color_total = "#FFEECB";
                    } else {
                        color_total = "#FFFFCC";
                    }

                    // loop through every 5th base_stage
                    for (stage_base = 0; stage_base <= 50; stage_base += 5) {
                        if (stage === 0) { // thead
                            colhead = $("<th/>", {
                                style: "background-color: #FFFFCC",
                                html: stage_base
                            });

                            row_cflevel.append(colhead);
                            row_cftotal.append(colhead.clone());
                        } else { // tbody
                            if (cfsum[stage_base] === undefined) {
                                // init sum for current stage->stage_base
                                cfsum[stage_base] = 0;
                            }

                            // calc time to learn stage {stage} with base_stage {base_stage}
                            timespan = Math.floor((stage - 1) * cftime * Math.pow(0.97, stage_base));

                            // sum the timespan
                            cfsum[stage_base] += timespan;

                            // create cflevel col
                            $("<td/>", {
                                html: timestring(timespan),
                                style: "background-color: " + color_level
                            }).appendTo(row_cflevel);

                            // create cftotal col
                            if (stage % 5 === 0) {
                                $("<td/>", {
                                    html: timestring(cfsum[stage_base]),
                                    style: "background-color: " + color_total
                                }).appendTo(row_cftotal);
                            }
                        }

                        // switch color
                        if (stage > 0) {
                            if (color_level === "#FFEECB") {
                                color_level = "#FFFFCC";
                            } else {
                                color_level = "#FFEECB";
                            }
                            if (color_total === "#FFEECB") {
                                color_total = "#FFFFCC";
                            } else {
                                color_total = "#FFEECB";
                            }
                        }
                    }

                    // append rows
                    table_cflevel.append(row_cflevel);
                    if (stage % 5 === 0) {
                        table_cftotal.append(row_cftotal);
                    }
                }
            }
        });
    } 
}(window, jQuery));