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

来自Cookie☆Wiki
(fix)
(修正错误 & 添加注释 & 整理代码)
第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(label, data, bgcolor)
return frame:expandTemplate{ title = 'Infobox/item/data', args = {
label = parent.args['label' .. i],
data = parent.args[unnamed[i]],
-- width = widthPercent,
bgcolor = p.getBackgroundColor(i, itemsPerRow)
} }
end

-- 构建信息框中多行多列的数据项
function p.make()
function p.make()
local frame = mw.getCurrentFrame()
local frame = mw.getCurrentFrame()
第17行: 第29行:
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 v ~= '' then
table.insert(unnamed, v)
table.insert(k, v)
count = count + 1
count = count + 1

end
end
end
end
第29行: 第41行:
local result = ''
local result = ''


-- 构建项数满足 'row-items' 数量的那几行
local widthPercent = string.format('%.2f', 100/itemsPerRow) .. '%'
-- local widthPercent = string.format('%.2f', 100/itemsPerRow) .. '%'
local majorPart = math.floor(count / itemsPerRow) * itemsPerRow
local majorPart = math.floor(count / itemsPerRow) * itemsPerRow
local i = 1
local i = 1
while (true) do
while (true) do
if i > majorPart then break end
-- if i > majorPart then break end
if i % itemsPerRow == 1 then result = result .. '<div style="display: flex">' end
if i > count then
result = result .. frame:expandTemplate{ title = 'Infobox/item/data', args = {
result = result .. '</div>'
width = widthPercent, data = unnamed[i],
break
label = parent.args['label' .. i],
elseif i % itemsPerRow == 1 then
bgcolor = p.getBackgroundColor(i, itemsPerRow)
if i ~= 1 then
} }
result = result .. '</div>'
if i % itemsPerRow == 0 then result = result .. '</div>' end
end
result = result .. '<div style="display: flex">'
end
result = result .. p.makeDataItem(parent.args['label' .. i],
parent.args[unnamed[i]], p.getBackgroundColor(i, itemsPerRow))
i = i + 1
i = i + 1
end
end


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


return result
return result

2019年1月15日 (二) 06:41的版本

可在模块: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(label, data, bgcolor)
    return frame:expandTemplate{ title = 'Infobox/item/data', args = {
        label = parent.args['label' .. i],
        data = parent.args[unnamed[i]],
        -- width = widthPercent,
        bgcolor = p.getBackgroundColor(i, itemsPerRow)
    } }
end

-- 构建信息框中多行多列的数据项
function p.make()
    local frame = mw.getCurrentFrame()
    local parent = frame:getParent()
    local itemsPerRow = tonumber(parent.args['row-items'])
    if itemsPerRow == nil then
        error('"row-items" is not a number')
    end
    
    -- 将非空的未命名参数的编号存入 unnamed 表中
    local count = 0
    local unnamed = {}
    for k, v in pairs(parent.args) do
        if tonumber(k) ~= nil and v ~= nil and v ~= '' then
            table.insert(k, v)
            count = count + 1
        end
    end

    local result = ''

    -- 构建项数满足 'row-items' 数量的那几行
    -- local widthPercent = string.format('%.2f', 100/itemsPerRow) .. '%'
    local majorPart = math.floor(count / itemsPerRow) * itemsPerRow
    local i = 1
    while (true) do
        -- if i > majorPart then break end
        if i > count then 
            result = result .. '</div>'
            break
        elseif i % itemsPerRow == 1 then
            if i ~= 1 then
                result = result .. '</div>'
            end
            result = result .. '<div style="display: flex">'
        end
        result = result .. p.makeDataItem(parent.args['label' .. i], 
            parent.args[unnamed[i]], p.getBackgroundColor(i, itemsPerRow))
        i = i + 1
    end

    -- local remain = count - majorPart
    -- if remain ~= 0 then
    --     widthPercent = string.format('%.2f', 100/(remain)) .. '%'
    --     result = result .. '<div style="display: flex">'
    --     while (true) do
    --         if i > count then break end
    --         result = result .. p.makeDataItem(parent.args['label' .. i], 
    --             parent.args[unnamed[i]], p.getBackgroundColor(i, itemsPerRow))
    --         i = i + 1
    --     end
    --     result = result .. '</div>'
    -- end

    return result
end

return p