MediaWiki:Common.js
From Card Hunter Wiki
(Difference between revisions)
| Line 259: | Line 259: | ||
if (blahstr.indexOf("blah")>0) $("#mw-content-text").html("phase test"); | if (blahstr.indexOf("blah")>0) $("#mw-content-text").html("phase test"); | ||
| − | + | var getdata=$.get("http://api.cardhunter.com/players"); | |
| − | + | var ajax_call = function() { | |
| − | + | $("#mw-content-text").html(getdata); | |
| − | + | }; | |
//var interval = 1000 * 60 * 1; | //var interval = 1000 * 60 * 1; | ||
//setInterval(ajax_call, interval); | //setInterval(ajax_call, interval); | ||
| − | |||
} | } | ||
Revision as of 19:36, 20 December 2013
/* Any JavaScript here will be loaded for all users on every page load. */
article = "";
var tooltipsOn = true;
var $tfb;
var $token;
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 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%', '99.74%', '99.85%', '99.91%', '99.95%', '99.98%', '99.99%', '100%', '100%', '100%', '100%'];
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%', '97.68%', '98.3%', '98.79%', '99.16%', '99.44%', '99.64%', '99.79%', '99.88%', '99.94%', '99.97%'];
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")+" .cardimage";
//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);
}
function testing(){
var blahstr=$("#mw-content-text").html();
if (blahstr.indexOf("blah")>0) $("#mw-content-text").html("phase test");
var getdata=$.get("http://api.cardhunter.com/players");
var ajax_call = function() {
$("#mw-content-text").html(getdata);
};
//var interval = 1000 * 60 * 1;
//setInterval(ajax_call, interval);
}
function showImages() {
$(this).each(function(){
if ($(this).val().indexOf("1 C")>0){
var label_conts=$(this).parent().contents()[$(this).parent().contents().length-1];
label_conts.nodeValue=label_conts.nodeValue.slice(0,-1);
$('<img width="21" height="21" alt="TalentNormalFlat.png" src="/images/f/fb/TalentNormalFlat.png" />').insertAfter(label_conts);
}
if ($(this).val().indexOf("1 S")>0){
var label_conts=$(this).parent().contents()[$(this).parent().contents().length-1];
label_conts.nodeValue=label_conts.nodeValue.slice(0,-1);
$('<img width="21" height="21" alt="TalentBronzeFlat.png" src="/images/9/9c/TalentBronzeFlat.png" />').insertAfter(label_conts);
}
if ($(this).val().indexOf("1 T")>0){
var label_conts=$(this).parent().contents()[$(this).parent().contents().length-1];
label_conts.nodeValue=label_conts.nodeValue.slice(0,-1);
$('<img width="21" height="21" alt="TalentSilverFlat.png" src="/images/2/2c/TalentSilverFlat.png" />').insertAfter(label_conts);
}
if ($(this).val().indexOf("1 P")>0){
var label_conts=$(this).parent().contents()[$(this).parent().contents().length-1];
label_conts.nodeValue=label_conts.nodeValue.slice(0,-1);
$('<img width="21" height="21" alt="TalentGoldFlat.png" src="/images/4/4f/TalentGoldFlat.png" />').insertAfter(label_conts);
}
if ($(this).val()=="CC"){
var label_conts=$(this).parent().contents()[$(this).parent().contents().length-1];
label_conts.nodeValue=label_conts.nodeValue.slice(0,-2);
$('<img width="21" height="21" alt="TalentNormalFlat.png" src="/images/f/fb/TalentNormalFlat.png" /><img width="21" height="21" alt="TalentNormalFlat.png" src="/images/f/fb/TalentNormalFlat.png" />').insertAfter(label_conts);
}
if ($(this).val()=="SC"){
var label_conts=$(this).parent().contents()[$(this).parent().contents().length-1];
label_conts.nodeValue=label_conts.nodeValue.slice(0,-2);
$('<img width="21" height="21" alt="TalentBronzeFlat.png" src="/images/9/9c/TalentBronzeFlat.png" /><img width="21" height="21" alt="TalentNormalFlat.png" src="/images/f/fb/TalentNormalFlat.png" />').insertAfter(label_conts);
}
if ($(this).val()=="TC"){
var label_conts=$(this).parent().contents()[$(this).parent().contents().length-1];
label_conts.nodeValue=label_conts.nodeValue.slice(0,-2);
$('<img width="21" height="21" alt="TalentSilverFlat.png" src="/images/2/2c/TalentSilverFlat.png" /><img width="21" height="21" alt="TalentNormalFlat.png" src="/images/f/fb/TalentNormalFlat.png" />').insertAfter(label_conts);
}
if ($(this).val()=="PC"){
var label_conts=$(this).parent().contents()[$(this).parent().contents().length-1];
label_conts.nodeValue=label_conts.nodeValue.slice(0,-2);
$('<img width="21" height="21" alt="TalentGoldFlat.png" src="/images/4/4f/TalentGoldFlat.png" /><img width="21" height="21" alt="TalentNormalFlat.png" src="/images/f/fb/TalentNormalFlat.png" />').insertAfter(label_conts);
}
if ($(this).val()=="SS"){
var label_conts=$(this).parent().contents()[$(this).parent().contents().length-1];
label_conts.nodeValue=label_conts.nodeValue.slice(0,-2);
$('<img width="21" height="21" alt="TalentBronzeFlat.png" src="/images/9/9c/TalentBronzeFlat.png" /><img width="21" height="21" alt="TalentBronzeFlat.png" src="/images/9/9c/TalentBronzeFlat.png" />').insertAfter(label_conts);
}
if ($(this).val()=="TS"){
var label_conts=$(this).parent().contents()[$(this).parent().contents().length-1];
label_conts.nodeValue=label_conts.nodeValue.slice(0,-2);
$('<img width="21" height="21" alt="TalentSilverFlat.png" src="/images/2/2c/TalentSilverFlat.png" /><img width="21" height="21" alt="TalentBronzeFlat.png" src="/images/9/9c/TalentBronzeFlat.png" />').insertAfter(label_conts);
}
if ($(this).val()=="PS"){
var label_conts=$(this).parent().contents()[$(this).parent().contents().length-1];
label_conts.nodeValue=label_conts.nodeValue.slice(0,-2);
$('<img width="21" height="21" alt="TalentGoldFlat.png" src="/images/4/4f/TalentGoldFlat.png" /><img width="21" height="21" alt="TalentBronzeFlat.png" src="/images/9/9c/TalentBronzeFlat.png" />').insertAfter(label_conts);
}
if ($(this).val()=="TT"){
var label_conts=$(this).parent().contents()[$(this).parent().contents().length-1];
label_conts.nodeValue=label_conts.nodeValue.slice(0,-2);
$('<img width="21" height="21" alt="TalentSilverFlat.png" src="/images/2/2c/TalentSilverFlat.png" /><img width="21" height="21" alt="TalentSilverFlat.png" src="/images/2/2c/TalentSilverFlat.png" />').insertAfter(label_conts);
}
if ($(this).val()=="PT"){
var label_conts=$(this).parent().contents()[$(this).parent().contents().length-1];
label_conts.nodeValue=label_conts.nodeValue.slice(0,-2);
$('<img width="21" height="21" alt="TalentGoldFlat.png" src="/images/4/4f/TalentGoldFlat.png" /><img width="21" height="21" alt="TalentSilverFlat.png" src="/images/2/2c/TalentSilverFlat.png" />').insertAfter(label_conts);
}
if ($(this).val()=="PP"){
var label_conts=$(this).parent().contents()[$(this).parent().contents().length-1];
label_conts.nodeValue=label_conts.nodeValue.slice(0,-2);
$('<img width="21" height="21" alt="TalentGoldFlat.png" src="/images/4/4f/TalentGoldFlat.png" /><img width="21" height="21" alt="TalentGoldFlat.png" src="/images/4/4f/TalentGoldFlat.png" />').insertAfter(label_conts);
}
});
}
// show token images in Item Search Form radiobuttons
function showTokenImages() {
$(article + " #mw-content-text" + " table span.radioButtonSpan label.radioButtonItem input").each(showImages);
}
// 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","border":"1px solid rgb(0,80,104)"});
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("Item Search Form")>0) showTokenImages();
else if (headerstr.indexOf("Testing")>0) testing();
else if (tooltipsOn) Inittooltips();
});