Module:Infobox: Difference between revisions

When a parameter is nil, make sure the function is called with it being set to nil (previously, all other parameters would be shifted left one)
(Clean up, annotated. Finalized, hopefully not many bugs)
(When a parameter is nil, make sure the function is called with it being set to nil (previously, all other parameters would be shifted left one))
 
(36 intermediate revisions by 6 users not shown)
Line 4:
 
-- <nowiki>
require('strict')
-- Edit button for unknown params
local editbutton = require('Module:Edit button')
local smwutils = require('Module:SMW Utils')
 
local Infobox = {}
Infobox.__index = Infobox
 
 
--[[
Line 24:
args_smw = {}, -- parameters parsed for smw
infobox_name = nil, -- template name
classes = {}, -- list of classes to add, using the default_version
param_names = {}, -- ordered param list
params = {}, -- param definitions
Line 31 ⟶ 32:
version_names = {}, -- title of each version (for selection and SMW)
rtable = nil, -- infobox table to return at the end
switch_datatable = nil'', -- datatable for javascript for switch infoboxes
categories = {}, -- set of categories
errors = {}, -- list of errors
Line 58 ⟶ 59:
--]]
function Infobox.param(param_name)
local param = {
property = 'args_parsed',
param_name = param_name,
Line 71 ⟶ 72:
--]]
function Infobox.raw_param(param_name)
local param = {
property = 'args_raw',
param_name = param_name,
Line 84 ⟶ 85:
--]]
function Infobox.smw_param(param_name)
local param = {
property = 'args_smw',
param_name = param_name,
Line 171 ⟶ 172:
_cell:wikitext(content)
-- Add the switch data if multiple values exist
local data_attr_param = self:add_switch_data(v.content)
if data_attr_param then
_cell:attr('data-attr-param', data_attr_param)
Line 188 ⟶ 189:
--]]
function Infobox:pad(colspan, class)
local tr = self.rtable:tag('tr'):tag('td'):attr('colspan', colspan or 1):addClass('infobox-padding')
if class then
tr:addClass(class)
Line 194 ⟶ 195:
return self
end
 
 
--[[
Line 209:
-- config: table containing configuration parameters
---- infobox_name = mandatory unique identifier for this infobox, used for css
---- max_buttuonsmax_buttons = max number of switch buttons before using a dropdown list instead
--]]
function Infobox:config(config)
Line 217:
elseif k == 'max_buttons' then
self.max_buttons = tonumber(v)
elseif k == 'class' then
if type(v) == 'string' then
self.versionsclasses = 0{v}
elseif type(v) == 'table' then
self.classes = v
end
end
end
Line 232 ⟶ 238:
--]]
function Infobox:parse_versions()
local function insert_version_name(version_name)
table.insert(self.version_names, self.args_raw['version'] or 'DEFAULT'version_name)
if smwutils.valid_subobject_name(version_name) == false then
table.insert(self.errors, 'Illegal version value: must not be "0" nor contain a "." in the first five characters')
end
end
-- Count the versions and setup self.version_names
local i = 1
while self.args_raw['version'..i] do
table.insertinsert_version_name(self.version_names, self.args_raw['version'..i])
i = i + 1
end
self.versions = i - 1
-- Handle the no version case - check for a custom version_name
if self.versions == 0 then
table.insert(self.version_names, self.args_raw['version'] or 'DEFAULT')
end
-- Should either have 0 or 2+ versions
if self.versions == 1 then
table.insert(self.errors, 'There should be multiple versions or no versions. If defining a custom version name for a single entry, use "version=Name" instead of "version1=Name".')
end
self.versions = 0
-- Handle the no version case - check for a custom version_name
if self.versions == 0 then
insert_version_name(self.args_raw['version'] or 'DEFAULT')
self.versions = 1
end
-- Check for a default_version
Line 356 ⟶ 369:
local func_params = func.params
local func_fetched_params = {}
local i = 1
for _, func_param in ipairs(func_params) do
table.insert(func_fetched_params,[i] = self:get_param(func_param, version))
i = i + 1
end
return func_name(unpack(func_fetched_params))
Line 396 ⟶ 411:
:addClass('infobox')
:addClass('infobox-'..self.infobox_name)
for key_, _class in pairsipairs(self.categoriesclasses) do
local class_name = self:get_param(class, self.default_version)
if type(class_name) == 'string' then
self.rtable:addClass('infobox-'..mw.ustring.gsub(class_name, '%s', '_'))
end
end
-- Create the switch datatable if multiple versions
if self.versions > 1 then
Line 403 ⟶ 424:
:addClass('infobox-resources-'..self.infobox_name)
:addClass('hidden')
self.switch_datatable:tag('span'):wikitext('Versions: '..self.versions)
self.switch_datatable:tag('span'):wikitext('Default version: '..self.default_version)
end
return self
Line 422 ⟶ 441:
:tag('div')
:addClass('infobox-buttons')
:attr('data-default-versionindex', self.default_version)
-- Dropdown list instead of buttons if too many versions
if self.versions > self.max_buttons then
Line 429 ⟶ 448:
-- Create all the buttons
for version=1, self.versions do
local button = buttons:tag('span')
:attr('data-switch-index', version)
:attr('data-switch-anchor', '#'..self.version_names[version])
:addClass('button')
:wikitext(self.version_names[version])
-- In case of dropdown list, hide the buttons as the switch gadget will convert them to a dropdown list - we can't directly create the dropdown list here as the select/option tags are rejected by wikitext
if self.versions > self.max_buttons then
button:addClass('hidden')
end
end
end
Line 445 ⟶ 468:
-- Generate a subobject name
-- Reminder - subobject name cannot have a . in the first 5 characters
local subobject_name = 'Infobox.'..mw.title.getCurrentTitle().fullText..'#'..self.version_names[version]
-- Store each param that has smw_property defined and is not nil
local subobject = {Infobox = self.infobox_name} -- Also store the current Infobox name
for _, param_name in ipairs(self.param_names) do
local property = self.params[param_name].smw_property
if property then
local value = self:get_param(self.smw_param(param_name), version)
if value ~= nil then
subobject[property] = value
Line 461 ⟶ 484:
-- Save subobjects if not in mainspace
if mw.title.getCurrentTitle():inNamespace(0) then
local result = mw.smw.subobject(subobject, subobject_name)true
if self.versions == 1 then
result = mw.smw.set(subobject)
else
result = mw.smw.subobject(subobject, subobject_name)
end
if result ~= true then
table.insert(self.errors, 'SMW error: '..result.error)
Line 499 ⟶ 527:
--]]
function Infobox:add_switch_data(content)
if self.versions <= 12 then
return false
end
Line 527 ⟶ 555:
name = 'smw__' + name
end
local data_param = self.switch_datatable:tag('span'):attr('data-attr-param', name)
-- Add each version to the datatable
for version=1, self.versions do
local text = self:get_param(content, version)
if text == nil then
text = self.params[content.param_name].empty
Line 552 ⟶ 580:
-- Create categories
local category_text = ''
if mw.title.getCurrentTitle():inNamespace(0) then
for key, _ in pairs(self.categories) do
for key, _ in pairs(self.categories) do
category_text = category_text..'[[Category:'..key..']]'
end
end
local dump = ''
if self.args_raw.__dump then
setmetatable(self, nil)
dump = '<nowiki>'..mw.dumpObject(self)..'</nowiki>'
setmetatable(self, Infobox)
end
return tostring(self.rtable) .. tostring(self.switch_datatable) .. error_text .. category_text .. dump
end
 
481

edits