Module:ServiceLink: Difference between revisions

From Against the Storm Official Wiki
(Error checking was in the wrong place)
(Whole rewrite; externalized the view; now uses the updated data models)
 
Line 1: Line 1:
---
---
--- This module renders the Service_link template to draw an icon and link in
---@module ServiceLink
--- wiki markup.
---
--- The template requires an argument, the name of the service to which
--- to link. Optionally, the template accepts a second argument to change the
--- size of the icon that accompanies the text hyperlink.
---
--- By default, the icon will default to a small size appropriate for display
--- in-line with other text. For display in tables to help with recognition,
--- use iconsize=med.
---
--- See the template documentation for more information about parameters and
--- errors and to see examples.
---
--- @module ServiceLink
local ServiceLink = {}
local ServiceLink = {}






local ServicesRecipesData = require("Module:ServicesRecipesData")
--region Dependencies
 
---@type BaseDataModel
local InstitutionsData = require("Module:InstitutionsData")
 
local VIEW_TEMPLATE = "Service_link/view"
 
--endregion




Line 25: Line 18:
--region Private constants
--region Private constants


local FLAG_NO_ICON = "none"
local VALID_SIZE = {
 
    ["none"] = true,
local DEFAULT_ICON_SIZE_TEMPLATE = "ImgS"
    ["small"] = true,
local MAP_ICON_SIZE_TEMPLATES = {
    ["medium"] = true,
["small"] = DEFAULT_ICON_SIZE_TEMPLATE,
    ["large"] = true,
["med"] = "ImgM",
    ["huge"] = true,
["large"] = "ImgL",
["huge"] = "ImgH"
}
}
local NBSP = " "


--endregion
--endregion


--region Localization string constants


local ERROR_MESSAGE_INVALID_NAME = "You must specify a service. Please see the template documentation for how to use the parameters"
local ERROR_MESSAGE_INVALID_NAME = "The service you specified was not found. Please see the template documentation for how to use the parameters"


--region Private methods
--endregion


---
--- Writes the wiki markup for the icon and the link. Uses the private constant
--- map to look up the size template and expands that template to conform to
--- standard size for the wiki.
---
---@param name string the name of the services' page to link to
---@param iconFilename string the complete filename to write
---@param iconSize string "none", "small", "med", "large", or "huge"
---@return string wiki markup of icon and hyperlink
local function makeLink(name, iconFilename, iconSize)


local linkPart = "[[" .. name .. "]]"
local iconPart = ""


if iconSize and iconSize ~= FLAG_NO_ICON then
--region Private methods


local iconTemplate = MAP_ICON_SIZE_TEMPLATES[iconSize] or DEFAULT_ICON_SIZE_TEMPLATE
local function validateServiceNameByIcon(name)
local pxSize = mw.getCurrentFrame():expandTemplate{ title=iconTemplate }


if iconFilename and iconFilename ~= "" then
    --Currently services have the same ID and name. Hopefully this doesn't change.
iconPart = "[[File: " .. iconFilename .. "|" .. pxSize .. "|link=" .. name
    local list = InstitutionsData:getIDsAndRecipesWhereProductID(name)
.. "|alt=" .. name .. "|" .. name .. "]]"
.. NBSP
end


end
    --If the list is empty, then the name is not valid.
    if #list < 1 then
        error(ERROR_MESSAGE_INVALID_NAME)
    end


return iconPart .. linkPart
    return "Icon_Need_" .. name .. ".png"
end
end


Line 79: Line 58:
--region Public methods
--region Public methods


---@public
---Extracts parameters, validates the service by getting its icon, and then sends the data to the view template for rendering.
---
---
--- Create a service link by passing the ID of the service rather than
---@param frame table the template's context, with arguments
--- through the template. Useful for calling from other modules.
---@return string wiki markup
---
function ServiceLink.main(frame)
---@param id string the ID of the service to link to
---@param iconSize string "small", "med", "large", or "huge"
---@return string wiki markup of icon and link
function ServiceLink.serviceLinkWithID(id, iconSize)
 
