Module:Slideshow

From The Museum of Human Achievement

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

-- ==========================================================
--  Module: Carousel
--  Usage:
--
--  {{#invoke:Carousel|create|Wikislider007.jpg,Wikislider008.png,Wikislider009.gif}}
--
--  The module outputs a Bootstrap carousel with indicators and slides.
-- ==========================================================

local p = {}

function p.create(frame)
    local id = frame.args[1] or ''
    local raw = frame.args[2] or ''
    local interval = tonumber(frame.args[3]) or 'false'
    local auto
    if interval ~= "false" then
    	auto = 'carousel'
    end
    local images = mw.text.split(raw, ",", true)
    -- split by comma, trim whitespace
    local imagesTable = {}
    for _, img in ipairs(images) do
        img = mw.text.trim(img)
        if img ~= "" then table.insert(imagesTable, img) end
    end
    -- If no images supplied – return an empty string (or a warning)
    if #images == 0 then
        return "<!-- Carousel: No image names supplied -->"
    end
    -- Assemble
    local html = mw.html.create()
    local carousel = html:tag("div")
        :addClass("carousel slide")
        :attr("id", id)
        :attr("data-ride", auto)
        :attr("data-interval", interval)
    -- Widgets
    local sliders = frame:preprocess('{{#widget:carousel|id=' .. id .. '}}')
    carousel:wikitext(sliders)
    -- Indicators
    local ol = carousel:tag("ol"):addClass("carousel-indicators")
    for i = 1, #imagesTable do
        local li = ol:tag("li")
            :attr("data-target", "#" .. id)
            :attr("data-slide-to", tostring(i-1))
        if i == 1 then li:addClass("active") end
    end
    -- Inner slides
    local inner = carousel:tag("div"):addClass("carousel-inner")
    for i, img in ipairs(imagesTable) do
    	local imageUrl = frame:preprocess('{{filepath:' .. img .. '}}')
        local item = inner:tag("div")
            :addClass(i == 1 and "carousel-item active" or "carousel-item")
            :attr("data-bg", imageUrl)
    end

    return html
end

function p.masonry(frame)
    local raw = frame.args[1] or ''
    local iheight = frame.args[2] .. '' or ''
    if raw == '' then
    	return
    end
    if iheight == '' then
    	iheight = '240'
    end
    local images = mw.text.split(raw, ",", true) or {}
    -- Widgets
    local widget = frame:preprocess('{{#widget:masonry|height=' .. iheight .. '}}')
    -- Assemble
    local html = mw.html.create()
    local masonry = html:tag("div")
        :attr("id", "masonry")
    for i, img in ipairs(images) do
    	local imageUrl = frame:preprocess('{{filepath:' .. img .. '}}')
    	if imageUrl ~= '' then
        	local item = masonry:tag("div")
            	:addClass("tile")
            	:attr("data-bg", imageUrl)
        end
    end
    for i, img in ipairs(images) do
    	local imageMin = string.format('[[File:%s|class=d-none|300px]]', img )
    	html:tag('div'):addClass('d-none'):wikitext(imageMin)
    end    
    html:tag('div'):addClass('d-none'):wikitext(widget)
    return html
end

return p