Module:ResourceLink: Difference between revisions
From Against the Storm Official Wiki
m (Now uses the correct default size) |
(Updated to externalize the view, making this module significantly simpler) |
||
Line 1: | Line 1: | ||
--- @module ResourceLink | --- @module ResourceLink | ||
local ResourceLink = {} | local ResourceLink = {} | ||
--region Dependencies | |||
local GoodsData = require("Module:GoodsData") | local GoodsData = require("Module:GoodsData") | ||
local VIEW_TEMPLATE = "Resource_link/view" | |||
--endregion | |||
Line 25: | Line 15: | ||
--region Private constants | --region Private constants | ||
local | local VALID_SIZE = { | ||
["none"] = true, | |||
["small"] = true, | |||
["medium"] = true, | |||
["small"] = | ["large"] = true, | ||
[" | ["huge"] = true, | ||
["large"] = | |||
["huge"] = | |||
} | } | ||
--endregion | --endregion | ||
Line 43: | Line 29: | ||
--region Private methods | --region Private methods | ||
---validateResourceName retrieves the icon from the data module. | |||
--- | --- | ||
---@param name string the display name of a resource | |||
---@return string the filename of the icon | |||
local function validateResourceName(name) | |||
---@param | |||
---@ | |||
local function | |||
local goodID = GoodsData.getGoodID(name) | |||
return GoodsData.getIcon(goodID) | |||
return | |||
end | end | ||
Line 79: | Line 45: | ||
--region Public methods | --region Public methods | ||
---main is called from the template to create a link, with an icon, to the page for the specified goods. | |||
--- | --- | ||
---@param frame table the Mediawiki template context | |||
---@return string wikimarkup generated by the view template | |||
function ResourceLink.main(frame) | |||
---@param | |||
---@return string | |||
function ResourceLink. | |||
local name = frame.args.name | |||
local iconSize = frame.args.size | |||
local display = frame.args.display | |||
if not name or name == "" then | |||
return "The Resource_link template requires the name of a resource." | |||
if not | |||
return " | |||
end | end | ||
-- Validate the name. | |||
validatedIcon = validateResourceName(name) | |||
-- Handle default icon size. | |||
if not iconSize or iconSize == "" or not VALID_SIZE[iconSize] then | |||
iconSize = "small" | |||
-- | |||
if not | |||
end | end | ||
-- The args to pass to the view. | |||
viewParameters = { | |||
["name"] = name, | |||
["iconfilename"] = validatedIcon, | |||
["iconsize"] = iconsize, | |||
["display"] = display, | |||
} | |||
return | return frame:expandTemplate{ | ||
title = VIEW_TEMPLATE, | |||
args = viewParameters, | |||
} | |||
end | end | ||
Revision as of 02:37, 19 October 2024
Documentation for this module may be created at Module:ResourceLink/doc
--- @module ResourceLink local ResourceLink = {} --region Dependencies local GoodsData = require("Module:GoodsData") local VIEW_TEMPLATE = "Resource_link/view" --endregion --region Private constants local VALID_SIZE = { ["none"] = true, ["small"] = true, ["medium"] = true, ["large"] = true, ["huge"] = true, } --endregion --region Private methods ---validateResourceName retrieves the icon from the data module. --- ---@param name string the display name of a resource ---@return string the filename of the icon local function validateResourceName(name) local goodID = GoodsData.getGoodID(name) return GoodsData.getIcon(goodID) end --endregion --region Public methods ---main is called from the template to create a link, with an icon, to the page for the specified goods. --- ---@param frame table the Mediawiki template context ---@return string wikimarkup generated by the view template function ResourceLink.main(frame) local name = frame.args.name local iconSize = frame.args.size local display = frame.args.display if not name or name == "" then return "The Resource_link template requires the name of a resource." end -- Validate the name. validatedIcon = validateResourceName(name) -- Handle default icon size. if not iconSize or iconSize == "" or not VALID_SIZE[iconSize] then iconSize = "small" end -- The args to pass to the view. viewParameters = { ["name"] = name, ["iconfilename"] = validatedIcon, ["iconsize"] = iconsize, ["display"] = display, } return frame:expandTemplate{ title = VIEW_TEMPLATE, args = viewParameters, } end --endregion return ResourceLink