Documentation for this module may be created at Module:ToggleUtil/doc
local util_vars = require('Module:VarsUtil')
local i18n = require('Module:I18nUtil')
local h = {}
function h.getShowData(data, isSection)
local default_text = isSection and 'Show All' or 'show'
local new_data = mw.clone(data)
new_data.text = data.show_text or default_text
new_data.verb = 'show'
new_data.id = data.show_id
return new_data
end
function h.getHideData(data, isSection)
local default_text = isSection and 'Hide All' or 'hide'
local new_data = mw.clone(data)
new_data.text = data.hide_text or default_text
new_data.verb = 'hide'
new_data.show_attr = data.hide_attr
new_data.hide_attr = data.show_attr
new_data.hide_class = data.show_class
new_data.initshown = not data.initshown
new_data.id = data.hide_id
return new_data
end
function h.addButton(div, data, addSectionClass)
local span = div:tag('span')
:addClass('sections-toggler')
:attr('data-toggler-hiddenclass', data.hiddenclass)
:attr('data-toggler-show', data.show_attr)
:attr('data-toggler-hide', data.hide_attr)
:attr('id', data.id)
:addClass(data.hide_class or data.hide_attr) -- hide yourself, and get shown by other
:wikitext(data.text)
if data.initshown then
span:addClass(data.hiddenclass or 'toggle-section-hidden')
end
if addSectionClass then
span:addClass('toggler-section-' .. data.verb) -- purely for style
end
end
local p = {}
-- to be used by a parent function before sending stuff here
-- replaces instances in classes as needed with the current week
function p.prepDataByWeek(data, i)
data.show_attr = data.show_attr:format(i)
data.hide_attr = data.hide_attr:format(i)
data.show_class = data.show_class:format(i)
data.hide_class = data.hide_class:format(i)
end
function p.prepDataByWeekAndGame(data, i, j)
data.show_attr = data.show_attr:format(i, j)
data.hide_attr = data.hide_attr:format(i, j)
data.show_class = data.show_class:format(i, i, j)
data.hide_class = data.hide_class:format(i, i, j)
end
-- to be called directly from a parent function
function p.printToggleHeader(tbl, colspan, display, data)
local th = tbl:tag('tr')
:tag('th')
:attr('colspan',colspan or 1)
local div = th:tag('div')
:cssText('position:relative;')
:tag('div')
:addClass('table-header-showhide')
p.printToggleButton(div, data)
th:wikitext(display)
end
-- in theory could be called directly but mostly should be called from above
function p.printToggleButton(div, data)
div:wikitext('[')
h.addButton(div, h.getShowData(data))
h.addButton(div, h.getHideData(data))
div:wikitext(']')
end
-- intended to be called externally, makes a section Show All button
function p.printSectionToggler(tbl, data)
if not tbl then tbl = mw.html.create() end
local div = tbl:tag('div'):addClass('expand-contract-button'):addClass(data.cssclass)
local showData = h.getShowData(data, true)
local hideData= h.getHideData(data, true)
h.addButton(div, showData, true)
h.addButton(div, hideData, true)
return tbl
end
-- separate from the above
-- this assumes a simple single class & single attr
function p.initSectionToggler()
util_vars.setGlobalIndex('SECTION_TOGGLE_SHOW')
util_vars.setGlobalIndex('SECTION_TOGGLE_HIDE')
end
function p.tagSectionToggler(tbl, isReversed, hiddenclass)
local show = 'SECTION_SHOW' .. util_vars.getGlobalIndex('SECTION_TOGGLE_SHOW')
local hide = 'SECTION_HIDE' .. util_vars.getGlobalIndex('SECTION_TOGGLE_HIDE')
local showNew = isReversed and hide or show
local hideNew = isReversed and show or hide
tbl:addClass('sections-toggler')
:attr('data-toggler-hiddenclass', hiddenclass)
:attr('data-toggler-show', showNew)
:attr('data-toggler-hide', hideNew)
p.tagSectionToggle(tbl, isReversed, hiddenClass)
end
function p.tagSectionToggle(tbl, isReversed, hiddenclass)
if isReversed then
tbl:addClass(hiddenclass or 'toggle-section-hidden')
tbl:addClass('SECTION_SHOW' .. util_vars.getGlobalIndex('SECTION_TOGGLE_SHOW'))
else
tbl:addClass('SECTION_HIDE' .. util_vars.getGlobalIndex('SECTION_TOGGLE_HIDE'))
end
end
-- option from list stuff
-- call printOptionFromListTogglers to make the toggles, and oflCellClasses to make the toggled data
-- if the toggled data is physically above the togglers, then call oflInit before doing anything else
function h.oflInitClasses(data, n)
if data.classes then return end
data.classes = {}
for k, v in ipairs(data.order) do
data.classes[v] = ('ofl-toggle-%s-%s'):format(n, k)
end
end
function h.oflPrintOneToggler(tbl, data, val, text, isActive)
if not tbl then tbl = mw.html.create() end
local span = tbl:tag('span')
:addClass('optionfromlist-toggler')
:addClass(data.section)
:attr('data-toggle-section', data.section)
:attr('data-toggler-show', data.classes[val])
:attr('data-toggler-hide', data.all)
:wikitext(h.getDisplay(data, val))
if (data.init or data.order[1]) == val then
span:addClass('active')
end
if data.hiddenclass then
span:attr('data-toggler-hiddenclass', data.hiddenclass)
end
return span
end
function h.getDisplay(data, val)
local output
if i18n.exists() then
output = i18n.print(val .. 'Toggle')
end
if not output then
output = data.displays[val] or val
end
return output
end
function h.oflTogglersAttrs(span, attrs, val)
if not attrs then return end
for attr, data in pairs(attrs) do
span:attr(attr, data[val])
end
end
function p.oflInit(data, frame)
if not frame then frame = mw.getCurrentFrame() end
local n = util_vars.setGlobalIndex('OFLToggler')
h.oflInitClasses(data, n)
data.all = data.all or ('ofl-toggler-%s-all'):format(n)
data.section = data.section or ('ofl-toggler-%s-section'):format(n)
data.init = data.init or data.order[1]
end
function p.printOptionFromListTogglers(tbl, data)
p.oflInit(data)
if not tbl then tbl = mw.html.create() end
if not data.displays then data.displays = {} end
local len = #data.order
for i, val in ipairs(data.order) do
local span = h.oflPrintOneToggler(tbl, data, val)
h.oflTogglersAttrs(span, data.attrs, val)
if i < len then
tbl:wikitext(data.sep or '|')
end
end
return tbl
end
function p.oflCellClasses(tbl, data, this)
tbl:addClass(data.classes[this])
:addClass(data.all)
if this ~= data.init then
tbl:addClass(data.hiddenclass or 'toggle-section-hidden')
end
end
-- always active stuff
function p.printSepToggler(tbl, data, isHideButton)
local suffix = '_opposite'
local class = isHideButton and (data.class .. suffix) or data.class
local otherClass = isHideButton and data.class or (data.class .. suffix)
local showText = data.show_text or 'show'
local hideText = data.hide_text or 'hide'
local text = isHideButton and hideText or showText
local span = tbl:tag('span')
:addClass('sections-toggler')
:wikitext(text or 'kitten')
:attr('data-toggler-show', class)
:attr('data-toggler-hide', otherClass)
end
function p.sepCellClasses(tbl, data, isActive)
local class = isActive and (data.class .. '_opposite') or data.class
tbl:addClass(class)
if not isActive then
tbl:addClass(data.hiddenclass or 'toggle-section-hidden')
end
end
return p