Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:InfoboxBuilder/doc: Difference between revisions

From Sekaipedia
Content added Content deleted
No edit summary
 
(46 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{documentation subpage}}
{{documentation subpage}}
{{Lua|Module:Navbar}}
{{Lua|Module:Navbar}}
{{Uses TemplateStyles|Module:InfoboxBuilder/styles.css}}

This module creates an infobox using the builder pattern. All infoboxes using this module can be found at [[:Category:Infobox modules]].


== Building an infobox ==
== Building an infobox ==
Line 40: Line 43:


=== Import the module ===
=== Import the module ===
<source lang="lua">
<syntaxhighlight lang="lua">
local InfoboxBuilder = require('Module:InfoboxBuilder')
local InfoboxBuilder = require('Module:InfoboxBuilder')
</syntaxhighlight>
</source>


=== Unpack the frame arguments ===
=== Unpack the frame arguments ===
<source lang="lua">
<syntaxhighlight lang="lua">
function p.main(frame)
function p.main(frame)
local args = frame:getParent().args
local args = frame:getParent().args
...
...
</syntaxhighlight>
</source>


=== Create the infobox object ===
=== Create the infobox object ===
<source lang="lua">
<syntaxhighlight lang="lua">
local infobox = InfoboxBuilder.new()
local infobox = InfoboxBuilder.new()
</syntaxhighlight>
</source>


=== Set the infobox name ===
=== Set the infobox name ===
Setting the infobox name sets the template that the infobox links to inside of the navbar.
Setting the infobox name sets the template that the infobox links to inside of the navbar.
<source lang="lua">
<syntaxhighlight lang="lua">
infobox:setName('Infobox')
infobox:setName('Infobox')
</syntaxhighlight>
</source>


=== Define the parameters ===
=== Define the parameters ===
When defining the parameters, the arguments will be processed and postprocessed when given a function or table. ''pFunc'' is an optional processing function or table that will transform the raw argument value into a specific format. ''dFunc'' is an optional postprocessing function or table that will transform the processed argument for display purposes. An optional default value for the raw argument can be set using the ''default'' option.
When defining the parameters, the arguments will be processed when given a function or table. <code>fn</code> is an optional processing function or table that will transform the raw argument value into a specific format. An optional default value for the raw argument can be set using the <code>default</code> option.


An example of using ''pFunc'' and ''dFunc'' would be to have ''pFunc'' be a function that splits a comma separated list into a table in Lua and have ''dFunc'' be a function that turns a table into an unordered list in HTML.
An example of using <code>fn</code> would be to have <code>fn</code> be a function that splits a comma separated list into a table in Lua.


<source lang="lua">
<syntaxhighlight lang="lua">
infobox:setParams {
infobox:setParams {
...
...
}
}
</syntaxhighlight>
</source>


=== Pass in the arguments ===
=== Pass in the arguments ===
<source lang="lua">
<syntaxhighlight lang="lua">
infobox:setArgs(args)
infobox:setArgs(args)
</syntaxhighlight>
</source>


=== Process the arguments ===
=== Process the arguments ===
Processing the arguments will set default value (if it exists) and turn the raw argument passed in from ''setArgs(args)'' into a processed format for each parameter. This step is '''required''' or else the infobox will not work.
Processing the arguments will set default value (if it exists) and turn the raw argument passed in from ''setArgs(args)'' into a processed format for each parameter. This step is '''required''' or else the infobox will not work.
<source lang="lua">
<syntaxhighlight lang="lua">
infobox:processArgs()
infobox:processArgs()
</syntaxhighlight>
</source>


=== Specify a category map ===
=== Specify a category map ===
A category map can be optionally set. This uses the processed (not postprocessed) argument and generates the correct categories.
A category map can be optionally set. This uses the processed argument and generates the correct categories.
<source lang="lua">
<syntaxhighlight lang="lua">
infobox:setCategoryMap({
infobox:setCategoryMap({
...
...
})
})
</syntaxhighlight>
</source>


=== Prepare ===
=== Prepare ===
Create the look of the infobox by adding headers, images, or rows. If the values shown in these cells use the value from parameters, it will use the postprocessed value.
Create the look of the infobox by adding headers, images, or rows. If the values shown in these cells use values from parameters, an optional postprocessing function or table <code>fn</code> will transform the processed arguments for display purposes.
<source lang="lua">
<syntaxhighlight lang="lua">
-- add a header or subheader
-- add a header or subheader
infobox:addHeader(
infobox:addHeader(
Line 112: Line 115:
{...}
{...}
)
)
</syntaxhighlight>
</source>


=== Build ===
=== Build ===
To build the infobox from data into an HTML table, call the ''tostring()'' function.
To build the infobox from data into an HTML table, call the ''tostring()'' function.
<source lang="lua">
<syntaxhighlight lang="lua">
infobox:tostring()
infobox:tostring()
</syntaxhighlight>
</source>


