Module:Tabs: Difference between revisions

Want an adless experience? Log in or Create an account.
better idea for transclude
(transclude from main namespace)
(better idea for transclude)
Line 176: Line 176:
   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
Line 260: Line 243:
   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 = tostring( editlink ) .. mw.getCurrentFrame():expandTemplate{ title = ':' .. template }
        }
      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