Module:Tabs

From Zelda Dungeon Wiki
Revision as of 00:09, June 23, 2020 by Locke (talk | contribs) (for tabcontent, read default from the id arg since it doesn't make sense to have an id in that case)
Jump to navigation Jump to search
Want an adless experience? Log in or Create an account.

Documentation for this module may be created at Module:Tabs/doc

function getRequiredArg( args, argName, fnName )
  return assert( args[argName], 'missing required arg for ' .. fnName .. ': ' .. argName )
end

local TabContainer = {}
TabContainer.__index = TabContainer

function TabContainer.new( args )
  return setmetatable( {
    id = getRequiredArg( args, 'id', 'TabContainer.new' ),
    content = args[1],
    tabsTop = args.top,
    tabsLeft = args.left,
    args = args
  }, TabContainer )
end

function TabContainer:render()
  local container = mw.html.create( 'div' )
    :attr( 'id', self.id )
    :addClass( 'zdw-tabcontainer zdw-box' )
  if self.args.width then container:css( 'width', self.args.width .. 'px' ) end
  if self.args.height then container:css( 'height', self.args.height .. 'px' ) end
  if self.tabsLeft then
    local width = tonumber( self.args.vtabwidth ) or 60
    container:addClass( 'zdw-tabcontainer--hastabsleft' )
    container:css( 'margin-left', tostring( width ) .. 'px' )
    container:tag( 'div' )
      :addClass( 'zdw-tabcontainer__tabset--left' )
      :css( 'width', tostring( width ) .. 'px' )
      :css( 'margin-left', '-' .. tostring( width + 10 ) .. 'px' )
      :wikitext( self.tabsLeft )
  end
  if self.tabsTop then
    container:addClass( 'zdw-tabcontainer--hastabstop' )
    container:tag( 'div' )
      :addClass( 'zdw-tabcontainer__tabset--top' )
      :wikitext( self.tabsTop )
  end
  container:wikitext( self.content )
  container:tag( 'div' )
    :css( 'clear', 'both' )

  return tostring( container )
end

local TabSet = {}
TabSet.__index = TabSet

function TabSet.new( args )
  return setmetatable( {
    target = getRequiredArg( args, 'target', 'TabSet.new' ),
    selector = args.selector and (tonumber(args.selector) or error('invalid arg: selector must be a number')) or 0,
    activation = args.activation or 'click',
    tabs = getRequiredArg( args, 1, 'TabSet.new' )
  }, TabSet )
end

function TabSet:render()
  local tabSet = mw.html.create( 'ul' )
    :addClass( 'zdw-tabset' )
    :attr( 'data-tab-target', self.target )
    :attr( 'data-tab-selector', self.selector )
    :attr( 'data-tab-type', self.activation )
    :wikitext( self.tabs )

  return tostring( tabSet )
end

local Tab = {}
Tab.__index = Tab

function Tab.new( args )
  return setmetatable( {
    selection = getRequiredArg( args, 1, 'Tab.new' ),
    label = args[2] or args[1],
    args = args
  }, Tab )
end

function Tab:render()
  local tab = mw.html.create( 'li' )
    :addClass( 'zdw-tab' )
    :attr( 'data-tab-selection', self.selection:gsub( "%s", "" ) )
    :wikitext( self.label )

  if self.args.default then
    tab:attr( 'data-tab-default', 'true' )
  end

  return tostring( tab )
end

local TabContent = {}
TabContent.__index = TabContent

function TabContent.new( args )
  return setmetatable( {
    contentId = getRequiredArg( args, 1, 'TabContent.new' ),
    content = args[2] or args[1],
    args = args
  }, TabContent )
end

function TabContent:render()
  local content = mw.html.create( 'div' )
    :addClass( 'zdw-tabcontent' )
    :attr( 'data-tab-content', self.contentId )
    :wikitext( self.content )

  if self.contentId == 'default' then
    content:addClass( 'default' )
  end

  if self.args.width then
    content:css( 'width', self.args.width .. 'px' )
  end

  return tostring( content )
end

local p = {}

p.TabContainer = TabContainer
p.TabSet = TabSet
p.Tab = Tab
p.TabContent = TabContent

function p.tabs( frame )
  local tabs = TabContainer.new( frame.args )
  return tabs:render()
end

function p.tabset( frame )
  local tabset = TabSet.new( frame.args )
  return tabset:render()
end

function p.tab( frame )
  local tab = Tab.new( frame.args )
  return tab:render()
end

function p.content( frame )
  local content = TabContent.new( frame.args )
  return content:render()
end

return p