Module:Squad: Difference between revisions

From TwogPedia
(Created page with "local Class = require('Module:Class') local Arguments = require('Module:Arguments') local String = require('Module:StringUtils') local Squad = Class.new() Squad.TYPE_ACTIVE = 0 Squad.TYPE_INACTIVE = 1 Squad.TYPE_FORMER = 2 Squad.TYPE_FORMER_INACTIVE = 3 function Squad:init(frame) self.frame = frame self.args = Arguments.getArgs(frame) self.root = mw.html.create('div') self.root :addClass('table-responsive') -- TODO: is this needed? :css('margin-bottom', '1...")
 
No edit summary
Line 1: Line 1:
local Class = require('Module:Class')
local getArgs = require('Module:Arguments').getArgs
local Arguments = require('Module:Arguments')
local String = require('Module:StringUtils')


local Squad = Class.new()
local TestFlags = require('Module:TestFlags')
local Squad = {}


Squad.TYPE_ACTIVE = 0
function Squad.run(frame)
Squad.TYPE_INACTIVE = 1
local args = getArgs(frame)
Squad.TYPE_FORMER = 2
Squad.TYPE_FORMER_INACTIVE = 3
-- Get all unique properties from Person templates
 
properties = {}
function Squad:init(frame)
for key, value in ipairs (args) do
self.frame = frame
local values = mw.text.jsonDecode(value)
self.args = Arguments.getArgs(frame)
for key2, value2 in pairs ( values ) do
self.root = mw.html.create('div')
if  string.find(mw.text.jsonEncode(properties), '"' .. key2 .. '"') then
self.root :addClass('table-responsive')
-- TODO: is this needed?
else
:css('margin-bottom', '10px')
table.insert(properties, key2)
-- TODO: is this needed?
end
:css('padding-bottom', '0px')
end
 
self.content = mw.html.create('table')
self.content:addClass('wikitable wikitable-striped roster-card')
 
if not String.isEmpty(self.args.team) then
self.args.isLoan = true
end
end
properties = mw.text.jsonEncode(properties)
local tableContainer = mw.html.create('table')
tableContainer:addClass('striped-table')
local titleRow = mw.html.create('tr')
titleRow:addClass('headerRow')


local status = (self.args.status or 'active'):lower()
local idCell = mw.html.create('th')
 
idCell:wikitext('ID')
if status == 'inactive' then
local nameCell = mw.html.create('th')
self.type = Squad.TYPE_INACTIVE
nameCell:wikitext('Name')
elseif status == 'former' then
local positionCell = mw.html.create('th')
self.type = Squad.TYPE_FORMER
positionCell:addClass('position')
else
positionCell:wikitext('Position')
self.type = Squad.TYPE_ACTIVE
local joindateCell = mw.html.create('th')
joindateCell:wikitext('Join Date')
titleRow:node(idCell):node(nameCell):node(positionCell):node(joindateCell)
-- If status is not active, then see what other th tags need to be added
if ( args.status ~= 'active' ) then
if ( string.find(properties, '"leavedate"') ) then
local leaveDate = mw.html.create('th')
leaveDate:wikitext('Leave Date')
titleRow:node(leaveDate)
end
end
end
 
return self
titleRow:node(titleHeader)
end
 
tableContainer:node(titleRow)
function Squad:title()
local defaultTitle
local gameCategory = mw.text.split(mw.title.getCurrentTitle().text, '/')[1]
if self.type == Squad.TYPE_FORMER then
defaultTitle = 'Former Squad'
-- Go through all the Person template instances
elseif self.type == Squad.TYPE_INACTIVE then
for key, value in ipairs (args) do
defaultTitle = 'Inactive Squad'
local values = mw.text.jsonDecode(value)
else
defaultTitle = 'Active Squad'
local personRow = mw.html.create('tr')
end
personRow:addClass('person')
 
local id = mw.html.create('td')
local titleText = String.isEmpty(self.args.title) and defaultTitle or self.args.title
id:addClass('playerID')
 
if ( values.flag ) then
local titleContainer = mw.html.create('tr')
flag = TestFlags.icon(values.flag, gameCategory)
 
id:wikitext(flag)
local titleRow = mw.html.create('th')
end
titleRow:addClass('large-only')
:attr('colspan', '1')
--Check if page with id exists and if it does, then link to it, otherwise string only
:wikitext(titleText)
local pageExists = frame:callParserFunction('#ifexist', values.id, 'yes', 'no' )
 
local titleRow2 = mw.html.create('th')
if ( pageExists == 'yes' ) then
titleRow2:addClass('large-only')
id:wikitext('<span>[[' .. values.id .. '|' .. values.id .. ']]</span>')
:attr('colspan', '10')
else
:addClass('roster-title-row2-border')
id:wikitext('<span>'.. values.id ..'</span>')
:wikitext(titleText)
end
 
titleContainer:node(titleRow):node(titleRow2)
if ( values.captain ) then
self.content:node(titleContainer)
id:wikitext('<span class="captain">[[File:Captain-32px.png|Captain|link=Category:'.. gameCategory .. '/Captains|Captain]]</span>')
 
end
return self
end
local name = mw.html.create('td')
 
name:addClass('playerName')
function Squad:header()
name:wikitext(values.name)
local makeHeader = function(wikiText)
local position = mw.html.create('td')
local headerCell = mw.html.create('th')
position:addClass('position')
 
