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
Line 1: Line 1:
local p = {}
local p = {}
local lib = require('Module:Feature')


function p.main(frame)
function p.main(frame)
Line 7: Line 6:
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
elseif not args[2] or args[2] == '' then
return args[1] -- Return plain text if no tooltip given
return args[1]
else
else
return p._main(args)
return p._main(args)
Line 19: Line 18:
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()
local out = mw.html.create()
out:wikitext('&zwj;')
 
local toggle = out:tag('span')
-- Build toggle tooltip
:addClass('custom-tt-wrapper')
local toggle = out:tag('span'):addClass('custom-tt-wrapper toggle-tooltip mw-collapsible mw-made-collapsible mw-collapsed giw-collapsible ' .. (args.class or ''))
:addClass('giw-collapsible')
:addClass(args.class or '')
-- move tooltip to detached icon when text is clickable
 
if lib.isNotEmpty(args.link) or (text:find('%[%[') and text:find('%]%]')) then
-- Make the text clickable if a link is provided,
local link = tostring(args.link)
-- or if the text already contains a wiki link.
toggle:wikitext('[[', lib.ternary(link == '1', text, link), '|', text, ']]', '&thinsp;')
if args.link and args.link ~= '' or (text:find('%[%[') and text:find('%]%]')) then
-- infoicon with semi-hidden text for screen readers
local link = tostring(args.link or '')
text = '<span class="screen-reader-text">Tooltip for ' .. text:gsub('%[%[?[^|%]]+|', ''):gsub('%[', ''):gsub('%]', '') .. '</span>&#9432;'
 
if link == '1' then
toggle:wikitext(
'[[', text, ']]'
)
elseif link ~= '' then
toggle:wikitext(
'[[', link, '|', text, ']]'
)
else
toggle:wikitext(text)
end
 
toggle:wikitext('&thinsp;')
 
-- Use an info icon when the text itself is clickable.
local cleanText = text
:gsub('%[%[?[^|%]]+|', '')
:gsub('%[%[', '')
:gsub('%]%]', '')
 
text =
'<span class="screen-reader-text">Tooltip for '
.. cleanText
.. '</span>ⓘ'
end
end
toggle:attr('data-tt-text', text)
toggle:attr('data-tt-text', text)
toggle
 
:tag('span')
-- Tooltip toggle
:addClass('mw-collapsible-toggle mw-collapsible-toggle-collapsed custom-tt toggle-tooltip')
toggle:tag('span')
:wikitext(text)
:addClass('mw-collapsible-toggle')
:done()
:addClass('mw-collapsible-toggle-collapsed')
toggle
:addClass('custom-tt')
:tag('span')
:wikitext(text)
:addClass('mw-collapsible-content custom-tt')
:done()
:css{ display = 'none' }
 
:wikitext(tooltip)
-- Tooltip content
:done()
toggle:tag('span')
:addClass('mw-collapsible-content')
:addClass('custom-tt')
:css('display', 'none')
:wikitext(tooltip)
:done()
 
out:wikitext('&zwj;')
out:wikitext('&zwj;')
 
return out
return out
end
end


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


return p
return p