Skip to content

Commit bca31f4

Browse files
authored
refactor(match2): remove MatchSummaryFooter class (#7965)
* refactor(match2): get rid of footer class * fixes * lint * fix comments * lint
1 parent bb1671b commit bca31f4

3 files changed

Lines changed: 178 additions & 189 deletions

File tree

‎lua/wikis/chess/MatchSummary.lua‎

Lines changed: 48 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -50,25 +50,16 @@ local KING_ICONS = {
5050
}
5151

5252
---@param args table
53-
---@return Widget
53+
---@return VNode
5454
function CustomMatchSummary.getByMatchId(args)
5555
return MatchSummary.defaultGetByMatchId(CustomMatchSummary, args)
5656
end
5757

58-
---@param match table
59-
---@param createGame fun(date: string, game: table, gameIndex: integer): Widget
60-
---@return Widget[]
61-
function CustomMatchSummary.createBody(match, createGame)
62-
return WidgetUtil.collect(
63-
Array.map(match.games, createGame),
64-
CustomMatchSummary._linksTable(match)
65-
)
66-
end
67-
58+
---@param date string
6859
---@param game MatchGroupUtilGame
6960
---@param gameIndex integer
70-
---@return MatchSummaryRow
71-
function CustomMatchSummary.createGame(game, gameIndex)
61+
---@return VNode
62+
function CustomMatchSummary.createGame(date, game, gameIndex)
7263
return MatchSummaryWidgets.Row{
7364
classes = {'brkts-popup-body-game'},
7465
children = WidgetUtil.collect(
@@ -108,7 +99,7 @@ end
10899

109100
---@param game MatchGroupUtilGame
110101
---@param gameIndex integer
111-
---@return Widget
102+
---@return VNode
112103
function CustomMatchSummary._getCenterContent(game, gameIndex)
113104
return Div{
114105
children = {
@@ -128,7 +119,7 @@ function CustomMatchSummary._getCenterContent(game, gameIndex)
128119
end
129120

130121
---@param gameOpponent table
131-
---@return Widget
122+
---@return VNode
132123
function CustomMatchSummary._getSideIcon(gameOpponent)
133124
return Div{
134125
classes = {'brkts-popup-spaced'},
@@ -137,7 +128,7 @@ function CustomMatchSummary._getSideIcon(gameOpponent)
137128
end
138129

139130
---@param game MatchGroupUtilGame
140-
---@return Widget?
131+
---@return VNode?
141132
function CustomMatchSummary._getHeader(game)
142133
return String.isNotEmpty(game.header) and {
143134
Div{
@@ -151,43 +142,57 @@ function CustomMatchSummary._getHeader(game)
151142
} or nil
152143
end
153144

154-
---@param match any
155-
---@return Widget?
156-
function CustomMatchSummary._linksTable(match)
157-
if Logic.isDeepEmpty(match.links) then
158-
return
159-
end
160-
161-
local rows = Array.map(match.games, function(game, gameIndex)
162-
local links = Table.mapValues(match.links, function(link)
163-
if type(link) ~= 'table' then return nil end
164-
return Table.extract(link, gameIndex)
145+
---@param match MatchGroupUtilMatch
146+
---@return VNode[]
147+
function CustomMatchSummary.createFooter(match)
148+
local gameLinks = Array.map(match.games, function(game, gameIndex)
149+
local linksForThisGame = {}
150+
Table.iter.forEachPair(match.links, function(linkType, link)
151+
if type(link) ~= 'table' then
152+
return
153+
end
154+
linksForThisGame[linkType] = link[gameIndex]
165155
end)
166-
local vod = Table.extract(game, 'vod')
167-
if not vod and Logic.isDeepEmpty(links) then return end
156+
if not game.vod and Logic.isEmpty(linksForThisGame) then
157+
return
158+
end
168159

169-
local linksFooter = MatchSummary.Footer()
170-
MatchSummary.addVodsToFooter({vod = vod, games = {}}, linksFooter)
171-
linksFooter:addLinks(links)
160+
local vods = MatchSummary.makeVodDisplay(game.vod, {})
161+
local links = MatchSummary.makeLinksDisplay(linksForThisGame)
172162

173163
return Tr{children = {
174164
Td{children = {'Game ', gameIndex}},
175-
Td{classes = {'brkts-popup-spaced', 'vodlink'}, children = Array.map(linksFooter.elements, tostring)}
165+
Td{classes = {'brkts-popup-spaced', 'vodlink'}, children = Array.extend(vods, links)}
176166
}}
177167
end)
178168

179-
if Logic.isEmpty(rows) then
180-
return
169+
local matchLinks = {}
170+
if match.vod then
171+
matchLinks = Array.extend(matchLinks, MatchSummary.makeVodDisplay(match.vod, {}))
181172
end
173+
local rawLinksForMatch = {}
174+
Table.iter.forEachPair(match.links, function(linkType, link)
175+
if type(link) ~= 'string' then
176+
return
177+
end
178+
rawLinksForMatch[linkType] = link
179+
end)
182180

183-
return Collapsible{
184-
tableClasses = {'wikitable-striped'},
185-
header = Tr{children = {
186-
Th{css = {width = '20%'}},
187-
Th{css = {width = '80%'}, children = {'Additional Links'}},
188-
}},
189-
children = rows,
190-
}
181+
matchLinks = Array.extend(matchLinks, MatchSummary.makeLinksDisplay(rawLinksForMatch))
182+
183+
return WidgetUtil.collect(
184+
Logic.isNotEmpty(gameLinks) and MatchSummaryWidgets.Footer{children = {
185+
Collapsible{
186+
tableClasses = {'wikitable-striped'},
187+
header = Tr{children = {
188+
Th{css = {width = '20%'}},
189+
Th{css = {width = '80%'}, children = {'Additional Links'}},
190+
}},
191+
children = gameLinks,
192+
}
193+
}} or nil,
194+
Logic.isNotEmpty(matchLinks) and MatchSummaryWidgets.Footer{children = matchLinks} or nil
195+
)
191196
end
192197

193198
return CustomMatchSummary

‎lua/wikis/commons/MatchSummary/Base.lua‎

Lines changed: 83 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ local Lua = require('Module:Lua')
99

1010
local Abbreviation = Lua.import('Module:Abbreviation')
1111
local Array = Lua.import('Module:Array')
12-
local Class = Lua.import('Module:Class')
1312
local FnUtil = Lua.import('Module:FnUtil')
1413
local Image = Lua.import('Module:Image')
1514
local Logic = Lua.import('Module:Logic')
@@ -34,89 +33,11 @@ local TBD = Abbreviation.make{text = 'TBD', title = 'To Be Determined'}
3433
---@field createHeader? fun(match: MatchGroupUtilMatch, options: {teamStyle: teamStyle?}?): Renderable
3534
---@field createBody? fun(match: MatchGroupUtilMatch): Renderable|Renderable[]
3635
---@field createGame? fun(date: string, game: table, gameIndex: integer): Renderable|Renderable[]
37-
---@field addToFooter? fun(match: MatchGroupUtilMatch, footer: MatchSummaryFooter): MatchSummaryFooter
36+
---@field createFooter? fun(match: MatchGroupUtilMatch): Renderable|Renderable[]
3837
---@field createMatch? fun(matchData: MatchGroupUtilMatch): VNode?
3938

40-
---@class MatchSummaryFooter: BaseClass
41-
---@operator call: MatchSummaryFooter
42-
---@field elements Renderable[]
43-
local Footer = Class.new(
44-
function(self)
45-
self.elements = {}
46-
end
47-
)
48-
49-
---@param element Renderable|nil
50-
---@return self
51-
function Footer:addElement(element)
52-
table.insert(self.elements, element)
53-
return self
54-
end
55-
56-
---@param link string
57-
---@param icon string
58-
---@param iconDark string?
59-
---@param text string
60-
---@param class string?
61-
---@return MatchSummaryFooter
62-
function Footer:addLink(link, icon, iconDark, text, class)
63-
table.insert(self.elements, Image.display(icon, iconDark, {
64-
link = link, size = '32px', caption = text, alt = link, class = class
65-
}))
66-
return self
67-
end
68-
69-
---@param links table<string, string|table>
70-
---@return self
71-
function Footer:addLinks(links)
72-
local processLink = function(linkType, link)
73-
local currentLinkData = Links.getMatchIconData(linkType)
74-
if not currentLinkData then
75-
mw.log('Unknown link: ' .. linkType)
76-
elseif type(link) == 'table' then
77-
for gameIdx, gameLink in Table.iter.spairs(link) do
78-
local newText = currentLinkData.text .. ' on Game ' .. gameIdx
79-
self:addLink(gameLink, currentLinkData.icon, currentLinkData.iconDark, newText)
80-
end
81-
else
82-
-- Temporary during MW/LH Migrations
83-
local class
84-
if linkType == 'headtohead_lh' then
85-
class = 'hide-when-mediawiki'
86-
elseif linkType == 'headtohead' then
87-
class = 'hide-when-lighthouse'
88-
end
89-
self:addLink(link, currentLinkData.icon, currentLinkData.iconDark, currentLinkData.text, class)
90-
end
91-
end
92-
93-
local processedLinks = {}
94-
Array.forEach(MATCH_LINK_PRIORITY, function(linkType)
95-
for linkKey, link in Table.iter.pairsByPrefix(links, linkType, {requireIndex = false}) do
96-
processLink(linkKey, link)
97-
processedLinks[linkKey] = true
98-
end
99-
end)
100-
101-
for linkKey, link in Table.iter.spairs(links) do
102-
-- Handle links not already processed via priority list
103-
if not processedLinks[linkKey] then
104-
processLink(linkKey, link)
105-
end
106-
end
107-
108-
return self
109-
end
110-
111-
---@return VNode
112-
function Footer:create()
113-
return MatchSummaryWidgets.Footer{children = self.elements}
114-
end
115-
11639
---@class MatchSummary
117-
local MatchSummary = {
118-
Footer = Footer,
119-
}
40+
local MatchSummary = {}
12041

12142
---Default header function
12243
---@param match MatchGroupUtilMatch
@@ -151,35 +72,98 @@ function MatchSummary.createDefaultBody(match, createGame)
15172
end
15273

15374
---Default footer function
154-
---@param match table
155-
---@param footer MatchSummaryFooter
156-
---@return MatchSummaryFooter
157-
function MatchSummary.createDefaultFooter(match, footer)
158-
return MatchSummary.addVodsToFooter(match, footer):addLinks(match.links)
75+
---@param match MatchGroupUtilMatch
76+
---@return Renderable
77+
function MatchSummary.createDefaultFooter(match)
78+
return MatchSummaryWidgets.Footer{children = WidgetUtil.collect(
79+
MatchSummary.makeVodDisplay(match.vod, match.games),
80+
MatchSummary.makeLinksDisplay(match.links)
81+
)}
15982
end
16083

161-
---Creates a match footer with vods if vods are set
162-
---@param match table
163-
---@param footer MatchSummaryFooter
164-
---@return MatchSummaryFooter
165-
function MatchSummary.addVodsToFooter(match, footer)
166-
if match.vod then
167-
footer:addElement(VodLink.display{
168-
vod = match.vod,
84+
---@param matchVod string?
85+
---@param games MatchGroupUtilGame[]
86+
---@return Renderable[]
87+
function MatchSummary.makeVodDisplay(matchVod, games)
88+
local vods = {}
89+
if matchVod then
90+
table.insert(vods, VodLink.display{
91+
vod = matchVod,
16992
})
17093
end
17194

172-
Array.forEach(match.games, function(game, gameIndex)
95+
Array.forEach(games, function(game, gameIndex)
17396
if not game.vod then
17497
return
17598
end
176-
footer:addElement(VodLink.display{
99+
table.insert(vods, VodLink.display{
177100
gamenum = gameIndex,
178101
vod = game.vod,
179102
})
180103
end)
181104

182-
return footer
105+
return vods
106+
end
107+
108+
---@param link string
109+
---@param icon string
110+
---@param iconDark string?
111+
---@param text string
112+
---@param class string?
113+
---@return string?
114+
function MatchSummary.makeLinkDisplay(link, icon, iconDark, text, class)
115+
return Image.display(icon, iconDark, {
116+
link = link, size = '32px', caption = text, alt = link, class = class
117+
})
118+
end
119+
120+
---@param links table<string, string|table>
121+
---@return Renderable[]
122+
function MatchSummary.makeLinksDisplay(links)
123+
local linkDisplays = {}
124+
125+
local makeAndSaveLink = function(link, icon, iconDark, text, class)
126+
local display = MatchSummary.makeLinkDisplay(link, icon, iconDark, text, class)
127+
table.insert(linkDisplays, display)
128+
end
129+
130+
local processLink = function(linkType, link)
131+
local currentLinkData = Links.getMatchIconData(linkType)
132+
if not currentLinkData then
133+
mw.log('Unknown link: ' .. linkType)
134+
elseif type(link) == 'table' then
135+
for gameIdx, gameLink in Table.iter.spairs(link) do
136+
local newText = currentLinkData.text .. ' on Game ' .. gameIdx
137+
makeAndSaveLink(gameLink, currentLinkData.icon, currentLinkData.iconDark, newText)
138+
end
139+
else
140+
-- Temporary during MW/LH Migrations
141+
local class
142+
if linkType == 'headtohead_lh' then
143+
class = 'hide-when-mediawiki'
144+
elseif linkType == 'headtohead' then
145+
class = 'hide-when-lighthouse'
146+
end
147+
makeAndSaveLink(link, currentLinkData.icon, currentLinkData.iconDark, currentLinkData.text, class)
148+
end
149+
end
150+
151+
local processedLinks = {}
152+
Array.forEach(MATCH_LINK_PRIORITY, function(linkType)
153+
for linkKey, link in Table.iter.pairsByPrefix(links, linkType, {requireIndex = false}) do
154+
processLink(linkKey, link)
155+
processedLinks[linkKey] = true
156+
end
157+
end)
158+
159+
for linkKey, link in Table.iter.spairs(links) do
160+
-- Handle links not already processed via priority list
161+
if not processedLinks[linkKey] then
162+
processLink(linkKey, link)
163+
end
164+
end
165+
166+
return linkDisplays
183167
end
184168

185169
---Default createMatch function for usage in Custom MatchSummary
@@ -194,7 +178,7 @@ function MatchSummary.createMatch(matchData, CustomMatchSummary, options)
194178

195179
local createHeader = CustomMatchSummary.createHeader or MatchSummary.createDefaultHeader
196180
local createBody = CustomMatchSummary.createBody or MatchSummary.createDefaultBody
197-
local createFooter = CustomMatchSummary.addToFooter or MatchSummary.createDefaultFooter
181+
local createFooter = CustomMatchSummary.createFooter or MatchSummary.createDefaultFooter
198182

199183
return Html.Fragment{children = WidgetUtil.collect(
200184
createHeader(matchData, options),
@@ -212,7 +196,7 @@ function MatchSummary.createMatch(matchData, CustomMatchSummary, options)
212196
}
213197
}
214198
},
215-
createFooter(matchData, MatchSummary.Footer()):create()
199+
createFooter(matchData)
216200
)
217201
},
218202
MatchButtonBar{match = matchData, showVods = false, variant = 'primary'} -- Vods are in the footer currently

0 commit comments

Comments
 (0)