[dismiss]
The wiki is currently a work in progress. If you'd like to help out, please check the Community Portal and our getting started guide. Also, check out our sister project on poewiki.net.
Module:Item link: Difference between revisions
Jump to navigation
Jump to search
(Better handling for maps, which nearly always return multiple results when queried by name. Now uses the one from the most recent map series, regardless of whether it's drop enabled. This prevents item links from breaking when maps are removed from the Atlas.) |
No edit summary |
||
(15 intermediate revisions by the same user not shown) | |||
Line 4: | Line 4: | ||
-- | -- | ||
-- This module implements Template:Item link. | -- This module implements Template:Item link. | ||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
require('Module:No globals') | |||
local m_util = require('Module:Util') | local m_util = require('Module:Util') | ||
-- Should we use the sandbox version of our submodules? | -- Should we use the sandbox version of our submodules? | ||
local use_sandbox = m_util.misc.maybe_sandbox('Item link') | local use_sandbox = m_util.misc.maybe_sandbox('Item link') | ||
local m_item_util = use_sandbox and require('Module:Item util/sandbox') or require('Module:Item util') | |||
-- The cfg table contains all localisable strings and configuration, to make it | -- The cfg table contains all localisable strings and configuration, to make it | ||
Line 27: | Line 21: | ||
-- ---------------------------------------------------------------------------- | -- ---------------------------------------------------------------------------- | ||
-- | -- Main functions | ||
-- ---------------------------------------------------------------------------- | -- ---------------------------------------------------------------------------- | ||
local | local function _main(args) | ||
function | |||
--[[ | --[[ | ||
Creates a link to the item and displays the item info box on hover | Creates a link to the item and displays the item info box on hover | ||
Line 44: | Line 33: | ||
= p.item_link{'Multistrike'} | = p.item_link{'Multistrike'} | ||
= p.item_link{'Multistrike Support'} | = p.item_link{'Multistrike Support'} | ||
--]] | |||
args.item_name = args.item_name or args[1] | |||
args.name = args.name or args[2] | |||
args.large = m_util.cast.boolean(args.large) | |||
local img | local img | ||
local result | local result | ||
local linked_page | |||
if m_util. | if m_util.cast.boolean(args.skip_query) then | ||
result = { | |||
_pageName = args.page or args.name | |||
} | } | ||
local | linked_page = args.link or result._pageName | ||
else | |||
local qargs = {} | |||
qargs.tables = {'main_pages'} | |||
qargs.join = 'items._pageName = main_pages.data_page' | |||
qargs.fields = { | |||
'items.name=name', | |||
'items.inventory_icon=inventory_icon', | |||
'items.html=html', | |||
'items.alternate_art_inventory_icons=alternate_art_inventory_icons', | |||
'items.size_x=size_x', | |||
'items.size_y=size_y', | |||
'main_pages._pageName=main_page', | |||
} | } | ||
result = m_item_util.query_item(args, qargs) | |||
if result.error then | |||
if not m_util.cast.boolean(args.nocat) then | |||
return result.error:get_html() .. m_util.misc.add_category({i18n.categories.broken_item_links}) | |||
if | |||
end | end | ||
return result.error:get_html() | |||
end | end | ||
if | -- If specifying an item by name, link to the main page; otherwise, link to where the item data lives. | ||
if result.main_page and m_util.table.has_any_key(args, {'item_name', 'item_name_exact'}) then | |||
linked_page = result.main_page | |||
else | |||
linked_page = result._pageName | |||
end | end | ||
end | end | ||
-- Overrides from template parameters | |||
for k, prop in pairs(cfg.parameters) do | for k, prop in pairs(cfg.parameters) do | ||
if | if args[k] ~= nil then | ||
result[prop] = | result[prop] = args[k] | ||
end | end | ||
end | end | ||
if | if args.image ~= nil then | ||
if result | if result.alternate_art_inventory_icons == nil then | ||
local err = m_util.Error{ | |||
message = string.format( | |||
i18n.errors.alt_art_undefined, | i18n.errors.alt_art_undefined, | ||
result | result._pageName | ||
), | |||
code = 'alt_art_undefined', | |||
issue = args.issue_all_errors or false, | |||
category = i18n.categories.broken_item_links, | |||
}:throw() | |||
return err:get_html() .. err:get_category() | |||
end | end | ||
result | result.alternate_art_inventory_icons = m_util.string.split( | ||
result | result.alternate_art_inventory_icons, | ||
',%s*' | ',%s*' | ||
) | ) | ||
local index = tonumber( | local index = tonumber(args.image) | ||
if index ~= nil then | if index ~= nil then | ||
img = result | img = result.alternate_art_inventory_icons[index] | ||
else | else | ||
-- offset 1 is needed | -- offset 1 is needed | ||
local suffix = string.len(' inventory icon.png') + 1 | local suffix = string.len(' inventory icon.png') + 1 | ||
-- add an extra offset by 1 to account for the space | -- add an extra offset by 1 to account for the space | ||
local prefix = string.len(string.sub(result | local prefix = string.len(string.sub(result.inventory_icon, 1, -suffix)) + 2 | ||
for _, filename in ipairs(result | for _, filename in ipairs(result.alternate_art_inventory_icons) do | ||
if string.sub(filename, prefix, -suffix) == | if string.sub(filename, prefix, -suffix) == args.image then | ||
img = filename | img = filename | ||
break | break | ||
Line 219: | Line 120: | ||
if img == nil then | if img == nil then | ||
local err = m_util.Error{ | |||
message = string.format( | |||
i18n.errors.alt_art_invalid_index, | i18n.errors.alt_art_invalid_index, | ||
args.image, | |||
result._pageName | |||
), | |||
code = 'alt_art_invalid_index', | |||
issue = args.issue_all_errors or false, | |||
category = i18n.categories.broken_item_links, | |||
}:throw() | |||
return err:get_html() .. err:get_category() | |||
end | end | ||
elseif result | elseif result.inventory_icon ~= nil then | ||
img = result | img = result.inventory_icon | ||
end | end | ||
Line 235: | Line 139: | ||
-- output | -- output | ||
-- | -- | ||
local container = mw.html.create('span') | local container = mw.html.create('span') | ||
container:addClass('c-item-hoverbox') | container:addClass('hoverbox c-item-hoverbox') | ||
if | if args.large then | ||
container:addClass('c-item-hoverbox--large') | container:addClass('c-item-hoverbox--large') | ||
end | end | ||
local activator = mw.html.create('span') | local activator = mw.html.create('span') | ||
activator:addClass('c-item-hoverbox__activator') | activator:addClass('hoverbox__activator c-item-hoverbox__activator') | ||
if img and not | if img and not args.large then | ||
activator:wikitext(string.format('[[%s| | activator:wikitext( | ||
string.format( | |||
'[[%s|%sx%spx|link=|alt=]]', | |||
img, | |||
cfg.icon_size_inline, | |||
cfg.icon_size_inline | |||
) | |||
) | |||
end | end | ||
if #result | if #result.name > 0 then | ||
activator:wikitext(string.format( | activator:wikitext( | ||
string.format( | |||
'[[%s|%s]]', | |||
linked_page, | |||
result.name or result._pageName | |||
) | |||
) | |||
end | |||
local width = tonumber(result.size_x) or tonumber(args.width) or 1 | |||
local height = tonumber(result.size_y) or tonumber(args.height) or 1 | |||
if img and args.large then | |||
activator:wikitext( | |||
string.format( | |||
'[[%s|%sx%spx|link=%s|alt=]]', | |||
img, | |||
width * cfg.icon_size_large, | |||
height * cfg.icon_size_large, | |||
linked_page | |||
) | ) | ||
) | ) | ||
Line 269: | Line 186: | ||
local display = mw.html.create('span') | local display = mw.html.create('span') | ||
display: | display:addClass('hoverbox__display c-item-hoverbox__display') | ||
if result.html ~= nil then | |||
if result | display:wikitext(result.html) | ||
display:wikitext(result | |||
if img then | if img then | ||
display:wikitext( | display:wikitext( | ||
string.format( | |||
'[[%s|%sx%spx|link=|alt=|class=item-icon]]', | |||
img, | |||
width * cfg.icon_size_full, | |||
height * cfg.icon_size_full | |||
) | |||
) | ) | ||
end | end | ||
end | end | ||
Line 317: | Line 204: | ||
:node(activator) | :node(activator) | ||
:node(display) | :node(display) | ||
return tostring(container) | return tostring(container) | ||
end | end | ||
-- ---------------------------------------------------------------------------- | |||
-- Exported functions | |||
-- ---------------------------------------------------------------------------- | |||
local p = {} | |||
-- | |||
-- Template:Item link | |||
-- | |||
p.main = m_util.misc.invoker_factory(_main, { | |||
parentFirst = true, | |||
removeBlanks = false, | |||
}) | |||
p.item_link = p.main | |||
return p | return p |
Latest revision as of 15:32, 15 April 2025
This module implements {{item link}} and facilitates the creation of item links.
The above documentation is transcluded from Module:Item link/doc.
Editors can experiment in this module's sandbox and testcases pages.
Subpages of this module.
Editors can experiment in this module's sandbox and testcases pages.
Subpages of this module.
-------------------------------------------------------------------------------
--
-- Module:Item link
--
-- This module implements Template:Item link.
-------------------------------------------------------------------------------
require('Module:No globals')
local m_util = require('Module:Util')
-- Should we use the sandbox version of our submodules?
local use_sandbox = m_util.misc.maybe_sandbox('Item link')
local m_item_util = use_sandbox and require('Module:Item util/sandbox') or require('Module:Item util')
-- The cfg table contains all localisable strings and configuration, to make it
-- easier to port this module to another wiki.
local cfg = use_sandbox and mw.loadData('Module:Item link/config/sandbox') or mw.loadData('Module:Item link/config')
local i18n = cfg.i18n
-- ----------------------------------------------------------------------------
-- Main functions
-- ----------------------------------------------------------------------------
local function _main(args)
--[[
Creates a link to the item and displays the item info box on hover
on the link.
Examples
--------
= p.item_link{'Multistrike'}
= p.item_link{'Multistrike Support'}
--]]
args.item_name = args.item_name or args[1]
args.name = args.name or args[2]
args.large = m_util.cast.boolean(args.large)
local img
local result
local linked_page
if m_util.cast.boolean(args.skip_query) then
result = {
_pageName = args.page or args.name
}
linked_page = args.link or result._pageName
else
local qargs = {}
qargs.tables = {'main_pages'}
qargs.join = 'items._pageName = main_pages.data_page'
qargs.fields = {
'items.name=name',
'items.inventory_icon=inventory_icon',
'items.html=html',
'items.alternate_art_inventory_icons=alternate_art_inventory_icons',
'items.size_x=size_x',
'items.size_y=size_y',
'main_pages._pageName=main_page',
}
result = m_item_util.query_item(args, qargs)
if result.error then
if not m_util.cast.boolean(args.nocat) then
return result.error:get_html() .. m_util.misc.add_category({i18n.categories.broken_item_links})
end
return result.error:get_html()
end
-- If specifying an item by name, link to the main page; otherwise, link to where the item data lives.
if result.main_page and m_util.table.has_any_key(args, {'item_name', 'item_name_exact'}) then
linked_page = result.main_page
else
linked_page = result._pageName
end
end
-- Overrides from template parameters
for k, prop in pairs(cfg.parameters) do
if args[k] ~= nil then
result[prop] = args[k]
end
end
if args.image ~= nil then
if result.alternate_art_inventory_icons == nil then
local err = m_util.Error{
message = string.format(
i18n.errors.alt_art_undefined,
result._pageName
),
code = 'alt_art_undefined',
issue = args.issue_all_errors or false,
category = i18n.categories.broken_item_links,
}:throw()
return err:get_html() .. err:get_category()
end
result.alternate_art_inventory_icons = m_util.string.split(
result.alternate_art_inventory_icons,
',%s*'
)
local index = tonumber(args.image)
if index ~= nil then
img = result.alternate_art_inventory_icons[index]
else
-- offset 1 is needed
local suffix = string.len(' inventory icon.png') + 1
-- add an extra offset by 1 to account for the space
local prefix = string.len(string.sub(result.inventory_icon, 1, -suffix)) + 2
for _, filename in ipairs(result.alternate_art_inventory_icons) do
if string.sub(filename, prefix, -suffix) == args.image then
img = filename
break
end
end
end
if img == nil then
local err = m_util.Error{
message = string.format(
i18n.errors.alt_art_invalid_index,
args.image,
result._pageName
),
code = 'alt_art_invalid_index',
issue = args.issue_all_errors or false,
category = i18n.categories.broken_item_links,
}:throw()
return err:get_html() .. err:get_category()
end
elseif result.inventory_icon ~= nil then
img = result.inventory_icon
end
--
-- output
--
local container = mw.html.create('span')
container:addClass('hoverbox c-item-hoverbox')
if args.large then
container:addClass('c-item-hoverbox--large')
end
local activator = mw.html.create('span')
activator:addClass('hoverbox__activator c-item-hoverbox__activator')
if img and not args.large then
activator:wikitext(
string.format(
'[[%s|%sx%spx|link=|alt=]]',
img,
cfg.icon_size_inline,
cfg.icon_size_inline
)
)
end
if #result.name > 0 then
activator:wikitext(
string.format(
'[[%s|%s]]',
linked_page,
result.name or result._pageName
)
)
end
local width = tonumber(result.size_x) or tonumber(args.width) or 1
local height = tonumber(result.size_y) or tonumber(args.height) or 1
if img and args.large then
activator:wikitext(
string.format(
'[[%s|%sx%spx|link=%s|alt=]]',
img,
width * cfg.icon_size_large,
height * cfg.icon_size_large,
linked_page
)
)
end
local display = mw.html.create('span')
display:addClass('hoverbox__display c-item-hoverbox__display')
if result.html ~= nil then
display:wikitext(result.html)
if img then
display:wikitext(
string.format(
'[[%s|%sx%spx|link=|alt=|class=item-icon]]',
img,
width * cfg.icon_size_full,
height * cfg.icon_size_full
)
)
end
end
container
:node(activator)
:node(display)
return tostring(container)
end
-- ----------------------------------------------------------------------------
-- Exported functions
-- ----------------------------------------------------------------------------
local p = {}
--
-- Template:Item link
--
p.main = m_util.misc.invoker_factory(_main, {
parentFirst = true,
removeBlanks = false,
})
p.item_link = p.main
return p