Module:Infobox/Widget/Header

From TwogPedia

Documentation for this module may be created at Module:Infobox/Widget/Header/doc

local LocationWidget = require('Module:Infobox/Widget/Location')
local RowWidget = require('Module:Infobox/Widget/Row')
local Header = {}

local function formatDate(dateStr)
    local year, month, day = dateStr:match("(%d+)%-(%d+)%-(%d+)")
    if year and month and day then
        return day .. "." .. month .. "." .. year
    else
        return dateStr
    end
end

function Header.make(title, logoLight, logoDark, logoAll, nationality, romanized, team, country, city, startDate, endDate, roster, region)
    local container = mw.html.create('div'):addClass('info-image')
    local imageURL = logoAll or logoLight or logoDark or 'Team placeholder light.png'
    local imageDiv = mw.html.create('div')
        :addClass('ib-image')
        :addClass('relative')
        :wikitext('[[File:' .. imageURL .. '|250px]]')
    container:node(imageDiv)
    
    local textOverlayDiv = mw.html.create('div'):addClass('text-overlay')
    local titleDiv = mw.html.create('div'):addClass('ib-title'):wikitext(title)
    textOverlayDiv:node(titleDiv)
    
    local infoRowDiv = mw.html.create('div'):addClass('info-row')
	
	if roster then
	    local currentTitleSplit = mw.text.split(mw.title.getCurrentTitle().text, '/')
	    local rosterContent = LocationWidget.make(roster, nil, currentTitleSplit[1] .. '/Teams')
	    
	    local rosterDiv = mw.html.create('div')
	        :addClass('roster')
	        :wikitext('Roster: ' .. tostring(rosterContent))
	    infoRowDiv:node(rosterDiv)
	end

	if region then
	    local currentTitleSplit = mw.text.split(mw.title.getCurrentTitle().text, '/')
	    local regionContent = LocationWidget.make(region, nil, currentTitleSplit[1] .. '/Teams')
	    
	    local regionDiv = mw.html.create('div')
	        :addClass('region')
	        :wikitext('Region: ' .. tostring(regionContent))
	    infoRowDiv:node(regionDiv)
	end

    if nationality then
        local category = team and mw.text.split(team, '/')[1] .. '/Players' or 'Players'
        local nationalityText = tostring(LocationWidget.make(nationality, nil, category))
        local nationalityDiv = mw.html.create('div'):addClass('nationality'):wikitext(nationalityText)
        infoRowDiv:node(nationalityDiv)
    end
  
	if country then
	    local currentTitleSplit = mw.text.split(mw.title.getCurrentTitle().text, '/')
	    local gameName = currentTitleSplit[1] or ""
	    gameName = gameName:gsub("Dota2", "Dota 2")
	    local locationContent = LocationWidget.make(country, city, currentTitleSplit[1] .. '/Tournaments')
	    
	    local rowDiv = mw.html.create('div')
	        :addClass('ib-row')
	        :css('display', 'flex')
	        :css('justify-content', 'space-between')
	        :css('align-items', 'center')
	        :css('width', '100%')
	        :css('flex-wrap', 'nowrap')
	    
	    local leftGroup = mw.html.create('div')
	        :css('display', 'flex')
	        :css('align-items', 'center')
	        :css('white-space', 'nowrap')
	    
	    local locationSpan = mw.html.create('span')
	        :css('color', '#C1C1CD')
	        :css('white-space', 'nowrap')
	        :node(locationContent)
	    leftGroup:node(locationSpan)
	    
	    if gameName:lower() == "dota 2" then
	        local gameIcon = mw.html.create('span')
	            :css('margin-left', '8px')
	            :wikitext('[[File:Mini-icon-dota2.png|18px]]')
	        leftGroup:node(gameIcon)
	        local gameNameSpan = mw.html.create('span')
	            :css('color', 'red')
	            -- :css('font-size', '15px')
	            :css('margin-left', '4px')
	            :css('white-space', 'nowrap')
	            :wikitext(gameName)
	        leftGroup:node(gameNameSpan)
	    end
	    
	    rowDiv:node(leftGroup)
	    
	    local rightGroup = mw.html.create('div')
	        :css('display', 'flex')
	        :css('align-items', 'center')
	        :css('white-space', 'nowrap')
	    
	    if startDate then
	        local formattedStart = formatDate(startDate)
	        local formattedEnd = endDate and formatDate(endDate) or ""
	        local dateText = formattedStart
	        if formattedEnd ~= "" then
	            dateText = dateText .. " - " .. formattedEnd
	        end
	        local dateSpan = mw.html.create('span')
	            :css('color', '#C1C1CD')
	            -- :css('font-size', '15px')
	            :css('white-space', 'nowrap')
	            :wikitext(dateText)
	        rightGroup:node(dateSpan)
	    end
	    
	    rowDiv:node(rightGroup)
	    infoRowDiv:node(rowDiv)
	end

    if romanized then
        local romanizedDiv = mw.html.create('div'):addClass('romanized-name'):wikitext(romanized)
        infoRowDiv:node(romanizedDiv)
    end
    
    textOverlayDiv:node(infoRowDiv)
    container:node(textOverlayDiv)
    
    if logoDark and not logoAll then
        imageDiv:addClass('light')
        local imageDivDark = mw.html.create('div')
            :addClass('ib-image dark')
            :wikitext('[[File:' .. logoDark .. '|250px]]')
        container:node(imageDivDark)
    end
    
    return container
end

return Header