Module:ElementAchievementHeader

Revision as of 00:31, 7 May 2025 by MarkWD (talk | contribs) (// via Wikitext Extension for VSCode)

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

--[[ 
 * Module:ElementAchievementHeader
 * Renders the achievement header for person templates
 * Displays title achievements with special styling
 * 
 * Integrates with Blueprint template system.
 ]]

local p = {}

p.elementName = "achievementHeader"

-- Load required modules
local ErrorHandling = require('Module:ErrorHandling')
local Achievements = require('Module:AchievementSystem')

-- Default configuration
p.defaultConfig = {
    headerClass = "achievement-header"
}

-- Create block function
function p.createBlock()
    return function(template, args)
        -- Protected execution wrapper
        local function execute()
            -- Get the current page ID
            local pageId = mw.title.getCurrentTitle().id
            if not pageId then
                return "" -- No page ID, can't get achievements
            end
            
-- Get title achievement data
local achievementId, displayName, achievementType = Achievements.getTitleAchievement(pageId, mw.getCurrentFrame())
            
            -- If no title achievement found, return empty string
            if not achievementId or achievementId == "" then
                return ""
            end
            
            -- Create the achievement header HTML
            local headerClass = template.config.achievementHeader 
                and template.config.achievementHeader.headerClass
                or p.defaultConfig.headerClass
            
            -- Build header HTML properly aligned with CSS expectations in Achievements.css
            -- Important: The CSS classes must be directly on the table cell element
            -- The CSS animations are attached to these elements via pseudo-elements
            return string.format(
                '|-\n! colspan="2" class="%s %s" data-achievement-id="%s" data-achievement-name="%s" | %s',
                headerClass, achievementId, achievementId, displayName, displayName
            )
        end
        
        -- Wrap with error handling
        if template._errorContext then
            return ErrorHandling.protect(
                template._errorContext,
                "ElementBlock_achievementHeader",
                execute,
                ""
            )
        else
            local ok, result = pcall(execute)
            return ok and result or ""
        end
    end
end

return p