Module:OOO/Variants: Difference between revisions

No edit summary
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
-- Module:OOO/Variants
-- Renders a TabberNeue block of OOOInfoBox variants, wrapped so it aligns with the infobox.
-- Usage:
--  {{OOOVariants|400162|400163|400113|400169|400170}}                    -- scroll is default
--  {{OOOVariants|400162|400163|...|mode=wrap}}                          -- opt-in wrap
--  {{OOOVariants | label1=2 m | art1=400162 | width=300 | align=left }}  -- still works
--
-- Global params:
--  |domain=Buildings      -- bucket (default: Buildings)
--  |align=right|left|none|center
--  |width=300              -- px; applies to wrapper + OOOInfoBox (240–600)
--  |mode=scroll|wrap      -- default: scroll (adds class used by gadget)
--
-- Per-tab overrides (append N):
--  |labelN=... |imageN=... |descriptionN=... |dimensionsN=... |classN=...
local DATA = require('Module:OOO/Data')
local DATA = require('Module:OOO/Data')
local p = {}
local p = {}
local PASSTHROUGH = { 'image', 'description', 'dimensions', 'class' }
local function tostring_or_nil(v)
  if v == nil then return nil end
  return tostring(v)
end
local function safe_label(s)
  s = tostring(s or '')
  s = s:gsub('|', '|'):gsub('=', '=')
  return s
end


local function label_for(domain, art, given)
local function label_for(domain, art, given)
Line 8: Line 38:
end
end


-- allow per-tab overrides like image3=..., description2=..., dimensions4=...
function p.tabs(frame)
local passthrough_keys = { 'image', 'description', 'dimensions', 'class' }
  local parent = frame:getParent() or frame
  local args  = parent.args or {}


function p.tabs(frame)
   local domain = tostring_or_nil(args.domain or args.type) or 'Buildings'
   local args    = (frame:getParent() or frame).args or {}
   local align = tostring_or_nil(args.align) or 'right'
   local domain = args.domain or args.type or 'Buildings'
   local mode   = tostring_or_nil(args.mode) or 'scroll'   -- DEFAULT: scroll
   local align   = args.align or 'right'
  local width  = tonumber(args.width)


   local entries, i = {}, 1
  -- Build each tab entry
   local entries = {}
  local i = 1
   while true do
   while true do
     local art = args['art'..i] or args[i]
     local art = args['art'..i] or args[i]
Line 23: Line 56:
     local label = label_for(domain, art, args['label'..i] or args['name'..i])
     local label = label_for(domain, art, args['label'..i] or args['name'..i])


     -- Build args for OOOInfoBox
     -- Args forwarded to OOOInfoBox
     local ibx = { art = art, align = align, type = domain }
     local ibx = {
     for _, key in ipairs(passthrough_keys) do
      art   = art,
      type  = domain,
      align = align,
    }
    if width and width >= 240 and width <= 600 then
      ibx.width = width
    end
     for _, key in ipairs(PASSTHROUGH) do
       local v = args[key..i]
       local v = args[key..i]
       if v and v ~= '' then ibx[key] = v end
       if v and v ~= '' then ibx[key] = v end
Line 31: Line 71:


     local content = frame:expandTemplate{ title = 'OOOInfoBox', args = ibx }
     local content = frame:expandTemplate{ title = 'OOOInfoBox', args = ibx }
    entries[#entries+1] = string.format('%s=\n%s\n', safe_label(label), content)


    -- Collect TabberNeue wikitext part: <label>=<content>
    entries[#entries+1] = string.format('%s=\n%s\n', label, content)
     i = i + 1
     i = i + 1
   end
   end
Line 41: Line 80:
   end
   end


  -- Force the tabber tag to be parsed by MediaWiki
   local inner  = table.concat(entries, '|-|\n')
   local inner  = table.concat(entries, '|-|\n')
   local tabber = frame:extensionTag('tabber', inner)
   local tabber = frame:extensionTag('tabber', inner)


   -- Wrap & attach styles
   -- Wrapper around tabber so it floats with the infobox and gets our styles
   local html = '<div class="ooo-infobox-tabwrap">' .. tabber .. '</div>'
   local wrap = mw.html.create('div'):addClass('ooo-infobox-tabwrap')
  if align == 'left' then
    wrap:addClass('ooo-infobox-tabwrap--left')
  elseif align == 'none' then
    wrap:addClass('ooo-infobox-tabwrap--none')
  elseif align == 'center' then
    wrap:addClass('ooo-infobox-tabwrap--center')
  end
 
  -- Scroll is default; only skip when explicitly |mode=wrap
  if mode ~= 'wrap' then
    wrap:addClass('ooo-infobox-tabwrap--scroll')
  end
 
  if width and width >= 240 and width <= 600 then
    wrap:css('width', tostring(width) .. 'px')
  end
 
  wrap:wikitext(tabber)
 
  -- Attach wrapper TemplateStyles
  local html = tostring(wrap)
   html = html .. frame:extensionTag('templatestyles', '', { src = 'Template:OOOInfoBoxTabber/styles.css' })
   html = html .. frame:extensionTag('templatestyles', '', { src = 'Template:OOOInfoBoxTabber/styles.css' })
  -- Optional debug comment
  if tostring(args.debug) == '1' then
    html = html .. string.format('<!-- OOOVariants: domain=%s, align=%s, mode=%s, width=%s, tabs=%d -->',
      domain, align, mode, width or 'default', #entries)
  end
   return html
   return html
end
end


return p
return p