Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(widget): replace customizable with contexts #4789

Draft
wants to merge 8 commits into
base: squad-core-into-widget
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ local Cell = Widgets.Cell
local Title = Widgets.Title
local TableCell = Widgets.TableCell
local TableRow = Widgets.TableRow
local WidgetTable = Widgets.Table
local WidgetTable = Widgets.TableOld

---@class ApexMapInfobox: MapInfobox
local CustomMap = Class.new(Map)
Expand Down
2 changes: 1 addition & 1 deletion components/prize_pool/commons/prize_pool_base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ local Opponent = OpponentLibraries.Opponent
local OpponentDisplay = OpponentLibraries.OpponentDisplay

local Widgets = require('Module:Widget/All')
local WidgetTable = Widgets.Table
local WidgetTable = Widgets.TableOld
local TableRow = Widgets.TableRow
local TableCell = Widgets.TableCell
local Div = Widgets.Div
Expand Down
4 changes: 2 additions & 2 deletions components/squad/commons/squad_custom.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ local SquadUtils = Lua.import('Module:Squad/Utils')
local CustomSquad = {}

---@param frame Frame
---@return string
---@return Widget
function CustomSquad.run(frame)
if not Info.config.squads.allowManual then
error('This wiki does not use manual squad tables')
Expand All @@ -28,7 +28,7 @@ end
---@param playerList table[]
---@param squadType integer
---@param customTitle string?
---@return string
---@return Widget
function CustomSquad.runAuto(playerList, squadType, customTitle)
return SquadUtils.defaultRunAuto(playerList, squadType, Squad, SquadUtils.defaultRow(SquadRow), customTitle)
end
Expand Down
32 changes: 16 additions & 16 deletions components/squad/commons/squad_row.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,17 @@ function SquadRow:id()
table.insert(content, ' ' .. roleIcon)
end