=== Get the categories from the category map ===
=== Get the categories from the category map ===
To get the categories, call the ''getCategories()'' function.
To get the categories, call the <code>getCategories()</code> function.
<source lang="lua">
<syntaxhighlight lang="lua">
infobox:getCategories()
infobox:getCategories()
</syntaxhighlight>
</source>


== Functions ==
== Functions ==
For the following functions, assume an InfoboxBuilder object called "infobox" already exists.
For the following functions, assume an InfoboxBuilder object called "infobox" already exists.
<source lang="lua">
<syntaxhighlight lang="lua">
local infobox = InfoboxBuilder.new()
local infobox = InfoboxBuilder.new()
</syntaxhighlight>
</source>


=== Public functions ===
=== Public functions ===
Line 136: Line 139:
''This function is '''required'''.''
''This function is '''required'''.''


<source lang="lua">
<syntaxhighlight lang="lua">
InfoboxBuilder.new()
InfoboxBuilder.new()
</syntaxhighlight>
</source>


Creates a new InfoboxBuilder object.
Creates a new InfoboxBuilder object.

<small>[[#L-31|↓ Jump to definition]]</small>


==== setName ====
==== setName ====
Line 147: Line 152:
''This function is chainable.''
''This function is chainable.''


<source lang="lua">
<syntaxhighlight lang="lua">
infobox:setName(arg)
infobox:setName(arg)
</syntaxhighlight>
</source>

;arg


<code>arg</code> should be a <code>string</code>. Setting the infobox name will link the navbar to the correct template page and template talk page.
<code>arg</code> should be a <code>string</code>. Setting the infobox name will link the navbar to the correct template page and template talk page.

<small>[[#L-5|↓ Jump to definition]]</small>


==== setHeaderTextColor ====
==== setHeaderTextColor ====
Line 158: Line 167:
''This function is chainable.''
''This function is chainable.''


<source lang="lua">
<syntaxhighlight lang="lua">
infobox:setHeaderTextColor(arg)
infobox:setHeaderTextColor(arg)
</syntaxhighlight>
</source>

;arg


<code>arg</code> should be a <code>string</code> that contains a valid CSS color (hex code, rgb value, hsl value, etc.). Calling this function will change the color of the text in the infobox's headers and subheaders. The changes will not apply to headers added before this function is called, so it is recommended to call this function early.
<code>arg</code> should be a <code>string</code> that contains a valid CSS color (hex code, rgb value, hsl value, etc.). Calling this function will change the color of the text in the infobox's headers and subheaders. The changes will not apply to headers added before this function is called, so it is recommended to call this function early.

<small>[[#L-73|↓ Jump to definition]]</small>


==== setHeaderBackgroundColor ====
==== setHeaderBackgroundColor ====
Line 169: Line 182:
''This function is chainable.''
''This function is chainable.''


<source lang="lua">
<syntaxhighlight lang="lua">
infobox:setHeaderBackgroundColor(arg)
infobox:setHeaderBackgroundColor(arg)
</syntaxhighlight>
</source>

;arg


<code>arg</code> should be a <code>string</code> that contains a valid CSS color (hex code, rgb value, hsl value, etc.). Calling this function will change the background color of the infobox's headers and subheaders. The changes will not apply to headers added before this function is called, so it is recommended to call this function early.
<code>arg</code> should be a <code>string</code> that contains a valid CSS color (hex code, rgb value, hsl value, etc.). Calling this function will change the background color of the infobox's headers and subheaders. The changes will not apply to headers added before this function is called, so it is recommended to call this function early.

<small>[[#L-89|↓ Jump to definition]]</small>


==== setParams ====
==== setParams ====
Line 180: Line 197:
''This function is chainable.''
''This function is chainable.''


<source lang="lua">
<syntaxhighlight lang="lua">
infobox:setParams(...)
infobox:setParams(...)
</syntaxhighlight>
</source>


This function sets the valid parameters, their default values, and their formatting.
This function sets the valid parameters, their default values, and their formatting.


The arguments passed in should be tables of format:
The arguments passed in should be tables of format:
<source lang="lua">
<syntaxhighlight lang="lua">
{
{
name = <paramName>,
name = <paramName>,
pFunc = <processFunc>,
fn = <processingFn>,
dFunc = <displayFunc>,
default = <default>
default = <default>
}
}
</syntaxhighlight>
</source>

;paramName


<code>paramName</code> should be a valid <code>string</code> that is unique. This name serves the key used to grab the raw values from the <code>args</code> table passed in from <code>setArgs()</code>. It also serves as the key for each parameter and will be referenced later.
<code>paramName</code> should be a valid <code>string</code> that is unique. This name serves the key used to grab the raw values from the <code>args</code> table passed in from <code>setArgs()</code>. It also serves as the key for each parameter and will be referenced later.


;processingFn
<code>processFunc</code> should be a <code>function</code> or a <code>table</code> that transforms the raw value of the argument passed in. The result of this function or table should be something that <code>displayFunc</code> an use as a parameter. This value is optional.


<code>displayFunc</code> should be a <code>function</code> that transforms the processed value of the argument into something that can be displayed (ex. an HTML list). This value is optional.
<code>processingFn</code> should be a <code>function</code> or a <code>table</code> that transforms the raw value of the argument passed in. This value is optional.


A collection of predefined processing functions can be found at [[Module:ProcessingFunctions]].
<code>default</code> should be a string that serves as the default value if no raw value is present for the key in the <code>args</code> table. This value is optional.


;default
;Predefined processing functions and tables
There are no predefined functions and tables at this time. The following table contains proposed functions.
{| class="wikitable"
! Function or table !! Use
|-
| split
| Splits a string into a numerical array with a specific delimiter
|}


<code>default</code> should be a string that serves as the default value if no raw value is present for the key in the <code>args</code> table. This value is optional.
;Predefined display functions

There are no predefined display functions at this time. The following table contains proposed functions.
<small>[[#L-111|↓ Jump to definition]]</small>
{| class="wikitable"
! Function !! Use
|-
| link
| Creates a link when given a numerical array of one or two elements.
|-
| blist
| Creates a bulleted list from a numerical array.
|-
| ulist
| Creates an unbulleted list from a numerical array.
|-
| hlist
| Creates a horizontal list from a numerical array.
|-
| olist
| Creates an ordered list from a numerical array.
|}


==== setArgs ====
==== setArgs ====
Line 239: Line 233:
''This function is chainable.''
''This function is chainable.''


<source lang="lua">
<syntaxhighlight lang="lua">
infobox:setArgs(args)
infobox:setArgs(args)
</syntaxhighlight>
</source>

;args


<code>args</code> should be a <code>table</code> with key value pairs representing parameter names and values.
<code>args</code> should be a <code>table</code> with key value pairs representing parameter names and values.

<small>[[#L-135|↓ Jump to definition]]</small>


==== getRawArgs ====
==== getRawArgs ====
<source lang="lua">
<syntaxhighlight lang="lua">
infobox:getRawArgs()
infobox:getRawArgs()
</syntaxhighlight>
</source>


This function returns the "private" <code>rawArgs</code> associative array containing the raw values passed in from <code>setArgs()</code>.
This function returns the "private" <code>rawArgs</code> associative array containing the raw values passed in from <code>setArgs()</code>.

<small>[[#L-155|↓ Jump to definition]]</small>


==== getProcessedArgs ====
==== getProcessedArgs ====
<source lang="lua">
<syntaxhighlight lang="lua">
infobox:getProcessedArgs()
infobox:getProcessedArgs()
</syntaxhighlight>
</source>
This function returns the "private" <code>procArgs</code> associative array after being processed.
This function returns the "private" <code>procArgs</code> associative array after being processed.

<small>[[#L-162|↓ Jump to definition]]</small>


==== getArgs ====
==== getArgs ====
<source lang="lua">
<syntaxhighlight lang="lua">
infobox:getArgs(which)
infobox:getArgs(which)
</syntaxhighlight>
</source>

;which


<code>which</code> should be a <code>string</code> that is either "raw" or "processed" for the raw argument values or processed argument values, respectively.
<code>which</code> should be a <code>string</code> that is either "raw" or "processed" for the raw argument values or processed argument values, respectively.


This function returns the "private" <code>rawArgs</code> or <code>procArgs</code> associative arrays depending on the value of <code>which</code>.
This function returns the "private" <code>rawArgs</code> or <code>procArgs</code> associative arrays depending on the value of <code>which</code>.

<small>[[#L-171|↓ Jump to definition]]</small>


==== setCategoryMap ====
==== setCategoryMap ====
Line 272: Line 278:
''This function is chainable.''
''This function is chainable.''


<source lang="lua">
<syntaxhighlight lang="lua">
infobox:setCategoryMap(catMap)
infobox:setCategoryMap(catMap)
</syntaxhighlight>
</source>

;catMap

<code>catMap</code> should be a <code>table</code> of <code>table</code>s, where the key of the outer table corresponds with a parameter name and the key of the inner tables correspond with possible values of the associated parameter. The inner table values should be the category name related to each parameter value.

<small>[[#L-185|↓ Jump to definition]]</small>


==== getCategories ====
==== getCategories ====
<source lang="lua">
<syntaxhighlight lang="lua">
infobox:getCategories()
infobox:getCategories()
</syntaxhighlight>
</source>

This function returns a <code>string</code> with autogenerated categories in wikitext form.

<small>[[#L-196|↓ Jump to definition]]</small>


==== processArgs ====
==== processArgs ====
Line 286: Line 302:
''This function is chainable.''
''This function is chainable.''


<source lang="lua">
<syntaxhighlight lang="lua">
infobox:processArgs()
infobox:processArgs()
</syntaxhighlight>
</source>

This function processes the all the arguments passed in with the given processing functions defined when setting parameters.

<small>[[#L-232|↓ Jump to definition]]</small>


==== addHeader ====
==== addHeader ====
Line 295: Line 315:
''This function is chainable.''
''This function is chainable.''


<source lang="lua">
<syntaxhighlight lang="lua">
infobox:addHeader(arg, options)
infobox:addHeader(arg, options)
</syntaxhighlight>
</source>

This functions adds a header or subheader to the infobox, depending on the option set.


;arg

<code>arg</code> should be a <code>table</code> of the following format:
<syntaxhighlight lang="lua">
{
tag = <tag>,
content = <content>,
fn = <displayFn>,
attr = <attr>,
colspan = <colspan>,
rowspan = <rowspan>,
css = <css>
}
</syntaxhighlight>

;tag

<code>tag</code> should be a <code>string</code> of value "argth" or "th".

;content

When <code>tag</code> is set to "th", <code>content</code> should contain the wikitext that will show up.

When <code>tag</code> is set to "argth", <code>content</code> should be a <code>string</code> or <code>table</code> of parameter names. This value will be passed into <code>displayFn</code> if it is set.

;displayFn

<code>displayFn</code> should be a <code>function</code> that transforms one or more processed arguments (based on <code>content</code> into something that can be displayed (ex. an HTML list). The order of the parameters in this function is the same as the order of <code>content</code>, if <code>content</code> is a <code>table</code>. This value is optional.

A collection of predefined display functions can be found at [[Module:DisplayFunctions]].

;attr

<code>attr</code> should be a <code>table</code> containing HTML attributes. The value gets passed into the [[mw:Extension:Scribunto/Lua_reference_manual#mw.html:attr|mw.html:attr()]] function. This value is optional.

;colspan

<code>colspan</code> should be a <code>number</code> or <code>string</code> denoting the number of columns this header spans. This value is optional.

;rowspan

<code>rowspan</code> should be a <code>number</code> or <code>string</code> denoting the number of rows this header spans. This value is optional.

;css

<code>css</code> should be a <code>table</code> of CSS names and values. The value gets passed into the [[mw:Extension:Scribunto/Lua_reference_manual#mw.html:css|mw.html:css()]] function. This value is optional.


;options

<code>options</code> should be a <code>table</code> of the following format:
<syntaxhighlight lang="lua">
{
hideIfEmpty = <hideIfEmpty>,
subheader = <subheader>
}
</syntaxhighlight>

;hideIfEmpty

<code>hideIfEmpty</code> should be a <code>table</code> containing strings of parameter names. If all values for those parameters are empty, then the header or subheader is hidden. This value is optional.

;subheader

<code>subheader</code> should be a <code>boolean</code> denoting whether the header should be a subheader or not. The default value is false. This value is optional.

<small>[[#L-344|↓ Jump to definition]]</small>


==== addImage ====
==== addImage ====
Line 304: Line 395:
''This function is chainable.''
''This function is chainable.''


<source lang="lua">
<syntaxhighlight lang="lua">
infobox:addImage(cols, options)
infobox:addImage(cols, options)
</syntaxhighlight>
</source>

This function adds a single image or multiple images inside of a row. For multiple images, it selectively hides all but one image using [[mw:Extension:TabberNeue|TabberNeue]].


;cols

<code>cols</code> should be a <code>table</code> that is a numeric array. Each entry in this table should have the following format:

<syntaxhighlight lang="lua">
{
tag = <tag>,
content = <content>,
fn = <displayFn>,
tabName = <tabName>,
required = <required>
}
</syntaxhighlight>

;tag

<code>tag</code> should be a <code>string</code> of value "argtd" or "td".

;content

When <code>tag</code> is set to "td", <code>content</code> should be a <code>string</code> that shows up as the wikitext.

When <code>tag</code> is set to "argtd", <code>content</code> should be a <code>string</code> or <code>table</code> of parameter names. This value will be passed into <code>displayFn</code> if it is set.

;displayFn

<code>displayFn</code> should be a <code>function</code> that transforms one or more processed arguments (based on <code>content</code> into something that can be displayed (ex. an HTML list). The order of the parameters in this function is the same as the order of <code>content</code>, if <code>content</code> is a <code>table</code>. This value is optional.

;tabName

<code>tabName</code> should be a <code>string</code> if there are multiple images. The value of this does nothing if there's only one image.

;required

<code>required</code> should be a <code>boolean</code> if the tab is required. This means that even if no value is provided, the tab will show up with a '?'.


;options

<code>options</code> should be a <code>table</code> of the following format:
<syntaxhighlight lang="lua">
{
hideIfEmpty = <hideIfEmpty>
}
</syntaxhighlight>

;hideIfEmpty

<code>hideIfEmpty</code> should be a <code>table</code>containing strings of parameter names. If all values for those parameters are empty, then the image section is hidden. This value is optional.

<small>[[#L-412|↓ Jump to definition]]</small>


==== addRow ====
==== addRow ====
Line 313: Line 459:
''This function is chainable.''
''This function is chainable.''


<source lang="lua">
<syntaxhighlight lang="lua">
infobox:addRow(cols, options)
infobox:addRow(cols, options)
</syntaxhighlight>
</source>

This function adds anywhere from 1 to 30 cells to a row. The colspan for each cell is automatically set to be evenly split so that all cells are the same width. The exception is when there are two cells, where the first cell spans 12 columns and the second cell spans 18 cells.


;cols

<code>cols</code> should be a <code>table</code> that is a numeric array. Each entry in this table should have the following format:

<syntaxhighlight lang="lua">
{
tag = <tag>,
content = <content>,
fn = <displayFn>,
attr = <attr>,
colspan = <colspan>,
rowspan = <rowspan>,
css = <css>,
class = <class>,
cssFns = <cssFns>,
classFns = <classFns>
}
</syntaxhighlight>

;tag

<code>tag</code> should be a <code>string</code> of value "argth", "argtd", "th", or "td".

;content

When <code>tag</code> is set to "td" or "th", <code>content</code> should contain the wikitext that will show up.

When <code>tag</code> is set to "argtd" or "argth", <code>content</code> should be a <code>string</code> or <code>table</code> of parameter names. This value will be passed into <code>displayFn</code> if it is set.

;displayFn

<code>displayFn</code> should be a <code>function</code> that transforms one or more processed arguments (based on <code>content</code> into something that can be displayed (ex. an HTML list). The order of the parameters in this function is the same as the order of <code>content</code>, if <code>content</code> is a <code>table</code>. This value is optional.

A collection of predefined display functions can be found at [[Module:DisplayFunctions]].

;attr

<code>attr</code> should be a <code>table</code> containing HTML attributes. The value gets passed into the [[mw:Extension:Scribunto/Lua_reference_manual#mw.html:attr|mw.html:attr()]] function. This value is optional.

;colspan

<code>colspan</code> should be a <code>number</code> or <code>string</code> denoting the number of columns this header spans. This value is optional.

;rowspan

<code>rowspan</code> should be a <code>number</code> or <code>string</code> denoting the number of rows this header spans. This value is optional.

;css

<code>css</code> should be a <code>table</code> of CSS names and values. The value gets passed into the [[mw:Extension:Scribunto/Lua_reference_manual#mw.html:css|mw.html:css()]] function. This value is optional.

;class

<code>class</code> should be either a <code>string</code> with a class name or <code>table</code> (numeric array) containing class names. Each class name gets passed into the [[mw:Extension:Scribunto/Lua_reference_manual#mw.html:addClass|mw.html:addClass()]] function. This value is optional.

;cssFns

<code>cssFns</code> should be a <code>table</code> of functions. Each function takes the processed value of parameter <code>content</code> as input and should output a valid CSS <code>table</code>. This value only applies to cells with tag "argth" or "argtd". This value is optional.

;classFns

<code>classFns</code> should be a <code>table</code> of functions. Each function takes the processed value of parameter <code>content</code> as input and should output a class name <code>string</code>. This value only applies to cells with tag "argth" or "argtd". This value is optional.


;options

<code>options</code> should be a <code>table</code> of the following format:

<syntaxhighlight lang="lua">
{
hideIfEmpty = <hideIfEmpty>,
defaultCss = <defaultCss>
}
</syntaxhighlight>

;hideIfEmpty

<code>hideIfEmpty</code> should be a <code>table</code> containing strings of parameter names. If all values for those parameters are empty, then the cell is hidden. This value is optional.

;defaultCss

<code>defaultCss</code> should be a <code>table</code> of CSS names and values. This CSS applies to all cells in this row. This value is optional.

<small>[[#L-518|↓ Jump to definition]]</small>


==== tostring ====
==== tostring ====
''This function is '''required'''.''
''This function is '''required'''.''


<source lang="lua">
<syntaxhighlight lang="lua">
infobox:tostring()
infobox:tostring()
</syntaxhighlight>
</source>

This function "finishes" the infobox and returns an output HTML table with all styles automatically applied from [[Template:Infobox/styles.css]]. In order to generate an infobox, this function '''must''' be called.

<small>[[#L-636|↓ Jump to definition]]</small>


=== Private functions ===
=== Private functions ===
Line 328: Line 566:


==== getContent ====
==== getContent ====
<source lang="lua">
<syntaxhighlight lang="lua">
infobox:getContent(paramName)
infobox:getContent(paramName)
</syntaxhighlight>
</source>

Gets the content for display for a specified parameter. If the value for a parameter has not been postprocessed, then the <code>dFunc</code> for that parameter, if it exists, will be called.

<small>[[#L-262|↓ Jump to definition]]</small>


==== shouldShow ====
==== shouldShow ====
<source lang="lua">
<syntaxhighlight lang="lua">
infobox:shouldShow(paramNames)
infobox:shouldShow(paramNames)
</syntaxhighlight>
</source>

This is a helper function to tell if a certain cell should be visible depending on the parameter names given. If all values for the parameters given have no value, then a cell should not show.

<small>[[#L-302|↓ Jump to definition]]</small>


==== addSpacer ====
==== addSpacer ====
<source lang="lua">
<syntaxhighlight lang="lua">
infobox:addSpacer()
infobox:addSpacer()
</syntaxhighlight>
</source>

This function sets up the 30 column layout by inserting an empty row. It also serves as a spacer.

<small>[[#L-611|↓ Jump to definition]]</small>


==== addLinks ====
==== addLinks ====
<source lang="lua">
<syntaxhighlight lang="lua">
infobox:addLinks()
infobox:addLinks()
</syntaxhighlight>
</source>

This function adds the navbar at the end of the infobox. This navbar contains links to the template page and the template talk page for this infobox. This uses the page using the value passed in from the <code>setName()</code> function.

<small>[[#L-621|↓ Jump to definition]]</small>

<includeonly>[[Category:Builder modules]]</includeonly>

Latest revision as of 16:33, 19 October 2022

This module creates an infobox using the builder pattern. All infoboxes using this module can be found at Category:Infobox modules.

Building an infobox[edit source]

For an infobox to be built without errors, certain essential functions must be called before others. The following table details groups of functions and their priorities. The lower the number, the higher the priority. The functions setHeaderTextColor() and setHeaderBackgroundColor() are not required, but their CSS changes will not be applied if called later. In addition, the functions that add rows to the table, addHeader(), addImage(), and addRow() are not all required, but is it recommended to use at least addHeader() and addRow().

Core function priority
Priority Functions
1
  • new
2
  • setParams
  • setArgs
3
  • processArgs
4
  • setHeaderTextColor
  • setHeaderBackgroundColor
5
  • setName
  • addHeader
  • addImage
  • addRow
6
  • tostring

Import the module[edit source]

local InfoboxBuilder = require('Module:InfoboxBuilder')

Unpack the frame arguments[edit source]

function p.main(frame)
	local args = frame:getParent().args
	...

Create the infobox object[edit source]

local infobox = InfoboxBuilder.new()

Set the infobox name[edit source]

Setting the infobox name sets the template that the infobox links to inside of the navbar.

infobox:setName('Infobox')

Define the parameters[edit source]

When defining the parameters, the arguments will be processed when given a function or table. fn is an optional processing function or table that will transform the raw argument value into a specific format. An optional default value for the raw argument can be set using the default option.

An example of using fn would be to have fn be a function that splits a comma separated list into a table in Lua.

infobox:setParams {
	...
}

Pass in the arguments[edit source]

infobox:setArgs(args)

Process the arguments[edit source]

Processing the arguments will set default value (if it exists) and turn the raw argument passed in from setArgs(args) into a processed format for each parameter. This step is required or else the infobox will not work.

infobox:processArgs()

Specify a category map[edit source]

A category map can be optionally set. This uses the processed argument and generates the correct categories.

infobox:setCategoryMap({
	...
})

Prepare[edit source]

Create the look of the infobox by adding headers, images, or rows. If the values shown in these cells use values from parameters, an optional postprocessing function or table fn will transform the processed arguments for display purposes.

-- add a header or subheader
infobox:addHeader(
	{...},
	{...}
)

-- add an image
infobox:addImage(
	{...},
	{...}
)

-- add a row
infobox:addRow(
	{...},
	{...}
)

Build[edit source]

To build the infobox from data into an HTML table, call the tostring() function.

infobox:tostring()

Get the categories from the category map[edit source]

To get the categories, call the getCategories() function.

infobox:getCategories()

Functions[edit source]

For the following functions, assume an InfoboxBuilder object called "infobox" already exists.

local infobox = InfoboxBuilder.new()

Public functions[edit source]

new[edit source]

This function is required.

InfoboxBuilder.new()

Creates a new InfoboxBuilder object.

↓ Jump to definition

setName[edit source]

This function is required.
This function is chainable.

infobox:setName(arg)
arg

arg should be a string. Setting the infobox name will link the navbar to the correct template page and template talk page.

↓ Jump to definition

setHeaderTextColor[edit source]

This function is optional.
This function is chainable.

infobox:setHeaderTextColor(arg)
arg

arg should be a string that contains a valid CSS color (hex code, rgb value, hsl value, etc.). Calling this function will change the color of the text in the infobox's headers and subheaders. The changes will not apply to headers added before this function is called, so it is recommended to call this function early.

↓ Jump to definition

setHeaderBackgroundColor[edit source]

This function is optional.
This function is chainable.

infobox:setHeaderBackgroundColor(arg)
arg

arg should be a string that contains a valid CSS color (hex code, rgb value, hsl value, etc.). Calling this function will change the background color of the infobox's headers and subheaders. The changes will not apply to headers added before this function is called, so it is recommended to call this function early.

↓ Jump to definition

setParams[edit source]

This function is required.
This function is chainable.

infobox:setParams(...)

This function sets the valid parameters, their default values, and their formatting.

The arguments passed in should be tables of format:

{
	name = <paramName>,
	fn = <processingFn>,
	default = <default>
}
paramName

paramName should be a valid string that is unique. This name serves the key used to grab the raw values from the args table passed in from setArgs(). It also serves as the key for each parameter and will be referenced later.

processingFn

processingFn should be a function or a table that transforms the raw value of the argument passed in. This value is optional.

A collection of predefined processing functions can be found at Module:ProcessingFunctions.

default

default should be a string that serves as the default value if no raw value is present for the key in the args table. This value is optional.

↓ Jump to definition

setArgs[edit source]

This function is required.
This function is chainable.

infobox:setArgs(args)
args

args should be a table with key value pairs representing parameter names and values.

↓ Jump to definition

getRawArgs[edit source]

infobox:getRawArgs()

This function returns the "private" rawArgs associative array containing the raw values passed in from setArgs().

↓ Jump to definition

getProcessedArgs[edit source]

infobox:getProcessedArgs()

This function returns the "private" procArgs associative array after being processed.

↓ Jump to definition

getArgs[edit source]

infobox:getArgs(which)
which

which should be a string that is either "raw" or "processed" for the raw argument values or processed argument values, respectively.

This function returns the "private" rawArgs or procArgs associative arrays depending on the value of which.

↓ Jump to definition

setCategoryMap[edit source]

This function is optional.
This function is chainable.

infobox:setCategoryMap(catMap)
catMap

catMap should be a table of tables, where the key of the outer table corresponds with a parameter name and the key of the inner tables correspond with possible values of the associated parameter. The inner table values should be the category name related to each parameter value.

↓ Jump to definition

getCategories[edit source]

infobox:getCategories()

This function returns a string with autogenerated categories in wikitext form.

↓ Jump to definition

processArgs[edit source]

This function is required.
This function is chainable.

infobox:processArgs()

This function processes the all the arguments passed in with the given processing functions defined when setting parameters.

↓ Jump to definition

addHeader[edit source]

This function is optional, but recommended.
This function is chainable.

infobox:addHeader(arg, options)

This functions adds a header or subheader to the infobox, depending on the option set.


arg

arg should be a table of the following format:

{
	tag = <tag>,
	content = <content>,
    fn = <displayFn>,
	attr = <attr>,
	colspan = <colspan>,
	rowspan = <rowspan>,
	css = <css>
}
tag

tag should be a string of value "argth" or "th".

content

When tag is set to "th", content should contain the wikitext that will show up.

When tag is set to "argth", content should be a string or table of parameter names. This value will be passed into displayFn if it is set.

displayFn

displayFn should be a function that transforms one or more processed arguments (based on content into something that can be displayed (ex. an HTML list). The order of the parameters in this function is the same as the order of content, if content is a table. This value is optional.

A collection of predefined display functions can be found at Module:DisplayFunctions.

attr

attr should be a table containing HTML attributes. The value gets passed into the mw.html:attr() function. This value is optional.

colspan

colspan should be a number or string denoting the number of columns this header spans. This value is optional.

rowspan

rowspan should be a number or string denoting the number of rows this header spans. This value is optional.

css

css should be a table of CSS names and values. The value gets passed into the mw.html:css() function. This value is optional.


options

options should be a table of the following format:

{
	hideIfEmpty = <hideIfEmpty>,
	subheader = <subheader>
}
hideIfEmpty

hideIfEmpty should be a table containing strings of parameter names. If all values for those parameters are empty, then the header or subheader is hidden. This value is optional.

subheader

subheader should be a boolean denoting whether the header should be a subheader or not. The default value is false. This value is optional.

↓ Jump to definition

addImage[edit source]

This function is optional, but recommended.
This function is chainable.

infobox:addImage(cols, options)

This function adds a single image or multiple images inside of a row. For multiple images, it selectively hides all but one image using TabberNeue.


cols

cols should be a table that is a numeric array. Each entry in this table should have the following format:

{
	tag = <tag>,
	content = <content>,
    fn = <displayFn>,
	tabName = <tabName>,
    required = <required>
}
tag

tag should be a string of value "argtd" or "td".

content

When tag is set to "td", content should be a string that shows up as the wikitext.

When tag is set to "argtd", content should be a string or table of parameter names. This value will be passed into displayFn if it is set.

displayFn

displayFn should be a function that transforms one or more processed arguments (based on content into something that can be displayed (ex. an HTML list). The order of the parameters in this function is the same as the order of content, if content is a table. This value is optional.

tabName

tabName should be a string if there are multiple images. The value of this does nothing if there's only one image.

required

required should be a boolean if the tab is required. This means that even if no value is provided, the tab will show up with a '?'.


options

options should be a table of the following format:

{
	hideIfEmpty = <hideIfEmpty>
}
hideIfEmpty

hideIfEmpty should be a tablecontaining strings of parameter names. If all values for those parameters are empty, then the image section is hidden. This value is optional.

↓ Jump to definition

addRow[edit source]

This function is optional, but recommended.
This function is chainable.

infobox:addRow(cols, options)

This function adds anywhere from 1 to 30 cells to a row. The colspan for each cell is automatically set to be evenly split so that all cells are the same width. The exception is when there are two cells, where the first cell spans 12 columns and the second cell spans 18 cells.


cols

cols should be a table that is a numeric array. Each entry in this table should have the following format:

{
	tag = <tag>,
	content = <content>,
    fn = <displayFn>,
	attr = <attr>,
	colspan = <colspan>,
	rowspan = <rowspan>,
	css = <css>,
	class = <class>,
	cssFns = <cssFns>,
	classFns = <classFns>
}
tag

tag should be a string of value "argth", "argtd", "th", or "td".

content

When tag is set to "td" or "th", content should contain the wikitext that will show up.

When tag is set to "argtd" or "argth", content should be a string or table of parameter names. This value will be passed into displayFn if it is set.

displayFn

displayFn should be a function that transforms one or more processed arguments (based on content into something that can be displayed (ex. an HTML list). The order of the parameters in this function is the same as the order of content, if content is a table. This value is optional.

A collection of predefined display functions can be found at Module:DisplayFunctions.

attr

attr should be a table containing HTML attributes. The value gets passed into the mw.html:attr() function. This value is optional.

colspan

colspan should be a number or string denoting the number of columns this header spans. This value is optional.

rowspan

rowspan should be a number or string denoting the number of rows this header spans. This value is optional.

css

css should be a table of CSS names and values. The value gets passed into the mw.html:css() function. This value is optional.

class

class should be either a string with a class name or table (numeric array) containing class names. Each class name gets passed into the mw.html:addClass() function. This value is optional.

cssFns

cssFns should be a table of functions. Each function takes the processed value of parameter content as input and should output a valid CSS table. This value only applies to cells with tag "argth" or "argtd". This value is optional.

classFns

classFns should be a table of functions. Each function takes the processed value of parameter content as input and should output a class name string. This value only applies to cells with tag "argth" or "argtd". This value is optional.


options

options should be a table of the following format:

{
	hideIfEmpty = <hideIfEmpty>,
	defaultCss = <defaultCss>
}
hideIfEmpty

hideIfEmpty should be a table containing strings of parameter names. If all values for those parameters are empty, then the cell is hidden. This value is optional.

defaultCss

defaultCss should be a table of CSS names and values. This CSS applies to all cells in this row. This value is optional.

↓ Jump to definition

tostring[edit source]

This function is required.

infobox:tostring()

This function "finishes" the infobox and returns an output HTML table with all styles automatically applied from Template:Infobox/styles.css. In order to generate an infobox, this function must be called.

↓ Jump to definition

Private functions[edit source]

While these are not necessarily "private", these functions are not intended to be called externally.

getContent[edit source]

infobox:getContent(paramName)

Gets the content for display for a specified parameter. If the value for a parameter has not been postprocessed, then the dFunc for that parameter, if it exists, will be called.

↓ Jump to definition

shouldShow[edit source]

infobox:shouldShow(paramNames)

This is a helper function to tell if a certain cell should be visible depending on the parameter names given. If all values for the parameters given have no value, then a cell should not show.

↓ Jump to definition

addSpacer[edit source]

infobox:addSpacer()

This function sets up the 30 column layout by inserting an empty row. It also serves as a spacer.

↓ Jump to definition

addLinks[edit source]

infobox:addLinks()

This function adds the navbar at the end of the infobox. This navbar contains links to the template page and the template talk page for this infobox. This uses the page using the value passed in from the setName() function.

↓ Jump to definition


Cookies help us deliver our services. By using our services, you agree to our use of cookies.