Module:LeaguesNavbox

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 i18n = require('Module:i18nUtil')

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

local h = {}

local p = {} function p.main(frame) local args = util_args.merge(true) 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) return util_cargo.queryAndCast(h.getLeagueQuery(basepages)) end

function h.getLeagueQuery(basepages) local ret = { tables = 'TournamentTabs', fields = { 'BasePage', 'Events' }, where = util_cargo.whereFromArgList('BasePage="%s"', basepages) }	return ret end

function h.sortData(data, basepageTable) local hash = util_table.hash(basepageTable) for _, row in ipairs(data) do		row.sortkey = hash[row.BasePage] end util_sort.tablesByKeys(data, 'sortkey', true) end

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

function h.getLink(basepage, titlepart) return util_text.intLink(basepage, util_title.titleparts(basepage, 1, tonumber(titlepart or 1))) end

function h.printPopupButton(output, basepage, events) local button = util_toggle.popupButton(output) button.inner:addClass('navbox-event-list') h.printEventList(button.inner, h.getEventLinks(basepage, events)) end

function h.getEventLinks(basepage, events) local function linkEvent(event) return util_text.intLink(util_title.concatSubpage(basepage, event), event) end return util_map.inPlace(events, linkEvent) 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