Module:FeaturedPowerRankings

From Fortnite Esports Wiki
Jump to: navigation, search

Documentation for this module may be created at Module:FeaturedPowerRankings/doc

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()
	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
			-- cannot just skip this because |this= is going to be absolute index
			ret[#ret+1] = false
		end
	end
	return ret
end

function h.getSecondLevelTabs(args, i)
	local regionTabs = { length = 0 }
	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
			regionTabs[#regionTabs+1] = args['r' .. i .. 't' .. j]
			regionTabs.length = regionTabs.length + 1
		else
			regionTabs[#regionTabs+1] = false
		end
	end
	return regionTabs
end

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

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

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

function h.sectionClass(i, j)
	return ('prfp-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
		util_vars.log(i)
		if region then
			h.printTopTab(div, i, region, tabs[region])
		end
	end
end

function h.printTopTab(div, i, region, regionTabs)
	local inner = h.initTopTab(div, i, regionTabs)
	inner:wikitext(region)
	h.printTopTabClasses(inner, i, regionTabs)
end

function h.initTopTab(div, i, regionTabs)
	local inner = div:tag('div')
		:addClass('titletabs-tab')
		:addClass('prfp-alltabs')
		:addClass(h.topClass(i))
		:attr('data-toggle-section', 'prfp-alltabs')
		:attr('data-toggler-setactive', h.getFirstBottomClass(i, regionTabs))
		:attr('data-toggler-show', h.getFirstSectionClass(i, regionTabs))
		:attr('data-toggler-hide', 'prfp-sections, .prfp-bottoms')
		:attr('data-replaceid', h.getFirstSectionClass(i, regionTabs))
	return inner
end

function h.printTopTabClasses(inner, i, regionTabs)
	if (TOURNAMENT ~= 1 and i == REGION) or i ~= REGION then
		inner:addClass('optionfromlist-toggler-lazy')
	else
		inner:addClass('optionfromlist-toggler')
	end
	-- we can't use #regionTabs because some could be false
	-- REGION == 1 might be too strong a condition to add active here
	-- but I honestly have no idea what's going on anymore
	-- so whatever, can't make it worse
	-- pls refactor this
	if i == REGION and (TOURNAMENT ~= 1 or regionTabs.length == 1 or REGION == 1 or true) then
		inner:addClass('active')
	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('prfp-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('prfp-alltabs')
		:addClass(h.bottomClass(i, j))
		:attr('data-toggle-section', 'prfp-alltabs')
		:attr('data-toggler-setactive', h.topClass(i))
		:attr('data-toggler-show', h.sectionClass(i, j))
		:attr('data-toggler-hide', 'prfp-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('pr-container')
				:addClass('prfp-sections')
				:addClass(h.sectionClass(i, j))
				:attr('id', h.sectionClass(i, j))
				:attr('data-templatecode', h.getExpandTitle(region, event))
			if i == REGION and j == TOURNAMENT then
				container:wikitext(h.getPageEmbed(region, event))
			else
				container: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 Power Rankings', region, event }
	return util_table.concat(tbl, '/')
end

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

return p