Jump to content

Module:ElementAchievementHeader: Difference between revisions

// via Wikitext Extension for VSCode
 
// via Wikitext Extension for VSCode
 
(17 intermediate revisions by the same user not shown)
Line 1: Line 1:
--[[  
--[[
* Module:ElementAchievementHeader
* Name: ElementAchievementHeader
* Renders the achievement header for person templates
* Author: Mark W. Datysgeld
* Displays title achievements with special styling
* Description: Element module that renders achievement headers for Person templates and handles all title-type achievements
*
* Notes: Creates blocks for Blueprint framework; loads achievement data using AchievementSystem; filters for title-type achievements; renders with animated backgrounds using two-layer approach for readability; includes phantom row when no achievements found; protected execution with error handling
* Integrates with Blueprint template system.
]]
]]


local p = {}
local p = {}
Line 25: Line 24:
         -- Protected execution wrapper
         -- Protected execution wrapper
         local function execute()
         local function execute()
            local frame = mw.getCurrentFrame()
           
             -- Get the current page ID
             -- Get the current page ID
             local pageId = mw.title.getCurrentTitle().id
             local pageId = mw.title.getCurrentTitle().id
             if not pageId then
             if not pageId then
                 return "" -- No page ID, can't get achievements
                -- Include debug comment when no page ID available
                 return "<!-- Achievement Header: No page ID available -->"
             end
             end
              
              
             -- Get title achievement data
             -- CRITICAL: WE MUST PRE-LOAD THE JSON DATA WITH THE FRAME BEFORE GETTING ANY ACHIEVEMENTS
             local achievementId, displayName, achievementType = Achievements.getTitleAchievement(pageId)
             pcall(function()
                Achievements.loadData(frame)
                Achievements.loadTypes(frame)
            end)
              
              
             -- If no title achievement found, return empty string
             -- Now get the title achievement
             if not achievementId or achievementId == "" then
             local titleAchievement = Achievements.getTitleAchievement(pageId, frame)
                return ""
            end
              
              
             -- Create the achievement header HTML
             -- Minimal debug comment only visible in source
             local headerClass = template.config.achievementHeader
             local debugInfo = string.format(
                 and template.config.achievementHeader.headerClass
                 "<!-- Achievement Header: pageId=%s -->",
                 or p.defaultConfig.headerClass
                 pageId or "nil"
            )
              
              
             -- Build header HTML that aligns with existing CSS in both Templates.css and Achievements.css
             -- Always create a row for debugging purposes
             -- The classes headerClass and achievementId (e.g., "achievement-header title-developer")  
             if titleAchievement then
            -- need to be on the TD element itself, as the pseudo-element animations are attached there
                -- Get category link from the centralized function
            -- The foreground layer is just for the text to ensure it stays on top of the animations
                local categoryLink = Achievements.getCategoryLinks({ { type = titleAchievement.id } }, frame)
            return string.format(
 
                '|-\n| colspan="2" class="%s %s" data-achievement-id="%s" data-achievement-name="%s" | ' ..
                -- Achievement found: create populated row with achievement data
                '<div class="achievement-foreground-layer">%s</div>',
                return string.format(
                headerClass, achievementId, achievementId, displayName, displayName
                    '|-\n! colspan="2" class="achievement-header %s" data-achievement-id="%s" data-achievement-name="%s" | ' ..
             )
                    '<div class="achievement-foreground-layer">%s</div>%s%s',
                    titleAchievement.id, titleAchievement.id, titleAchievement.name, titleAchievement.name, debugInfo, categoryLink
                )
             else
                -- No achievement found: create empty phantom row
                return string.format(
                    '|-\n! colspan="2" class="achievement-header-phantom" | ' ..
                    '<div class="achievement-foreground-layer"></div>%s',
                    debugInfo
                )
            end
         end
         end
          
          
Line 65: Line 79:
         else
         else
             local ok, result = pcall(execute)
             local ok, result = pcall(execute)
             return ok and result or ""
             return ok and result or "<!-- Achievement Header: Protected execution failed -->"
         end
         end
     end
     end

Latest revision as of 02:58, 25 August 2025

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

--[[
* Name: ElementAchievementHeader
* Author: Mark W. Datysgeld
* Description: Element module that renders achievement headers for Person templates and handles all title-type achievements
* Notes: Creates blocks for Blueprint framework; loads achievement data using AchievementSystem; filters for title-type achievements; renders with animated backgrounds using two-layer approach for readability; includes phantom row when no achievements found; protected execution with error handling
]]

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()
            local frame = mw.getCurrentFrame()
            
            -- Get the current page ID
            local pageId = mw.title.getCurrentTitle().id
            if not pageId then
                -- Include debug comment when no page ID available
                return "<!-- Achievement Header: No page ID available -->"
            end
            
            -- CRITICAL: WE MUST PRE-LOAD THE JSON DATA WITH THE FRAME BEFORE GETTING ANY ACHIEVEMENTS
            pcall(function()
                Achievements.loadData(frame)
                Achievements.loadTypes(frame)
            end)
            
            -- Now get the title achievement
            local titleAchievement = Achievements.getTitleAchievement(pageId, frame)
            
            -- Minimal debug comment only visible in source
            local debugInfo = string.format(
                "<!-- Achievement Header: pageId=%s -->",
                pageId or "nil"
            )
            
            -- Always create a row for debugging purposes
            if titleAchievement then
                -- Get category link from the centralized function
                local categoryLink = Achievements.getCategoryLinks({ { type = titleAchievement.id } }, frame)

                -- Achievement found: create populated row with achievement data
                return string.format(
                    '|-\n! colspan="2" class="achievement-header %s" data-achievement-id="%s" data-achievement-name="%s" | ' ..
                    '<div class="achievement-foreground-layer">%s</div>%s%s',
                    titleAchievement.id, titleAchievement.id, titleAchievement.name, titleAchievement.name, debugInfo, categoryLink
                )
            else
                -- No achievement found: create empty phantom row
                return string.format(
                    '|-\n! colspan="2" class="achievement-header-phantom" | ' ..
                    '<div class="achievement-foreground-layer"></div>%s',
                    debugInfo
                )
            end
        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 "<!-- Achievement Header: Protected execution failed -->"
        end
    end
end

return p