Pergi ke kandungan

Modul:ToggleText

Daripada Wikikamus

--[[
Robust Module:ToggleText for Wiktionary-style usage examples.
This version avoids relying on <span> wrappers (some wikis sanitize them).
It uses <input type="checkbox"> + a superscript label + two <label> wrappers
for the two variants. The variant wrappers are plain <label> elements WITHOUT a
`for` attribute (so clicking the example text doesn't toggle), while the
superscript label **does** have `for=<id>` and toggles the checkbox.

Parameters:
  1 or text1  - first phrase (default shown)
  2 or text2  - second phrase (hidden by default)
  label       - superscript toggle label (defaults to "[eja]")

Usage:
  {{ToggleText|Alik mano ghumoh tuh?|Alék mmano rumoh tuh?|label=[eja]}}

Notes:
  * Requires Template-scoped CSS (Template:ToggleText/style.css) or site CSS.
  * After creating/updating the style page you may need to purge the page that
    uses the template (append ?action=purge or use "Purge" on the page).
]]

local p = {}

local function getArgs(frame)
    local args = frame.args or {}
    if next(args) == nil then
        local parent = frame:getParent()
        if parent then args = parent.args or {} end
    end
    return args
end

function p.main(frame)
    local args = getArgs(frame)
    local t1 = args[1] or args.text1 or ''
    local t2 = args[2] or args.text2 or ''
    local label = args.label or '[eja]'

    -- create stable-ish id using hash of the content
    local id = 'toggletext-' .. mw.hash.hashValue('md5', (t1 or '') .. '||' .. (t2 or '') .. '||' .. label)

    local html = mw.html.create()

    -- Hidden checkbox that drives the CSS
    html:tag('input')
        :attr('type', 'checkbox')
        :addClass('toggletext-box')
        :attr('id', id)

    -- The superscript toggle label (clicking it toggles the checkbox)
    html:tag('label')
        :addClass('toggletext-sup')
        :attr('for', id)
        :wikitext(label)

    -- Variant wrappers. Use <label> (without 'for') because label tags are
    -- commonly allowed; they act as inline containers.
    html:tag('label'):addClass('toggletext-a'):wikitext(t1)
    html:tag('label'):addClass('toggletext-b'):wikitext(t2)

    return tostring(html)
end

return p