Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

此模块的文档可以在Module:Navbar/doc创建

local p = {}

local getArgs

local iconMap = {
	["查看"] = 'visibility',
	["讨论"] = 'forum',
	["编辑"] = 'edit',
	["历史"] = 'history',
	["移动"] = 'drive_file_move',
	["监视"] = 'notifications'
}

function p.addItem(full, link, descrip, args, url)
	local linkStart, linkEnd
	if url then
		linkStart = '[' .. link .. ' '
		linkEnd = ']'
	else
		linkStart = '[[' .. link .. '|'
		linkEnd = ']]'
	end

	local iconName = iconMap[full] or 'help'

	return string.format(
		'%s<span title="%s该模板" class="navbar-icon"><span class="md">%s</span></span>%s',
		linkStart,
		descrip,
		iconName,
		linkEnd
	)
end

function p.brackets(position, c, args, div)
	if args.brackets then
		div:tag('span')
			:css('margin-'..position, '-0.125em')
			:wikitext(c)
	end
end

function p._navbar(args)
	local show = {true, true, true, false, false, false}
	local titleArg = 1

	if args.collapsible then
		titleArg = 2
		if not args.plain then args.mini = 1 end
		args.style = 'float:left; text-align:left'
	end

	if args.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}
		for k,v in ipairs(require ('Module:TableTools').compressSparseArray(args)) do
			local num = index[v]
			if num then show[num] = true end
		end
	end

	if args.noedit then show[3] = false end

	local titleText = args[titleArg] or (':' .. mw.getCurrentFrame():getParent():getTitle())
	local title = mw.title.new(mw.text.trim(titleText), 'Template')
	if not title then
		error('无效的标题 ' .. titleText)
	end
	local talkpage = title.talkPageTitle and title.talkPageTitle.fullText or ''

	local div = mw.html.create():tag('div')
	div
		:addClass('plainlinks')
		:addClass('navbar')
		:cssText(args.style)

	if args.mini then div:addClass('mini') end

	if not (args.mini or args.plain) then
		div:tag('span')
			:css('word-spacing', 0)
			:wikitext(args.text or '此框:')
			:wikitext(' ')
	end

	p.brackets('right', '&#91; ', args, div)

	local flexContainer = div:tag('div')
		:css('display', 'inline-flex')
		:css('align-items', 'center')
		:css('gap', '12px')
		:css('line-height', '1')

	local items = {}
	if show[1] then table.insert(items, p.addItem('查看', title.fullText, '查看', args)) end
	if show[2] then table.insert(items, p.addItem('讨论', talkpage, '讨论', args)) end
	if show[3] then table.insert(items, p.addItem('编辑', title:fullUrl('action=edit'), '编辑', args, true)) end
	if show[4] then table.insert(items, p.addItem('历史', title:fullUrl('action=history'), '历史', args, true)) end
	if show[5] then
		local move = mw.title.new ('Special:Movepage')
		table.insert(items, p.addItem('移动', move:fullUrl('target='..title.fullText), '移动', args, true))
	end
	if show[6] then table.insert(items, p.addItem('监视', title:fullUrl('action=watch'), '监视', args, true)) end

	flexContainer:wikitext(table.concat(items))

	p.brackets('left', ' &#93;', args, div)

	if args.collapsible then
		div:done()
		   :tag('div')
			:css('font-size', '114%')
			:css('margin', args.mini and '0 4em' or '0 7em')
			:wikitext(args[1])
	end

	local frame = mw.getCurrentFrame()
	return frame:extensionTag{ name = 'templatestyles', args = { src = 'Module:Navbar/styles.css' } }
		.. tostring(div:done())
end

function p.navbar(frame)
	if not getArgs then
		getArgs = require('Module:Arguments').getArgs
	end
	return p._navbar(getArgs(frame))
end

return p