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)
(Shorten subobject name)
(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))
 
(24 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 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 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 444 ⟶ 465:
--]]
function Infobox:store_smw()
for version=1, self.versions do
local versions = (self.versions > 1) and self.versions or 1 -- Grab versions, but convert 0 -> 1
for version=1, versions do
-- Generate a subobject name
-- Reminder - subobject name cannot have a . in the first 5 characters
local subobject_name = 'Infobox.'..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
Line 454 ⟶ 474:
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 464 ⟶ 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 502 ⟶ 527:
--]]
function Infobox:add_switch_data(content)
if self.versions <= 12 then
return false
end
Line 530 ⟶ 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 555 ⟶ 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