position:wikitext(values.position)
if wikiText == nil then
local joindate = mw.html.create('td')
return headerCell
joindate:wikitext(values.joindate)
personRow:node(id):node(name):node(position):node(joindate)
-- If not active squad, then go add additional columns to table
if ( args.status ~= 'active' ) then
if ( string.find(properties, '"leavedate"') ) then
local leaveText = mw.html.create('td')
leaveText:wikitext(values.leavedate)
personRow:node(leaveText)
end
if ( string.find(properties, '"newteam"') ) then
local newTeam = mw.html.create('td')
newTeam:wikitext(values.newteam)
personRow:node(newTeam)
end
end
end
 
return headerCell:wikitext(wikiText):addClass('divCell')
titleRow:node(personRow)
end
 
local headerRow = mw.html.create('tr'):addClass('HeaderRow')
 
headerRow :node(makeHeader('ID'))
:node(makeHeader()) -- "Team Icon" (most commmonly used for loans)
:node(makeHeader('Name'))
:node(makeHeader()) -- "Role"
:node(makeHeader('Join Date'))
if self.type == Squad.TYPE_FORMER then
headerRow :node(makeHeader('Leave Date'))
:node(makeHeader('New Team'))
elseif self.type == Squad.TYPE_INACTIVE then
headerRow:node(makeHeader('Inactive Date'))
end
end
 
self.content:node(headerRow)
return tableContainer
 
return self
end
 
function Squad:row(row)
self.content:node(row)
return self
end
 
function Squad:create()
self.root:node(self.content)
 
return self.root
end
end


return Squad
return Squad

Revision as of 20:37, 25 August 2022

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

local getArgs = require('Module:Arguments').getArgs

local TestFlags = require('Module:TestFlags')
local Squad = {}

function Squad.run(frame)
	local args = getArgs(frame)
	
	-- Get all unique properties from Person templates
	properties = {}
	for key, value in ipairs (args) do
		local values = mw.text.jsonDecode(value)
		for key2, value2 in pairs ( values ) do
			if  string.find(mw.text.jsonEncode(properties), '"' .. key2 .. '"') then
			
			else
				table.insert(properties, key2)
			end
		end
	end
	
	properties = mw.text.jsonEncode(properties)
		
	local tableContainer = mw.html.create('table')
	tableContainer:addClass('striped-table')
	
	local titleRow = mw.html.create('tr')
	titleRow:addClass('headerRow')

	local idCell = mw.html.create('th')
		idCell:wikitext('ID')
	local nameCell = mw.html.create('th')
		nameCell:wikitext('Name')
	local positionCell = mw.html.create('th')
		positionCell:addClass('position')
		positionCell:wikitext('Position')
	local joindateCell = mw.html.create('th')
		joindateCell:wikitext('Join Date')
		
	titleRow:node(idCell):node(nameCell):node(positionCell):node(joindateCell)
	
	-- If status is not active, then see what other th tags need to be added
	if ( args.status ~= 'active' ) then
		if ( string.find(properties, '"leavedate"') ) then
			local leaveDate = mw.html.create('th')
				leaveDate:wikitext('Leave Date')
				titleRow:node(leaveDate)
		end
	end
	
	titleRow:node(titleHeader)
	
	tableContainer:node(titleRow)
	
	local gameCategory = mw.text.split(mw.title.getCurrentTitle().text, '/')[1]
	
	-- Go through all the Person template instances
	for key, value in ipairs (args) do
		local values = mw.text.jsonDecode(value)
		
		local personRow = mw.html.create('tr')
			personRow:addClass('person')
		local id = mw.html.create('td')
			id:addClass('playerID')
		if ( values.flag ) then
			flag = TestFlags.icon(values.flag, gameCategory)
			id:wikitext(flag)
		end
		
		--Check if page with id exists and if it does, then link to it, otherwise string only
		local pageExists = frame:callParserFunction('#ifexist', values.id, 'yes', 'no' )
	
		if ( pageExists == 'yes' ) then 
			id:wikitext('<span>[[' .. values.id .. '|' .. values.id .. ']]</span>')
		else
			id:wikitext('<span>'.. values.id ..'</span>')
		end
		
		if ( values.captain ) then
			id:wikitext('<span class="captain">[[File:Captain-32px.png|Captain|link=Category:'.. gameCategory .. '/Captains|Captain]]</span>')
		end
			
		local name = mw.html.create('td')
			name:addClass('playerName')
			name:wikitext(values.name)
		local position = mw.html.create('td')
			position:addClass('position')
			position:wikitext(values.position)
		local joindate = mw.html.create('td')
			joindate:wikitext(values.joindate)
		
		personRow:node(id):node(name):node(position):node(joindate)
		
		-- If not active squad, then go add additional columns to table
		if ( args.status ~= 'active' ) then
			if ( string.find(properties, '"leavedate"') ) then
				local leaveText = mw.html.create('td')
					leaveText:wikitext(values.leavedate)
				personRow:node(leaveText)
			end
			if ( string.find(properties, '"newteam"') ) then
				local newTeam = mw.html.create('td')
					newTeam:wikitext(values.newteam)
				personRow:node(newTeam)
			end
		end
		
		titleRow:node(personRow)
	end
	
	return tableContainer
end

return Squad