Module:Reload

From Call of Duty Esports Wiki
Jump to: navigation, search

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

local util_args = require('Module:ArgsUtil')
local util_table = require('Module:TableUtil')
local util_vars = require('Module:VarsUtil')

local h = {}
local p = {}

function p.intro(widget, template, args)
	if not args then args = {} end
	if util_vars.getBool('isReload') then
		return mw.html.create()
	end
	args.isReload = 'Yes'
	local output = mw.html.create('div')
		:addClass('template-reload')
		:attr('data-for-template', template)
		:attr('data-target-id', h.getKey(util_vars.setGlobalIndex('tr_key')))
	if args and next(args) then
		output:attr('data-default-args', h.concatArgs(args))
	end
	output:wikitext(mw.getCurrentFrame():callParserFunction{
		name = '#widget',
		args = { widget }
	})
	return output
end

function p.wrapper()
	if util_vars.getBool('isReload') then
		return mw.html.create()
	end
	local output = mw.html.create('div')
		:attr('id', h.getKey(util_vars.getGlobalIndex('tr_key')))
	return output
end

function p.isReload()
	return util_vars.getBool('isReload')
end

function h.getKey(i)
	return ('template-reload-%s'):format(i)
end

function h.concatArgs(args)
	local transformedArgs = {}
	for k, v in pairs(args) do
		transformedArgs[#transformedArgs+1] = ('%s=%s'):format(k, v)
	end
	return util_table.concat(transformedArgs, '', h.formatArg)
end

function h.formatArg(arg)
	return ('|%s'):format(arg)
end

return p