Module:Sandbox/LVL: Difference between revisions

Jump to navigation Jump to search
LVL (talk | contribs)
No edit summary
LVL (talk | contribs)
No edit summary
Line 447: Line 447:
if (spirit_power == nil or spirit_power == '') then spirit_power = 0 end
if (spirit_power == nil or spirit_power == '') then spirit_power = 0 end
-- Allow overriding sticky background colors, trim whitespace for robustness
-- Helper function to validate hex color code
local sticky_header_bg_arg = args['sticky-header-bg']
local function validate_color(color_str, default_color, param_name)
if sticky_header_bg_arg then
if color_str and color_str ~= '' then
sticky_header_bg_arg = mw.text.trim(sticky_header_bg_arg)
-- Pattern for a 6-digit or 3-digit hex code
end
if color_str:match("^#([0-9a-fA-F]{3})$") or color_str:match("^#([0-9a-fA-F]{6})$") then
local sticky_cell_bg_arg = args['sticky-cell-bg']
return color_str
if sticky_cell_bg_arg then
else
sticky_cell_bg_arg = mw.text.trim(sticky_cell_bg_arg)
-- Return an error for editors
return '<strong class="error">Error: Invalid color format for ' .. param_name .. '. Please use a 3 or 6-digit hex code like #333 or #333333.</strong>'
end
end
return default_color
end
end
local sticky_header_bg = (sticky_header_bg_arg and sticky_header_bg_arg ~= '') and sticky_header_bg_arg or '#333333'
-- Allow overriding sticky background colors, trim whitespace and validate
local sticky_cell_bg = (sticky_cell_bg_arg and sticky_cell_bg_arg ~= '') and sticky_cell_bg_arg or '#202122'
local sticky_header_bg = validate_color(mw.text.trim(args['sticky-header-bg'] or ''), '#333333', 'sticky-header-bg')
local sticky_cell_bg = validate_color(mw.text.trim(args['sticky-cell-bg'] or ''), '#202122', 'sticky-cell-bg')
-- Check if validation returned an error message
if string.find(sticky_header_bg, "Error:") then return sticky_header_bg end
if string.find(sticky_cell_bg, "Error:") then return sticky_cell_bg end
-- Show scaling icons if power/increases/spirit power are 0/not specified
-- Show scaling icons if power/increases/spirit power are 0/not specified
local display_scaling_icons = false
local display_scaling_icons = false
Line 465: Line 474:
display_scaling_icons = true
display_scaling_icons = true
end
end
-- Initializations and declarations
local body_rows = {}
local header_cells = {}
-- Add hero comp stats to  
-- Add hero comp stats to  
Line 475: Line 488:
}
}
}
}
-- Use mw.html builder to construct the table safely
local table = mw.html.create('table')
:addClass('wikitable sortable')
:cssText('table-layout: auto; width: 100%;')
-- Build Header
-- Build Header Cells
local thead = table:tag('thead')
table.insert(header_cells, '<th style="position: sticky; top: 0; left: 0; z-index: 2; background-color: ' .. sticky_header_bg .. ';">Hero</th>')
local header_row = thead:tag('tr')
-- Top-left corner cell
header_row:tag('th')
:cssText('position: sticky; top: 0; left: 0; z-index: 2; background-color: ' .. sticky_header_bg .. ';')
:wikitext('Hero')
local category_data = attribute_module.get_category_data()
local category_data = attribute_module.get_category_data()
local postfix_key_map = {
local postfix_key_map = {
Line 516: Line 518:
end
end
header_row:tag('th')
local header_style = 'style="position: sticky; top: 0; z-index: 1; background-color: rgb(' .. category_rgb .. ');"'
:cssText('position: sticky; top: 0; z-index: 1; background-color: rgb(' .. category_rgb .. ');')
table.insert(header_cells, '<th ' .. header_style .. '>' .. attr_localized .. postfix .. '</th>')
:wikitext(attr_localized .. postfix)
end
end
end
end
end
end


-- Build Body
-- Build Body Rows
local tbody = table:tag('tbody')
for hero_key, hero_data in pairs(heroes_data) do
for hero_key, hero_data in pairs(heroes_data) do
local in_development = hero_data["InDevelopment"]
local in_development = hero_data["InDevelopment"]
Line 530: Line 530:
local is_disabled = hero_data["IsDisabled"]
local is_disabled = hero_data["IsDisabled"]
if (not is_disabled) then  
if (not is_disabled) then  
local body_row = tbody:tag('tr')
local row_cells = {}
-- Hero Icon cell
-- Hero Icon cell
Line 538: Line 538:
local hero_icon = frame:expandTemplate{ title = "Template:HeroIcon", args = hero_icon_args }
local hero_icon = frame:expandTemplate{ title = "Template:HeroIcon", args = hero_icon_args }
body_row:tag('td')
table.insert(row_cells, '<td style="position: sticky; left: 0; z-index: 1; background-color: ' .. sticky_cell_bg .. ';">' .. hero_icon .. '</td>')
:cssText('position: sticky; left: 0; z-index: 1; background-color: ' .. sticky_cell_bg .. ';')
:wikitext(hero_icon)
-- Stat cells
-- Stat cells
Line 576: Line 574:
end
end
body_row:tag('td'):wikitext(stat_value .. scaling_strs)
table.insert(row_cells, '<td>' .. stat_value .. scaling_strs .. '</td>')
end
end
end
end
end
end
table.insert(body_rows, '<tr>' .. table.concat(row_cells) .. '</tr>')
end
end
end
end
end
end


-- Assemble the final table string
local headers_str = '<thead><tr>' .. table.concat(header_cells) .. '</tr></thead>'
local body_str = '<tbody>' .. table.concat(body_rows) .. '</tbody>'
local table_str = '<table class="wikitable sortable" style="table-layout: auto; width: 100%;">' .. headers_str .. body_str .. '</table>'
-- Final wrapper div
-- Final wrapper div
local div = mw.html.create('div')
return '<div style="overflow-x: auto; width: 100%;">' .. table_str .. '</div>'
:cssText('overflow-x: auto; width: 100%;')
:node(table)
 
return tostring(div)
end
end