Difference between revisions of "MediaWiki:Common.js"

From Emps-World Wiki
Jump to: navigation, search
Line 24: Line 24:
  
 
$(document).ready(function() {
 
$(document).ready(function() {
function format(x) {
+
    function format(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
+
        return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
+
    }
+
   
 
     $(".item-bonus-table, .item-info-table").each(function() {
 
     $(".item-bonus-table, .item-info-table").each(function() {
 
         var table = $(this);
 
         var table = $(this);
Line 41: Line 41:
 
             //console.log("data4: "+json.name);
 
             //console.log("data4: "+json.name);
 
              
 
              
// item info table
+
            // item info table
if (table.is(".item-info-table")) {
+
            if (table.is(".item-info-table")) {
table.find("th").each(function() {
+
                table.find("th").each(function() {
if (dc % 2 === 1) {
+
                    if (dc % 2 === 1) {
var idx = parseInt(dc / 2);
+
                        var idx = parseInt(dc / 2);
var note = parseInt(json.note) === parseInt(itemId);
+
                        var note = parseInt(json.note) === parseInt(itemId);
if (note)
+
                        if (note)
note = "noted";
+
                            note = "noted";
else
+
                        else
note = "yes";
+
                            note = "yes";
+
                       
var halch = format(parseInt(json.price * 0.9375));
+
                        var halch = format(parseInt(json.price * 0.9375));
var lalch = format(parseInt(json.price * 0.9375 * 0.85));
+
                        var lalch = format(parseInt(json.price * 0.9375 * 0.85));
var shop = format(Math.round(json.price * 0.6));
+
                        var shop = format(Math.round(json.price * 0.6));
+
                       
if (idx > 0) {
+
                        if (idx > 0) {
if (idx == 1) // tradeable
+
                            if (idx == 1) // tradeable
$(this).text(json.trade);
+
                                $(this).text(json.trade);
else if (idx == 2) // equippable
+
                            else if (idx == 2) // equippable
$(this).text(json.slot === 6 ? "no" : "yes");
+
                                $(this).text(json.slot === 6 ? "no" : "yes");
else if (idx == 3) // stackable
+
                            else if (idx == 3) // stackable
$(this).text(json.stack);
+
                                $(this).text(json.stack);
else if (idx == 4) // noteable
+
                            else if (idx == 4) // noteable
$(this).text(note);
+
                                $(this).text(note);
else if (idx == 5) // destroy
+
                            else if (idx == 5) // destroy
$(this).text(json.destroy);
+
                                $(this).text(json.destroy);
else if (idx == 6) // general store
+
                            else if (idx == 6) // general store
$(this).text(shop);
+
                                $(this).text(shop);
else if (idx == 7) // low alch
+
                            else if (idx == 7) // low alch
$(this).text(lalch);
+
                                $(this).text(lalch);
else if (idx == 8) // high alch
+
                            else if (idx == 8) // high alch
$(this).text(halch);
+
                                $(this).text(halch);
else if (idx == 9) // weight
+
                            else if (idx == 9) // weight
$(this).text(json.weight+" kg");
+
                                $(this).text(json.weight+" kg");
//else if (idx == 10) // examine
+
                            else if (idx == 10) // examine
// $(this).text(json.stack);
+
                                $(this).text("You have a "+json.name+".");
}
+
                        }
}
+
                    }
dc++;
+
                    dc++;
});
+
                });
+
               
// bonuses table
+
            // bonuses table
} else {
+
            } else {
// iterate over headers
+
                // iterate over headers
table.find("th").each(function() {
+
                table.find("th").each(function() {
if (hc === 0)
+
                    if (hc === 0)
$(this).text(json.name);
+
                        $(this).text(json.name);
hc++;
+
                    hc++;
});
+
                });
+
               
// iterate over table data
+
                // iterate over table data
table.find("td").each(function() {
+
                table.find("td").each(function() {
if (dc === 0)
+
                    if (dc === 0)
$(this).text(json.mlAcc);
+
                        $(this).text(json.mlAcc);
else if (dc === 1)
+
                    else if (dc === 1)
$(this).text(json.rnAcc);
+
                        $(this).text(json.rnAcc);
else if (dc === 2)
+
                    else if (dc === 2)
$(this).text(json.mgAcc);
+
                        $(this).text(json.mgAcc);
else if (dc === 3)
+
                    else if (dc === 3)
$(this).text(json.mlDef);
+
                        $(this).text(json.mlDef);
else if (dc === 4)
+
                    else if (dc === 4)
$(this).text(json.rnDef);
+
                        $(this).text(json.rnDef);
else if (dc === 5)
+
                    else if (dc === 5)
$(this).text(json.mgDef);
+
                        $(this).text(json.mgDef);
else if (dc == 6)
+
                    else if (dc == 6)
$(this).text(json.pray);
+
                        $(this).text(json.pray);
else if (dc == 7) {
+
                    else if (dc == 7) {
this.innerHTML = "";
+
                        this.innerHTML = "";
var slotFile = slotToImg[json.slot];
+
                        var slotFile = slotToImg[json.slot];
var slotName = slotFile.substring(5, slotFile.length-8);
+
                        var slotName = slotFile.substring(5, slotFile.length-8);
var iconLink = document.createElement("a");
+
                        var iconLink = document.createElement("a");
var iconImg = document.createElement("img");
+
                        var iconImg = document.createElement("img");
$(iconLink).attr("href", "/wiki/File:"+slotName+"slot.png");
+
                        $(iconLink).attr("href", "/wiki/File:"+slotName+"slot.png");
$(iconLink).attr("class", "image");
+
                        $(iconLink).attr("class", "image");
$(iconImg).attr("alt", slotName+"slot.png");
+
                        $(iconImg).attr("alt", slotName+"slot.png");
$(iconImg).attr("src", "/wiki/images/thumb/"+slotFile+"/30px-"+slotName+"slot.png");
+
                        $(iconImg).attr("src", "/wiki/images/thumb/"+slotFile+"/30px-"+slotName+"slot.png");
$(iconImg).attr("width", "30");
+
                        $(iconImg).attr("width", "30");
$(iconImg).attr("height", "30");
+
                        $(iconImg).attr("height", "30");
$(iconImg).attr("srcset", "/wiki/images/"+slotFile+" 1.5x, /wiki/images/"+slotFile+" 2x");
+
                        $(iconImg).attr("srcset", "/wiki/images/"+slotFile+" 1.5x, /wiki/images/"+slotFile+" 2x");
iconLink.appendChild(iconImg);
+
                        iconLink.appendChild(iconImg);
this.appendChild(iconLink);
+
                        this.appendChild(iconLink);
} else if (dc === 8)
+
                    } else if (dc === 8)
$(this).text(json.mlStr);
+
                        $(this).text(json.mlStr);
else if (dc === 9)
+
                    else if (dc === 9)
$(this).text(json.rnStr);
+
                        $(this).text(json.rnStr);
else if (dc === 10)
+
                    else if (dc === 10)
$(this).text(json.mgStr);
+
                        $(this).text(json.mgStr);
else if (dc === 11)
+
                    else if (dc === 11)
$(this).text(json.degrade === "1" ? "Yes" : "No");
+
                        $(this).text(json.degrade === "1" ? "Yes" : "No");
else if (dc === 12)
+
                    else if (dc === 12)
$(this).text(json.repair === "1" ? "Yes" : "No");
+
                        $(this).text(json.repair === "1" ? "Yes" : "No");
else if (dc === 13) {
+
                    else if (dc === 13) {
if (json.req === "")
+
                        if (json.req === "")
$(this).text("None");
+
                            $(this).text("None");
else
+
                        else
$(this).text(json.req);
+
                            $(this).text(json.req);
} else if (dc === 14) {
+
                    } else if (dc === 14) {
if (json.slot === "3")
+
                        if (json.slot === "3")
$(this).text(json.speed);
+
                            $(this).text(json.speed);
else
+
                        else
$(this).text("-");
+
                            $(this).text("-");
} else
+
                    } else
$(this).text(dc+" data column");
+
                        $(this).text(dc+" data column");
+
                   
dc++;
+
                    dc++;
});
+
                });
}
+
            }
 
         });
 
         });
 
     });
 
     });
 
});
 
});

