Module:Card: Difference between revisions
Jump to navigation
Jump to search
Created page with "local p = {} local lib = require('Module:Feature') local ne = lib.isNotEmpty local link_label = require('Module:Link_Label')._main local namespace = require('Module:Namespace_detect')._main local getTemplateArgs = require('Module:Parser').getTemplateArgs local array = require('Module:Array') local allData = { {'Survivor', mw.loadData('Module:Card/Survivors')}, {'Weapon', mw.loadData('Module:Card/Weapons')}, {'Item', mw.loadData('Module:Card/Items')}, } local COLON =..." |
No edit summary |
||
| Line 43: | Line 43: | ||
local link = args.link or lib.ternary(item == 'Unknown', '', item) | local link = args.link or lib.ternary(item == 'Unknown', '', item) | ||
local link_suffix = args.link_suffix or '' | local link_suffix = args.link_suffix or '' | ||
if lib.isEmpty(suffix) then | if lib.isEmpty(suffix) then | ||
| Line 127: | Line 123: | ||
card_caption:css('width', caption_width) | card_caption:css('width', caption_width) | ||
end | end | ||
end | end | ||
Latest revision as of 13:02, 11 June 2025
This module implements {{Card}}.
local p = {}
local lib = require('Module:Feature')
local ne = lib.isNotEmpty
local link_label = require('Module:Link_Label')._main
local namespace = require('Module:Namespace_detect')._main
local getTemplateArgs = require('Module:Parser').getTemplateArgs
local array = require('Module:Array')
local allData = {
{'Survivor', mw.loadData('Module:Card/Survivors')},
{'Weapon', mw.loadData('Module:Card/Weapons')},
{'Item', mw.loadData('Module:Card/Items')},
}
local COLON = ':'
function p.main(frame)
local args = require('Module:Arguments').getArgs(frame, {
wrapper = { 'Template:Card' },
parentOnly = false
})
return p._main(args)
end
function p._main(args)
local item = args[1] or args.name or 'Unknown'
local label = args[2] or args.label or args.text or args.count or args.amount or args.x or nil
-- Reset specific data to prevent carry-over from previous cards
local notice = nil, nil
-- Caption Params:
local caption = args.caption or nil
local caption_width = args.caption_width or args.captionWidth or nil
-- Image File Params:
local type, data = p.getTypeAndData(item, args.type)
local suffix = args.suffix or ''
local imageSize = args.image_size or args.imageSize or lib.ternary(args.mini == '1', 48, 70)
local imageExt = args.image_ext or args.imageExt or 'png'
-- Link Params:
local link = args.link or lib.ternary(item == 'Unknown', '', item)
local link_suffix = args.link_suffix or ''
if lib.isEmpty(suffix) then
local lcType = type:lower()
if (lcType == 'agent') then
suffix = ' Icon'
--In Full-Size Card-Mode, if there is no specified text, Agents will default to using their full name displayed underneath.
if args.mini ~= "1" then
if (label == nil) then label = item end
end
-- In Mini-Size Card-Mode, if there is no specified text, Agents will use their "shortName" pulled from /data as the displayed text.
-- In cases where Agents do not have a "shortName", their full name will be displayed instead.
if args.mini == "1" then
if (label == nil) and data['shortName'] ~= nil then label = data['shortName']
elseif (label == nil) and data['shortName'] == nil then label = item
end
end
-- hackish workaround to account for the slight transparent borders on hoyolab agent icons
if (tonumber(imageSize) ~= nil) then
imageSize = tonumber(imageSize) + 2
end
else
suffix = ' Icon'
end
end
local final_file = type .. ' ' .. lib.sanitizeFileName(item) .. suffix .. '.' .. imageExt
local final_link = link .. link_suffix
local file = '[[File:' .. final_file .. '|' .. imageSize .. 'px|alt=' .. item .. '|link=' .. final_link .. ']]'
local result = mw.html.create()
local card_container = result:tag('div')
:addClass('card-container')
if args.mini == "1" then
card_container:addClass('mini-card')
end
local card_wrapper = card_container:tag('div')
:addClass('card-wrapper')
local card_body = card_wrapper:tag('div')
:addClass('card-body')
local card_link = card_wrapper:tag('div')
:addClass('card-link')
:wikitext('[[' .. final_link .. ']]')
local card_image = card_body:tag('div')
:addClass('card-image-container')
:wikitext(file)
--[[if label and not (args.label == "0" or args.text == "0") then --]]
if (label or args.show_text) and (args.label ~= "0" or args.text ~= "0") then
if args.show_label == "1" or args.show_text == "1" then
label = data and data.name or item
end
local card_label = card_container:tag('div')
:addClass('card-label')
:wikitext(label)
end
if (caption or args.show_caption) and args.show_caption ~= "0" then
if args.show_caption == "1" then
caption = (data and data.name) or item
end
local card_caption = card_container:tag('div')
:addClass('card-caption')
:wikitext('[[', final_link, '|', caption, ']]')
if caption_width == 'auto' or lib.isEmpty(caption_width) then
card_caption:addClass('auto-width')
else
card_caption:css('width', caption_width)
end
end
if args.mini ~= "1" then -- Only display the icons if mini is not set to "1"
local notice = args.notice or nil
if ne(notice) then
card_body:tag('span')
:addClass('card-notice-label')
:wikitext(notice)
end
end
return tostring(result)
end
function p.getTypeAndData(name, type)
local img_type = type or 'Item'
local img_data = nil
for i, v in ipairs(allData) do
-- Only if the type was either not provided in the card args or if the provided type from card args is equal
if lib.isEmpty(type) or v[1] == type then
local data = v[2][name]
if data then
img_type = v[1]
img_data = data
break
end
end
end
return img_type, img_data
end
return p