Module:InfoboxHelper

From Noita Wiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:InfoboxHelper/doc

local ibh = {};

local ytils = require('Module:Y_util');

local ytbl,yxml = ytils.table, ytils.xml;
local isStrExist = ytils.string.isStrExist;

local UID = "";for i=1,5 do local N=math.random(0x41,0x74);N=(N<0x5B)and(N)or(N+6);UID=UID..string.char(N);end

local collap_ct = 0;
function ibh.opts_handle( opts, attr )
  if( attr == nil ) then attr={}; end
  if( type(opts) == "table" ) then
    for _,v in ipairs({"class", "id", "style"}) do
      if( attr[v] == nil) then attr[v]={}; end
      if( opts[v] ~= nil ) then ytbl.merge( opts[v], attr[v] ); end
    end
    local test = ytbl.vtok_m(opts);
    if( test["collapse_btn"] ) then collap_ct = collap_ct + 1;
      table.insert( attr["class"], "mw-customtoggle-ibx"..UID.."-"..collap_ct ); end
    if( test["columnize"] ) then table.insert( attr["class"], "ib-columnize" ); end
    if( test["columnize_right"] ) then table.insert( attr["class"], "ib-columnize-right" ); end
    if( opts["columns"] ) then local n = tonumber(opts["columns"]); if( n ~= nil ) then -- CSS variable override
      table.insert( attr["style"], "--ib-columnize-flex:"..tostring(math.floor(100/n)-1).."%" ); end end
  end
  return attr;
end
function ibh.collapse( opts, content )
  local attribs = ibh.opts_handle( opts, { ["class"]={"mw-collapsible"},["id"]={"mw-customcollapsible-ibx"..UID.."-"..collap_ct} } );
  return yxml.tagGen( "div", attribs, content );
end
function ibh.group( opts, content )
  local attribs = ibh.opts_handle( opts, { ["class"]={"ib-group","tabs-group"} } );
  return yxml.tagGen( "div", attribs, content );
end
function ibh.header( opts, content )
  local attribs = ibh.opts_handle( opts, { ["class"]={"ib-header"} } );
  return yxml.tagGen( "div", attribs, content );
end

function ibh.vinfobox( vadata, opts, content )
  local float = "float:"..(vadata.float or "right")..";";
  local clear = "clear:"..(vadata.clear or "right")..";";
  local width = "width:"..(vadata.width or "30em" )..";";

  local attribsContainer = { ["class"]={"ib-infobox-container"}, ["style"]={"resize:horizontal;overflow:hidden;"} };
  local attribsCorner = { ["style"]={"position:absolute;background-color:#b19c72cc;width:1em;height:1em;margin:-0.25em;bottom:0;"} };
  table.insert( attribsContainer.style, float..clear..width );
  if( vadata.float=="left" ) then
    table.insert( attribsCorner.style, "right:0;border-radius:0.75em 1em 1em 1em;" );
  else -- right or null (which default to right)
    table.insert( attribsContainer.style, "direction:rtl;" );
    table.insert( attribsCorner.style, "left:0;border-radius:1em 0.75em 1em 1em;" );
  end

  local attribsBox = ibh.opts_handle( opts, { ["class"]={"ib-infobox","tabs-container"}, ["style"]={"direction:initial;grid-template-columns:auto auto;"} } );
  return yxml.tagGen( "div", attribsContainer, {
    yxml.tagGen( "dl", attribsBox, content ),
    yxml.tagGen( "div", attribsCorner, "" )
  });
end

function ibh.vtabs_parse( vadata, opts, properties )
  local attribs = ibh.opts_handle( opts, { ["class"]={"ib-tabs"}, ["style"]={"overflow:hidden;"} } );

  local res = {};
  local tabs_ct = vadata.tab_count;
  if( tabs_ct > 1 ) then
    for iter=1, tabs_ct do
      local content = {};
      for _,prop_v in pairs( properties ) do
        if( vadata.v_exists[prop_v] ~= nil ) then
          table.insert( content, formatVar( vadata, prop_v, iter, "tab" ) );
        end
      end
      content = table.concat( content, " " );
      table.insert( res, yxml.tagGen( "div", {
        ["id"]={ formatVar( vadata, "name", iter ):gsub(" ","_") }, -- anchor
        ["class"]={ "tabs-btn", "tabs-id-"..iter } }, content ) );
      iter = iter + 1;
    end
    table.insert( res, yxml.tagGen( "div", {
      ["style"]="float:right;", ["class"]={"tabs-btn", "tabs-tcmp"} }, "Compare" ) );
  end
  return yxml.tagGen( "div", attribs, res );
end

function ibh.vprops_parse( vadata, opts, properties, opts_vi )
  local attribs = ibh.opts_handle( opts, { ["class"]={"ib-propsg"} } );
  local attr_vi = ibh.opts_handle( opts_vi, { ["class"]={"ib-item", "tabs-tab"} } );
  local attr_temp = ytbl.deepclonev(attr_vi);
  -- the magic happens here, seriously pray that this never bugs out
  local res = {};
  local tabs_ct = vadata.tab_count;
  for _,prop_v in pairs( properties ) do
    if( vadata.v_exists[prop_v] ~= nil ) then -- there is at least one tab which has a value for this
      local or_fmts = nil; -- image set res
      local val_tabs = {}; -- table for all the values
      for iter=1, tabs_ct do
        attr_temp.class = ytbl.deepclonev(attr_vi.class); table.insert( attr_temp.class, "tabs-id-"..iter );
        table.insert( val_tabs, yxml.tagGen( "dd", attr_temp, formatVar( vadata, prop_v, iter ) ) );
        iter = iter + 1;
      end
      table.insert( res,
        yxml.tagGen( "div", { ["class"]={"ib-prop", "tabs-group"} }, { -- has K and V
          yxml.tagGen( "div", { ["class"]={"ib-key"} },
            yxml.tagGen( "dt", { ["class"]={"ib-item"} },
              formatVar( vadata, prop_v ) -- language formatted
            )
          ),
          yxml.tagGen( "div", { ["class"]={"ib-val"} }, table.concat( val_tabs ) )
        })
      );
    end
  end
  return yxml.tagGen( "div", attribs, res );
end

return ibh;