Модуль:Navbar: различия между версиями

Перейти к навигации Перейти к поиску
более контрастная серая иконка шестерёнки в тёмной теме
(Per edit request on talk)
 
(более контрастная серая иконка шестерёнки в тёмной теме)
Строка 1: Строка 1:
local p = {}
local p = {}
local cfg = mw.loadData('Module:Navbar/configuration')


local function get_title_arg(is_collapsible, template)
local hlist = 'Template:Flatlist/styles.css'
local title_arg = 1
local templatestyles = 'Module:Navbar/styles.css'
if is_collapsible then title_arg = 2 end
local getArgs
if template then title_arg = 'template' end
 
return title_arg
function p._ruwikiNavbar( args )
end
local titleText = args[ 1 ] or (':' .. mw.getCurrentFrame():getParent():getTitle())
local title = mw.title.new(mw.text.trim(titleText), 'Template');


local function choose_links(template, args)
if not title then
-- The show table indicates the default displayed items.
error('Invalid title ' .. titleText)
-- view, talk, edit, hist, move, watch
-- TODO: Move to configuration.
local show = {true, true, true, false, false, false}
if template then
show[2] = false
show[3] = 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 _, v in ipairs(require ('Module:TableTools').compressSparseArray(args)) do
local num = index[v]
if num then show[num] = true end
end
end
end
local remove_edit_link = args.noedit
if remove_edit_link then show[3] = false end
return show
local whiteColorDefs = {
['#fff'] = true,
['#ffffff'] = true,
['white'] = true,
}
local fontColor = args.fontcolor and args.fontcolor:lower()
local isWhite = fontColor and whiteColorDefs[ fontColor ] == true
local fontStyle = args.fontstyle and mw.text.unstripNoWiki( args.fontstyle:lower() )
if not isWhite and fontStyle then
local styleratio = require( 'Module:Color contrast' )._styleratio
isWhite = styleratio( {
fontStyle .. '; color:#666;',
'',
'#666'
} ) < 3.66
end
local isTemplate = title.namespace == 10
local altText = string.format(
'Перейти %s «%s»',
isTemplate and 'к шаблону' or 'на страницу',
mw.text.split( isTemplate and title.text or title.fullText, '#' )[ 1 ]
)
return string.format(
'[[Файл:Wikipedia interwiki section gear icon%s.svg|14px|class=noprint%s|link=%s|%s]]',
isWhite and ' white' or '',
isWhite and '' or ' skin-invert-image',
tostring( title ),
altText
)
end
end


local function add_link(link_description, ul, is_mini, font_style)
function p.ruwikiNavbar(frame)
local l
if not getArgs then
if link_description.url then
getArgs = require('Module:Arguments').getArgs
l = {'[', '', ']'}
else
l = {'[[', '|', ']]'}
end
end
ul:tag('li')
return p._ruwikiNavbar(getArgs(frame))
:addClass('nv-' .. link_description.full)
:wikitext(l[1] .. link_description.link .. l[2])
:tag(is_mini and 'abbr' or 'span')
:attr('title', link_description.html_title)
:cssText(font_style)
:wikitext(is_mini and link_description.mini or link_description.full)
:done()
:wikitext(l[3])
:done()
end
end


local function make_list(title_text, has_brackets, displayed_links, is_mini, font_style)
function p._navbar(args)
local titleArg = 1
 
if args.collapsible then
titleArg = 2
if not args.plain then
args.mini = 1
end
if args.fontcolor then
args.fontstyle = 'color:' .. args.fontcolor .. ';'
end
args.style = 'float:left; text-align:left; white-space:nowrap;'
end
local title = mw.title.new(mw.text.trim(title_text), cfg.title_namespace)
if args[titleArg] == 'off' then
if not title then
return
error(cfg.invalid_title .. title_text)
end
end
local talkpage = title.talkPageTitle and title.talkPageTitle.fullText or ''
-- TODO: Get link_descriptions and show into the configuration module.
-- link_descriptions should be easier...
local link_descriptions = {
{ ['mini'] = 'v', ['full'] = 'view', ['html_title'] = 'View this template',
['link'] = title.fullText, ['url'] = false },
{ ['mini'] = 't', ['full'] = 'talk', ['html_title'] = 'Discuss this template',
['link'] = talkpage, ['url'] = false },
{ ['mini'] = 'e', ['full'] = 'edit', ['html_title'] = 'Edit this template',
['link'] = 'Special:EditPage/' .. title.fullText, ['url'] = false },
{ ['mini'] = 'h', ['full'] = 'hist', ['html_title'] = 'History of this template',
['link'] = 'Special:PageHistory/' .. title.fullText, ['url'] = false },
{ ['mini'] = 'm', ['full'] = 'move', ['html_title'] = 'Move this template',
['link'] = mw.title.new('Special:Movepage'):fullUrl('target='..title.fullText), ['url'] = true },
{ ['mini'] = 'w', ['full'] = 'watch', ['html_title'] = 'Watch this template',
['link'] = title:fullUrl('action=watch'), ['url'] = true }
}


