打开/关闭菜单
打开/关闭个人菜单
未登录
未登录用户的IP地址会在进行任意编辑后公开展示。

模块:Infobox/item/multidata:修订间差异

来自Cookie☆Wiki
(test)
(导入28个版本)
 
(未显示同一用户的15个中间版本)
第1行: 第1行:
local p = {}
local p = {}


-- 获取每一格的背景颜色, 规则为:
-- 在奇数行: 深、浅交错; 在偶数行: 浅、深交错
function p.getBackgroundColor(i, perRow)
function p.getBackgroundColor(i, perRow)
local isEvenColumn = ((math.floor((i-1)/perRow)%2)==0)
local isEvenColumn = ((math.floor((i-1)/perRow)%2)==0)
第9行: 第11行:
end
end


-- 构建一个数据项
function p.makeDataItem(frame, args) -- args: label, data, bgcolor
return frame:expandTemplate{ title = 'Infobox/item/data', args = args }
end

-- 构建信息框中多行多列的数据项
function p.make()
function p.make()
local frame = mw.getCurrentFrame()
local frame = mw.getCurrentFrame()
local parent = frame:getParent()
local parent = frame:getParent()
local itemsPerRow = tonumber(parent.args['row-items'])
local itemsPerRow = string.gsub(parent.args['row-items'] or '', '%s+', '')
if itemsPerRow == nil then
if itemsPerRow == 'flex' then
error('"row-items" is not a number')
itemsPerRow = 0
else
itemsPerRow = tonumber(itemsPerRow)
if itemsPerRow == nil or itemsPerRow <= 0 then
error('"row-items" should be either a positive integer or "flex" or empty (= "flex")')
end
end
end

-- 将非空的未命名参数 (数据项) 的编号存入 unnamed 表中
local count = 0
local count = 0
local unnamed = {}
local unnamed = {}
for k, v in pairs(parent.args) do
for k, v in pairs(parent.args) do
if tonumber(k) ~= nil and v ~= nil and v ~= '' then
if tonumber(k) ~= nil and v ~= nil and string.gsub(v, '%s+', '') ~= '' then
table.insert(unnamed, v)
unnamed[#unnamed + 1] = k
count = count + 1

end
end
end
end

if #unnamed == 0 then return '' end


local result = ''
local result = ''


result = result .. '<div class="pi-item-wrap">'
local widthPercent = string.format('%.2f', 100/itemsPerRow) .. '%'
if itemsPerRow == 0 then
local majorPart = math.floor(count / itemsPerRow) * itemsPerRow
local i = 1
-- flex
while (true) do
for i = 1, #unnamed do
result = result .. p.makeDataItem(frame, {
if i > majorPart then break end
if i % itemsPerRow == 1 then result = result .. '<div style="display: flex">' end
label = parent.args['label' .. unnamed[i]],
result = result .. frame:expandTemplate{ title = 'Infobox/item/data', args = {
data = parent.args[unnamed[i]],
width = widthPercent, data = unnamed[i], bgcolor = p.getBackgroundColor(i, itemsPerRow)
bgcolor = p.getBackgroundColor(i, 65535),
} }
})
end
if i % itemsPerRow == 0 then result = result .. '</div>' end
i = i + 1
else
local percentage = math.floor(10000 / itemsPerRow) / 100
end
percentage = tostring(percentage) .. '%'

local remain = count - majorPart
for i = 1, #unnamed do
result = result .. p.makeDataItem(frame, {
if remain ~= 0 then
widthPercent = string.format('%.2f', 100/(remain)) .. '%'
label = parent.args['label' .. unnamed[i]],
data = parent.args[unnamed[i]],
result = result .. '<div style="display: flex">'
bgcolor = p.getBackgroundColor(i, 65535),
while (true) do
if i > count then break end
width = percentage,
})
result = result .. frame:expandTemplate{ title = 'Infobox/item/data', args = {
width = widthPercent, data = unnamed[i],
label = parent.args['label' .. i],
bgcolor = p.getBackgroundColor(i, itemsPerRow)
} }
i = i + 1
end
end
result = result .. '</div>'
end
end
result = result .. '</div>'


return result
return result

2022年9月21日 (三) 23:44的最新版本

此模块的文档可以在模块:Infobox/item/multidata/doc创建

local p = {}

-- 获取每一格的背景颜色, 规则为:
-- 在奇数行: 深、浅交错; 在偶数行: 浅、深交错
function p.getBackgroundColor(i, perRow)
    local isEvenColumn = ((math.floor((i-1)/perRow)%2)==0)
    local delta = 0
    if not isEvenColumn then delta = 1 end
    if (((i-1)%perRow)+delta) % 2 == 1 then return nil end
    return '#e5ded6'
end

-- 构建一个数据项
function p.makeDataItem(frame, args) -- args: label, data, bgcolor
    return frame:expandTemplate{ title = 'Infobox/item/data', args = args }
end

-- 构建信息框中多行多列的数据项
function p.make()
    local frame = mw.getCurrentFrame()
    local parent = frame:getParent()
    local itemsPerRow = string.gsub(parent.args['row-items'] or '', '%s+', '')
    if itemsPerRow == 'flex' then
        itemsPerRow = 0
    else
        itemsPerRow = tonumber(itemsPerRow)
        if itemsPerRow == nil or itemsPerRow <= 0 then
            error('"row-items" should be either a positive integer or "flex" or empty (= "flex")')
        end
    end
    

    -- 将非空的未命名参数 (数据项) 的编号存入 unnamed 表中
    local count = 0
    local unnamed = {}
    for k, v in pairs(parent.args) do
        if tonumber(k) ~= nil and v ~= nil and string.gsub(v, '%s+', '') ~= '' then
            unnamed[#unnamed + 1] = k
        end
    end

    if #unnamed == 0 then return '' end

    local result = ''

    result = result .. '<div class="pi-item-wrap">'
    if itemsPerRow == 0 then
        -- flex
        for i = 1, #unnamed do
            result = result .. p.makeDataItem(frame, {
                label = parent.args['label' .. unnamed[i]],
                data = parent.args[unnamed[i]],
                bgcolor = p.getBackgroundColor(i, 65535),
            })
        end
    else
        local percentage = math.floor(10000 / itemsPerRow) / 100
        percentage = tostring(percentage) .. '%'
        for i = 1, #unnamed do
            result = result .. p.makeDataItem(frame, {
                label = parent.args['label' .. unnamed[i]],
                data = parent.args[unnamed[i]],
                bgcolor = p.getBackgroundColor(i, 65535),
                width = percentage,
            })
        end
    end
    result = result .. '</div>'

    return result
end

return p