Module:Tabs: Difference between revisions

From Zelda Dungeon Wiki
Jump to navigation Jump to search
Want an adless experience? Log in or Create an account.
(implement the other classes)
(adjust some args)
Line 48: Line 48:
     selector = args.selector or args.target,
     selector = args.selector or args.target,
     activation = args.activation or 'click',
     activation = args.activation or 'click',
     tabs = assert( args.tabs, 'missing required arg: tabs' )
     tabs = assert( args[1], 'missing required arg: [1]' )
   }, TabSet )
   }, TabSet )
end
end
Line 67: Line 67:


function Tab.new( args )
function Tab.new( args )
  local obj = {
    args = args
  }
   return setmetatable( {
   return setmetatable( {
     selection = assert( args.selection, 'missing required arg: selection' ),
     selection = assert( args[1], 'missing required arg: [1]' ),
     label = args.label or args.selection
     label = args[2] or args[1]
   }, Tab )
   }, Tab )
end
end
Line 91: Line 87:
function TabContent.new( args )
function TabContent.new( args )
   return setmetatable( {
   return setmetatable( {
     contentId = assert( args.id, 'missing required arg: id' ),
     contentId = assert( args[1], 'missing required arg: [1]' ),
     content = args.content or args.id,
     content = args[2] or args[1],
     args = args
     args = args
   }, TabContent )
   }, TabContent )

Revision as of 21:49, June 22, 2020

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

local TabContainer = {}
TabContainer.__index = TabContainer

function TabContainer.new( args )
  return setmetatable( {
    id = assert( args.id, 'missing required arg: id' ),
    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 tabtarget box tabs2d' )
  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: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 = assert( args.target, 'missing required arg: target' ),
    selector = args.selector or args.target,
    activation = args.activation or 'click',
    tabs = assert( args[1], 'missing required arg: [1]' )
  }, 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 = assert( args[1], 'missing required arg: [1]' ),
    label = args[2] or args[1]
  }, 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 )

  return tostring( tab )
end

local TabContent = {}
TabContent.__index = TabContent

function TabContent.new( args )
  return setmetatable( {
    contentId = assert( args[1], 'missing required arg: [1]' ),
    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.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