Revision as of 13:50, 5 December 2018

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

// TODO: link images directly, because the parser has already run at this point
// cannot use wiki commands here anymore.
// mapping slot id to a file
var slotToImg = [
    "c/ca/Helmetslot.png",
    "d/d7/Capeslot.png",
    "8/85/Amuletslot.png",
    "a/aa/Weaponslot.png",
    "9/94/Bodyslot.png",
    "9/91/Shieldslot.png",
    "Arms",
    "5/5e/Legsslot.png",
    "",
    "8/89/Glovesslot.png",
    "f/f8/Bootsslot.png",
    "",
    "c/ce/Ringslot.png",
    "5/54/Ammoslot.png",
    "7/77/Wingsslot.png",
    "d/d5/Auraslot.png"
];

$(document).ready(function() {
    function format(x) {
        return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    }
    
    $(".item-bonus-table, .item-info-table").each(function() {
        var table = $(this);
        var itemId = $(this).data("item-id");
        
        $.get( "../scripts/fetch_item.php", { id: itemId }).done(function(data) {
            var json = JSON.parse(data);
            var hc = 0, dc = 0;

            //console.log("data1: "+JSON.stringify(data));
            //console.log("data2: "+data);
            //console.log("data3: "+json);
            //console.log("data4: "+json.name);
            
            // item info table
            if (table.is(".item-info-table")) {
                table.find("th").each(function() {
                    if (dc % 2 === 1) {
                        var idx = parseInt(dc / 2);
                        var note = parseInt(json.note) === parseInt(itemId);
                        if (note)
                            note = "noted";
                        else
                            note = "yes";
                        
                        var halch = format(parseInt(json.price * 0.9375));
                        var lalch = format(parseInt(json.price * 0.9375 * 0.85));
                        var shop = format(Math.round(json.price * 0.6));
                        
                        if (idx > 0) {
                            if (idx == 1) // tradeable
                                $(this).text(json.trade);
                            else if (idx == 2) // equippable
                                $(this).text(json.slot === 6 ? "no" : "yes");
                            else if (idx == 3) // stackable
                                $(this).text(json.stack);
                            else if (idx == 4) // noteable
                                $(this).text(note);
                            else if (idx == 5) // destroy
                                $(this).text(json.destroy);
                            else if (idx == 6) // general store
                                $(this).text(shop);
                            else if (idx == 7) // low alch
                                $(this).text(lalch);
                            else if (idx == 8) // high alch
                                $(this).text(halch);
                            else if (idx == 9) // weight
                                $(this).text(json.weight+" kg");
                            else if (idx == 10) // examine
                                $(this).text("You have a "+json.name+".");
                        }
                    }
                    dc++;
                });
                
            // bonuses table
            } else {
                // iterate over headers
                table.find("th").each(function() {
                    if (hc === 0)
                        $(this).text(json.name);
                    hc++;
                });
                
                // iterate over table data
                table.find("td").each(function() {
                    if (dc === 0)
                        $(this).text(json.mlAcc);
                    else if (dc === 1)
                        $(this).text(json.rnAcc);
                    else if (dc === 2)
                        $(this).text(json.mgAcc);
                    else if (dc === 3)
                        $(this).text(json.mlDef);
                    else if (dc === 4)
                        $(this).text(json.rnDef);
                    else if (dc === 5)
                        $(this).text(json.mgDef);
                    else if (dc == 6)
                        $(this).text(json.pray);
                    else if (dc == 7) {
                        this.innerHTML = "";
                        var slotFile = slotToImg[json.slot];
                        var slotName = slotFile.substring(5, slotFile.length-8);
                        var iconLink = document.createElement("a");
                        var iconImg = document.createElement("img");
                        $(iconLink).attr("href", "/wiki/File:"+slotName+"slot.png");
                        $(iconLink).attr("class", "image");
                        $(iconImg).attr("alt", slotName+"slot.png");
                        $(iconImg).attr("src", "/wiki/images/thumb/"+slotFile+"/30px-"+slotName+"slot.png");
                        $(iconImg).attr("width", "30");
                        $(iconImg).attr("height", "30");
                        $(iconImg).attr("srcset", "/wiki/images/"+slotFile+" 1.5x, /wiki/images/"+slotFile+" 2x");
                        iconLink.appendChild(iconImg);
                        this.appendChild(iconLink);
                    } else if (dc === 8)
                        $(this).text(json.mlStr);
                    else if (dc === 9)
                        $(this).text(json.rnStr);
                    else if (dc === 10)
                        $(this).text(json.mgStr);
                    else if (dc === 11)
                        $(this).text(json.degrade === "1" ? "Yes" : "No");
                    else if (dc === 12)
                        $(this).text(json.repair === "1" ? "Yes" : "No");
                    else if (dc === 13) {
                        if (json.req === "")
                            $(this).text("None");
                        else
                            $(this).text(json.req);
                    } else if (dc === 14) {
                        if (json.slot === "3")
                            $(this).text(json.speed);
                        else
                            $(this).text("-");
                    } else
                        $(this).text(dc+" data column");
                    
                    dc++;
                });
            }
        });
    });
});