Module:Navbox

From Fanon Wiki

This module allows to generate a navbox in an easier way, with automatic items, or links, and/or colors.

This module is not intended to be invoked if it's not a template

Basic example

{{#invoke:Navbox|MODE|bg=BGCOLOR|expand=COLLAPSE|text=TEXTCOLOR|TITLE|
Head // Item; Item; Item; Item
Head // Item; Item; Item
Head // Item; Item; Item; Item
Head // Item; Item; Item
...
}}

To set each item, break a line after the invocation and start with the title of the row, which is represented on the example above as Head. This is the "header" of each row in the table. Then split it with a forward slash and add your item, then split each item with a semicolon.

Remember that each section needs to be in the same row! If you break a line it will start a new section.

  • MANDATORY: MODE — sets the mode of the navbox. The available ones are:
    • list: adds an item normally, but without links automatically. To set a link to each page you need to add MediaWiki's [[syntax]] manually. This is recommended for most cases.
    • autolink: each item will be turn into a link automatically, so Item will have a link to the page called "Item". This is good for some situations, but it can have some limitations.
  • MANDATORY: TITLE — sets the title of the navbox. OBS: even if the mode is set to autolink, the navbox needs a manually-placed link.
  • Optional: bg=BGCOLOR — sets the background and border color of the navbox. Replace BGCOLOR with a hex, hsv, hsl, rgb, or rgba value.
  • Optional: text=TEXTCOLOR — sets the text color of the title and "headers". Black by default. If changed, it's recommended to be white (#ffffff) in 99.9% of cases.
  • Optional: expand=COLLAPSE — makes the navbox collapsed by default. It can be any given value, however, use the word "Collapsed" to be more clear.

Demo

{{#invoke:Navbox|list|bg=#0ae744|text=#000|My Title|
Asia // Japan; China; Korea; India; Indonesia; Iran
Africa // Egypt; Tanzania; Morocco; Gabon
South America // Argentina; Colombia; Brazil
North America // Canada; United States; Jamaica; Mexico
Europe // Austria
Moon // Mario; Knuckles
}}

local p = {}

function p.list (frame)
	local c1, c2, collapse
	c1 = frame.args["bg"] or "#429fff"-- background color
	c2 = frame.args["text"] or "#000" -- text color
	collapse = frame.args["expand"]
	if collapse ~= nil then collapse = "mw-collapsed" else collapse = "" end
	local title = frame.args[1]
    local items = frame.args[2]
    if items ~= nil then items = mw.text.split(items, "\n") else items = {} end
    local result = {}
    table.insert(result, ' <div class="navbox mt-2 mb-2 p-1 mw-collapsible '.. collapse ..'" style="border-color:' .. c1 .. '"><div class="p-1 br-4 text-center" style="background-color: '.. c1 ..'; color: '.. c2 ..' ; font-weight: bold">'.. title ..'</div><table class="table-responsive mw-collapsible-content"> ')
    local i = 1
    local x = 1
    local y = 2
    local item = {}
    local parents = {}
    local children = {}
    local resultItems = ""
    local resultSection = ""
    while items[i] do
        item = mw.text.split(items[i], "//")
        if item[2] and mw.text.trim(item[1]) ~= "" and mw.text.trim(item[2]) ~= "" then
            x = 1
            parents = mw.text.split(item[2], ";")
            resultItems = ""
            while parents[x] do
                resultItems = resultItems .. '<li>' .. parents[x] .. '</li>'
                x = x + 1
            end
            y = 2
            children = mw.text.split(item[1], ";")
            resultSection = children[1]
            
            table.insert(result, '<tr><th style="background:' .. c1 .. '; color: ' .. c2 .. '">' .. resultSection .. '</th><td><ul>' .. resultItems .. '</ul></td></tr>')
        end
        i = i + 1
    end
    table.insert(result, '</table></div></div>')
    return table.concat(result, '')
end

function p.autolink (frame)
	local c1, c2, collapse
	c1 = frame.args["bg"] or "#429fff" -- background color
	c2 = frame.args["text"] or "#000" -- text color
	collapse = frame.args["expand"]
	if collapse ~= nil then collapse = "mw-collapsed" else collapse = "" end
	local title = frame.args[1]
    local items = frame.args[2]
    if items ~= nil then items = mw.text.split(items, "\n") else items = {} end
    local result = {}
    table.insert(result, ' <div class="navbox mt-2 mb-2 p-1 mw-collapsible '.. collapse ..'" style="border-color:' .. c1 .. '"><div class="p-1 br-4 text-center" style="background-color: '.. c1 ..'; color: '.. c2 ..' ; font-weight: bold">'.. title ..'</div><table class="table-responsive mw-collapsible-content"> ')
    local i = 1
    local x = 1
    local y = 2
    local item = {}
    local parents = {}
    local children = {}
    local resultItems = ""
    local resultSection = ""
    while items[i] do
        item = mw.text.split(items[i], "//")
        if item[2] and mw.text.trim(item[1]) ~= "" and mw.text.trim(item[2]) ~= "" then
            x = 1
            parents = mw.text.split(item[2], ";")
            resultItems = ""
            while parents[x] do
                resultItems = resultItems .. '<li>[[' .. parents[x] .. ']]</li>'
                x = x + 1
            end
            y = 2
            children = mw.text.split(item[1], ";")
            resultSection = children[1]
            
            table.insert(result, '<tr><th style="background:' .. c1 .. '; color: ' .. c2 .. '">' .. resultSection .. '</th><td><ul>' .. resultItems .. '</ul></td></tr>')
        end
        i = i + 1
    end
    table.insert(result, '</table></div></div>')
    return table.concat(result, '')
end

return p