-- At runtime, this should never be nil or empty.
if not id or id == "" then
error("No service specified to which to make a link.")
end
 
local name = ServicesRecipesData.getRecipeServiceNameByID(id)
 
if not name then
return "No service found with that ID: " .. id .. "."
end
 
local iconFilename = ServicesRecipesData.getRecipeServiceIconByID(id)
 
return makeLink(name, iconFilename, iconSize)
end
 
 
 
---
--- Called from Template:Service_link to create a link, with an icon, to the
--- page for the specified service.
---
---@param frame table the Mediawiki context from the template
---@return string wikimarkup representing an icon and link
function ServiceLink.serviceLink(frame)


-- Extract template parameters.
    local name = frame.args.name
local argService = frame.args["service"]
    local iconSize = frame.args.size
local argIconSize = frame.args["iconsize"]
    local displayOverride = frame.args.display


if not argService or argService == "" then
    if not name or name == "" then
return "The Service link template requires the name of a service."
        error(ERROR_MESSAGE_INVALID_NAME)
end
    end


local id = ServicesRecipesData.getAllRecipeIDsForServiceName(argService)
    --Validate the name
    validatedIcon = validateServiceNameByIcon(name)


if not id then
    --Handle default icon size
return "No service found with that name: " .. argService .. "."
    if not iconSize or iconSize == "" or not VALID_SIZE[iconSize] then
end
        iconSize = "small"
    end


local iconFilename = ServicesRecipesData.getRecipeServiceIconByID(id[1])
    --The args to pass to the view.
    viewParameters = {
        ["name"] = name,
        ["iconfilename"] = validatedIcon,
        ["iconsize"] = iconSize,
        ["display"] = displayOverride,
    }


return makeLink(argService, iconFilename, argIconSize)
    return frame:expandTemplate{
        title = VIEW_TEMPLATE,
        args = viewParameters,
    }
end
end



Latest revision as of 14:25, 9 November 2024

Documentation for this module may be created at Module:ServiceLink/doc

---
---@module ServiceLink
local ServiceLink = {}



--region Dependencies

---@type BaseDataModel
local InstitutionsData = require("Module:InstitutionsData")

local VIEW_TEMPLATE = "Service_link/view"

--endregion



--region Private constants

local VALID_SIZE = {
    ["none"] = true,
    ["small"] = true,
    ["medium"] = true,
    ["large"] = true,
    ["huge"] = true,
}

--endregion

--region Localization string constants

local ERROR_MESSAGE_INVALID_NAME = "You must specify a service. Please see the template documentation for how to use the parameters"
local ERROR_MESSAGE_INVALID_NAME = "The service you specified was not found. Please see the template documentation for how to use the parameters"

--endregion



--region Private methods

local function validateServiceNameByIcon(name)

    --Currently services have the same ID and name. Hopefully this doesn't change.
    local list = InstitutionsData:getIDsAndRecipesWhereProductID(name)

    --If the list is empty, then the name is not valid.
    if #list < 1 then
        error(ERROR_MESSAGE_INVALID_NAME)
    end

    return "Icon_Need_" .. name .. ".png"
end

--endregion



--region Public methods

---@public
---Extracts parameters, validates the service by getting its icon, and then sends the data to the view template for rendering.
---
---@param frame table the template's context, with arguments
---@return string wiki markup
function ServiceLink.main(frame)

    local name = frame.args.name
    local iconSize = frame.args.size
    local displayOverride = frame.args.display

    if not name or name == "" then
        error(ERROR_MESSAGE_INVALID_NAME)
    end

    --Validate the name
    validatedIcon = validateServiceNameByIcon(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"] = displayOverride,
    }

    return frame:expandTemplate{
        title = VIEW_TEMPLATE,
        args = viewParameters,
    }
end

--endregion

return ServiceLink