Module:Tabs: Difference between revisions

Jump to navigation Jump to search
Want an adless experience? Log in or Create an account.
(use the right variables...)
(split contentId into separate attributes for each selector, part 2 (try to handle defaults correctly))
Line 47: Line 47:
function TabContainer:addTabLeftWithContent( args )
function TabContainer:addTabLeftWithContent( args )
   -- normalize args so they can just be passed to everything. definitely a bad idea but...
   -- normalize args so they can just be passed to everything. definitely a bad idea but...
   args.selection = getRequiredArg( args, 'contentId', 'TabContainer:addTabLeft' )
   args.selection = getRequiredArg( args, 'contentId', 'TabContainer:addTabLeftWithContent' )
   self:leftTabs( args ):addTab( args )
   self:leftTabs( args ):addTab( args )
   self:addContent( args )
   self:addContent( args )
Line 54: Line 54:
function TabContainer:addTabTopWithContent( args )
function TabContainer:addTabTopWithContent( args )
   -- normalize args so they can just be passed to everything. definitely a bad idea but...
   -- normalize args so they can just be passed to everything. definitely a bad idea but...
   args.selection = getRequiredArg( args, 'contentId', 'TabContainer:addTabTop' )
   args.selection = getRequiredArg( args, 'contentId', 'TabContainer:addTabTopWithContent' )
   self:topTabs( args ):addTab( args )
   self:topTabs( args ):addTab( args )
   self:addContent( args )
   self:addContent( args )
Line 69: Line 69:
   if self.args.width then container:css( 'width', self.args.width .. 'px' ) end
   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.args.height then container:css( 'height', self.args.height .. 'px' ) end
  local defaults = {}


   if self.tabsLeft then
   if self.tabsLeft then
Line 81: Line 79:
       :css( 'margin-left', '-' .. tostring( width + 10 ) .. 'px' )
       :css( 'margin-left', '-' .. tostring( width + 10 ) .. 'px' )
       :wikitext( self.tabsLeft:render() )
       :wikitext( self.tabsLeft:render() )
    if self.tabsLeft.defaultTab > 0 then
      defaults[self.tabsLeft.selector] = self.tabsLeft.tabs[self.tabsLeft.defaultTab].selection
    end
   end
   end


Line 92: Line 86:
       :addClass( 'zdw-tabcontainer__tabset--top' )
       :addClass( 'zdw-tabcontainer__tabset--top' )
       :wikitext( self.tabsTop:render() )
       :wikitext( self.tabsTop:render() )
    if self.tabsTop.defaultTab > 0 then
      defaults[self.tabsTop.selector] = self.tabsTop.tabs[self.tabsTop.defaultTab].selection
    end
   end
   end


Line 103: Line 93:


   for _, c in ipairs( self.contents ) do
   for _, c in ipairs( self.contents ) do
     if c.contentId == defaultContent then c.default = true end
     if self.tabsLeft and self.tabsLeft.defaultSelection == c.selection
      or self.tabsTop and self.tabsTop.defaultSelection == c.selection
      or type( c.selection ) == 'table'
        and self.tabsLeft and self.tabsLeft.defaultSelection == c.selection.left
        and self.tabsTop and self.tabsTop.defaultSelection == c.selection.top then
      c.default = true
    end
     container:wikitext( c:render() )
     container:wikitext( c:render() )
   end
   end
Line 126: Line 122:
function TabSet:addTab( args )
function TabSet:addTab( args )
   local index = #self.tabs + 1
   local index = #self.tabs + 1
   if index == self.defaultTab then args.default = true end
   if index == self.defaultTab then
    self.defaultSelection = args.selection
    args.default = true
  end
   self.tabs[index] = Tab.new( args )
   self.tabs[index] = Tab.new( args )
end
end
Line 170: Line 169:
   args = args or {}
   args = args or {}
   return setmetatable( {
   return setmetatable( {
     contentId = getRequiredArg( args, 'contentId', 'TabContent.new' ),
     selection = getRequiredArg( args, 'selection', 'TabContent.new' ),
     content = args.content or args.contentId,
     content = args.content or args.selection,
     args = args
     args = args
   }, TabContent )
   }, TabContent )
Line 179: Line 178:
   local content = mw.html.create( 'div' )
   local content = mw.html.create( 'div' )
     :addClass( 'zdw-tabcontent' )
     :addClass( 'zdw-tabcontent' )
    :attr( 'data-tab-content', self.contentId )
     :wikitext( '\n' .. self.content ) -- newline is needed for tables, lists, etc.
     :wikitext( '\n' .. self.content ) -- newline is needed for tables, lists, etc.


   for k, v in ipairs( mw.text.split( self.contentId, ' ' ) ) do
   if type( self.selection ) == 'table' then
    content:attr( 'data-tab-content-' .. (k - 1), v )
    for k, v in pairs( self.selection ) do
      content:attr( 'data-tab-content-' .. k, v )
    end
  else
    content:attr( 'data-tab-content', v )
   end
   end


Line 206: Line 208:


   -- set selectors
   -- set selectors
   if args.left then args.left.selector = '0' end
   if args.left then args.left.selector = args.top and not args.combine and 'left' or nil end
   if args.top then args.top.selector = args.combine and '0' or '1' end
   if args.top then args.top.selector = args.left and not args.combine and 'top' or nil end


   -- fix default tabs if combined
   -- fix default tabs if combined
Line 243: Line 245:
   end
   end
    
    
   for contentId, contentArgs in pairs( args.content or {} ) do -- order doesn't matter here since only one is displayed at a time
   for contentSelection, contentArgs in pairs( args.content or {} ) do -- order doesn't matter here since only one is displayed at a time
    local leftSelection, topSelection = string.match( contentSelection, '(.+) (.+)' )
     contentArgs = Args.getTable( contentArgs )
     contentArgs = Args.getTable( contentArgs )
     contentArgs.contentId = contentId
     contentArgs.selection = leftSelection and { left = leftSelection, top = topSelection } or contentSelection
     contentArgs.content = Args.getValue( contentArgs )
     contentArgs.content = Args.getValue( contentArgs )
     tabs:addContent( contentArgs )
     tabs:addContent( contentArgs )