-- This modules serves as the code for Template:Tab2
local p = {}
local utilsCode = require("Module:UtilsCode")
local utilsTable = require("Module:UtilsTable")
-- Main function
function p.Main(tabs, defaultTab, tabFormat, tabsPerRow, tabsWidth, contentWidth, contentHeight, align)
if utilsCode.IsEmpty(tabsWidth) then
tabsWidth = "100%"
end
local result = mw.html.create("div")
-- Create contents and containers depending on the format.
if (tabFormat == "bottom") then
result:node(p.GenerateContent(tabs, defaultTab, contentWidth, contentHeight))
result:node(p.GenerateContainer(tabs, defaultTab, tabFormat, tabsPerRow, tabsWidth, align))
else
result:node(p.GenerateContainer(tabs, defaultTab, tabFormat, tabsPerRow, tabsWidth, align))
result:node(p.GenerateContent(tabs, defaultTab, contentWidth, contentHeight))
end
return result
end
-- Main function wrapper for template calls
function p._Main(frame)
local args = frame:getParent().args
-- Defaults
local defaultTab = 1
local tabFormat = "top"
local align = "left"
-- Overriding defaults if providen
if not (utilsCode.IsEmpty(args["default"])) then
defaultTab = args["default"]
end
if not (utilsCode.IsEmpty(args["format"])) then
tabFormat = args["format"]
end
if not (utilsCode.IsEmpty(args["align"])) then
align = args["align"]
end
-- Generates a table of tab-content combinations if both have been provided
-- values for the same index. (ex: tabs[1] = {"tabName1", {"tabContent1"})
local tabs = {}
for key, value in pairs(args) do
if ((string.match(key, "tab[0-9]+") ~= nil) and (value ~= "")) then
if not (utilsCode.IsEmpty(args["content" .. string.match(key, "[0-9]+")])) then
local index = tonumber(string.sub(key, 4))
tabs[index] = {tabName = value, tabContent = args["content" .. string.match(key, "[0-9]+")]}
end
end
end
tabs = utilsTable.compact(tabs) -- just in case. e.g. template call specifies tab1= and tab3= but not tab=2
return p.Main(tabs, defaultTab, tabFormat, args["tabsPerRow"], args["tabsWidth"], contentWidth, contentHeight, align)
end
-- Generate containers (tabs).
function p.GenerateContainer(tabs, defaultTab, tabFormat, tabsPerRow, tabsWidth, align)
local result = mw.html.create("div")
:addClass("tabcontainer tabcontainer-" .. tabFormat)
if align then
result:addClass("tabcontainer--"..align)
end
-- Ordering tabs by keys.
local orderedKeys = {}
for key in pairs(tabs) do
table.insert(orderedKeys, key)
end
table.sort(orderedKeys)
for i = 1, #orderedKeys do
local span = mw.html.create("span")
:addClass("tab explain")
:attr("title", tabs[orderedKeys[i]]["tabCaption"])
:wikitext(tabs[orderedKeys[i]]["tabName"]):done()
if not utilsCode.IsEmpty(tabsPerRow) then
span:css{
["width"] = "calc((" .. tabsWidth .. " / " .. tabsPerRow .. ") - (1px * 2) - (2px * 2) - (3px * 2))",
["white-space"] = "normal",
}:done()
end
if (tostring(defaultTab) == tostring(orderedKeys[i])) then
span:addClass("active"):done()
end
result:node(span)
end
return result
end
-- Generate contents.
function p.GenerateContent(tabs, defaultTab, contentWidth, contentHeight)
local result = mw.html.create("div")
:addClass("tabcontents")
:done()
if not (utilsCode.IsEmpty(contentHeight)) then
result:addClass("vertical-centered-content")
:css{
["min-height"] = contentHeight,
["height"] = contentHeight
}:done()
end
if not (utilsCode.IsEmpty(contentWidth)) then
result
:css{
["min-width"] = contentWidth,
["width"] = contentWidth
}:done()
end
-- Ordering tabs by keys.
local orderedKeys = {}
for key in pairs(tabs) do
table.insert(orderedKeys, key)
end
table.sort(orderedKeys)
for i = 1, #orderedKeys do
local container = mw.html.create("div")
:addClass("content")
:wikitext("\n",tabs[orderedKeys[i]]["tabContent"])
:done()
if not (utilsCode.IsEmpty(contentHeight)) then
container:css{
["height"] = contentHeight
}:done()
end
if not (utilsCode.IsEmpty(contentWidth)) then
container:css{
["width"] = contentWidth
}:done()
end
if (tostring(defaultTab) == tostring(orderedKeys[i])) then
container:addClass("content--active")
end
result:node(container):done()
end
return result
end
return p
Advertisement
Module:Tab2
Advertisement