Module:ItemLink

From CrossCode Wiki
Jump to navigation Jump to search
File:Template-noinfo.svg Module Documentation

This module has no documentation. If you know how to use this module, please add some.


-- <nowiki>
--------------------------------------------------------------------------------
-- Converted from [[Template:ItemLink]]
--
-- @module itemLink
-- @alias  p
-- @require Module:WsBool
-- @require Module:Arguments
-- @require Module:ItemLink/nameList
-- @require Module:ItemLink/levelList
-- @require Module:ItemLink/foodIconList
-- @author [[User:ExE Boss]]
--------------------------------------------------------------------------------

local libraryUtil = require("libraryUtil");
local wsBool = require("Module:WsBool");
local getArgs = require("Module:Arguments").getArgs;

local nameOverrides = mw.loadData("Module:ItemLink/nameList");
local levelList = mw.loadData("Module:ItemLink/levelList");
local foodIconList = mw.loadData("Module:ItemLink/foodIconList");

local p = {};

--------------------------------------------------------------------------------
-- Strips characters that are invalid in MediaWiki page titles.
--
-- @param {string} text
-- @return {string}
--------------------------------------------------------------------------------
local function fixLink(text)
	return (mw.ustring.gsub(text, "[%#%<%>%[%]%{%}%|]", ""))
end

--------------------------------------------------------------------------------
-- The main entrypoint
--
-- @function p.main
-- @param {Frame|table} args
-- @param {string} args[1] The item name
-- @return {string|number|nil}
--------------------------------------------------------------------------------
function p.main(frame)
	local args = getArgs(frame);
	libraryUtil.checkType("_main", 1, args, "table");

	args = p.validateArgs(args);
	if (args.levelOverride) then
		local level = levelList[args.name];
		if (level == "ascended" and not args.allowAscended) then
			level = nil;
		end
		return level;
	end

	return p.createItemLink(args);
end

--------------------------------------------------------------------------------
-- @function p.valiadateArgs
-- @param {table} args
-- @return {table}
--------------------------------------------------------------------------------
function p.validateArgs(args)
	libraryUtil.checkType("validateArgs", 1, args, "table");

	local name = args[1];
	libraryUtil.checkType("validateArgs", 1, name, "string", true);

	local result = {};
	result.name = name;

	result.levelOverride = wsBool(args.levelOverride) or false;
	if (result.levelOverride) then
		result.allowAscended = wsBool(args.allowAscended) or false;
		return result;
	end

	local nowrap = wsBool(args.nowrap, true);
	nowrap = nowrap == nil and true or nowrap;
	result.nowrap = nowrap;

	local showLevel = not wsBool(args.noLevel);
	if (showLevel == true) then
		local level = args.level;
		if (level ~= "ascended") then
			if (type(level) == "string") then
				level = tonumber(level) or level;
			end
			libraryUtil.checkTypeForNamedArg("validateArgs", "level", level, "number", true);
			level = level or levelList[name];
		end
		result.level = level ~= 0 and level;
	end

	local foodIcon = args.foodIcon;
	libraryUtil.checkTypeForNamedArg("validateArgs", "foodIcon", foodIcon, "string", true);
	if (not foodIcon and wsBool(args.showFoodIcon)) then
		foodIcon = foodIconList[name];
	end
	result.foodIcon = foodIcon;

	result.showIcon = not wsBool(args.noIcon);

	local displayName = args.showName;
	libraryUtil.checkTypeForNamedArg("validateArgs", "showName", displayName, "string", true);

	if ((displayName == nil) and not wsBool(args.noRename)) then
		displayName = nameOverrides[name];
		result.autoRenamed = displayName ~= nil;
	else
		result.autoRenamed = false;
	end

	result.displayName = displayName or name;
	result.link = not wsBool(args.noLink);

	return result;
end

--------------------------------------------------------------------------------
-- @function p.createItemLink
-- @param {table} data
-- @return {string}
--------------------------------------------------------------------------------
function p.createItemLink(args)
	libraryUtil.checkType("createItemLink", 1, args, "table");

	local root = mw.html.create();
	if (not args or not args.name) then
		root
			:tag("strong")
			:addClass("error")
			:wikitext(string.format(
				"Missing required parameter #1 to <code>%s[[Template:ItemLink|ItemLink]]%s</code>",
				mw.text.nowiki('{{'), mw.text.nowiki('}}')
			));
		return tostring(root);
	end
	
	local current = root;
	if (args.nowrap) then
		current = current
			:tag("span")
			:addClass("nowrap")
			:css("white-space", "nowrap");
	end

	local level = args.level;
	if (level) then
		local levelTag = current
			:tag("span")
			:addClass("itemLinkLevel");
		if (level == "ascended") then
			levelTag
				:addClass("ascended")
				:wikitext("LVL↑");
		else
			local levelString = level < 10 and "0"..tostring(level) or tostring(level);
			levelTag
				:wikitext("LV")
				:tag("sub")
					:wikitext(levelString)
		end
	end

	if (args.foodIcon) then
		current
			:tag("span")
				:addClass("itemLinkFood")
				-- This needs to be set here as opposed to [[MediaWiki:Common.css]], so that it takes effect on the FandomMobile skin as well:
				:cssText("image-rendering: -webkit-optimize-contrast; image-rendering: crisp-edges;")
				:wikitext("[[File:", fixLink(args.foodIcon), "]]");
	end

	if (args.showIcon) then
		local iconSize = "24x24px";
		local iconTag = current
			:tag("span")
			:addClass("itemLinkIcon")
			-- This needs to be set here as opposed to [[MediaWiki:Common.css]], so that it takes effect on the FandomMobile skin as well:
			:cssText("image-rendering: -webkit-optimize-contrast; image-rendering: crisp-edges;");

		if (level == "ascended") then
			iconTag:addClass("ascended");
			iconSize = "26x26px";
		end

		iconTag:wikitext("[[File:", fixLink(args.name), ".png|", iconSize, "]]");
	end

	if (args.autoRenamed) then
		current:wikitext("[[Category:AutoRenamedItem]]");
	end

	if (args.link) then
		current:wikitext(
			"[[",	fixLink(args.name),
			"|",	args.displayName);
		if (args.autoRenamed) then
			current:tag("sub"):wikitext("r");
		end
		current:wikitext("]]");
	else
		current:wikitext(args.displayName);
		if (args.autoRenamed) then
			current:tag("sub"):wikitext("r");
		end
	end

	return tostring(root);
end

return p;