-- <nowiki>
--------------------------------------------------------------------------------
-- Creates a hidden spoiler text with an inline toggle that works both on
-- desktop and on mobile
--
-- Syntax:
-- {{#invoke:MobileSpoiler|main|Spoiler text}}
--
-- The labels "[Show Spoiler]" and "[Hide Spoiler]" are localized via
-- [[w:dev:Module:MobileSpoiler/i18n]]
--
-- Second and third parameters can be used to customize the text of the
-- labels on a specific case
--
--------------------------------------------------------------------------------
local p = {}
local getArgs = require('Dev:Arguments').getArgs
local i18n = require('Dev:I18n').loadMessages('MobileSpoiler')
function p.main(frame)
local args = getArgs(frame)
local text = args[1]
if not text or text == '' then return end
local showLabel = args[2] or '[<u>' .. i18n:msg('show') .. '</u>]'
local hideLabel = args[3] or '[<u>' .. i18n:msg('hide') .. '</u>]'
-- This ensures a different ID for each invocation on the same page
local unique = tostring(math.floor(os.clock() * 100000))
local hidden = mw.html.create('span')
:addClass('mw-collapsible mw-collapsed')
:attr('id', 'mw-customcollapsible-spoiler-' .. unique)
:tag('span')
:addClass('mw-customtoggle-spoiler-' .. unique)
:addClass('mw-customtoggle-spoiler-t' .. unique)
:wikitext(hideLabel)
:done()
:wikitext(' ' .. text)
:allDone()
local show = mw.html.create('span')
:addClass('mw-collapsible')
:addClass('mw-customtoggle-spoiler-' .. unique)
:addClass('mw-customtoggle-spoiler-t' .. unique)
:attr('id', 'mw-customcollapsible-spoiler-t' .. unique)
:wikitext(showLabel)
return tostring(hidden) .. tostring(show)
end
return p
Advertisement
Module:MobileSpoiler
Advertisement