Module:HeroTabsGive feedback
Jump to navigation
Jump to search
Documentation for this module may be created at Module:HeroTabs/doc
local p = {}
local lang_module = require('Module:Lang')
local dictionary_module = require('Module:Dictionary')
-- Tab bar definition, in display order.
-- subpage : subpage name appended to the base page ('' = the base page itself)
-- key : translation key
-- source : 'dictionary' (via Module:Dictionary) or 'lang' (via Module:Lang, Valve strings)
local TABS = {
{ subpage = '', key = 'Overview', source = 'dictionary' },
{ subpage = 'Update_history', key = 'Update history', source = 'dictionary' },
{ subpage = 'Strategy', key = 'Strategy', source = 'dictionary' },
{ subpage = 'Movement', key = 'Citadel_ShopFilter_Movement', source = 'lang' },
{ subpage = 'Lore', key = 'Lore', source = 'dictionary' },
{ subpage = 'Voice_lines', key = 'Voice lines', source = 'dictionary' },
{ subpage = 'Sounds', key = 'Sounds', source = 'dictionary' },
{ subpage = 'Music', key = 'Music', source = 'dictionary' },
}
local function lang_suffix()
local code = lang_module.get_lang_code()
if code == 'en' then
return ''
end
return '/' .. code
end
local function get_label(tab)
if tab.source == 'lang' then
return lang_module.get_string(tab.key)
end
return dictionary_module.translate(tab.key)
end
-- Returns the title object of the first page that exists, or nil.
-- Checks the localized page first, then falls back to the unlocalized one.
local function resolve_target(base, subpage, suffix)
local path = base
if subpage ~= '' then
path = path .. '/' .. subpage
end
if suffix ~= '' then
local localized = mw.title.new(path .. suffix)
if localized and localized.exists then
return localized
end
end
local plain = mw.title.new(path)
if plain and plain.exists then
return plain
end
return nil
end
function p.render(frame)
local args = frame.args
local current = mw.title.getCurrentTitle()
local base = args[1]
if not base or base == '' then
base = current.rootText
end
local suffix = lang_suffix()
local root = mw.html.create('div')
:addClass('hero-tabs')
for _, tab in ipairs(TABS) do
local target = resolve_target(base, tab.subpage, suffix)
if target then
local is_active = (target.fullText == current.fullText)
root:tag('div')
:addClass('hero-tab')
:addClass(is_active and 'hero-tab-active' or 'hero-tab-inactive')
:wikitext(string.format('[[%s|<span>%s</span>]]', target.fullText, get_label(tab)))
end
end
return frame:extensionTag{
name = 'templatestyles',
args = { src = 'HeroTabs/styles.css' }
} .. tostring(root)
end
return p