Module:Navbar/sandbox
Appearance
| This is the module sandbox page for Module:Navbar (diff). See also the companion subpage for test cases (run). |
| This Lua module is used in system messages, and on approximately 5,110,000 pages, or roughly 8% of all pages. Changes to it can cause immediate changes to the Wikipedia user interface. To avoid major disruption and server load, any changes should be tested in the module's /sandbox or /testcases subpages, or in your own module sandbox. The tested changes can be added to this page in a single edit. Please discuss changes on the talk page before implementing them. |
| This module is rated as ready for general use. It has reached a mature state, is considered relatively stable and bug-free, and may be used wherever appropriate. It can be mentioned on help pages and other Wikipedia resources as an option for new users. To minimise server load and avoid disruptive output, improvements should be developed through sandbox testing rather than repeated trial-and-error editing. |
| This module depends on the following other modules: |
| This module uses TemplateStyles: |
This is a Lua implementation of {{Navbar}}. It is used in Module:Navbox.
local p = {}
local cfg = mw.loadData('Module:Navbar/configuration')
local function addItem (link_description, ul, is_mini, font_style)
local l
if link_description[5] then
l = {'[', '', ']'}
else
l = {'[[', '|', ']]'}
end
ul:tag('li')
:addClass('nv-' .. link_description[2])
:wikitext(l[1] .. link_description[3] .. l[2])
:tag(is_mini and 'abbr' or 'span')
:attr('title', link_description[4])
:cssText(font_style)
:wikitext(is_mini and link_description[1] or link_description[2])
:done()
:wikitext(l[3])
:done()
end
local function add_bracket(class, bracket, div, has_brackets, font_style)
if has_brackets then
div
:tag('span')
:addClass(class)
:cssText(font_style)
:wikitext(bracket)
:done()
end
end
function p._navbar(args)
local font_style = args.fontstyle
local font_color = args.fontcolor
local is_collapsible = args.collapsible
local is_mini = args.mini
local is_plain = args.plain
local titleArg = 1
local collapsible_class = nil
if is_collapsible then
collapsible_class = cfg.classes.collapsible
titleArg = 2
if not is_plain then is_mini = 1 end
if font_color then
font_style = font_style .. '; color: ' .. font_color .. ';'
end
end
-- The show table indicates the default displayed items.
-- view, talk, edit, hist, move, watch
-- TODO: Move to configuration.
local show = {true, true, true, false, false, false}
local template = args.template
if template then
titleArg = 'template'
show = {true, false, false, false, false, false}
local index = {t = 2, d = 2, e = 3, h = 4, m = 5, w = 6,
talk = 2, edit = 3, hist = 4, move = 5, watch = 6}
-- TODO: Consider removing TableTools dependency.
for k, v in ipairs(require ('Module:TableTools').compressSparseArray(args)) do
local num = index[v]
if num then show[num] = true end
end
end
local remove_edit_link = args.noedit
if remove_edit_link then show[3] = false end
local titleText = args[titleArg] or (':' .. mw.getCurrentFrame():getParent():getTitle())
local title = mw.title.new(mw.text.trim(titleText), cfg.title_namespace)
if not title then
error(cfg.invalid_title .. titleText)
end
local talkpage = title.talkPageTitle and title.talkPageTitle.fullText or ''
local navbar_style = args.style
local div = mw.html.create():tag('div')
div
:addClass(cfg.classes.navbar)
:addClass(cfg.classes.plainlinks)
:addClass(cfg.classes.horizontal_list)
:addClass(collapsible_class) -- we made the determination earlier
:cssText(navbar_style)
if is_mini then div:addClass(cfg.classes.mini) end
local box_text = args.text or cfg.box_text
if not (is_mini or is_plain) then
div
:tag('span')
:addClass(cfg.classes.box_text)
:cssText(font_style)
:wikitext(box_text .. ' ') -- the concatenated space guarantees the box text is separated
end
local has_brackets = args.brackets
add_bracket(cfg.classes.left_bracket, cfg.left_bracket, div, has_brackets, font_style)
-- TODO: Get link_descriptions and show into the configuration module.
-- link_descriptions should be easier...
local link_descriptions = {
{'v', 'view', title.fullText, 'View this template', false},
{'t', 'talk', talkpage, 'Discuss this template', false},
{'e', 'edit', title:fullUrl('action=edit'), 'Edit this template', true},
{'h', 'hist', title:fullUrl('action=history'), 'History of this template', true},
{'m', 'move', mw.title.new ('Special:Movepage'):fullUrl('target='..title.fullText),
'Move this template', true },
{'w', 'watch', title:fullUrl('action=watch'), 'Watch this template', true}
}
local ul = mw.html.create('ul')
for i, _ in ipairs(show) do
if show[i] then addItem(link_descriptions[i], ul, is_mini, font_style) end
end
ul:done()
div:node(ul)
add_bracket(cfg.classes.right_bracket, cfg.right_bracket, div, has_brackets, font_style)
if is_collapsible then
local title_text_class
if is_mini then
title_text_class = cfg.classes.collapsible_title_mini
else
title_text_class = cfg.classes.collapsible_title_full
end
div:done()
:tag('div')
:addClass(title_text_class)
:cssText(font_style)
:wikitext(args[1])
end
return mw.getCurrentFrame():extensionTag{
name = 'templatestyles', args = { src = cfg.templatestyles }
} .. tostring(div:done())
end
function p.navbar(frame)
return p._navbar(require('Module:Arguments').getArgs(frame))
end
return p