Module:Tabs: Difference between revisions

Want an adless experience? Log in or Create an account.
I knew the logic was bad but not that bad. breaking it into two ifs for clarity
(transclude from main namespace)
(I knew the logic was bad but not that bad. breaking it into two ifs for clarity)
 
(5 intermediate revisions by the same user not shown)
Line 91: Line 91:


   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 176: Line 178:
   local content = mw.html.create( 'div' )
   local content = mw.html.create( 'div' )
     :addClass( 'zdw-tabcontent' )
     :addClass( 'zdw-tabcontent' )
 
     :wikitext( '\n' .. self.content ) -- newline is needed for tables, lists, etc.
  if self.content == 'transclude' then
    local template = mw.title.getCurrentTitle().text .. '/'
     if type( self.selection ) == 'table' then
      template = template .. self.selection.left .. '/' .. self.selection.top
    else
      template = template .. self.selection
    end
    content:tag( 'span' )
      :addClass( 'edit plainlinks' )
      :css( 'position', 'absolute' )
      :css( 'top', '12px' )
      :css( 'right', '15px' )
      :wikitext( '[' .. tostring( mw.uri.fullUrl( template, { action = 'edit' } ) ) .. ' [edit]]' )
    content:wikitext( '\n' .. mw.getCurrentFrame():expandTemplate{ title = ':' .. template } )
  else
    content:wikitext( '\n' .. self.content ) -- newline is needed for tables, lists, etc.
  end


   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 260: 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