Module:Sidebar

From TechInfoDepot
Jump to navigationJump to search
Documentation icon Module documentation[view] [edit] [history] [purge]

This module implements the templates {{sidebar}} and {{sidebar with collapsible lists}}. See the individual template pages for documentation.

  1 --
  2 -- This module implements {{Sidebar}}
  3 --
  4 require('Module:No globals')
  5 
  6 local p = {}
  7 
  8 local getArgs = require('Module:Arguments').getArgs
  9 local navbar = require('Module:Navbar')._navbar
 10 
 11 local function trimAndAddAutomaticNewline(s)
 12 	-- For compatibility with the original {{sidebar with collapsible lists}}
 13 	-- implementation, which passed some parameters through {{#if}} to trim
 14 	-- their whitespace. This also triggered the automatic newline behavior.
 15 	-- ([[meta:Help:Newlines and spaces#Automatic newline]])
 16 	s = mw.ustring.gsub(s, "^%s*(.-)%s*$", "%1")
 17 	if mw.ustring.find(s, '^[#*:;]') or mw.ustring.find(s, '^{|') then
 18 		return '\n' .. s
 19 	else
 20 		return s
 21 	end
 22 end
 23 
 24 function p.sidebar(frame, args)
 25 	if not args then
 26 		args = getArgs(frame)
 27 	end
 28 	local root = mw.html.create()
 29 	local child = args.child and mw.text.trim(args.child) == 'yes'
 30 
 31 	if not child then
 32 		root = root
 33 			:tag('table')
 34 			:addClass('vertical-navbox')
 35 			:addClass(args.wraplinks ~= 'true' and 'nowraplinks' or nil)
 36 			:addClass(args.bodyclass or args.class)
 37 			:css('float', args.float or 'right')
 38 			:css('clear', (args.float == 'none' and 'both') or args.float or 'right')
 39 			:css('width', args.width or '22.0em')
 40 			:css('margin', args.float == 'left' and '0 1.0em 1.0em 0' or '0 0 1.0em 1.0em')
 41 			:css('background', '#f9f9f9')
 42 			:css('border', '1px solid #aaa')
 43 			:css('padding', '0.2em')
 44 			:css('border-spacing', '0.4em 0')
 45 			:css('text-align', 'center')
 46 			:css('line-height', '1.4em')
 47 			:css('font-size', '88%')
 48 			:cssText(args.bodystyle or args.style)
 49 
 50 		if args.outertitle then
 51 			root
 52 				:tag('caption')
 53 					:addClass(args.outertitleclass)
 54 					:css('padding-bottom', '0.2em')
 55 					:css('font-size', '125%')
 56 					:css('line-height', '1.2em')
 57 					:css('font-weight', 'bold')
 58 					:cssText(args.outertitlestyle)
 59 					:wikitext(args.outertitle)
 60 		end
 61 
 62 		if args.topimage then
 63 			local imageCell = root:tag('tr'):tag('td')
 64 
 65 			imageCell
 66 				:addClass(args.topimageclass)
 67 				:css('padding', '0.4em 0')
 68 				:cssText(args.topimagestyle)
 69 				:wikitext(args.topimage)
 70 
 71 			if args.topcaption then
 72 				imageCell
 73 					:tag('div')
 74 						:css('padding-top', '0.2em')
 75 						:css('line-height', '1.2em')
 76 						:cssText(args.topcaptionstyle)
 77 						:wikitext(args.topcaption)
 78 			end
 79 		end
 80 
 81 		if args.pretitle then
 82 			root
 83 				:tag('tr')
 84 					:tag('td')
 85 						:addClass(args.pretitleclass)
 86 						:cssText(args.basestyle)
 87 						:css('padding-top', args.topimage and '0.2em' or '0.4em')
 88 						:css('line-height', '1.2em')
 89 						:cssText(args.pretitlestyle)
 90 						:wikitext(args.pretitle)
 91 		end
 92 
 93 	end
 94 
 95 	if args.title then
 96 		if child then
 97 			root
 98 				:wikitext(args.title)
 99 				:wikitext('</th></tr>') -- @todo replace this with unclosed again once mw.html gets it
100 		else
101 			root
102 				:tag('tr')
103 					:tag('th')
104 						:addClass(args.titleclass)
105 						:cssText(args.basestyle)
106 						:css('padding', '0.2em 0.4em 0.2em')
107 						:css('padding-top', args.pretitle and 0)
108 						:css('font-size', '145%')
109 						:css('line-height', '1.2em')
110 						:cssText(args.titlestyle)
111 						:wikitext(args.title)
112 		end
113 	end
114 
115 	if args.image then
116 		local imageCell = root:tag('tr'):tag('td')
117 
118 		imageCell
119 			:addClass(args.imageclass)
120 			:css('padding', '0.2em 0 0.4em')
121 			:cssText(args.imagestyle)
122 			:wikitext(args.image)
123 
124 		if args.caption then
125 			imageCell
126 				:tag('div')
127 					:css('padding-top', '0.2em')
128 					:css('line-height', '1.2em')
129 					:cssText(args.captionstyle)
130 					:wikitext(args.caption)
131 		end
132 	end
133 
134 	if args.above then
135 		root
136 			:tag('tr')
137 				:tag('td')
138 					:addClass(args.aboveclass)
139 					:css('padding', '0.3em 0.4em 0.3em')
140 					:css('font-weight', 'bold')
141 					:cssText(args.abovestyle)
142 					:newline() -- newline required for bullet-points to work
143 					:wikitext(args.above)
144 	end
145 
146 	local rowNums = {}
147 	for k, v in pairs(args) do
148 		k = '' .. k
149 		local num = k:match('^heading(%d+)$') or k:match('^content(%d+)$')
150 		if num then table.insert(rowNums, tonumber(num)) end
151 	end
152 	table.sort(rowNums)
153 	-- remove duplicates from the list (e.g. 3 will be duplicated if both heading3 and content3 are specified)
154 	for i = #rowNums, 1, -1 do
155 		if rowNums[i] == rowNums[i - 1] then
156 			table.remove(rowNums, i)
157 		end
158 	end
159 
160 	for i, num in ipairs(rowNums) do
161 		local heading = args['heading' .. num]
162 		if heading then
163 			root
164 				:tag('tr')
165 					:tag('th')
166 						:addClass(args.headingclass)
167 						:css('padding', '0.1em')
168 						:cssText(args.basestyle)
169 						:cssText(args.headingstyle)
170 						:cssText(args['heading' .. num .. 'style'])
171 						:newline()
172 						:wikitext(heading)
173 		end
174 
175 		local content = args['content' .. num]
176 		if content then
177 			root
178 				:tag('tr')
179 					:tag('td')
180 						:addClass(args.contentclass)
181 						:css('padding', '0 0.1em 0.4em')
182 						:cssText(args.contentstyle)
183 						:cssText(args['content' .. num .. 'style'])
184 						:newline()
185 						:wikitext(content)
186 						:done()
187 					:newline() -- Without a linebreak after the </td>, a nested list like "* {{hlist| ...}}" doesn't parse correctly.
188 		end
189 	end
190 
191 	if args.below then
192 		root
193 			:tag('tr')
194 				:tag('td')
195 					:addClass(args.belowclass)
196 					:css('padding', '0.3em 0.4em 0.3em')
197 					:css('font-weight', 'bold')
198 					:cssText(args.belowstyle)
199 					:newline()
200 					:wikitext(args.below)
201 	end
202 
203 	if not child then
204 		local navbarArg = args.navbar or args.tnavbar
205 		if navbarArg ~= 'none' and navbarArg ~= 'off' and (args.name or frame:getParent():getTitle():gsub('/sandbox$', '') ~= 'Template:Sidebar') then
206 			root
207 				:tag('tr')
208 					:tag('td')
209 						:css('text-align', 'right')
210 						:css('font-size', '115%')
211 						:cssText(args.navbarstyle or args.tnavbarstyle)
212 						:wikitext(navbar{
213 							args.name,
214 							mini = 1,
215 							fontstyle = args.navbarfontstyle or args.tnavbarfontstyle
216 						})
217 		end
218 	end
219 
220 	return tostring(root)
221 end
222 
223 function p.collapsible(frame)
224 	local args = getArgs(frame)
225 
226 	args.abovestyle = 'border-top: 1px solid #aaa; border-bottom: 1px solid #aaa;' .. (args.abovestyle or '')
227 	args.belowstyle = 'border-top: 1px solid #aaa; border-bottom: 1px solid #aaa;' .. (args.belowstyle or '')
228 	args.navbarstyle = 'padding-top: 0.6em;' .. (args.navbarstyle or args.tnavbarstyle or '')
229 	if not args.name and frame:getParent():getTitle():gsub('/sandbox$', '') == 'Template:Sidebar with collapsible lists' then
230 		args.navbar = 'none'
231 	end
232 
233 	local contentArgs = {}
234 
235 	for k, v in pairs(args) do
236 		local num = string.match(k, '^list(%d+)$')
237 		if num then
238 			local expand = args.expanded and (args.expanded == 'all' or args.expanded == args['list' .. num .. 'name'])
239 
240 			local row = mw.html.create('div')
241 			row
242 				:addClass('NavFrame')
243 				:addClass((not expand) and 'collapsed' or nil)
244 				:css('border', 'none')
245 				:css('padding', 0)
246 				:cssText(args.listframestyle)
247 				:cssText(args['list' .. num .. 'framestyle'])
248 				:tag('div')
249 					:addClass('NavHead')
250 					:addClass(args.listtitleclass)
251 					:css('font-size', '105%')
252 					:css('background', 'transparent')
253 					:css('text-align', 'left')
254 					:cssText(args.basestyle)
255 					:cssText(args.listtitlestyle)
256 					:cssText(args['list' .. num .. 'titlestyle'])
257 					:wikitext(trimAndAddAutomaticNewline(args['list' .. num .. 'title'] or 'List'))
258 					:done()
259 				:tag('div')
260 					:addClass('NavContent')
261 					:addClass(args.listclass)
262 					:addClass(args['list' .. num .. 'class'])
263 					:css('font-size', '105%')
264 					:css('padding', '0.2em 0 0.4em')
265 					:css('text-align', 'center')
266 					:cssText(args.liststyle)
267 					:cssText(args['list' .. num .. 'style'])
268 					:wikitext(trimAndAddAutomaticNewline(args['list' .. num]))
269 
270 			contentArgs['content' .. num] = tostring(row)
271 		end
272 	end
273 
274 	for k, v in pairs(contentArgs) do
275 		args[k] = v
276 	end
277 
278 	return p.sidebar(frame, args)
279 end
280 
281 return p