Open main menu

Changes

Module:Navbox

1,977 bytes added, 04:36, 1 March 2013
rewrite with htmlbuilder
-- This module will implement {{Navbox}}
--
local p = {}
local HtmlBuilder = require('Module:Sandbox/Toohool/HtmlBuilder')
local gutterRow argslocal tableRowAdded = '<tr style="height:2px;"><td></td></tr>'false
local border
local listnums = {}
local ret = {} function add(...) local args = {...} for i = 1, #args do if args[i] then table.insert(ret, args[i]) end endend
function trim(s)
return (mw.ustring.gsub(s, "^%s*(.-)%s*$", "%1"))
end
function addTableRow(tbl)
-- If any other rows have already been added, then we add a 2px gutter row.
if tableRowAdded then
tbl
.tag('tr')
.css('height', '2px')
.tag('td')
end
tableRowAdded = true
return tbl.tag('tr')
end
 ---- Title row--function renderTitleRow(argstbl)
if not args.title then return end
local titleRow = addTableRow(tbl)
if args.titlegroup then
titleRow
.tag('th')
.attr('scope', 'row')
.addClass('navbox-group')
.addClass(args.titlegroupclass)
.cssText(args.basestyle)
.cssText(args.groupstyle)
.cssText(args.titlegroupstyle)
.wikitext(args.titlegroup)
end
addlocal titleCell = titleRow.tag('<tr>th').attr('scope', 'col')
if args.titlegroup then
add titleCell .css('<th scope="row" class="navboxborder-group ', args.titlegroupclass, '" style="', args.basestyle, ';', args.groupstyle, ';', args.titlegroupstyle, left'">', args.titlegroup, '</th><th scope="col" style="border-left:2px solid #fdfdfd;width:100%;') else add .css('<th scope="col" style="width', '100%')
end
local colspan titleColspan = 2 if args.imageleft then colspan titleColspan = colspan titleColspan + 1 end if args.image then colspan titleColspan = colspan titleColspan + 1 end if args.titlegroup then colspan titleColspan = colspan titleColspan - 1 end add titleCell .cssText(args.basestyle, ';', ) .cssText(args.titlestyle, ) .addClass('" class="navbox-title" ') .attr('colspan=', colspantitleColspan) renderNavBar(titleCell) titleCell .tag('div') .addClass(args.titleclass) .css('font-size', '>110%') .newline() .wikitext(args.title)end function renderNavBar(titleCell) local stateLinkPlaceholder addStateLinkPlaceholder = '<span style="float:right;width:6em;">&nbsp;</span>'false
if args.navbar == 'plain' or args.navbar == 'off' or (not args.name and (border == 'subgroup' or border == 'child' or border == 'none')) then
if args.navbar == 'off' then
if addStateLinkPlaceholder = (args.state == 'plain' then add(stateLinkPlaceholder) end
else
if addStateLinkPlaceholder = (args.state ~= 'plain' then add(stateLinkPlaceholder) end
end
else
if args.name then
addtitleCell.wikitext(mw.getCurrentFrame():expandTemplate{ title = 'navbar', args = {
args.name,
mini = 1,
}})
else
add titleCell .tag('<span class="') .addClass('error" style="') .css('float:', 'left;') .css('white-space:', 'nowrap;">') .wikitext('Error: No name provided</span>') if addStateLinkPlaceholder = (args.state == 'plain' then add(stateLinkPlaceholder) end
end
end
if addStateLinkPlaceholder then
titleCell
.tag('span')
.css('float', 'right')
.css('width', '6em')
.wikitext('&nbsp;')
end
end
add('<div class="', args.titleclass, '" style="font-size:110%;">')
add(args.title)
add('</div></th></tr>')
end
---- Above/Below rows--function renderAboveRow(argstbl)
if not args.above then return end
if args addTableRow(tbl) .title then addtag(gutterRow'td') end add .addClass('<tr><td class="navbox-abovebelow ', ) .addClass(args.aboveclass, '" style="', ) .cssText(args.basestyle, ';', ) .cssText(args.abovestyle, ) .attr('" colspan="', getAboveBelowColspan(args), '">') add .tag('<div>\n', ) .newline() .wikitext(args.above, '</div></td></tr>')
end
 function renderBelowRow(argstbl) if not args.below thenreturn end if args addTableRow(tbl) .title or args.above or #listnums > 0 then addtag(gutterRow'td') end add .addClass('<tr><td class="navbox-abovebelow ', ) .addClass(args.belowclass, '" style="', ) .cssText(args.basestyle, ';', ) .cssText(args.belowstyle, ) .attr('" colspan="', getAboveBelowColspan(args), '">') add .tag('<div>\n', ) .newline() .wikitext(args.below, '</div></td></tr>') end
end
 function getAboveBelowColspan(args)
local ret = 2
if args.imageleft then ret = ret + 1 end
return ret
end
---- List rows--function renderFirstListRow(argstbl)
if not args.list1 then return end
if args.title or args.above then add local row = addTableRow(gutterRowtbl) end add('<tr>')
if args.imageleft then
add row .tag('<td class="') .addClass('navbox-image ', ) .addClass(args.imageclass) .css('width', '" style="width:0%;') .css('padding:', '0px 2px 0px 0px;', ) .cssText(args.imageleftstyle, '"') add .attr(' rowspan=', (2 * #listnums - 1), .tag('><div>\n', ) .newline() .wikitext(args.imageleft, '</div></td>')
end
if args.group1 then
local groupCell = row.tag('th')
groupCell
.attr('scope', 'row')
.addClass('navbox-group')
.addClass(args.groupclass)
.cssText(args.basestyle)
if args.groupwidth then
groupCell.css('width', args.groupwidth)
end
groupCell
.cssText(args.groupstyle)
.cssText(args.group1style)
.wikitext(args.group1)
end
local listCell = row.tag('td')
if args.group1 then
add listCell .css('<th scope="row" class="navboxtext-group align', args.groupclass, '" style="', args.basestyle, ';left') if args .groupwidth then addcss('border-left-width:', args.groupwidth, ';') end add(args.groupstyle, ';', args.group1style, '">2px') add(args .group1, '</th>') addcss('<td style="text-align:left;border-left-width:2px;border-left-style:', 'solid;')
else
add listCell.attr('<td colspan=', 2 style="')
end
if not args.groupwidth then add listCell.css('width:100%;') end add('padding:0px;', args.liststyle, ';', args.oddstyle, ';', args.list1style, '" class="navbox-list navbox-') if args.evenodd == 'swap' then add('even') else add(args.evenodd or 'odd100%')
end
  local evenOdd = args.evenodd or 'odd' addif args.evenodd == 'swap' then evenOdd = 'even' end listCell .css(' padding', '0px') .cssText(args.liststyle) .cssText(args.oddstyle) .cssText(args.list1style) .addClass('navbox-list') .addClass('navbox-' .. evenOdd) .addClass(args.listclass, ) .tag('">div') add .css('<div style="padding:', args.list1padding or args.listpadding or '0em 0.25em', '">\n') add .newline() .wikitext(args.list1) add('</div></td>')
if args.image then
add row .tag('<td class="') .addClass('navbox-image ', ) .addClass(args.imageclass) .css('width', '" style="width:0%;') .css('padding:', '0px 0px 0px 2px;', ) .cssText(args.imagestyle, '" ') add .attr(' rowspan=', (2 * #listnums - 1) .tag('div') .newline() .wikitext(args.image) endend function renderNthListRow(tbl, listnum) local row = addTableRow(tbl) if args['>group' .. listnum] then local groupCell = row.tag('th') add groupCell .attr('<div>\nscope', 'row') .addClass('navbox-group') .addClass(args.imagegroupclass) .cssText(args.basestyle) if args.groupwidth then groupCell.css('width', args.groupwidth) end groupCell .cssText(args.groupstyle) .cssText(args['group' .. listnum .. '</div></td>style']) .wikitext(args['group'.. listnum])
end
addlocal listCell = row.tag('</tr>td')end
function renderNthListRow(args, listnum)
if args.title or args.above or args.list1 then
add(gutterRow)
end
add('<tr>')
if args['group' .. listnum] then
add listCell .css('<th scope="row" class="navboxtext-group align', args.groupclass, '" style="', args.basestyle, ';left') if args .groupwidth then addcss('border-left-width:', args.groupwidth, ';') end add(args.groupstyle, ';', args['group' .. listnum .. 'style'], '">2px') add(args['group' .. listnum]) addcss('</th><td style="text-align:left;border-left-width:2px;border-left-style:', 'solid;')
else
add listCell.attr('<td colspan=', 2 style=") end if not args.groupwidth then listCell.css('width', '100%')
end
if not args.groupwidth then add('width:100%;') end
local isOdd = (listnum % 2) == 1
local rowstyle = args.evenstyle
if isOdd then rowstyle = args.oddstyle end
add('padding:0px;', args.liststyle, ';', rowstyle, ';', args['list' .. listnum .. 'style'], '" ') add('class="navbox-list navbox-') local evenOdd if args.evenodd == 'swap' then if isOdd then add(evenOdd = 'even') else add(evenOdd = 'odd') end else if isOdd then add(evenOdd = args.evenodd or 'odd') else add(evenOdd = args.evenodd or 'even') end end addlistCell .css(' padding', '0px') .cssText(args.listclass, liststyle) .cssText(rowstyle) .cssText(args['list' .. listnum .. '">style']) .addClass('navbox-list') .addClass('navbox-' .. evenOdd) .addClass(args.listclass) add .tag('<div style="') .css('padding:', args.listpadding or '0em 0.25em', '">\n', ) .newline() .wikitext(args['list' .. listnum], '</div></td></tr>')
end
 ---- Main navbox tables--function p._navbox(args) for k, v in pairsrenderMainTable(args) do local listnum tbl = ('' HtmlBuilder.. k):matchcreate('^list(%d+)$table') if listnum then table .insertattr(listnums'cellspacing', tonumber(listnum0)) end end table .sortaddClass(listnums) border = trim(args.border or args[1] or 'nowraplinks') if border == 'subgroup' or border == 'child' then add('</div>') elseif border ~= 'none' then add('<table cellspacing="0" class="navbox" style="border-spacing:0;', args .bodystyle, ';', args.style, '"><tr><td style="padding:2px;">') end addaddClass('<table cellspacing="0" class="nowraplinks ', args.bodyclass, ' ')
if args.title and (args.state ~= 'plain' and args.state ~= 'off') then
add tbl .addClass('collapsible ', ) .addClass(args.state or 'autocollapse', ' ')
end
tbl.css('border-spacing', 0)
if border == 'subgroup' or border == 'child' or border == 'none' then
add tbl .addClass('navbox-subgroup" style="border-spacing:0;', ) .cssText(args.bodystyle, ';', bodyStyle) .cssText(args.style) else-- regular navobx - bodyStyle and style will be applied to the wrapper table add tbl .addClass('navbox-inner" style="border-spacing:0;') .css('background:', 'transparent;') .css('color:', 'inherit')
end
addtbl.cssText(';', args.innerstyle, ';">') renderTitleRow(argstbl) renderAboveRow(argstbl) renderFirstListRow(argstbl)
-- render lists 2 through N
for i, listnum in ipairs(listnums) do
if listnum > 1 then
renderNthListRow(argstbl, listnum)
end
end
renderBelowRow(tbl)
return tbl
end
 
function p._navbox(navboxArgs)
args = navboxArgs
for k, v in pairs(args) do
local listnum = ('' .. k):match('^list(%d+)$')
if listnum then table.insert(listnums, tonumber(listnum)) end
end
table.sort(listnums)
border = trim(args.border or args[1] or '')
renderBelowRow -- render the main body of the navbox local tbl = renderMainTable(args)
add -- render the appropriate wrapper around the navbox, depending on the border param local res = HtmlBuilder.create('</table>')
if border == 'subgroup' or border == 'child' then
addres .tag('</div>', {unclosed = true}) .done() .node(tbl) .tag('div', {unclosed = true})
elseif border ~= 'none' then
add res .tag('table') .attr('cellspacing', 0) .addClass('navbox') .css('border-spacing', 0) .cssText(args.bodystyle) .cssText(args.style) .tag('tr') .tag('</td></tr></table>') .css('padding', '2px') .node(tbl)
end
-- TODO: add tracking categories
return table.concattostring(ret, ''res)
end
function p.navbox(frame)
return p._navbox(frame:getParent().args)
end
return p
Anonymous user