Module:Navbar

From WikiProjectMed
Jump to navigation Jump to search

This is a Lua implementation of {{Navbar}}. It is used in Module:Navbox.


  1 local p = {}
  2 local cfg = mw.loadData('Module:Navbar/configuration')
  3 
  4 local function get_title_arg(is_collapsible, template)
  5 	local title_arg = 1
  6 	if is_collapsible then title_arg = 2 end
  7 	if template then title_arg = 'template' end
  8 	return title_arg
  9 end
 10 
 11 local function choose_links(template, args)
 12 	-- The show table indicates the default displayed items.
 13 	-- view, talk, edit, hist, move, watch
 14 	-- TODO: Move to configuration.
 15 	local show = {true, true, true, false, false, false}
 16 	if template then
 17 		show[2] = false
 18 		show[3] = false
 19 		local index = {t = 2, d = 2, e = 3, h = 4, m = 5, w = 6,
 20 			talk = 2, edit = 3, hist = 4, move = 5, watch = 6}
 21 		-- TODO: Consider removing TableTools dependency.
 22 		for _, v in ipairs(require ('Module:TableTools').compressSparseArray(args)) do
 23 			local num = index[v]
 24 			if num then show[num] = true end
 25 		end
 26 	end
 27 
 28 	local remove_edit_link = args.noedit
 29 	if remove_edit_link then show[3] = false end
 30 	
 31 	return show
 32 	
 33 end
 34 
 35 local function add_link(link_description, ul, is_mini, font_style)
 36 	local l
 37 	if link_description.url then
 38 		l = {'[', '', ']'}
 39 	else
 40 		l = {'[[', '|', ']]'}
 41 	end
 42 	ul:tag('li')
 43 		:addClass('nv-' .. link_description.full)
 44 		:wikitext(l[1] .. link_description.link .. l[2])
 45 		:tag(is_mini and 'abbr' or 'span')
 46 			:attr('title', link_description.html_title)
 47 			:cssText(font_style)
 48 			:wikitext(is_mini and link_description.mini or link_description.full)
 49 			:done()
 50 		:wikitext(l[3])
 51 		:done()
 52 end
 53 
 54 local function make_list(title_text, has_brackets, displayed_links, is_mini, font_style)
 55 	
 56 	local title = mw.title.new(mw.text.trim(title_text), cfg.title_namespace)
 57 	if not title then
 58 		error(cfg.invalid_title .. title_text)
 59 	end
 60 	local talkpage = title.talkPageTitle and title.talkPageTitle.fullText or ''
 61 	
 62 	-- TODO: Get link_descriptions and show into the configuration module.
 63 	-- link_descriptions should be easier...
 64 	local link_descriptions = {
 65 		{ ['mini'] = 'v', ['full'] = 'view', ['html_title'] = 'View this template',
 66 			['link'] = title.fullText, ['url'] = false },
 67 		{ ['mini'] = 't', ['full'] = 'talk', ['html_title'] = 'Discuss this template',
 68 			['link'] = talkpage, ['url'] = false },
 69 		{ ['mini'] = 'e', ['full'] = 'edit', ['html_title'] = 'Edit this template',
 70 			['link'] = title:fullUrl('action=edit'), ['url'] = true },
 71 		{ ['mini'] = 'h', ['full'] = 'hist', ['html_title'] = 'History of this template',
 72 			['link'] = title:fullUrl('action=history'), ['url'] = true },
 73 		{ ['mini'] = 'm', ['full'] = 'move', ['html_title'] = 'Move this template',
 74 			['link'] = mw.title.new('Special:Movepage'):fullUrl('target='..title.fullText), ['url'] = true },
 75 		{ ['mini'] = 'w', ['full'] = 'watch', ['html_title'] = 'Watch this template', 
 76 			['link'] = title:fullUrl('action=watch'), ['url'] = true }
 77 	}
 78 
 79 	local ul = mw.html.create('ul')
 80 	if has_brackets then
 81 		ul:addClass(cfg.classes.brackets)
 82 			:cssText(font_style)
 83 	end
 84 	
 85 	for i, _ in ipairs(displayed_links) do
 86 		if displayed_links[i] then add_link(link_descriptions[i], ul, is_mini, font_style) end
 87 	end
 88 	return ul:done()
 89 	
 90 end
 91 
 92 function p._navbar(args)
 93 	
 94 	-- TODO: We probably don't need both fontstyle and fontcolor...
 95 	local font_style = args.fontstyle
 96 	local font_color = args.fontcolor
 97 	local is_collapsible = args.collapsible
 98 	local is_mini = args.mini
 99 	local is_plain = args.plain
100 	
101 	local collapsible_class = nil
102 	if is_collapsible then
103 		collapsible_class = cfg.classes.collapsible
104 		if not is_plain then is_mini = 1 end
105 		if font_color then
106 			font_style = (font_style or '') .. '; color: ' .. font_color .. ';'
107 		end
108 	end
109 	font_style = (font_style or '') .. ';text-decoration:inherit;'
110 	
111 	local navbar_style = args.style
112 	local div = mw.html.create():tag('div')
113 	div
114 		:addClass(cfg.classes.navbar)
115 		:addClass(cfg.classes.plainlinks)
116 		:addClass(cfg.classes.horizontal_list)
117 		:addClass(collapsible_class) -- we made the determination earlier
118 		:cssText(navbar_style)
119 
120 	if is_mini then div:addClass(cfg.classes.mini) end
121 
122 	local box_text = (args.text or cfg.box_text) .. ' '
123 	 -- the concatenated space guarantees the box text is separated
124 	if not (is_mini or is_plain) then
125 		div
126 			:tag('span')
127 				:addClass(cfg.classes.box_text)
128 				:cssText(font_style)
129 				:wikitext(box_text)
130 	end
131 	
132 	local template = args.template
133 	local displayed_links = choose_links(template, args)
134 	local has_brackets = args.brackets
135 	local title_arg = get_title_arg(is_collapsible, template)
136 	local title_text = args[title_arg] or (':' .. mw.getCurrentFrame():getParent():getTitle())
137 	local list = make_list(title_text, has_brackets, displayed_links, is_mini, font_style)
138 	div:node(list)
139 
140 	if is_collapsible then
141 		local title_text_class
142 		if is_mini then
143 			title_text_class = cfg.classes.collapsible_title_mini
144 		else
145 			title_text_class = cfg.classes.collapsible_title_full
146 		end
147 		div:done()
148 			:tag('div')
149 			:addClass(title_text_class)
150 			:cssText(font_style)
151 			:wikitext(args[1])
152 	end
153 	
154 	return mw.getCurrentFrame():extensionTag{
155 		name = 'templatestyles', args = { src = cfg.templatestyles }
156 	} .. tostring(div:done())
157 end
158 
159 function p.navbar(frame)
160 	return p._navbar(require('Module:Arguments').getArgs(frame))
161 end
162 
163 return p