Module:clickable button
Appearance
- This module lacks a documentation subpage. Please create it.
- Useful links: subpage list • links • transclusions • testcases • sandbox
local export = {}
local M = require("Module:module loader").init({
require = {
parameters = "Module:parameters",
yesno = "Module:yesno",
},
})
local OLD_STYLE_CLASS_SET = {
["ui-button-red"] = true,
["ui-button-pink"] = true,
["ui-button-purple"] = true,
["ui-button-deep-purple"] = true,
["ui-button-indigo"] = true,
["ui-button-blue"] = true,
["ui-button-light-blue"] = true,
["ui-button-cyan"] = true,
["ui-button-teal"] = true,
["ui-button-green"] = true,
["ui-button-light-green"] = true,
["ui-button-lime"] = true,
["ui-button-yellow"] = true,
["ui-button-amber"] = true,
["ui-button-orange"] = true,
["ui-button-deep-orange"] = true,
["ui-button-brown"] = true,
["ui-button-grey"] = true,
["ui-button-blue-grey"] = true,
["ui-button-black"] = true,
}
local function is_old_style_class(class)
return class and OLD_STYLE_CLASS_SET[class]
end
function export.main(frame)
local parent = frame:getParent()
local raw_args = parent and parent.args or frame.args
local args = M.parameters.process(raw_args, {
[1] = true,
[2] = true,
url = true,
text = true,
class = true,
style = true,
category = true,
}, true)
return export.lua_main(args)
end
function export.lua_main(args)
if not args[1] and not args.url then
return ''
end
local data = export.make_link_data(args)
local link = export.render_link(data)
local tracking_categories = export.render_tracking_categories(args)
return link .. tracking_categories
end
function export.make_link_data(args)
local data = {}
-- Get the link and display values, and find whether we are outputting a
-- wikilink or a URL.
if args.url then
data.is_url = true
data.link = args.url
if args[1] then
data.display = args[1]
else
data.display = args.text or args.url
end
else
data.is_url = false
data.link = args[1]
if args[2] then
data.display = args[2]
else
data.display = args[1]
end
end
-- Classes
local class = args.class and args.class:lower()
data.classes = {}
if is_old_style_class(class) then
table.insert(
data.classes,
'submit ui-button ui-widget ui-state-default ui-corner-all'
.. ' ui-button-text-only ui-button-text'
)
else
table.insert(data.classes, 'mw-ui-button')
end
if class then
table.insert(data.classes, class)
end
-- Styles
do
--[[
-- Check whether we are on the same page as we have specified in
-- args[1], but not if we are using a URL link, as then args[1] is only
-- a display value. If we are currently on the page specified in
-- args[1] make the button colour darker so that it stands out from
-- other buttons on the page.
--]]
local success, link_title, current_title
if not data.is_url then
current_title = mw.title.getCurrentTitle()
success, link_title = pcall(mw.title.new, args[1])
end
if success
and link_title
and mw.title.equals(current_title, link_title)
then
if class == 'ui-button-red'
or class == 'mw-ui-progressive'
then
data.background_color = '#F44336'
elseif class == 'ui-button-pink'
or class == 'mw-ui-constructive'
then
data.background_color = '#E91E63'
elseif class == 'ui-button-purple'
or class == 'mw-ui-constructive'
then
data.background_color = '#9C27B0'
elseif class == 'ui-button-deep-purple'
or class == 'mw-ui-constructive'
then
data.background_color = '#673AB7'
elseif class == 'ui-button-indigo'
or class == 'mw-ui-constructive'
then
data.background_color = '#3F51B5'
elseif class == 'ui-button-blue'
or class == 'mw-ui-constructive'
then
data.background_color = '#2196F3'
elseif class == 'ui-button-light-blue'
or class == 'mw-ui-constructive'
then
data.background_color = '#03A9F4'
elseif class == 'ui-button-cyan'
or class == 'mw-ui-constructive'
then
data.background_color = '#00BCD4'
elseif class == 'ui-button-teal'
or class == 'mw-ui-constructive'
then
data.background_color = '#009688'
elseif class == 'ui-button-green'
or class == 'mw-ui-constructive'
then
data.background_color = '#4CAF50'
elseif class == 'ui-button-light-green'
or class == 'mw-ui-constructive'
then
data.background_color = '#8BC34A'
elseif class == 'ui-button-lime'
or class == 'mw-ui-constructive'
then
data.background_color = '#CDDC39'
elseif class == 'ui-button-yellow'
or class == 'mw-ui-constructive'
then
data.background_color = '#FFEB3B'
elseif class == 'ui-button-amber'
or class == 'mw-ui-constructive'
then
data.background_color = '#FFC107'
elseif class == 'ui-button-orange'
or class == 'mw-ui-constructive'
then
data.background_color = '#FF9800'
elseif class == 'ui-button-deep-orange'
or class == 'mw-ui-constructive'
then
data.background_color = '#FF5722'
elseif class == 'ui-button-brown'
or class == 'mw-ui-constructive'
then
data.background_color = '#795548'
elseif class == 'ui-button-grey'
or class == 'mw-ui-constructive'
then
data.background_color = '#9E9E9E'
elseif class == 'ui-button-blue-grey'
or class == 'mw-ui-destructive'
then
data.background_color = '#607D8B'
elseif class == 'ui-button-black'
or class == 'mw-ui-destructive'
then
data.background_color = '#000000'
else
data.background_color = '#CCC'
data.color = '#666'
end
end
-- Add user-specified styles.
data.style = args.style
end
return data
end
function export.render_link(data)
-- Render the display span tag.
local display
do
local display_span = mw.html.create('span')
for i, class in ipairs(data.classes or {}) do
display_span:addClass(class)
end
display_span
:attr('role', 'button')
:attr('aria-disabled', 'false')
:css{
['background-color'] = data.background_color,
color = data.color
}
if data.style then
display_span:cssText(data.style)
end
display_span:wikitext(data.display)
display = tostring(display_span)
end
-- Render the link
local link
if data.is_url then
link = string.format('[%s %s]', data.link, display)
else
link = string.format('[[%s|%s]]', data.link, display)
end
return string.format('<span class="plainlinks">%s</span>', link)
end
function export.render_tracking_categories(args)
if M.yesno(args.category) == false then
return ''
end
local class = args.class and args.class:lower()
if is_old_style_class(class) then
return '[[Category:Pages using old style ui-button-color]]'
else
return ''
end
end
return export