Module:Toolbar: Difference between revisions

m
1 revision
m (1 revision)
 
m (1 revision)
(One intermediate revision by the same user not shown)
Line 1: Line 1:
local p = {}
local p = {}


-- Get the keys of the numerical arguments that are present.
local function getArgNums(args)
local function getArgNums(args)
    local nums = {}
-- Get the keys of the numerical arguments that are present.
    local tinsert = table.insert
local nums = {}
    for k, v in pairs(args) do
local tinsert = table.insert
        if type(k) == 'number' then
for k, v in pairs(args) do
            tinsert(nums, k)
if type(k) == 'number' then
        end
tinsert(nums, k)
    end
end
    table.sort(nums)
end
    return nums
table.sort(nums)
return nums
end
end


local function makeToolbarItems(args)
local function makeToolbarItems(args)
    -- Get numerical argument keys.
local nums = getArgNums(args)
    local nums = getArgNums(args)
local sep = (args.separator or 'pipe') .. '-separator'
    -- Get the separator text.
sep = mw.message.new(sep):plain()
    local sep = (args.separator or 'pipe') .. '-separator'
local ret = {}
    sep = mw.message.new(sep):plain()
local tinsert = table.insert
   
for i, v in ipairs(nums) do
    -- Generate the toolbar items.
tinsert(ret, args[v])
    local ret = {}
end
    local tinsert = table.insert
if #ret > 0 then
    for i, v in ipairs(nums) do
return table.concat(ret, sep)
    tinsert(ret, args[v])
end
    end
    return table.concat(ret, sep)
end
end


local function makeToolbar(args)
function p._main(args)
    return mw.ustring.format(
local toolbarItems = makeToolbarItems(args)
    '<span class="plainlinks%s"%s>(%s)</span>',
if not toolbarItems then return '' end -- Return the blank string if no arguments were specified, rather than returning empty brackets
    type(args.class) == 'string' and ' ' .. args.class or '',
return mw.ustring.format(
    type(args.style) == 'string' and mw.ustring.format(' style="%s"', args.style) or '',
'<span class="plainlinks%s"%s>(%s)</span>',
    makeToolbarItems(args)
type(args.class) == 'string' and ' ' .. args.class or '',
    )
type(args.style) == 'string' and mw.ustring.format(' style="%s"', args.style) or '',
toolbarItems
)
end
end


function p.main(frame)
function p.main(frame)
    -- If called via #invoke, use the args passed into the invoking template.
-- If called via #invoke, use the args passed into the invoking template.
    -- Otherwise, for testing purposes, assume args are being passed directly in.
-- Otherwise, for testing purposes, assume args are being passed directly in.
    local origArgs
local origArgs
    if frame == mw.getCurrentFrame() then
if frame == mw.getCurrentFrame() then
        origArgs = frame:getParent().args
origArgs = frame:getParent().args
        for k, v in pairs(frame.args) do
for k, v in pairs(frame.args) do
        origArgs = frame.args
origArgs = frame.args
        break
break
    end
end
    else
else
        origArgs = frame
origArgs = frame
    end
end
   
-- Strip whitespace and remove nil values
    -- Strip whitespace and remove nil values
local args = {}
    local args = {}
for k, v in pairs(origArgs) do
    for k, v in pairs(origArgs) do
if type(v) == 'string' then
    if type(v) == 'string' then
v = mw.text.trim(v)
    v = mw.text.trim(v)
end
    end
if v ~= '' then
        if v ~= '' then
args[k] = v
            args[k] = v
end
        end
end
    end
return p._main(args)
   
    return makeToolbar(args)
end
end
 
return p
return p
14,061

edits