MediaWiki:Common.js

From Wiki The-West EN
Revision as of 09:19, 15 October 2022 by Criminus (talk | contribs)
Jump to navigation

Note: After publishing, 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 / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/* Any JavaScript here will be loaded for all users on every page load. */
$('.tbbox-logo').wrap('<a href="/wiki/Main_Page"></a>');
if ($('.item_container').length !== 0) {
  $('.item_container').each(function () {
    $(this).append(
      '<img src="https://westzz.innogamescdn.com/images/items/' +
        $(this).attr('data-url') +
        '?1">'
    );
  });
  $.getScript('/wiki/Popup.js?action=raw');
}

$(window).scroll(function () {
  if ($(this).scrollTop() > 300) {
    $('#back-top').fadeIn();
  } else {
    $('#back-top').fadeOut();
  }
});
// Scroll body to top on click
$('#back-top a').click(function () {
  $('body,html').animate(
    {
      scrollTop: 0,
    },
    800
  );
  return false;
});
// Embed youtube videos
if ($('.youtube_video').length !== 0) {
  $('.youtube_video').each(function () {
    $(this).html(
      '<iframe width="100%" height="100%" src="https://www.youtube.com/embed/' +
        $(this).attr('data-id') +
        '?cc_load_policy=1&cc_lang_pref=' +
        $(this).attr('data-subtitles') +
        '" frameborder="0" allowfullscreen></iframe>'
    );
  });
}
//calculate box - n3mesis
if ($('.calculate_box').length) {
  $.getScript('/wiki/Calculate.js?action=raw');
};
//crafting
if($('.target_input').length){$('.target_input p').each(function(){var id=$(this).attr('id');var phrase=$(this).text();var newInput="<input type='number' name='input_new' value='"+
phrase+"' class='target' max='50000' min='' />";$(this).replaceWith(newInput);});var multiplierValue=1;var currentAmountsArray=[];$('.amount').each(function(index){currentAmountsArray[index]=parseInt($(this).text());});$("input[name='input_new']").on('input paste keyup',function(){this.value>50000?(this.value=50000):this.value<0&&(this.value=0);multiplierValue=this.value;$('.amount').each(function(index){$(this).text(currentAmountsArray[index]*multiplierValue);});$('.craft_extra').show();$('.craft_extra .item_container').each(function(){$(this).children().addClass('OverlayItem');});});}

// As seen on https://bitbucket.org/cmcqueen1975/htmlfloatingtableheader/overview
function UpdateTableHeaders() {
	$("div.divTableWithFloatingHeader").each(function() {
		var originalHeaderRow = $(".tableFloatingHeaderOriginal", this);
		var floatingHeaderRow = $(".tableFloatingHeader", this);
		var offset = $(this).offset();
		var scrollTop = $(window).scrollTop();
		if ((scrollTop > originalHeaderRow.offset().top) && (scrollTop < offset.top + $(this).height() - originalHeaderRow.height())) {
			floatingHeaderRow.css("visibility", "visible");
			floatingHeaderRow.css("top", "0px");

			// Copy cell widths from original header
			$("th", floatingHeaderRow).each(function(index) {
				var cellWidth = $("th", originalHeaderRow).eq(index).css('width');
				$(this).css('width', cellWidth);
			});

			// Copy row width from whole table
			floatingHeaderRow.css("width", $(this).css("width"));
		}
		else {
			floatingHeaderRow.css("visibility", "hidden");
			floatingHeaderRow.css("top", "0px");
		}
	});
};

$(function() {
	$("table.tableWithFloatingHeader").each(function() {
		$(this).wrap("<div class=\"divTableWithFloatingHeader\" style=\"position:relative\"></div>");

		var cls = "tr.floatingHeader";
		if($(cls, this).length == 0) {
			cls = "tr";
		}
		var originalHeaderRow = $(cls, this).first();
		var clonedHeaderRow = originalHeaderRow.clone().insertBefore(originalHeaderRow);

		clonedHeaderRow.addClass("tableFloatingHeader");
		clonedHeaderRow.css("position", "fixed");
		clonedHeaderRow.css("top", "0px");
		clonedHeaderRow.css("left", $(this).offset().left);
		clonedHeaderRow.css("visibility", "hidden");
		clonedHeaderRow.css("z-index", 1);

		originalHeaderRow.addClass("tableFloatingHeaderOriginal");
	});
	UpdateTableHeaders();
	$(window).on('scroll', UpdateTableHeaders);
	$(window).on('resize', UpdateTableHeaders);
});

function sortTable(column, type) {

  //Get and set order
  //Use -data to store wheater it will be sorted ascending or descending
  var order = $('.table thead tr>th:eq(' + column + ')').data('order');
  order = order === 'ASC' ? 'DESC' : 'ASC';
  $('.table thead tr>th:eq(' + column + ')').data('order', order);

  //Sort the table
  $('.table tbody tr').sort(function(a, b) {
  //                                 ^  ^
  //                                 |  | 
  //        The 2 parameters needed to be compared. 
  //        Since you are sorting rows, a and b are <tr>                                 

    //Find the <td> using the column number and get the text value.
    //Now, the a and b are the text of the <td>
    a = $(a).find('td:eq(' + column + ')').text();
    b = $(b).find('td:eq(' + column + ')').text();

    switch (type) {
      case 'text':
        //Proper way to compare text in js is using localeCompare
        //If order is ascending you can - a.localeCompare(b)
        //If order is descending you can - b.localeCompare(a);
        return order === 'ASC' ? a.localeCompare(b) : b.localeCompare(a);
        break;
      case 'number':
        //You can use deduct to compare if number.
        //If order is ascending you can -> a - b. 
        //Which means if a is bigger. It will return a positive number. b will be positioned first
        //If b is bigger, it will return a negative number. a will be positioned first
        return order === 'ASC' ? a - b : b - a;
        break;
      case 'date':
        var dateFormat = function(dt) {
          [m, d, y] = dt.split('/');
          return [y, m - 1, d];
        }

        //convert the date string to an object using `new Date`
        a = new Date(...dateFormat(a));
        b = new Date(...dateFormat(b));

        //You can use getTime() to convert the date object into numbers. 
        //getTime() method returns the number of milliseconds between midnight of January 1, 1970
        //So since a and b are numbers now, you can use the same process if the type is number. Just deduct the values.
        return order === 'ASC' ? a.getTime() - b.getTime() : b.getTime() - a.getTime();
        break;
    }

  }).appendTo('.table tbody');
}

$('#money').click(function() {
  sortTable(1, 'text');
});
$('#xp').click(function() {
  sortTable(2, 'number');
});
$('#luck').click(function() {
  sortTable(3, 'date');
});