Module:FeaturedLeagues

local util_args = require('Module:ArgsUtil') local util_table = require('Module:TableUtil') local util_title = require('Module:TitleUtil') local util_vars = require('Module:VarsUtil') local SETTINGS = mw.loadData('Module:FeaturedLeagues/Settings') local REGION local TOURNAMENT

local h = {}

local p = {} function p.main(frame) local args = util_args.merge(true) local tabs = h.getTabsFromArgs(args) REGION = tonumber(args.showregion) TOURNAMENT = tonumber(args.showtournament) return h.makeOutput(tabs) end

function h.getTabsFromArgs(args) local ret = {} for i = 1, SETTINGS.maxregions do if args['r' .. i] and util_args.castAsBool(args['r' .. i .. 'active']) then ret[#ret+1] = args['r' .. i] ret[args['r' .. i]] = h.getSecondLevelTabs(args, i)		else ret[#ret+1] = false end end return ret end

function h.getSecondLevelTabs(args, i)	local ret = {} for j = 1, SETTINGS.maxtournaments do		if args[('r%st%s'):format(i, j)] and util_args.castAsBool(args[('r%st%sactive'):format(i, j)]) then ret[#ret+1] = args['r' .. i .. 't' .. j]		else ret[#ret+1] = false end end return ret end

function h.makeOutput(tabs) local output = mw.html.create('div') :addClass('fl-section') h.printTopLevelTabs(output, tabs) h.printAllSecondLevelTabs(output, tabs) h.printAllContent(output, tabs) return output end

function h.topClass(i) return ('flfp-top-%s'):format(i) end

function h.bottomClass(i, j)	return ('flfp-bottom-%s-%s'):format(i, j) end

function h.sectionClass(i, j)	return ('flfp-section-%s-%s'):format(i, j) end

function h.printTopLevelTabs(output, tabs) local div = output:tag('div') :addClass('titletabs-tabs') for i, region in ipairs(tabs) do		if region then h.printTopTab(div, i, region, tabs[region]) end end end

function h.printTopTab(div, i, region, regionTabs) local inner = div:tag('div') :addClass('titletabs-tab') :addClass('flfp-alltabs') :addClass(h.topClass(i)) :attr('data-toggle-section', 'flfp-alltabs') :attr('data-toggler-setactive', h.getFirstBottomClass(i, regionTabs)) :attr('data-toggler-show', h.getFirstSectionClass(i, regionTabs)) :attr('data-toggler-hide', 'flfp-sections, .flfp-bottoms') :attr('data-replaceid', h.getFirstSectionClass(i, regionTabs)) :wikitext(region) if i == REGION then inner:addClass('active') inner:addClass('optionfromlist-toggler') else inner:addClass('optionfromlist-toggler-lazy') end end

function h.getFirstSectionClass(i, regionTabs) return h.sectionClass(i, h.getFirstIndex(regionTabs)) end

function h.getFirstBottomClass(i, regionTabs) return h.bottomClass(i, h.getFirstIndex(regionTabs)) end

function h.getFirstIndex(regionTabs) for j, region in ipairs(regionTabs) do		if region then return j		end end end

function h.printAllSecondLevelTabs(output, tabs) for i, region in ipairs(tabs) do		if region then local div = output:tag('div') :addClass('titletabs-tabs titletabs-row2') :addClass('flfp-bottoms') if i ~= REGION then div:addClass('toggle-section-hidden') end h.printSetOfSecondLevelTabs(div, tabs[region], i)		end end end

function h.printSetOfSecondLevelTabs(div, regionTabs, i)	for j, tournament in ipairs(regionTabs) do		if tournament then h.printBottomTab(div, i, j, tournament, regionTabs) end end end

function h.printBottomTab(div, i, j, tournament, regionTabs) div:addClass(h.getFirstSectionClass(i, regionTabs)) local inner = div:tag('div') :addClass('titletabs-tab') :addClass('flfp-alltabs') :addClass(h.bottomClass(i, j)) :attr('data-toggle-section', 'flfp-alltabs') :attr('data-toggler-setactive', h.topClass(i)) :attr('data-toggler-show', h.sectionClass(i, j)) :attr('data-toggler-hide', 'flfp-sections') :attr('data-replaceid', h.sectionClass(i, j)) :wikitext(tournament) if i == REGION and j == TOURNAMENT then inner:addClass('active') inner:addClass('optionfromlist-toggler') else inner:addClass('optionfromlist-toggler-lazy') end end

function h.printAllContent(output, tabs) for i, region in ipairs(tabs) do		if region then h.printRegionContent(output, i, region, tabs) end end end

function h.printRegionContent(output, i, region, tabs) for j, event in ipairs(tabs[region]) do		if event then local container = output:tag('div') :addClass('fl-container') :addClass('flfp-sections') :addClass(h.sectionClass(i, j)) :attr('id', h.sectionClass(i, j)) if i == REGION and j == TOURNAMENT then container:wikitext(h.getPageEmbed(region, event)) else container:attr('data-templatecode', h.getExpandTitle(region, event)) :addClass('toggle-section-hidden') end end end end

function h.getPageEmbed(region, event) return h.expandTemplate(h.getExpandTitle(region, event)) end

function h.getExpandTitle(region, event) local tbl = { 'Project:Featured Leagues', region, event } return util_table.concat(tbl, '/') end

function h.expandTemplate(title) return mw.getCurrentFrame:expandTemplate{ title = title, args = {} } end

return p