Jump to content

Module:Navbar/sandbox

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Izno (talk | contribs) at 09:07, 4 December 2020 (cover up that lurking bug for font_color, more todos). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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)
	-- TODO: We probably don't need both fontstyle and fontcolor...
	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 or '') .. '; 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

	-- TODO: Support some limited parameters worth of templatestyles so that
	-- these styles can be easily overridden if desired.
	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