MediaWiki:Common.js

From Calamity Mod Wiki
Jump to navigation Jump to search

CSS and Javascript changes must comply with the wiki design rules.


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. */

// AJAX tables (for Template:Ajax)
function addAjaxDisplayLink() {
	$("table.ajax").each(function (i) {
		var table = $(this).attr("id", "ajaxTable" + i);
		table.find(".nojs-message").remove();
		var headerLinks = $('<span style="float: right;">').appendTo(table.find('th').first());
		var cell = table.find("td").first(), needLink = true;
		cell.parent().show();
		if (cell.hasClass("showLinkHere")) {
			var old = cell.html(), rep = old.replace(/\[link\](.*?)\[\/link\]/, '<a href="javascript:;" class="ajax-load-link">$1</a>');
			if (rep != old) {
				cell.html(rep);
				needLink = false;
			}
		}
		if (needLink) headerLinks.html('[<a href="javascript:;" class="ajax-load-link">show data</a>]');
		table.find(".ajax-load-link").parent().addBack().filter('a').click(function(event) {
			event.preventDefault();
			var sourceTitle = table.data('ajax-source-page'), baseLink = mw.config.get('wgScript') + '?';
			cell.text('Please wait, the content is being loaded...');
			$.get(baseLink + $.param({ action: 'render', title: sourceTitle }), function (data) {
				if (data) {
					cell.html(data);
					cell.find('.ajaxHide').remove();
					cell.find('.terraria').removeClass('terraria');
					if (cell.find("table.sortable").length) {
						mw.loader.using('jquery.tablesorter', function() {
							cell.find("table.sortable").tablesorter();
						});
					}
					headerLinks.text('[');
					headerLinks.append($('<a>edit</a>').attr('href', baseLink + $.param({ action: 'edit', title: sourceTitle })));
					headerLinks.append(document.createTextNode(']\u00A0['));
					var shown = true;
					$("<a href='javascript:;'>hide</a>").click(function() {
						shown = !shown;
						shown ? cell.show() : cell.hide();
						$(this).text(shown ? "hide" : "show");
					}).appendTo(headerLinks);
					headerLinks.append(document.createTextNode(']'));
				}
			}).error(function() {
				cell.text('Unable to load table; the source article for it might not exist.');
			});
		});
	});
}

$(addAjaxDisplayLink);

   //main page header.
   var $btn = $('#mf-wikiheader #mf-wikiheader-toggle-link');
   if($btn.length){
      var $box = $('#mf-wikiheader');
      $btn.css('display', 'inline');
      if($box.innerHeight() > 180){
         $box.addClass('collapsed');
      }
      $btn.on('click', function(){
         $box.toggleClass('collapsed');
      });
   }


// Recipe finder functionality - by Philo04
if(mw.config.get("wgPageName") == "Calamity_Mod_Wiki:Recipe_Finder"){
	var RecipeFinderSearchBox = $("<input>")
		.addClass("mw-inputbox-input mw-ui-input mw-ui-input-inline")
		.css("vertical-align", "top")
		.attr("id", "RecipeFinderSearchInput")
		.attr("autocomplete", "off");
	var RecipeFinderSearchButton = $("<button>")
		.addClass("mw-ui-button mw-ui-progressive")
		.attr("id", "RecipeFinderSearchButton")
		.attr("title", "Search")
		.append("Search");
	$("#RecipeFinderInput").append(RecipeFinderSearchBox, RecipeFinderSearchButton);

	function FindRecipes(){
		var inputArray = $("#RecipeFinderSearchInput").val().trim().toLowerCase().split(" ");
		var excludedWords = ["of"];
		for (var i = 0; i < inputArray.length; i++){
			if (!excludedWords.includes(inputArray[i])) {
				inputArray[i] = inputArray[i][0].toUpperCase() + inputArray[i].substr(1);
			}
		}
		
		var RecipeFinderSearchQuery = "{{tr2e|" + inputArray.join(" ") + "}}";
		RecipeFinderSearchQuery = RecipeFinderSearchQuery.replace("{{tr2e|#", "#{{tr2e|");
		new mw.Api().get({
			action: "parse",
			text: "{{#if:{{recipes/exist|ingredient=" + RecipeFinderSearchQuery + "}}|{{recipes|ingredient=" + RecipeFinderSearchQuery + "|title={{item|" + RecipeFinderSearchQuery.replace(/^#/, "") + "|note=({{recipes/count|ingredient=" + RecipeFinderSearchQuery + "}} recipes)}}}}|<span style=\"color:red;font-weight:bold;\">Recipes: No result</span>}}",
			disablelimitreport: true,
			format: "json"
		}).then(function(data){
			$("#RecipeFinderOutput").empty();
			$("#RecipeFinderOutput").append(data.parse.text['*']);
			mw.loader.using(["jquery.tablesorter"], function(){// have to load separately to make the table sortable
				$("#RecipeFinderOutput table.sortable").tablesorter();
			});
		});
	}

	$("#RecipeFinderSearchButton").on("click", FindRecipes);
	
	$("#RecipeFinderSearchInput").on("keyup", function(event){
		if(event.key == "Enter"){
			FindRecipes();
		}
	});
}