(Support an optional table caption) |
(Flag column view) |
||
| (One intermediate revision by the same user not shown) | |||
| Line 11: | Line 11: | ||
return d.path(path) | return d.path(path) | ||
end | end | ||
return function(d) return d end | |||
end | end | ||
| Line 92: | Line 94: | ||
return value | return value | ||
end | end | ||
end | |||
return view | |||
end | |||
p.list = function(args) | |||
local getter,view = p.makeView(args) | |||
view.format = function(data) | |||
local items = getter(data) | |||
if items ~= nil then | |||
if type(items) ~= 'table' then | |||
items = {items} | |||
end | |||
local result = {} | |||
for i,item in ipairs(items) do | |||
table.insert(result, args.itemView.format(item)) | |||
end | |||
return table.concat(result, args.separator or ', ') | |||
end | |||
end | |||
return view | |||
end | |||
p.flag = function(args) | |||
local getter,view = p.makeView(args) | |||
view.format = function(data) | |||
local flag = getter(data) | |||
if flag then | |||
return (args.symbol or '☑') | |||
end | |||
end | end | ||
return view | return view | ||
Latest revision as of 09:02, 14 March 2023
Documentation for this module may be created at Module:Data/View/doc
local p = {}
local d = require('Module:Data')
local makeGetter = function(path)
if type(path) == 'function' then
return path
elseif type(path) == 'string' then
return d.path{path}
elseif type(path) == 'table' then
return d.path(path)
end
return function(d) return d end
end
p.makeView = function(args)
local path = table.remove(args, 1)
local getter = makeGetter(path)
local view = {}
for k,v in pairs(args) do
view[k] = v
end
return getter, view
end
p.viewNotFound = function(name)
return {
heading = string.format("View %s is not defined", name),
format = function(data) return nil end
}
end
p.displayTitle = function(title, text)
if title and text then
return string.format('[[%s|%s]]', title, p.displayValue(text))
elseif title then
return string.format('[[%s]]', title)
end
return text
end
p.title = function(args)
local getter, view = p.makeView(args)
local textGetter = nil
if args.text then
textGetter = makeGetter(args.text)
end
view.format = function(data)
local title = getter(data)
local text = nil
if textGetter then
text = textGetter(data)
end
return p.displayTitle(title, text)
end
return view
end
p.text = function(args)
local getter, view = p.makeView(args)
view.format = getter
return view
end
p.formatNum = function(n, ...)
local unit = select(1, ...)
if unit then
return string.format('%s %s', mw.getContentLanguage():formatNum(n), unit)
else
return mw.getContentLanguage():formatNum(n)
end
end
p.number = function(args)
local getter,view = p.makeView(args)
view.format = function(data)
local value = getter(data)
if args.omitZero and value == 0 then
return ''
end
if type(value) == 'number' then
return p.formatNum(value, args.unit)
end
return value
end
view.sortType = 'number'
view.sortValue = function(data)
local value = getter(data)
if type(value) == 'number' then
return value
end
end
return view
end
p.list = function(args)
local getter,view = p.makeView(args)
view.format = function(data)
local items = getter(data)
if items ~= nil then
if type(items) ~= 'table' then
items = {items}
end
local result = {}
for i,item in ipairs(items) do
table.insert(result, args.itemView.format(item))
end
return table.concat(result, args.separator or ', ')
end
end
return view
end
p.flag = function(args)
local getter,view = p.makeView(args)
view.format = function(data)
local flag = getter(data)
if flag then
return (args.symbol or '☑')
end
end
return view
end
p.displayValue = function(value)
if value == nil then
return ''
end
return tostring(value)
end
p.displayTable = function(data, view, columns, ...)
local views = {}
for i,col in ipairs(columns) do
v = view[col] or p.viewNotFound(col)
table.insert(views, v)
end
local options = select(1, ...) or {}
local result = {'{| class="wikitable sortable"'}
if options.caption then
table.insert(result, '|+')
table.insert(result, options.caption)
end
table.insert(result, '|-')
for j,v in ipairs(views) do
local cell = {'!'}
if v.sortType then
table.insert(cell, string.format('data-sort-type="%s"|', tostring(v.sortType)))
end
table.insert(cell, p.displayValue(v.heading))
table.insert(result, table.concat(cell))
end
for i,row in ipairs(data) do
table.insert(result ,'|-')
for j,v in ipairs(views) do
local cell = {'|'}
if v.sortValue then
local sortValue = v.sortValue(row)
if sortValue ~= nil then
table.insert(cell, string.format('data-sort-value="%s"|', p.displayValue(sortValue)))
end
end
table.insert(cell, p.displayValue(v.format(row)))
table.insert(result, table.concat(cell))
end
end
table.insert(result,'|}')
return table.concat(result, '\n')
end
return p