Difference between revisions of "Module:OInfobox-Building"

From Foundation - Wiki
Jump to navigation Jump to search
m
m
Tag: Undo
Line 43: Line 43:
         return box
         return box
     end
     end
 
   
     if header then
     if header then
         box:tag('div')
         box:tag('div')
Line 50: Line 50:
             :done()
             :done()
     end
     end
 
   
     local function createDataRow(label, value, defaultValue)
     local function createDataRow(label, value, defaultValue)
        if not value and not defaultValue then
    if not value and not defaultValue then
            return nil
        return nil
        end
 
        local dataRow = mw.html.create('div')
        dataRow:addClass('infobox-data-section')
 
        if label then
            dataRow:tag('span')
                :addClass('infobox-label')
                :wikitext(label)
                :done()
            :tag('span')
                :addClass('infobox-data')
                :wikitext(value or defaultValue)
        else
            dataRow:tag('span')
                :addClass('infobox-data')
                :wikitext(value or defaultValue)
        end
 
        return dataRow
     end
     end
 
   
     if columnCount and columnCount > 1 then
     local dataRow = mw.html.create('div')
        local row = box:tag('div'):addClass('infobox-row')
    dataRow:addClass('infobox-data-section')
        for i, item in ipairs(data) do
   
            local label = (header == 'Construction Requirements') and nil or item.label
    if label then
            local dataDiv = createDataRow(label, item.value, item.label) -- Pass icon as defaultValue if no label
        dataRow:tag('span')
            if dataDiv then
            :addClass('infobox-label')
                row:tag('div'):addClass('infobox-data-item'):node(dataDiv):done()
            :wikitext(label)
                if i % columnCount == 0 and i < #data then
            :done()
                    row = box:tag('div'):addClass('infobox-row'):done()
        :tag('span')
                end
            :addClass('infobox-data')
             end
             :wikitext(value or defaultValue)
        end
        if row.parent == box then
            row:done()
        end
     else
     else
         for _, item in ipairs(data) do
         dataRow:tag('span')
             local label = (header == 'Construction Requirements') and nil or item.label
            :addClass('infobox-data')
            local dataRow = createDataRow(label, item.value, item.label) -- Pass icon as defaultValue if no label
             :wikitext(value or defaultValue)
            if dataRow then
    end
                box:node(dataRow)
   
            end
    return dataRow
end
   
    local rowClass = 'infobox-row'
    if columnCount then
        rowClass = rowClass .. ' data-rows-' .. columnCount
    end
   
    local rowContainer = mw.html.create('div')
    rowContainer:addClass(rowClass)
   
    for _, item in ipairs(data) do
        local dataRow = createDataRow(item.label, item.value, item.defaultValue)
        if dataRow then
            rowContainer:node(dataRow)
         end
         end
     end
     end
 
   
    box:node(rowContainer)
     return box
     return box
end
end
Line 225: Line 216:
             for resource, amount in pairs(buildingData.construction) do
             for resource, amount in pairs(buildingData.construction) do
                 local resourceIcon = i.icon(resource, amount, nil, com.firstUpper({resource}), 'en')
                 local resourceIcon = i.icon(resource, amount, nil, com.firstUpper({resource}), 'en')
                 table.insert(constructionData, { label = resourceIcon, value = amount })
                 table.insert(constructionData, { label = resourceIcon })
             end
             end



Revision as of 08:10, 19 April 2025

Documentation for this module may be created at Module:OInfobox-Building/doc

-- adapted from Levrin's iteration to Sakaratte's original --
local com = require('Module:Common')
local a = require('Module:Arguments')
local i = require('Module:Icon')
local p = {}

local function createInfobox(title)
    local box = mw.html.create('div')
    box:addClass('infobox')
        :tag('div')
        :addClass('infobox-title')
        :wikitext(com.modulePagename(title))
        :done()
    return box
end

local function addImage(box, image, caption, defaultImage, defaultCaption)
    if not image and not defaultImage then
        box:wikitext('[[Category:Image Needed]]')
        return box
    end
    
    local imageDiv = mw.html.create('div')
    imageDiv:addClass('infobox-image')
    
    local fileName = image or defaultImage
    local fileWiki = '[[File:' .. fileName .. '|260px|frameless]]'
    
    imageDiv:wikitext(fileWiki)
    
    if caption or defaultCaption then
        imageDiv:tag('span')
            :addClass('infoboxCaption')
            :wikitext(caption or defaultCaption)
    end
    
    box:node(imageDiv)
    return box
end

local function addSection(box, data, header, columnCount)
    if not data or #data == 0 then
        return box
    end
    
    if header then
        box:tag('div')
            :addClass('infobox-header')
            :wikitext(header)
            :done()
    end
    
    local function createDataRow(label, value, defaultValue)
    if not value and not defaultValue then
        return nil
    end
    
    local dataRow = mw.html.create('div')
    dataRow:addClass('infobox-data-section')
    
    if label then
        dataRow:tag('span')
            :addClass('infobox-label')
            :wikitext(label)
            :done()
        :tag('span')
            :addClass('infobox-data')
            :wikitext(value or defaultValue)
    else
        dataRow:tag('span')
            :addClass('infobox-data')
            :wikitext(value or defaultValue)
    end
    
    return dataRow