local cell = Widget.TableCellNew{
local cell = Widget.Td{
classes = {'ID'},
content = content,
children = content,
}

local date = self.model.leavedate or self.model.inactivedate
local hasTeam = self.model.extradata.loanedto and mw.ext.TeamTemplate.teamexists(self.model.extradata.loanedto)
local hasTeamRole = hasTeam and self.model.extradata.loanedtorole
local teamNode = Widget.TableCellNew{
local teamNode = Widget.Td{
css = hasTeamRole and {'text-align', 'center'} or nil,
content = {
children = {
hasTeam and mw.ext.TeamTemplate.teamicon(self.model.extradata.loanedto, date) or nil,
hasTeamRole and mw.html.create('small'):tag('i'):wikitext(self.model.extradata.loanedtorole) or nil,
}
Expand All @@ -77,9 +77,9 @@ end

---@return self
function SquadRow:name()
table.insert(self.children, Widget.TableCellNew{
table.insert(self.children, Widget.Td{
classes = {'Name'},
content = String.isNotEmpty(self.model.name) and {
children = String.isNotEmpty(self.model.name) and {
mw.html.create('div'):addClass('MobileStuff'):wikitext('(', self.model.name, ')'),
mw.html.create('div'):addClass('LargeStuff'):wikitext(self.model.name),
} or nil
Expand All @@ -92,9 +92,9 @@ end
function SquadRow:role()
local display = String.isNotEmpty(self.model.role) and not RoleIcons[self.model.role:lower()]

table.insert(self.children, Widget.TableCellNew{
table.insert(self.children, Widget.Td{
classes = {'Position'},
content = display and {
children = display and {
mw.html.create('div'):addClass('MobileStuff'):wikitext('Role: '),
mw.html.create('i'):wikitext('(' .. self.model.role .. ')'),
} or nil,
Expand Down Expand Up @@ -123,9 +123,9 @@ function SquadRow:position()
end
end

table.insert(self.children, Widget.TableCellNew{
table.insert(self.children, Widget.Td{
classes = {'Position'},
content = content,
children = content,
})

return self
Expand All @@ -135,9 +135,9 @@ end
---@param cellTitle string?
---@return self
function SquadRow:date(field, cellTitle)
table.insert(self.children, Widget.TableCellNew{
table.insert(self.children, Widget.Td{
classes = {'Date'},
content = self.model[field] and {
children = self.model[field] and {
mw.html.create('div'):addClass('MobileStuffDate'):wikitext(cellTitle),
mw.html.create('div'):addClass('Date')
:tag('i'):wikitext(self.model.extradata[field .. 'display'] or self.model[field])
Expand Down Expand Up @@ -182,15 +182,15 @@ function SquadRow:newteam()
return content
end

table.insert(self.children, Widget.TableCellNew{
table.insert(self.children, Widget.Td{
classes = {'NewTeam'},
content = createContent(),
children = createContent(),
})

return self
end

---@return WidgetTableRowNew
---@return WidgetTr
function SquadRow:create()
-- Set row background for certain roles
local backgrounds = {'Player'}
Expand All @@ -203,7 +203,7 @@ function SquadRow:create()
table.insert(backgrounds, 'roster-coach')
end

return Widget.TableRowNew{
return Widget.Tr{
classes = backgrounds,
children = self.children,
}
Expand Down
55 changes: 20 additions & 35 deletions components/squad/commons/squad_utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

local Arguments = require('Module:Arguments')
local Array = require('Module:Array')
local Class = require('Module:Class')
local Flags = require('Module:Flags')
local Info = require('Module:Info')
local Json = require('Module:Json')
Expand All @@ -22,7 +21,7 @@ local Variables = require('Module:Variables')
local Lpdb = Lua.import('Module:Lpdb')
local Faction = Lua.import('Module:Faction')
local SquadAutoRefs = Lua.import('Module:SquadAuto/References')
local Injector = Lua.import('Module:Widget/Injector')
local SquadContexts = Lua.import('Module:Widget/Contexts/Squad')
local Widget = Lua.import('Module:Widget/All')

local SquadUtils = {}
Expand Down Expand Up @@ -179,17 +178,13 @@ end

---@param frame table
---@param squadWidget SquadWidget
---@param rowCreator fun(player: table, squadType: integer):WidgetTableRowNew
---@param injector WidgetInjector?
---@return string
function SquadUtils.defaultRunManual(frame, squadWidget, rowCreator, injector)
---@param rowCreator fun(player: table, squadType: integer):WidgetTr
---@return Widget
function SquadUtils.defaultRunManual(frame, squadWidget, rowCreator)
local args = Arguments.getArgs(frame)
local props = {
type = SquadUtils.statusToSquadType(args.status) or SquadUtils.SquadType.ACTIVE,
title = args.title,
injector = (injector and injector()) or
(Info.config.squads.hasPosition and SquadUtils.positionHeaderInjector()()) or
nil,
}
local players = SquadUtils.parsePlayers(args)

Expand All @@ -201,36 +196,38 @@ function SquadUtils.defaultRunManual(frame, squadWidget, rowCreator, injector)
return rowCreator(player, props.type)
end)

return tostring(squadWidget(props))
if Info.config.squads.hasPosition then
return SquadContexts.Role{value = SquadUtils.positionHeader(), children = {squadWidget(props)}}
end
return squadWidget(props)
end

---@param players table[]
---@param squadType SquadType
---@param squadWidget SquadWidget
---@param rowCreator fun(person: table, squadType: integer):WidgetTableRowNew
---@param rowCreator fun(person: table, squadType: integer):WidgetTr
---@param customTitle string?
---@param injector? WidgetInjector
---@param personMapper? fun(person: table): table
---@return string
function SquadUtils.defaultRunAuto(players, squadType, squadWidget, rowCreator, customTitle, injector, personMapper)
---@return Widget
function SquadUtils.defaultRunAuto(players, squadType, squadWidget, rowCreator, customTitle, personMapper)
local props = {
type = squadType,
title = customTitle,
injector = (injector and injector()) or
(Info.config.squads.hasPosition and SquadUtils.positionHeaderInjector()()) or
nil,
title = customTitle
}

local mappedPlayers = Array.map(players, personMapper or SquadUtils.convertAutoParameters)
props.children = Array.map(mappedPlayers, function(player)
return rowCreator(player, props.type)
end)

return tostring(squadWidget(props))
if Info.config.squads.hasPosition then
return SquadContexts.Role{value = SquadUtils.positionHeader(), children = {squadWidget(props)}}
end
return squadWidget(props)
end

---@param squadRowClass SquadRow
---@return fun(person: table, squadType: integer):WidgetTableRowNew
---@return fun(person: table, squadType: integer):WidgetTr
function SquadUtils.defaultRow(squadRowClass)
return function(person, squadType)
local squadPerson = SquadUtils.readSquadPersonArgs(Table.merge(person, {type = squadType}))
Expand Down Expand Up @@ -258,21 +255,9 @@ function SquadUtils.defaultRow(squadRowClass)
end
end

---@return WidgetInjector
function SquadUtils.positionHeaderInjector()
local CustomInjector = Class.new(Injector)

function CustomInjector:parse(id, widgets)
if id == 'header_role' then
return {
Widget.TableCellNew{content = {'Position'}, header = true}
}
end

return widgets
end

return CustomInjector
---@return WidgetTh
function SquadUtils.positionHeader()
return {Widget.Th{content = {'Position'}}}
end

return SquadUtils
22 changes: 10 additions & 12 deletions components/squad/wikis/dota2/squad_custom.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,9 @@ local Widget = require('Module:Widget/All')
local Squad = Lua.import('Module:Widget/Squad/Core')
local SquadRow = Lua.import('Module:Squad/Row')
local SquadUtils = Lua.import('Module:Squad/Utils')
local SquadContexts = Lua.import('Module:Widget/Contexts/Squad')

local CustomSquad = {}
local CustomInjector = Class.new(SquadUtils.positionHeaderInjector())

function CustomInjector:parse(id, widgets)
if id == 'header_inactive' then
table.insert(widgets, Widget.TableCellNew{content = {'Active Team'}, header = true})
end

return self._base:parse(id, widgets)
end

---@class Dota2SquadRow: SquadRow
local ExtendedSquadRow = Class.new(SquadRow)
Expand All @@ -48,16 +40,22 @@ function ExtendedSquadRow:activeteam()
end

table.insert(self.children,
Widget.TableCellNew{classes = {'NewTeam'}, content = content}
Widget.Td{classes = {'NewTeam'}, content = content}
)

return self
end

---@param frame Frame
---@return string
---@return Widget
function CustomSquad.run(frame)
return SquadUtils.defaultRunManual(frame, Squad, CustomSquad._playerRow, CustomInjector)
return SquadContexts.Inactive{
value = function(widgets)
table.insert(widgets, Widget.Th{content = {'Inactive Date'}})
return widgets
end,
children = {SquadUtils.defaultRunManual(frame, Squad, CustomSquad._playerRow)}
}
end

function CustomSquad._playerRow(person, squadType)
Expand Down
42 changes: 22 additions & 20 deletions components/squad/wikis/overwatch/squad_custom.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,16 @@ local Widget = require('Module:Widget/All')
local Squad = Lua.import('Module:Widget/Squad/Core')
local SquadRow = Lua.import('Module:Squad/Row')
local SquadUtils = Lua.import('Module:Squad/Utils')
local SquadContexts = Lua.import('Module:Widget/Contexts/Squad')

local CustomSquad = {}
local CustomInjector = Class.new(SquadUtils.positionHeaderInjector())
local HAS_NUMBER = false

function CustomInjector:parse(id, widgets)
if id == 'header_name' and HAS_NUMBER then
table.insert(widgets, 1, Widget.TableCellNew{content = {'Number'}, header = true})
end

return self._base:parse(id, widgets)
end

---@class OverwatchSquadRow: SquadRow
local ExtendedSquadRow = Class.new(SquadRow)

---@return self
function ExtendedSquadRow:number()
table.insert(self.children, Widget.TableCellNew{
table.insert(self.children, Widget.Td{
classes = {'Number'},
content = String.isNotEmpty(self.model.extradata.number) and {
mw.html.create('div'):addClass('MobileStuff'):wikitext('Number: '),
Expand All @@ -48,45 +39,56 @@ function ExtendedSquadRow:number()
end

---@param frame Frame
---@return string
---@return Widget
function CustomSquad.run(frame)
local args = Arguments.getArgs(frame)
local props = {
injector = CustomInjector(),
type = SquadUtils.statusToSquadType(args.status) or SquadUtils.SquadType.ACTIVE,
title = args.title,
}
local players = SquadUtils.parsePlayers(args)

HAS_NUMBER = Array.any(players, Operator.property('number'))
local showNumber = Array.any(players, Operator.property('number'))

props.children = Array.map(players, function(player)
return CustomSquad._playerRow(player, props.type)
return CustomSquad._playerRow(player, props.type, showNumber)
end)

return tostring(Squad(props))
local root = SquadContexts.Role{value = SquadUtils.positionHeader(), children = {Squad(props)}}
if not showNumber then
return root
end

return SquadContexts.HeaderName{
value = function(widgets)
table.insert(widgets, 1, Widget.Th{content = {'Number'}})
return widgets
end,
children = {root},
}
end

---@param playerList table[]
---@param squadType integer
---@param customTitle string?
---@return string?
---@return Widget
function CustomSquad.runAuto(playerList, squadType, customTitle)
return SquadUtils.defaultRunAuto(playerList, squadType, Squad, SquadUtils.defaultRow(SquadRow), customTitle)
end

---@param person table
---@param squadType integer
---@return WidgetTableRowNew
function CustomSquad._playerRow(person, squadType)
---@param showNumber boolean
---@return WidgetTr
function CustomSquad._playerRow(person, squadType, showNumber)
local squadPerson = SquadUtils.readSquadPersonArgs(Table.merge(person, {type = squadType}))
squadPerson.extradata.number = person.number
SquadUtils.storeSquadPerson(squadPerson)

local row = ExtendedSquadRow(squadPerson)

row:id()
if HAS_NUMBER then
if showNumber then
row:number()
end
row:name():position():date('joindate', 'Join Date: ')
Expand Down
Loading