Module:Tt: Difference between revisions

ReisuDesign (talk | contribs)
Created page with "local p = {} local lib = require('Module:Feature') function p.main(frame) local args = require('Module:Arguments').getArgs(frame, { parentFirst = true, wrappers = { 'Template:Tt' } }) args[1] = args[1] or '<i>Missing text</i>' if args.clean then return p.CleanTT(args[1]) -- Return plain text if tooltip return given elseif lib.isEmpty(args[2]) then return args[1] -- Return plain text if no tooltip given else return p._main(args) end end function p._m..."
 
ReisuDesign (talk | contribs)
No edit summary
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
local p = {}
local p = {}
local lib = require('Module:Feature')


function p.main(frame)
function p.main(frame)
local args = require('Module:Arguments').getArgs(frame, {
local args = require('Module:Arguments').getArgs(frame, {
parentFirst = true,
parentFirst = true,
wrappers = { 'Template:Tt' }
wrappers = {'Template:Tt'}
})
})
args[1] = args[1] or '<i>Missing text</i>'
args[1] = args[1] or '<i>Missing text</i>'


if args.clean then
if args.clean then
return p.CleanTT(args[1]) -- Return plain text if tooltip return given
return p.CleanTT(args[1])
elseif lib.isEmpty(args[2]) then
end
return args[1] -- Return plain text if no tooltip given
 
else
if not args[2] or mw.text.trim(args[2]) == '' then
return p._main(args)
return args[1]
end
end
return p._main(args)
end
end


function p._main(args, options)
function p._main(args)
local text = args[1]
local text = args[1]
local tooltip = mw.text.decode(args[2], true)
local tooltip = mw.text.decode(args[2], true)
local out = mw.html.create()
 
out:wikitext('&zwj;')
local out = mw.html.create('span')
:addClass('custom-tt-wrapper')
-- Build toggle tooltip
:addClass(args.class or '')
local toggle = out:tag('span'):addClass('custom-tt-wrapper toggle-tooltip mw-collapsible mw-made-collapsible mw-collapsed giw-collapsible ' .. (args.class or ''))
 
local visibleText = text
-- move tooltip to detached icon when text is clickable
local cleanText = text
if lib.isNotEmpty(args.link) or (text:find('%[%[') and text:find('%]%]')) then
 
-- Handle explicit link parameter
if args.link and mw.text.trim(tostring(args.link)) ~= '' then
local link = tostring(args.link)
local link = tostring(args.link)
toggle:wikitext('[[', lib.ternary(link == '1', text, link), '|', text, ']]', '&thinsp;')
 
-- infoicon with semi-hidden text for screen readers
visibleText = '[[' ..
text = '<span class="screen-reader-text">Tooltip for ' .. text:gsub('%[%[?[^|%]]+|', ''):gsub('%[', ''):gsub('%]', '') .. '</span>&#9432;'
(link == '1' and text or link) ..
'|' ..
text ..
']]'
 
cleanText = text
 
-- Handle text that is already an internal link
elseif text:find('%[%[') and text:find('%]%]') then
visibleText = text
 
cleanText = text
:gsub('%[%[[^%]|]+|', '')
:gsub('%[%[', '')
:gsub('%]%]', '')
end
end
toggle:attr('data-tt-text', text)
 
toggle
-- Visible text
:tag('span')
out:wikitext(visibleText)
:addClass('mw-collapsible-toggle mw-collapsible-toggle-collapsed custom-tt toggle-tooltip')
 
:wikitext(text)
-- Tooltip trigger
:done()
local trigger = out:tag('span')
toggle
:addClass('custom-tt-trigger')
:tag('span')
:attr('tabindex', '0')
:addClass('mw-collapsible-content custom-tt')
:attr('role', 'button')
:css{ display = 'none' }
:attr('aria-label', 'Tooltip for ' .. cleanText)
:wikitext(tooltip)
:wikitext('ⓘ')
:done()
 
out:wikitext('&zwj;')
-- Tooltip
out:tag('span')
:addClass('custom-tt-content')
:wikitext(tooltip)
 
return out
return out
end
end


--for modules that need the plain text input
function p.CleanTT(tx)
function p.CleanTT(tx)
local clean = tx:gsub('&zwj;<span class="custom%-tt%-wrapper[^>]-data%-tt%-text="(.-)">.-&zwj;', '%1')
return tx:gsub(
-- mw.logObject(clean)
'<span class="custom%-tt%-wrapper[^>]*>.-</span>',
return clean
''
)
end
end


return p
return p