end
    
    local rowClass = 'infobox-row'
    if columnCount then
        rowClass = rowClass .. ' data-rows-' .. columnCount
    end
    
    local rowContainer = mw.html.create('div')
    rowContainer:addClass(rowClass)
    
    for _, item in ipairs(data) do
        local dataRow = createDataRow(item.label, item.value, item.defaultValue)
        if dataRow then
            rowContainer:node(dataRow)
        end
    end
    
    box:node(rowContainer)
    return box
end

--[[
I left next section, that I commented "Start" and "End," in here in case you all needed or wanted it (blame Pelendur if it's bad.....just kidding (maybe) :p)
It seemed to be in other templates, and I didn't want to remove it if it was needed or useful to you all. ]]--

-- Start
local function createIconRow(iconName, value) 
    if not value then
        return nil
    end
    
    local label = i.icon(iconName, nil, nil, com.firstUpper({iconName}), 'en')
    return createDataRow(label, value)
end
-- End
--INFOBOX-STRUCTURE
function p.building(frame)
    local args = a.getArgs(frame)
    
    local titleArg = args['title']
    local currentTitle = mw.title.getCurrentTitle().text
    local building = com.modulePagename(titleArg or currentTitle)
    
    local buildingData = require('Module:BuildingIndex')[string.lower(building)]
    
    local infobox = createInfobox(building)
        
    addImage(infobox, args['image'], args['caption'], buildingData and buildingData.defaultImage, buildingData and buildingData.defaultCaption)
    
    if buildingData then
		--SECTION-PRODUCTION
        local productionData = {}
        if buildingData.employees then
            for type, value in pairs(buildingData.employees) do
                table.insert(productionData, {label = com.firstUpper({type}), value = value})
            end
        end

        if buildingData.workers then
            local workers = buildingData.workers
            if buildingData.rank then
                workers = workers .. ' ' .. buildingData.rank
            end
            table.insert(productionData, {label = 'Workers', value = workers.firstUpper})
        end
        --
        if buildingData.input then
            local input = ''
            for k, output in ipairs(buildingData.input) do
                input = input .. i.icon(output, nil, nil, com.firstUpper({output}), 'en')
                if k < #buildingData.input then
                    input = input .. '<br />'
                end
            end
            
            if com.exists(input) then
                table.insert(productionData, {label = 'Consumes', value = input})
            end
        end
        
        if buildingData.output then
            local output = ''
            for k, product in ipairs(buildingData.output) do
                output = output .. i.icon(product, nil, nil, com.firstUpper({product}), 'en')
                if k < #buildingData.output then
                    output = output .. '<br />'
                end
            end
            
            if com.exists(output) then
                table.insert(productionData, {label = 'Produces', value = output})
            end
        end
        
        if #productionData > 0 then
            addSection(infobox, productionData, 'Production', 2) -- The 2 means 2 columns, it can add more or less
        end
        --/
		--SECTION-REREQUISITES
        local prereqData = {}
        
        if buildingData.building then
            table.insert(prereqData, {label = 'Building', value = buildingData.building})
        end
        
        if buildingData.population then
            table.insert(prereqData, {label = 'Population', value = buildingData.population})
        end
        
        if buildingData.influence then
            for type, value in pairs(buildingData.influence) do
                table.insert(prereqData, {label = type .. ' Influence', value = value})
            end
        end
        
        if buildingData.splendor then
            for type, value in pairs(buildingData.splendor) do
                table.insert(prereqData, {label = type .. ' Splendor', value = value})
            end
        end
		--/
        --SECTION-PROSPERITY
        if #prereqData > 0 then
            addSection(infobox, prereqData, 'Prerequisites')
        end
        
        if buildingData.prosperity then
            local prosperityData = {}
            for type, value in pairs(buildingData.prosperity) do
                table.insert(prosperityData, {label = com.firstUpper({type}), value = value})
            end
            
            if #prosperityData > 0 then
                addSection(infobox, prosperityData, 'Prosperity', 2)
            end
        end
        --/
        ---SECTION-CONSTRUCTION
        if buildingData.construction then
            local constructionData = {}
            for resource, amount in pairs(buildingData.construction) do
                local resourceIcon = i.icon(resource, amount, nil, com.firstUpper({resource}), 'en')
                table.insert(constructionData, { label = resourceIcon })
            end

            if #constructionData > 0 then
                addSection(infobox, constructionData, 'Construction Requirements', 2)
            end
        end
        --/
        --SECTION-RUNNING-COSTS
        local costData = {}
        
        if buildingData.maintenance then
            local maintenanceLabel = i.icon('maintenance cost', nil, nil, 'Maintenance Cost', 'en')
            table.insert(costData, {label = maintenanceLabel, value = buildingData.maintenance})
        end
        
        if buildingData.tax then
            local taxLabel = i.icon('coin', nil, nil, 'Tax', 'en')
            table.insert(costData, {label = taxLabel, value = buildingData.tax})
        end
        
        if #costData > 0 then
            addSection(infobox, costData, 'Running Costs')
        end
		--/
    end
    --/
    return infobox
end

return p