Module:Row

From JoJo's Bizarre Encyclopedia - JoJo Wiki
Jump to navigation Jump to search

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

-- Module definition
local p = {}

-- Function to generate the row
function p.Row(frame)
    local args = frame:getParent().args
    local numColumns = tonumber(args.columns) or 5 -- Maximum number of columns

    -- Determine base class
    local baseClass = "jjbe-row"
    if args.bg ~= "none" then
        baseClass = baseClass .. " volume"
    end
    if args.diamond == "true" then
        baseClass = baseClass .. " diamond"
    end
    if args.class then
        baseClass = baseClass .. " " .. args.class
    end

    -- Table attributes
    local style = "display: table; width: " .. (args.width or "100%") .. "; height: " .. (args.height or "100%") .. ";"
    style = style .. "font-size: " .. (args.font or "13px") .. ";"
    style = style .. (args.align == "center" and "margin: auto;" or "") .. (args.align == "right" and "float: right;" or "")
    style = style .. "background-color: " .. (args.bg or "white") .. ";"
    style = style .. "text-align: " .. (args.textAlign or "center") .. ";"
    if args.color then
        style = style .. "color: " .. args.color .. ";"
    end

    -- Add id if provided
    local id = args.id and (' id="' .. args.id .. '"') or ""

    local langAttr = args.lang and (' lang="' .. args.lang .. '"') or ""

    local result = '<div class="' .. baseClass .. '"' .. id .. langAttr .. ' style="' .. style .. '">'
    local columnCount = 0

    for i = 1, numColumns do
        if args[i] and args[i] ~= "" then
            columnCount = columnCount + 1
        end
    end

    -- Generate columns
    for i = 1, numColumns do
        if args[i] and args[i] ~= "" then -- Check if column content exists
            local columnClass = "rowTemplate" .. (i == 1 and args.image == "true" and " rowheadimg" or "")
            columnClass = columnClass .. (args["class" .. i] and (" " .. args["class" .. i]) or "")
            local customClass = args["c" .. i] and (" " .. args["c" .. i]) or ""

            -- Determine the width for this column
            local width = args["w" .. i] -- Specific width for this column if provided
            if not width then
                -- If specific width is not provided, calculate default based on the number of columns
                width = (100 / columnCount) .. "%"
            elseif not width:match("%D") then
                -- If width is numeric without a unit, assume it’s in pixels
                width = width .. "px"
            end

            -- Font per column
            local fontSize = args["font" .. i] or args.font or "13px"

            -- Lang per column
            local langColumnAttr = args["lang" .. i] and (' lang="' .. args["lang" .. i] .. '"') or ""

            local columnStyle = "display: table-cell; padding: 3px;"
            columnStyle = columnStyle .. "width: " .. width .. ";"
            columnStyle = columnStyle .. "text-align: " .. (args["textAlign" .. i] or args.textAlign or "center") .. ";"
            columnStyle = columnStyle .. "background-color: " .. (args["bg" .. i] or args.bg or "transparent") .. ";"
            columnStyle = columnStyle .. "font-size: " .. fontSize .. ";"
            if (args["color" .. i] or args.color) then
                columnStyle = columnStyle .. "color: " .. (args["color" .. i] or args.color or "initial") .. ";"
            end
            columnStyle = columnStyle .. "border: " .. (args.borderSize or "1px") .. " solid " .. (args.border or "#aaaaaa") .. ";"
            columnStyle = columnStyle .. "vertical-align: middle;"

            result =
                result ..
                '<div class="' ..
                    columnClass .. (args.diamond == "true" and " diamond" or "") ..
                        '" style="' .. columnStyle .. '"' .. langColumnAttr ..
                        '>' .. '<div class="' .. customClass .. ' rowColumn">' .. args[i] .. "</div></div>"
        end
    end

    result = result .. "</div>"
    return result
end

return p