ഘടകം:Multiple gallery
ഈ ഘടകത്തിന്റെ വിവരണം ഘടകം:Multiple gallery/വിവരണം എന്ന താളിൽ നിർമ്മിക്കാവുന്നതാണ്
local p = {}
-- Helper function to parse color values
local function parseColor(color)
if not color or color == '' then
return nil
end
return color
end
-- Helper function to calculate scaled dimensions
local function calculateScaledDimensions(images, totalWidth)
local totalOriginalWidth = 0
local baseHeight = nil
-- Find the base height (from first image with height specified)
for _, img in ipairs(images) do
if img.height and img.width then
baseHeight = img.height
break
end
end
if not baseHeight then
return images -- Can't scale without height info
end
-- Calculate total width at same height
for _, img in ipairs(images) do
if img.height and img.width then
local scaledWidth = (img.width * baseHeight) / img.height
totalOriginalWidth = totalOriginalWidth + scaledWidth
end
end
-- Scale each image
local scaleFactor = totalWidth / totalOriginalWidth
for i, img in ipairs(images) do
if img.height and img.width then
images[i].scaledWidth = math.floor((img.width * baseHeight * scaleFactor) / img.height)
images[i].scaledHeight = math.floor(baseHeight * scaleFactor)
end
end
return images
end
-- Main function to generate the gallery
function p.gallery(frame)
local args = frame:getParent().args
-- Get configuration parameters
local align = args.align or 'right' -- right (default), left, center
local direction = args.direction or 'horizontal' -- horizontal (default), vertical
local bgColor = parseColor(args['background color'])
local headerBg = parseColor(args.header_background)
local headerAlign = args.header_align or 'center'
local header = args.header or ''
local width = args.width -- uniform width for all images
local totalWidth = tonumber(args.total_width) -- total width for scaling
local captionAlign = args.caption_align or 'left'
local footerBg = parseColor(args.footer_background)
local footerAlign = args.footer_align or 'left'
local footer = args.footer or ''
-- Collect all images and their properties
local images = {}
for i = 1, 10 do
if args['image' .. i] then
table.insert(images, {
file = args['image' .. i],
width = tonumber(args['width' .. i]),
height = tonumber(args['height' .. i]),
alt = args['alt' .. i] or '',
link = args['link' .. i],
thumbtime = args['thumbtime' .. i],
caption = args['caption' .. i] or ''
})
end
end
if #images == 0 then
return '<div class="error">No images provided. Use image1, image2, etc.</div>'
end
-- Calculate scaled dimensions if total_width is specified
if totalWidth then
images = calculateScaledDimensions(images, totalWidth)
end
-- Create main container
local container = mw.html.create('div')
:addClass('responsive-gallery-container')
:addClass('gallery-align-' .. align)
-- Set container alignment
if align == 'center' then
container:css('margin', '0 auto')
:css('display', 'table')
elseif align == 'left' then
container:css('float', 'left')
:css('margin-right', '1em')
elseif align == 'right' then
container:css('float', 'right')
:css('margin-left', '1em')
end
-- Create gallery box
local box = container:tag('div')
:addClass('responsive-gallery-box')
if bgColor then
box:css('background-color', bgColor)
else
box:css('background-color', '#f8f9fa')
end
box:css('border', '1px solid #c8ccd1')
:css('padding', '8px')
:css('margin', '0.5em 0')
-- Add header if provided
if header ~= '' then
local headerDiv = box:tag('div')
:addClass('gallery-header')
:css('text-align', headerAlign)
:css('font-weight', 'bold')
:css('padding', '8px')
:css('margin-bottom', '8px')
:css('border-bottom', '1px solid #c8ccd1')
if headerBg then
headerDiv:css('background-color', headerBg)
end
headerDiv:wikitext(header)
end
-- Create images container
local imagesContainer = box:tag('div')
:addClass('gallery-images')
:addClass('gallery-' .. direction)
if direction == 'horizontal' then
imagesContainer:css('display', 'flex')
:css('flex-wrap', 'wrap')
:css('justify-content', 'center')
:css('gap', '4px')
else
imagesContainer:css('display', 'flex')
:css('flex-direction', 'column')
:css('gap', '8px')
end
-- Generate each gallery item
for idx, img in ipairs(images) do
local item = imagesContainer:tag('div')
:addClass('gallery-item')
:css('display', 'inline-block')
:css('vertical-align', 'top')
if direction == 'vertical' then
item:css('width', '100%')
end
-- Create image wrapper
local imgWrapper = item:tag('div')
:addClass('gallery-image-wrapper')
:css('text-align', 'center')
:css('background', '#fff')
:css('border', '1px solid #c8ccd1')
:css('padding', '3px')
-- Build image tag
local imgTag = '[[File:' .. img.file
-- Determine width to use
local imageWidth
if width then
imageWidth = width .. 'px'
elseif totalWidth and img.scaledWidth then
imageWidth = img.scaledWidth .. 'px'
elseif img.width then
imageWidth = img.width .. 'px'
else
imageWidth = '200px'
end
imgTag = imgTag .. '|' .. imageWidth
-- Add thumbtime for video files
if img.thumbtime then
imgTag = imgTag .. '|thumbtime=' .. img.thumbtime
end
-- Add alt text
if img.alt ~= '' then
imgTag = imgTag .. '|alt=' .. img.alt
end
-- Add link (or disable linking if link is explicitly empty)
if img.link ~= nil then
if img.link == '' then
imgTag = imgTag .. '|link='
else
imgTag = imgTag .. '|link=' .. img.link
end
end
imgTag = imgTag .. ']]'
imgWrapper:wikitext(imgTag)
-- Add caption if exists
if img.caption ~= '' then
item:tag('div')
:addClass('gallery-caption')
:css('text-align', captionAlign)
:css('padding', '4px 2px')
:css('font-size', '94%')
:css('line-height', '1.4')
:wikitext(img.caption)
end
end
-- Add footer if provided
if footer ~= '' then
local footerDiv = box:tag('div')
:addClass('gallery-footer')
:css('text-align', footerAlign)
:css('padding', '8px')
:css('margin-top', '8px')
:css('border-top', '1px solid #c8ccd1')
:css('font-size', '94%')
if footerBg then
footerDiv:css('background-color', footerBg)
end
footerDiv:wikitext(footer)
end
return tostring(container)
end
return p