MediaWiki:Common.js

From Card Hunter Wiki
Revision as of 01:26, 5 April 2013 by Phaselock (Talk | contribs)

Jump to: navigation, search

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;
var activeHoverLink = null;
var tipCache = new Object();
var LOADING = '<div class="tooltip-loader" style="visibility: visible;">Loading...</div>';
var LOADERROR = '<div class="tooltip-loader"><b>Error</b><br />This target either has no tooltip<br />or was not intended to have one.</div>';

function twoprocess(list){
    var chance2 = ['16.67%', '30.95%', '43.14%', '53.48%', '62.2%', '69.52%', '75.61%', '80.66%', '84.8%', '88.18%', '90.91%', '93.09%', '94.82%', '96.17%', '97.21%', '98.01%', '98.61%', '99.05%', '99.36%', '99.59%'];
    var list2=list.clone();
    list2.find('li').each(function(){
        var li2 = $('<li/>').attr('id', 'drawchance').css({'font-weight':'bold','color':'rgb(255,213,23)'});
        var count2 = parseInt($(this).find('#cardtypenum').text());
        li2.text(chance2[count2-1]);
        $(this).find('#cardtypenum').remove();
        $(this).after(li2);
    });
    return list2;
}

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 = $('<li/>').attr('id', 'drawchance').css({'font-weight':'bold','color':'rgb(0,255,0)'});
        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);
        }
    });
    var chancetwo=twoprocess(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#cardtype').length));
    if (statlistlength % 2 != 0 ) statlistlength+=1;
    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));
    var sixlist = $("<ul/>").attr('id','chdeck').append(chancetwo.find('li').slice(0, statlistlength));
    var sevenlist = $("<ul/>").attr('id','chdeck').append(chancetwo.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('<tbody><tr><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr></tbody>');
        $(this).find('td:eq(0)').text("Deck List").css({'text-align':'left','padding-left':'20px','font-weight':'bold','font-size':'125%'}).attr('colspan','3');
        $(this).find('td:eq(1)').text("Opening hand, at least 1").css({'text-align':'left','padding-left':'20px','font-weight':'bold'}).attr('colspan','2');
        $(this).find('td:eq(2)').text("Second turn, at least 1").css({'text-align':'left','padding-left':'20px','font-weight':'bold'}).attr('colspan','2');        
        $(this).find('td:eq(3)').html(onelist).css('vertical-align','top');
        $(this).find('td:eq(4)').html(twolist).css('vertical-align','top');
        $(this).find('td:eq(5)').html(threelist).css('vertical-align','top');
        $(this).find('td:eq(6)').html(fourlist).css('vertical-align','top');
        $(this).find('td:eq(7)').html(fivelist).css('vertical-align','top');
        $(this).find('td:eq(8)').html(sixlist).css('vertical-align','top');
        $(this).find('td:eq(9)').html(sevenlist).css('vertical-align','top');
    });
    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"});
}

//This function removes a cached "Loading" entry.
function clearCache(url) {
    if (tipCache[url] != null) {
        //Cache is not empty, let's see if we are still loading.
        if (tipCache[url].indexOf("Loading") != -1) {
            tipCache[url] = null;
        }
    }
}

// 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";
    //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);
    }else {
      $tfb.html(LOADING);
      //We put the "Loading..." into the cache, to avoid multiple fetchings
      tipCache[url] = $tfb.html();
            $tfb.load(url, function(responseText, textStatus, XMLHttpRequest) {
                if (textStatus == "timeout") {
                    //Fetching has timed out, so we remove the cached "Loading..." entry (hence enable another try)
                    clearCache(url);
                } else if ($tfb.html() == "" || textStatus != "success") {
$tfb.html('<div class="tooltip-content"><b>Error</b><br />This target either has no tooltip<br />or was not intended to have one.</div>');
                } else {
                    $tfb.find(".image").css("display", "");
                    tipCache[url] = $tfb.html();
                }
                if ($t == activeHoverLink) {
                    $tfb.html(tipCache[url]);
                    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");
  }
  $(article + " a[href*='wiki/Cards/']:not(:has(span.ajaxttlink))").each(addTT);
  $(article + " span.ajaxttlink:not(:has(span.disablett))").each(bindTT);
}

// hides the keyword tooltip
function hidekeywordTip() {
  $tfb.html("").removeClass("tooltip-ready").addClass("hidden").css("visibility","hidden"); 
  activeHoverLink = null;
}

// displays the keyword tooltip
function displaykeywordTip(e) {
  $tfb.not(":empty").removeClass("hidden").addClass("tooltip-ready");
  movekeywordTip(e);
  $tfb.not(":empty").css("visibility","visible");
  movekeywordTip(e);
}

// moves the tooltip
function movekeywordTip(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"});
}

//This function removes a cached "Loading" entry.
function clearkeywordCache(url) {
    if (tipCache[url] != null) {
        //Cache is not empty, let's see if we are still loading.
        if (tipCache[url].indexOf("Loading") != -1) {
            tipCache[url] = null;
        }
    }
}

// AJAX tooltips
function showkeywordTip(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").replace("Keywords#","")+" .keywordtt";
    //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]);
      displaykeywordTip(e);
    }else {
      $tfb.html(LOADING);
      //We put the "Loading..." into the cache, to avoid multiple fetchings
      tipCache[url] = $tfb.html();
            $tfb.load(url, function(responseText, textStatus, XMLHttpRequest) {
                if (textStatus == "timeout") {
                    //Fetching has timed out, so we remove the cached "Loading..." entry (hence enable another try)
                    clearkeywordCache(url);
                } else if ($tfb.html() == "" || textStatus != "success") {
//$tfb.html('<div class="tooltip-content"><b>Error</b><br />This target either has no tooltip<br />or was not intended to have one.</div>');
$tfb.html(LOADERROR);
tipCache[url] = $tfb.html()
                } else {
                    $tfb.find(".keywordtt").css({"display": ""});
                    $tfb.css({"background-color":"rgb(0,21,27)","max-width":"15em","padding":"7px 15px","border-radius":"0.5em","font-weight":"bold","font-size":"85%","height":"auto","text-align":"justify"}).fadeIn();
                    tipCache[url] = $tfb.html();
                }
                if ($t == activeHoverLink) {
                    $tfb.html(tipCache[url]);
                    displaykeywordTip(e);
                }
            });
    }
  }
}

function bindkeywordTT() {
  $t=$(this);
  $p=$t.parent();
  if ($p.hasClass("selflink") == false) {
    $t.data("tt", $p.attr("title")).hover(showkeywordTip,hidekeywordTip).mousemove(movekeywordTip);
  }
}

function addkeywordTT() {
    $(this).wrapInner('<span class="ajaxttlink" />');
}

function Initkeywordtips() {
  if ($tfb == null) {
    $(article).append('<div id="tfb" class="htt"></div>');
    $tfb = $("#tfb");
  }
  $(article + " a[href*='wiki/Keywords#']:not(:has(span.ajaxttlink))").each(addkeywordTT);
  $(article + " span.ajaxttlink:not(:has(span.disablekeywordtt))").each(bindkeywordTT);
}

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();
     Initkeywordtips();
  }
  else if (headerstr.indexOf("Category:")>0) ;
  else if (tooltipsOn) Inittooltips();
});
      
Personal tools
Namespaces

Variants
Actions
Navigation
Tools