Module:ServiceLink: Difference between revisions

From Against the Storm Official Wiki
(Created to make linking to pages on services easier)
 
(Whole rewrite; externalized the view; now uses the updated data models)
 
(One intermediate revision by the same user not shown)
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.
    local name = frame.args.name
if not id or id == "" then
    local iconSize = frame.args.size
error("No service specified to which to make a link.")
    local displayOverride = frame.args.display
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.
    if not name or name == "" then
local argService = frame.args["service"]
        error(ERROR_MESSAGE_INVALID_NAME)
local argIconSize = frame.args["iconsize"]
    end


if not argService or argService == "" then
    --Validate the name
return "The Service link template requires the name of a service."
    validatedIcon = validateServiceNameByIcon(name)
end


local id = ServicesRecipesData.getAllRecipeIDsForServiceName(argService)
    --Handle default icon size
    if not iconSize or iconSize == "" or not VALID_SIZE[iconSize] then
        iconSize = "small"
    end


local iconFilename = ServicesRecipesData.getRecipeServiceIconByID(id)
    --The args to pass to the view.
if not iconFilename then
    viewParameters = {
return "No service found with that name: " .. argService .. "."
        ["name"] = name,
end
        ["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