Editing Module:Sandbox/User:The Gaffer/Modules/Infobox Switch

Jump to navigation Jump to search
Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.

Latest revision Your text
Line 6: Line 6:
-- Module access point for infobox switch
-- Module access point for infobox switch
p._infobox_switch = function(args)
p._infobox_switch = function(args)
-- Prepare content list from arguments
-- Prepare content list from arguments
local contents = {}
local contents = {}
local i = 1
local i = 1
while args['item'..i] do
while args['item' .. i] do
table.insert(contents, {
table.insert(contents, { text = args['text'..i] or ('Item '..i),
content = '\n' .. args['item'..i],
text = args['text' .. i] or ('Item ' .. i),
id = args['switch_id'..i] or i})
content = '\n' .. args['item' .. i],
id = args['switch_id' .. i] or i
i = i + 1
})
end
i = i + 1
end
-- Create the main table structure for the infobox switch

local structure = mw.html.create('table')
-- Create the main table structure for the infobox switch
:addClass('infobox-switch')
local structure = mw.html.create('table')
:tag('caption')
:addClass('infobox-switch')
:done();
:tag('caption')
:done()
-- Create the div for the buttons inside the caption

local buttons = structure:tag('div')
-- Create the div for the buttons inside the caption
:attr('data-default-index', 1)
local buttons = structure:tag('div')
:addClass('infobox-buttons')
:attr('data-default-index', 1)

-- Add buttons for each item in contents
for i, v in ipairs(contents) do
buttons:tag('button')
:addClass('button')
:attr('data-switch-index', i)
:attr('data-switch-anchor', '#section' .. i)
:wikitext(v.text)
:done()
end


-- Create the tbody for the main content area of the infobox
-- Add buttons for each item in contents
local content_body = structure:tag('tbody')
for i, v in ipairs(contents) do
buttons:tag('button')
:addClass('button')
:attr('data-switch-index', i)
:attr('data-switch-anchor', '#section' ..i)
:wikitext(v.text)
:done()
end
local content_body = structure:tag('tbody')
local default_content = contents[1] -- Assuming default index is 1
local default_content = contents[1] -- Assuming default index is 1
if default_content then
if default_content then
Line 46: Line 50:
row:done()
row:done()
end
end

-- Create the hidden resources section that contains all switchable content
-- Create the hidden resources section that contains all switchable content
local resources_div = structure:tag('div')
local resources_div = structure:tag('div')
Line 64: Line 68:
end
end


-- Return the complete HTML structure as a string
mw.logObject(structure)
-- Return the complete HTML structure
return tostring(structure)
return tostring(structure)
end
end


-- Public function to be called by templates
p.infobox_switch = function(frame)
p.infobox_switch = function(frame)
-- Directly return the raw HTML output
return p._infobox_switch(frame:getParent().args)
return p._infobox_switch(frame:getParent().args)
end
end
Please note that all contributions to Brighter Shores Wiki are considered to be released under the CC BY-NC-SA 3.0 (see Brighter Shores:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!
Cancel Editing help (opens in new window)
Preview page with this template