Module:LeaguesNavbox

From Leaguepedia | League of Legends Esports Wiki
Jump to: navigation, search

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

local util_args = require('Module:ArgsUtil')
local util_cargo = require('Module:CargoUtil')
local util_html = require('Module:HtmlUtil')
local util_map = require('Module:MapUtil')
local util_sort = require('Module:SortUtil')
local util_table = require('Module:TableUtil')
local util_text = require('Module:TextUtil')
local util_title = require('Module:TitleUtil')
local util_toggle = require("Module:ToggleUtil")
local util_vars = require("Module:VarsUtil")
local i18n = require('Module:i18nUtil')

local VALID_ARGS = { 'group', 'list', 'basepages' }

local h = {}

local p = {}
function p.main(frame)
	local args = util_args.merge()
	i18n.init('LeaguesNavbox')
	h.processArgs(args)
	return h.makeNavbox(args)
end

function h.processArgs(args)
	local i = 1
	while h.hasValidArg(args, i) do
		h.processList(args, i)
		h.processBasepageArg(args, i)
		i = i + 1
	end
end

function h.hasValidArg(args, i)
	for _, k in ipairs(VALID_ARGS) do
		if args[k .. i] then
			return true
		end
	end
	return false
end

function h.processList(args, i)
	-- mostly for child navboxes
	if not args['list' .. i] then return end
	args['list' .. i] = mw.getCurrentFrame():preprocess(args['list' .. i])
end

function h.processBasepageArg(args, i)
	if not args['basepages' .. i] then return end
	args['group' .. i] = args['group' .. i] or i18n.print('group1')
	args['list' .. i] = h.getToplist(args, i)
end

function h.getToplist(args, i)
	return tostring(util_html.makeFlatlist(h.getToplistData(args, i)))
end

function h.getToplistData(args, i)
	local data = h.getLeagueData(args['basepages' .. i])
	h.sortData(data, util_text.split(args['basepages' .. i]))
	util_map.inPlace(data, h.makeLeagueDisplay, args['titlepart' .. i])
	return data
end

function h.getLeagueData(basepages)
	local result = util_cargo.queryAndCast(h.getLeagueQuery(basepages))
	return result
end

function h.getLeagueQuery(basepages)
	local ret = {
		tables = { 'TournamentTabs=TT', 'TournamentTabs__Events=Ev' },
		join = { 'TT._ID=Ev._rowID' },
		fields = {
			'TT.BasePage',
			'TT.BasePageDisplay',
			'TT._pageName',
		},
		where = util_cargo.whereFromArgList('TT.BasePage="%s"', basepages),
		oneToMany = {
			groupBy = { '_pageName' },
			fields = {
				Events = 'Ev._value=Event',
			}
		},
		orderBy = 'TT._pageName, Ev._position',
	}
	return ret
end

function h.sortData(data, basepageTable)
	-- sort according to the input order given by the user
	local hash = util_table.hash(basepageTable)
	for _, row in ipairs(data) do
		row.sortkey = hash[row.BasePage]
	end
	util_sort.tablesByKeys(data, {'sortkey', 'index'}, { true, true })
end

function h.makeLeagueDisplay(row, titlepart)
	local output = mw.html.create('span')
		:wikitext(util_text.intLink(row.BasePage, h.getDisplay(row, titlepart)))
	h.printPopupButton(output, row)
	return tostring(output)
end

function h.getDisplay(row, titlepart)
	if row.BasePageDisplay then return row.BasePageDisplay end
	return util_title.titleparts(
		util_title.concatSubpage(row.BasePage),
		1, -- number of parts to return
		tonumber(titlepart or 1)
	)
end

function h.printPopupButton(output, row)
	local button = util_toggle.popupButton(output)
	button.inner:addClass('navbox-event-list')
	h.printEventList(
		button.inner,
		util_map.inPlace(row.Events, h.linkEvent, row.BasePage)
	)
end

function h.linkEvent(eventRow, basePage)
	return util_text.intLink(
		util_title.concatSubpage(basePage, eventRow.Event),
		eventRow.Event
	)
end

function h.printEventList(div, eventList)
	local ul = div:tag('ul')
	for _, v in ipairs(eventList) do
		ul:tag('li')
			:wikitext(v)
	end
end

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

return p