Module:Tabs: Difference between revisions

Want an adless experience? Log in or Create an account.
1,650 bytes added ,  July 26, 2020
I knew the logic was bad but not that bad. breaking it into two ifs for clarity
mNo edit summary
(I knew the logic was bad but not that bad. breaking it into two ifs for clarity)
 
(13 intermediate revisions by the same user not shown)
Line 74: Line 74:
     container:addClass( 'zdw-tabcontainer--hastabsleft' )
     container:addClass( 'zdw-tabcontainer--hastabsleft' )
     container:css( 'margin-left', tostring( width ) .. 'px' )
     container:css( 'margin-left', tostring( width ) .. 'px' )
     container:tag( 'div' )
     local left = container:tag( 'div' )
       :addClass( 'zdw-tabcontainer__tabset--left' )
       :addClass( 'zdw-tabcontainer__tabset--left' )
       :css( 'width', tostring( width ) .. 'px' )
       :css( 'width', tostring( width ) .. 'px' )
       :css( 'margin-left', '-' .. tostring( width + 10 ) .. 'px' )
       :css( 'margin-left', '-' .. tostring( width + 10 ) .. 'px' )
       :wikitext( self.tabsLeft:render() )
       :wikitext( self.tabsLeft:render() )
    if self.args.height then left:css( 'height', self.args.height .. 'px' ) end
   end
   end


   if self.tabsTop then
   if self.tabsTop then
     container:addClass( 'zdw-tabcontainer--hastabstop' )
     container:addClass( 'zdw-tabcontainer--hastabstop' )
     container:tag( 'div' )
     local top = container:tag( 'div' )
       :addClass( 'zdw-tabcontainer__tabset--top' )
       :addClass( 'zdw-tabcontainer__tabset--top' )
       :wikitext( self.tabsTop:render() )
       :wikitext( self.tabsTop:render() )
    if self.args.width then top:css( 'width', self.args.width .. 'px' ) end
   end
   end


   for _, c in ipairs( self.contents ) do
   for _, c in ipairs( self.contents ) do
     if self.tabsLeft and self.tabsLeft.defaultSelection == c.selection
     if type( c.selection ) == 'string'
      or self.tabsTop and self.tabsTop.defaultSelection == c.selection
        and (self.tabsLeft and self.tabsLeft.defaultSelection == c.selection:gsub( "%s", "" )
       or type( c.selection ) == 'table'
        or self.tabsTop and self.tabsTop.defaultSelection == c.selection:gsub( "%s", "" )) then
         and self.tabsLeft and self.tabsLeft.defaultSelection == c.selection.left
       c.default = true
         and self.tabsTop and self.tabsTop.defaultSelection == c.selection.top then
    elseif type( c.selection ) == 'table'
         and self.tabsLeft and self.tabsLeft.defaultSelection == c.selection.left:gsub( "%s", "" )
         and self.tabsTop and self.tabsTop.defaultSelection == c.selection.top:gsub( "%s", "" ) then
       c.default = true
       c.default = true
     end
     end
