FANDOM


-- <nowiki>
--
-- Implements {{navbox}}
--
 
local p = {}
 
local tnavbar = require( 'Module:Tnavbar' )
local yesno = require( 'Module:Yesno' )
 
local function insertRow( tbl )
    return tbl
        :tag( 'tr' )
end
 
local function createTbl( args )
 
    local tbl = mw.html.create( 'table' )
 
    tbl
        :addClass( yesno( args.sublist ) and 'navbox-sublist' or 'navbox' )
        :addClass( 'nowraplinks' )
 
    if not yesno( args.sublist ) and
        ( args.state == 'collapsed' or
          args.state == 'uncollapsed' or
          args.state == 'autocollapse' or
          -- defaults to autocollapse
          args.state == nil )
    then
        tbl
            :addClass( 'mw-collapsible' )
 
        if args.state ~= 'collapsed' then
            tbl
                :addClass( 'mw-uncollapsed' )
        end
    end
 
    tbl
        :css( 'width', '100%' )
 
    if yesno( args.collapsible ) then
        tbl
            :css( {
                border = '0',
                ['margin-top'] = '-2px'
            } )
    end
 
    if args.style then
        tbl
            :cssText( args.style )
    end
 
    -- manually set collapse/expand messages
    -- bug causing the default database messages to be used
    tbl
        :attr( {
            ['data-expandtext'] = 'show',
            ['data-collapsetext'] = 'hide'
        } )
 
    return tbl
 
end
 
local function navbar( args )
    return tnavbar._collapsible( { [1] = args.title, [2] = args.name } )
end
 
local function header( tbl, args )
 
    local div = insertRow( tbl )
        :tag( 'th' )
            :attr( 'colspan', '2' )
            :addClass( 'navbox-title' )
                :tag( 'div' )
 
    -- @todo move this css to site css so we can simplify this (hook off a class)
    -- to something like div:wikitext( args.name and navbar( args ) or args.title )
    -- which can be appended to the above and returned straight away
    if args.name then
        div
            :css( 'padding-right', args.state == 'plain' and '6em' or '0' )
            :wikitext( navbar( args ) )
    else
        div
            :css( 'padding-left', args.state == 'plain' and '0' or '6em' )
            :wikitext( args.title )
    end
 
    return div
        :allDone()
 
end
 
--[[ by @AemonTargaryen
  introduce group-style, odd-style, even-style from old-fashioned Template:Navbox generic
]]
local function row(tbl, group, list, gtype, gstyle, style)
 
    local tr = insertRow( tbl )
    local td
 
    if group then
      local gsty = gstyle
      if gsty == nil then
        gsty = "width: 15%;"
      end
      td = tr:tag('td')
               :addClass('navbox-list')
               :cssText(gsty)
               :wikitext(group)
               :done()
             :tag('td')
    else
        td = tr
            :tag( 'td' )
                :attr( 'colspan', '2' )
    end
 
    --[[
       Experimental list styling
       This is unlikely to be implemented in the near future due to it requiring extra css to work
       and wikiamobile currently not supporting that css.
       As an example, it lets you do the following instead if using {{*}} all the time
       | list3 =
       * {{plink|foo}}
       * {{plink|bar}}
       * {{plink|baz}}
    ]]
    if mw.ustring.match( list, '^%s*%*' ) then
        td
            :newline()
    end
 
    td
        :addClass( 'navbox-list' )
        :wikitext( list )
 
    if gtype and mw.ustring.lower( gtype ) == 'sublist' then
        td
            :css( {
                padding = '0',
                ['border-bottom'] = '0'
            } )
    end
 
    if style then
      td:cssText(style)
    end
 
    return td
        :allDone()
 
end
 
local function below(tbl, args)
  local tr = insertRow(tbl)
    :tag('td')
      :attr('colspan', '2')
      :wikitext(args.below)
  if args.belowStyle then
    tr:cssText(args.belowStyle)
  end
 
  return tr:allDone()
end
 
local function footer( tbl, args )
 
    local th = insertRow( tbl )
        :tag( 'th' )
            :attr( 'colspan', '2' )
            :addClass( 'navbox-footer' )
            :wikitext( args.footer )
 
    if args.fstyle then
        th
            :cssText( args.fstyle )
    end
 
    return th
        :allDone()
 
end
 
local function categories()
 
    local title = mw.title.getCurrentTitle()
    local page = title.text
    local ns = title.nsText
 
    if ns == 'Template' then
        return '[[Category:Navigational templates|' .. page .. ']]'
    else
        return ''
    end
 
end
 
local function docs( frame, args )
 
    local title = mw.title.getCurrentTitle()
    local base = title.baseText
    local ns = title.nsText
 
    if base ~= 'Navbox' and
        ns == 'Template' and
        not yesno( args.sublist ) and
        not yesno( args.collapsible ) and
        ( args.doc == nil or yesno( args.doc ) )
    then
        return frame:expandTemplate{ title = 'Navbox/doc' }
    else
        return ''
    end
 
end
 
function p.navbox( frame )
 
    local parent = frame:getParent()
    local _args = parent.args
    local args = {}
    local j
 
    for k, v in pairs( _args ) do
        if v ~= '' then
            args[k] = v
        end
    end
 
    local tbl = createTbl( args )
 
    if not yesno( args.sublist ) then
        tbl = header( tbl, args )
    end
 
    for i = 1, 20 do
      --[[ by @AemonTargaryen
        add separating tr
      ]]
      tbl:tag('tr')
           :css('height', '1px')
           :allDone()
 
        j = tostring( i )
 
        if args['list' .. j] then
          --[[ by @AemonTargaryen
            introduce group-style, odd-style, even-style of the old version of Template:Navbox generic
          ]]
          local style = args['style' .. j]
          if style == nil then
            if math.mod(j, 2) == 1 then
              style = args['odd-style']
            else
              style = args['even-style']
            end
          end
          tbl = row(tbl, args['group' .. j], args['list' .. j], args['gtype' .. j], args['group-style'], style)
        else
          break
        end
    end
 
    if args.below then
      tbl = below(tbl, args)
    end
 
    if args.footer then
        tbl = footer( tbl, args )
    end
 
    tbl = tostring( tbl )
 
    local cats = categories()
    local docs = docs( frame, args )
 
    return tbl .. cats .. docs
 
end
 
return p
除了特别提示,社区内容遵循CC-BY-SA 授权许可。