MediaWiki:Common.js
From Card Hunter Wiki
Note: After saving, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Internet Explorer: Hold Ctrl while clicking Refresh, or press Ctrl-F5
- Opera: Clear the cache in Tools → Preferences
/* Any JavaScript here will be loaded for all users on every page load. */
article = "";
var tooltipsOn = true;
var $tfb, $ttfb, $htt;
var activeHoverLink = null;
var tipCache = new Object();
function process(list){
var chance = ['11.11%', '21.27%', '30.53%', '38.95%', '46.58%', '53.48%', '59.68%', '65.24%', '70.21%', '74.62%', '78.52%', '81.96%', '84.97%', '87.58%', '89.84%', '91.77%', '93.42%', '94.81%', '95.96%', '96.91%'];
list.find('li').each(function(){
var li = $('<span/>').attr('id', 'drawchance').css('font-weight','bold');
var count = parseInt($(this).find('#cardtypenum').text());
li.text(" "+chance[count-1]);
$(this).find('#cardtypenum').remove();
$(this).after(li);
});
}
function finalDeck(Inlist){
var cardtypes = ['Armor\n', 'Assist\n', 'Attack\n', 'Block\n', 'Boost\n', 'Handicap\n', 'Movement\n', 'Utility\n', 'Hybrid\n', 'No card type\n'];
var flist = $("<ul/>")
$.each(cardtypes, function(i){
var li = $('<li/>')
.attr('id', 'ctypeheader')
.css('font-weight','bold')
.text(cardtypes[i])
.appendTo(flist);
});
Inlist.find('li').each(function(){
for (var m=0;m<flist.find('li#ctypeheader').length;m++){
if ($(this).find('span#ctype').text()==flist.find('li#ctypeheader').eq(m).text()){
$(this).find('span#ctype').remove();
var entry=$(this);
flist.find('li#ctypeheader').eq(m).after(entry);
}
}
});
var statlist=$("<ul/>").attr('id','stats');
flist.find('li#ctypeheader').each(function(){
var sum=0;
var statli=$("<li/>").attr('id','cardtype');
var statspan=$("<span/>").attr('id','cardtypenum');
if ($(this).next().is('li#ctypeheader')||($(this).next().length==0)) $(this).hide();
else{
$(this).nextUntil('li#ctypeheader').each(function(){
sum+=parseInt($(this).find('span#cqty').text());
});
statli.text($(this).text()).append(statspan.text(sum));
statli.appendTo(statlist);
}
});
process(statlist);
var tablelength=Math.ceil((flist.find('li').length)/3);
var onelist = $("<ul/>").attr('id','chdeck').append(flist.find('li').slice(0, tablelength));
var twolist = $("<ul/>").attr('id','chdeck').append(flist.find('li').slice(0, tablelength));
var threelist = $("<ul/>").attr('id','chdeck').append(flist.find('li').slice(0, tablelength));
var statlistlength=Math.ceil((statlist.find('li').length)/2);
var fourlist = $("<ul/>").attr('id','chdeck').append(statlist.find('li').slice(0, statlistlength));
var fivelist = $("<ul/>").attr('id','chdeck').append(statlist.find('li').slice(0, statlistlength));
if (onelist.find('li').last().is('li#ctypeheader')) {
var crossover=onelist.find('li').last();
onelist.find('li').last().remove();
twolist.prepend(crossover);
crossover=twolist.find('li').last();
twolist.find('li').last().remove();
threelist.prepend(crossover);
}
if (twolist.find('li').last().is('li#ctypeheader')) {
var crossover=twolist.find('li').last();
twolist.find('li').last().remove();
threelist.prepend(crossover);
}
var deck = $("<table/>").attr('id','decklist').addClass('chtable_r');
deck.each(function(){
$(this).append('<caption></caption><tbody><tr><td></td><td></td><td></td><td></td><td></td></tr></tbody>');
$(this).find('caption').text("Deck List").css({'text-align':'left','padding-left':'25px'});
$(this).find('td:eq(0)').html(onelist).css('vertical-align','top');
$(this).find('td:eq(1)').html(twolist).css('vertical-align','top');
$(this).find('td:eq(2)').html(threelist).css('vertical-align','top');
$(this).find('td:eq(3)').html(fourlist).css('vertical-align','top');
$(this).find('td:eq(4)').html(fivelist).css('vertical-align','top');
});
/*var chancedeck = $("<table/>").attr('id','chancedeck').addClass('chtable_r');
chancedeck.each(function(){
$(this).append('<caption></caption><tbody><tr><td></td><td></td></tr></tbody>');
$(this).find('caption').text("Opening hand, at least 1").css({'text-align':'left','padding-left':'25px'});
$(this).find('td:eq(0)').html(fourlist).css('vertical-align','top');
$(this).find('td:eq(1)').html(fivelist).css('vertical-align','top');
});
deck.append(chancedeck);*/
return deck;
}
// takes a character build and consolidate all its cards into a deck list
function binddeck(){
$(this).each(function () {
var list = $("<ul/>");
var typelist = $("<ul/>").attr('id','chtypes');
var rowCount=$(this).find('tr').length;
var colCount=12;
$(this).find('tr').each(function() {
$(this).find('td').each (function() {
if ($(this).children().is('a')) {
var valThis = $(this).text();
var valThiscount = parseInt($(this).prev().text());
var myRow = $(this).closest('tr').index();
for (var i = myRow+1; i < rowCount; i++) {
for (var j = 0; j < colCount; j++) {
var listSel = $(this).closest('table').find('tr').eq(i).find('td').eq(j);
if ($(listSel).children().is('a')){
var listVal = $(listSel).text();
if (listVal === valThis){
var listValCount = parseInt($(listSel).prev().text());
valThiscount+=listValCount;
$(listSel).html("");
}
}
}
}
var p = $(this).map(function () {
if ($(this).html()!=null){
var thiscolCount = $(this).closest('tr').find('td').length;
for (var y = 12; y < thiscolCount; y+=2) {
var ctype = $(this).closest('tr').find('td').eq(y);
if (ctype.text()===$(this).text()){
var type=$("<span/>").attr('id','ctype');
if (ctype.next().text().trim()!=""){
type.text(ctype.next().text());
} else {
type.text("No card type\n");
}
}
}
$(this).append(type);
var cqty=$("<span/>").attr('id','cqty').text(valThiscount+" ");
$(this).prepend(cqty);
return "<li>" + $(this).html() + "</li>";
}
});
$(this).html("");
list.append($.makeArray(p).join(""));
}
});
});
$(this).parent().replaceWith(finalDeck(list));
});
}
// hides the tooltip
function hideTip() {
$tfb.html("").removeClass("tooltip-ready").addClass("hidden").css("visibility","hidden");
activeHoverLink = null;
}
// displays the tooltip
function displayTip(e) {
$tfb.not(":empty").removeClass("hidden").addClass("tooltip-ready");
moveTip(e);
$tfb.not(":empty").css("visibility","visible");
moveTip(e);
}
// moves the tooltip
function moveTip(e) {
$ct = $tfb.not(":empty");
var newTop = e.clientY + ((e.clientY > ($(window).height()/2)) ? -($ct.innerHeight()+20):20);
var newLeft = e.clientX + ((e.clientX > ($(window).width()/2)) ? -($ct.innerWidth()+20):20);
$ct.css({"position":"fixed","top":newTop + "px","left":newLeft + "px"});
}
// AJAX tooltips
function showTip(e) {
var $t=$(this);
activeHoverLink = $t;
$p=$t.parent();
if ($p.hasClass("selflink")==false) {
$t.removeAttr("title");
$p.removeAttr("title");
//var url=$p.attr("href")+" .image"; VERY SLOW LOADING OF IMAGES !
var url="/wiki/Template:Spotlight .image"; //Remember: changing the image in the template will change the 'Coming Soon' image!!!
if (tipCache[url] != null) {
$tfb.html(tipCache[url]);
displayTip(e);
return;
}
}
$tfb.load(url,function () {
if ($t != activeHoverLink) return;
if ($tfb.html() == "") $tfb.html('<div class="tooltip-content"><b>Testing tooltip</b></div>');
$tfb.find(".image").css("display","");
tipCache[url] = $tfb.html();
displayTip(e);
});
}
function bindTT() {
$t=$(this);
$p=$t.parent();
if ($p.hasClass("selflink") == false) {
$t.data("tt", $p.attr("title")).hover(showTip,hideTip).mousemove(moveTip);
}
}
function addTT() {
$(this).wrapInner('<span class="ajaxttlink" />');
}
function Inittooltips() {
if ($tfb == null) {
$(article).append('<div id="tfb" class="htt"></div><div id="templatetfb" class="htt"></div>');
$tfb = $("#tfb");
$ttfb = $("#templatetfb");
$htt = $("#tfb,#templatetfb");
}
$(article + " a[href*='wiki/Cards/']:not(:has(span.ajaxttlink))").each(addTT);
$(article + " span.ajaxttlink:not(:has(span.disablett))").each(bindTT);
}
function modifyHeader() {
var str=$("#firstHeading").html();
if (str.indexOf("Cards/")==17||str.indexOf("Items/")==17) $("#firstHeading").html(str.slice(23));
}
$(function() {
article = "#bodyContent";
$(article + " #builddeck" + " table.smwtable").each(binddeck);
var headerstr=$("#firstHeading").html();
/*js to remove ugly header in Main page.*/
if (headerstr.indexOf("Cardhuntria:Community portal")>0) $("#firstHeading").remove();
/*js to strip "Cards/" and "Items/" from headers belonging to card and item pages.*/
if ((headerstr.indexOf("Cards/")>0) || (headerstr.indexOf("Items/")>0)) modifyHeader();
else if (headerstr.indexOf("Category:")>0) ;
else if (tooltipsOn) Inittooltips();
});