Line 109: Line 113:
   return setmetatable( {
   return setmetatable( {
     target = args.target,
     target = args.target,
     selector = args.selector or '0',
     selector = args.selector,
     activation = args.activation or 'click',
     activation = args.activation or 'click',
     defaultTab = args.default and (tonumber(args.default) or error('invalid arg: default must be a number')) or 1,
     defaultTab = args.default and (tonumber(args.default) or error('invalid arg: default must be a number')) or 1,
Line 119: Line 123:
   local index = #self.tabs + 1
   local index = #self.tabs + 1
   if index == self.defaultTab then
   if index == self.defaultTab then
     self.defaultSelection = args.selection
     self.defaultSelection = args.selection:gsub( "%s", "" )
     args.default = true
     args.default = true
   end
   end
Line 128: Line 132:
   local tabSet = mw.html.create( 'ul' )
   local tabSet = mw.html.create( 'ul' )
     :addClass( 'zdw-tabset' )
     :addClass( 'zdw-tabset' )
    :attr( 'data-tab-selector', self.selector )
     :attr( 'data-tab-type', self.activation )
     :attr( 'data-tab-type', self.activation )
   if self.target then tabSet:attr( 'data-tab-target', self.target ) end
   if self.target then tabSet:attr( 'data-tab-target', self.target ) end
  if self.selector then tabSet:attr( 'data-tab-selector', self.selector ) end


   for _, tab in ipairs( self.tabs ) do
   for _, tab in ipairs( self.tabs ) do
Line 142: Line 146:
   args = args or {}
   args = args or {}
   return setmetatable( {
   return setmetatable( {
     selection = getRequiredArg( args, 'selection', 'Tab.new' ),
     selection = getRequiredArg( args, 'selection', 'Tab.new' ):gsub( "%s", "" ),
     label = args.label or args.selection,
     label = args.label or args.selection,
     args = args
     args = args
Line 151: Line 155:
   local tab = mw.html.create( 'li' )
   local tab = mw.html.create( 'li' )
     :addClass( 'zdw-tab' )
     :addClass( 'zdw-tab' )
     :attr( 'data-tab-selection', self.selection:gsub( "%s", "" ) )
     :attr( 'data-tab-selection', self.selection )
     :wikitext( self.label )
     :wikitext( self.label )


Line 178: Line 182:
   if type( self.selection ) == 'table' then
   if type( self.selection ) == 'table' then
     for k, v in pairs( self.selection ) do
     for k, v in pairs( self.selection ) do
       content:attr( 'data-tab-content-' .. k, v )
       content:attr( 'data-tab-content-' .. k, v:gsub( "%s", "" ) )
     end
     end
   else
   else
     content:attr( 'data-tab-content', self.selection )
     content:attr( 'data-tab-content', self.selection:gsub( "%s", "" ) )
   end
   end


Line 241: Line 245:
   end
   end
    
    
   for contentSelection, contentArgs in pairs( args.content or {} ) do -- order doesn't matter here since only one is displayed at a time
   -- Special case where content is transcluded from subpages rather than provided directly
    local leftSelection, topSelection = string.match( contentSelection, '(.+) (.+)' )
  -- For now, assumes 2D mode (both left and top tabs)
    contentArgs = Args.getTable( contentArgs )
  if Args.getValue( args.content or {} ) == 'transclude' then
    contentArgs.selection = leftSelection and { left = leftSelection, top = topSelection } or contentSelection
    for _, leftArgs in ipairs( args.left ) do
    contentArgs.content = Args.getValue( contentArgs )
      local leftSelection = Args.getValue( leftArgs )
    tabs:addContent( contentArgs )
      for _, topArgs in ipairs( args.top ) do
        local topSelection = Args.getValue( topArgs )
        local template = mw.title.getCurrentTitle().text .. '/' .. leftSelection .. '/' .. topSelection
 
        -- add edit link, aligned to transcluded table's header
        local editlink = mw.html.create( 'span' )
          :addClass( 'edit plainlinks' )
          :css( 'position', 'absolute' ) -- css to align with table header
          :css( 'top', '12px' )
          :css( 'right', '15px' )
          :wikitext( '[' .. tostring( mw.uri.fullUrl( template, { action = 'edit' } ) ) .. ' [edit]]' )
 
        -- add the transcluded content
        tabs:addContent{
          selection = { left = leftSelection, top = topSelection },
          content = mw.getCurrentFrame():expandTemplate{ title = ':' .. template } .. tostring( editlink )
        }
      end
    end
  else -- content is provided by the caller
    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.selection = leftSelection and { left = leftSelection, top = topSelection } or contentSelection
      contentArgs.content = Args.getValue( contentArgs )
      tabs:addContent( contentArgs )
    end
   end
   end