local ul = mw.html.create('ul')
local titleText = args[titleArg] or (':' .. mw.getCurrentFrame():getParent():getTitle())
if has_brackets then
local title = mw.title.new(mw.text.trim(titleText), 'Template');
ul:addClass(cfg.classes.brackets)
 
:cssText(font_style)
if not title then
end
error('Invalid title ' .. titleText)
for i, _ in ipairs(displayed_links) do
if displayed_links[i] then add_link(link_descriptions[i], ul, is_mini, font_style) end
end
end
return ul:done()
end


function p._navbar(args)
local talkpage = title.talkPageTitle and title.talkPageTitle.fullText or '';
local tag
-- TODO: We probably don't need both fontstyle and fontcolor...
if args.nodiv then
local font_style = args.fontstyle
tag = 'span'
local font_color = args.fontcolor
else
local is_collapsible = args.collapsible
tag = 'div'
local is_mini = args.mini
local is_plain = args.plain
local collapsible_class = nil
if is_collapsible then
collapsible_class = cfg.classes.collapsible
if not is_plain then is_mini = 1 end
if font_color then
font_style = (font_style or '') .. '; color: ' .. font_color .. ';'
end
end
end
local navbar_style = args.style
local div = mw.html.create():tag(tag)
local div = mw.html.create():tag('div')
div
div
:addClass(cfg.classes.navbar)
:addClass('navbar hlist plainlinks noprint')
:addClass(cfg.classes.plainlinks)
:attr('data-navboxnavigation-link', '0')
:addClass(cfg.classes.horizontal_list)
:cssText(args.style)
:addClass(collapsible_class) -- we made the determination earlier
:cssText(navbar_style)


if is_mini then div:addClass(cfg.classes.mini) end
if args.mini then div:addClass('navbar-mini') end


local box_text = (args.text or cfg.box_text) .. ' '
if not (args.mini or args.plain) then
-- the concatenated space guarantees the box text is separated
if not (is_mini or is_plain) then
div
div
:tag('span')
:tag('span')
:addClass(cfg.classes.box_text)
:addClass('navbar-boxtext')
:cssText(font_style)
:cssText(args.fontstyle)
:wikitext(box_text)
:wikitext(args.text or 'Шаблон:')
:wikitext(' ')
end
end
local template = args.template
local ul = div:tag('ul');
local displayed_links = choose_links(template, args)
if args.brackets then
local has_brackets = args.brackets
ul:addClass('navbar-brackets')
local title_arg = get_title_arg(is_collapsible, template)
end
local title_text = args[title_arg] or (':' .. mw.getCurrentFrame():getParent():getTitle())
local list = make_list(title_text, has_brackets, displayed_links, is_mini, font_style)
div:node(list)


if is_collapsible then
ul
local title_text_class
:tag('li')
if is_mini then
:addClass('nv-view')
title_text_class = cfg.classes.collapsible_title_mini
:wikitext('[[' .. title.fullText .. '|')
else
:tag(args.mini and 'abbr' or 'span')
title_text_class = cfg.classes.collapsible_title_full
:addClass(args.mini and 'navbar-mini-abbr')
end
:attr('title', 'Просмотр этого шаблона')
:cssText(args.fontstyle)
:wikitext(args.mini and 'п' or 'просмотр')
:done()
:wikitext(']]')
:done()
:tag('li')
:addClass('nv-talk')
:wikitext('[[' .. talkpage .. '|')
:tag(args.mini and 'abbr' or 'span')
:attr('title', 'Обсуждение этого шаблона')
:cssText(args.fontstyle)
:wikitext(args.mini and 'о' or 'обсуждение')
:done()
:wikitext(']]');
 
if not args.noedit then
ul
:tag('li')
:addClass('nv-edit')
:wikitext('[[Special:EditPage/' .. title.fullText .. '|')
:tag(args.mini and 'abbr' or 'span')
:attr('title', 'Править этот шаблон')
:cssText(args.fontstyle)
:wikitext(args.mini and 'р' or 'править')
:done()
:wikitext(']]');
end
 
if args.collapsible then
div:done()
div:done()
:tag('div')
:tag('span')
:addClass(title_text_class)
:addClass( args.mini and 'navbar-ct-mini' or 'navbar-ct-full' )
:cssText(font_style)
:cssText(args.fontstyle)
:wikitext(args[1])
:wikitext(args[1])
end
end
 
local frame = mw.getCurrentFrame()
local frame = mw.getCurrentFrame()
-- hlist -> navbar is best-effort to preserve old Common.css ordering.
return frame:extensionTag{
return frame:extensionTag{
name = 'templatestyles', args = { src = cfg.hlist_templatestyles }
name = 'templatestyles', args = { src = hlist }
} .. frame:extensionTag{
} .. frame:extensionTag{
name = 'templatestyles', args = { src = cfg.templatestyles }
name = 'templatestyles', args = { src = templatestyles }
} .. tostring(div:done())
} .. tostring(div:done())
end
end


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


return p
return p
Анонимный участник

Навигация