Skip to content

Commit

Permalink
refactor(widget): improve injector handling (#4129)
Browse files Browse the repository at this point in the history
* refactor: clean up widget code

* fix merge conflict
  • Loading branch information
Rathoz authored Apr 4, 2024
1 parent 645eedb commit beaa47a
Show file tree
Hide file tree
Showing 20 changed files with 44 additions and 64 deletions.
1 change: 0 additions & 1 deletion components/infobox/commons/infobox.lua
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ function Infobox:build(widgets)
if widget == nil or widget['is_a'] == nil then
error('Infobox:build can only accept Widgets')
end
widget:setContext({injector = self.injector})

local contentItems = WidgetFactory.work(widget, self.injector)

Expand Down
27 changes: 7 additions & 20 deletions components/infobox/commons/infobox_widget.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,31 @@
--
local Class = require('Module:Class')
local Lua = require('Module:Lua')

local Injector = Lua.import('Module:Infobox/Widget/Injector')
local String = require('Module:StringUtils')

---@class Widget: BaseClass
---@operator call(): Widget
---@field public injector WidgetInjector?
local Widget = Class.new()

---Asserts the existence of a value and copies it
---@param value string
---@return string
function Widget:assertExistsAndCopy(value)
if value == nil or value == '' then
error('Tried to set a nil value to a mandatory property')
end

return value
return assert(String.nilIfEmpty(value), 'Tried to set a nil value to a mandatory property')
end

---@param injector WidgetInjector?
---@return Widget[]|Html[]|nil
function Widget:make()
function Widget:make(injector)
error('A Widget must override the make() function!')
end

---@param injector WidgetInjector?
---@return Widget[]|Html[]|nil
function Widget:tryMake()
function Widget:tryMake(injector)
local _, output = xpcall(
function()
return self:make()
return self:make(injector)
end,
function(errorMessage)
mw.log('-----Error in Widget:tryMake()-----')
Expand All @@ -50,13 +46,4 @@ function Widget:tryMake()
return output
end

---Sets the context of a widget
---@param context table
function Widget:setContext(context)
self.context = context
if context.injector ~= nil and (context.injector['is_a'] == nil or context.injector:is_a(Injector) == false) then
error('Valid Injector from Infobox/Widget/Injector needs to be provided')
end
end

return Widget
3 changes: 2 additions & 1 deletion components/infobox/commons/infobox_widget_breakdown.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ local Breakdown = Class.new(
end
)

---@param injector WidgetInjector?
---@return {[1]: Html?}
function Breakdown:make()
function Breakdown:make(injector)
return {Breakdown:_breakdown(self.contents, self.classes, self.contentClasses)}
end

Expand Down
11 changes: 4 additions & 7 deletions components/infobox/commons/infobox_widget_builder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
-- Please see https://github.com/Liquipedia/Lua-Modules to contribute
--

local Array = require('Module:Array')
local Class = require('Module:Class')
local Lua = require('Module:Lua')

Expand All @@ -22,17 +23,13 @@ local Builder = Class.new(
end
)

---@param injector WidgetInjector?
---@return Widget[]
function Builder:make()
function Builder:make(injector)
local children = self.builder()
local widgets = {}
for _, child in ipairs(children or {}) do
child:setContext{injector = self.context.injector}
local childOutput = WidgetFactory.work(child, self.context.injector)
-- Our child might contain a list of children, so we need to iterate
for _, item in ipairs(childOutput) do
table.insert(widgets, item)
end
Array.extendWith(widgets, WidgetFactory.work(child, injector))
end
return widgets
end
Expand Down
3 changes: 2 additions & 1 deletion components/infobox/commons/infobox_widget_cell.lua
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ function Cell:_content(...)
return self
end

---@param injector WidgetInjector?
---@return {[1]: Html?}
function Cell:make()
function Cell:make(injector)
self:_new(self.name)
self:_class(unpack(self.classes or {}))
self:_content(unpack(self.content))
Expand Down
3 changes: 2 additions & 1 deletion components/infobox/commons/infobox_widget_center.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ local Center = Class.new(
end
)

---@param injector WidgetInjector?
---@return {[1]: Html?}
function Center:make()
function Center:make(injector)
return {Center:_create(self.content, self.classes)}
end

Expand Down
3 changes: 2 additions & 1 deletion components/infobox/commons/infobox_widget_chronology.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ local Chronology = Class.new(
end
)

---@param injector WidgetInjector?
---@return Html[]
function Chronology:make()
function Chronology:make(injector)
return Chronology:_chronology(self.links)
end

Expand Down
15 changes: 5 additions & 10 deletions components/infobox/commons/infobox_widget_customizable.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
--

local Class = require('Module:Class')
local Logic = require('Module:Logic')
local Lua = require('Module:Lua')

local Widget = Lua.import('Module:Infobox/Widget')
Expand All @@ -24,17 +23,13 @@ local Customizable = Class.new(
end
)

---@return Widget[]
function Customizable:make()
if self.context.injector == nil then
---@param injector WidgetInjector?
---@return Widget[]?
function Customizable:make(injector)
if injector == nil then
return self.children
end
if self.id == 'custom' then
local widgets = Logic.emptyOr(self.context.injector:addCustomCells(self.children), self.children, {})
---@cast widgets -nil
self.children = widgets
end
return self.context.injector:parse(self.id, self.children)
return injector:parse(self.id, self.children)
end

return Customizable
5 changes: 2 additions & 3 deletions components/infobox/commons/infobox_widget_factory.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ local Widget = Lua.import('Module:Infobox/Widget')
local WidgetFactory = Class.new()

---@param widget Widget
---@param injector WidgetInjector
---@param injector WidgetInjector?
---@return Html[]
function WidgetFactory.work(widget, injector)
local convertedWidgets = {} ---@type Html[]
Expand All @@ -25,10 +25,9 @@ function WidgetFactory.work(widget, injector)
return {}
end

for _, child in ipairs(widget:tryMake() or {}) do
for _, child in ipairs(widget:tryMake(injector) or {}) do
if type(child) == 'table' and type(child['is_a']) == 'function' and child:is_a(Widget) then
---@cast child Widget
child:setContext{injector = injector}
Array.extendWith(convertedWidgets, WidgetFactory.work(child, injector))
else
---@cast child Html
Expand Down
3 changes: 2 additions & 1 deletion components/infobox/commons/infobox_widget_header.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ local Header = Class.new(
end
)

---@param injector WidgetInjector?
---@return Html[]
function Header:make()
function Header:make(injector)
local header = {
Header:_name(self.name),
Header:_image(
Expand Down
3 changes: 2 additions & 1 deletion components/infobox/commons/infobox_widget_highlights.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ local Highlights = Class.new(
end
)

---@param injector WidgetInjector?
---@return {[1]: Html}?
function Highlights:make()
function Highlights:make(injector)
return Highlights:_highlights(self.list)
end

Expand Down
7 changes: 0 additions & 7 deletions components/infobox/commons/infobox_widget_injector.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,4 @@ function Injector:parse(id, widgets)
return widgets
end

---Adds custom cells
---@param widgets Widget[]
---@return Widget[]?
function Injector:addCustomCells(widgets)
return {}
end

return Injector
3 changes: 2 additions & 1 deletion components/infobox/commons/infobox_widget_links.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ local Links = Class.new(

local PRIORITY_GROUPS = Lua.import('Module:Links/PriorityGroups', {loadData = true})

---@param injector WidgetInjector?
---@return {[1]: Html}
function Links:make()
function Links:make(injector)
local infoboxLinks = mw.html.create('div')
infoboxLinks :addClass('infobox-center')
:addClass('infobox-icons')
Expand Down
3 changes: 2 additions & 1 deletion components/infobox/commons/infobox_widget_title.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ local Title = Class.new(
end
)

---@param injector WidgetInjector?
---@return {[1]: Html}
function Title:make()
function Title:make(injector)
return {Title:_create(self.content)}
end

Expand Down
2 changes: 1 addition & 1 deletion components/infobox/wikis/esports/infobox_game_custom.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ end
---@param id string
---@param widgets Widget[]
---@return Widget[]
function CustomInjector:addCustomCells(id, widgets)
function CustomInjector:parse(id, widgets)
local args = self.caller.args
if id == 'custom' then
Array.append(widgets,
Expand Down
2 changes: 1 addition & 1 deletion components/infobox/wikis/wildrift/infobox_team_custom.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ end
---@param id string
---@param widgets Widget[]
---@return Widget[]
function CustomInjector:addCustomCells(id, widgets)
function CustomInjector:parse(id, widgets)
local args = self.caller.args

if id == 'custom' then
Expand Down
1 change: 0 additions & 1 deletion components/prize_pool/commons/prize_pool_base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,6 @@ function BasePrizePool:_buildTable(isAward)
tbl:addRow(row)
end

tbl:setContext{self._widgetInjector}
local tableNode = mw.html.create('div'):css('overflow-x', 'auto')
for _, node in ipairs(WidgetFactory.work(tbl, self._widgetInjector)) do
tableNode:node(node)
Expand Down
5 changes: 3 additions & 2 deletions components/widget/widget_table.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ function Table:addClass(class)
return self
end

---@param injector WidgetInjector?
---@return {[1]: Html}
function Table:make()
function Table:make(injector)
local displayTable = mw.html.create('div'):addClass('csstable-widget')
displayTable:css{
['grid-template-columns'] = 'repeat(' .. (self.columns or self:_getMaxCells()) .. ', auto)',
Expand All @@ -63,7 +64,7 @@ function Table:make()
displayTable:css(self.css)

for _, row in ipairs(self.rows) do
for _, node in ipairs(WidgetFactory.work(row, self.injector)) do
for _, node in ipairs(WidgetFactory.work(row, injector)) do
displayTable:node(node)
end
end
Expand Down
3 changes: 2 additions & 1 deletion components/widget/widget_table_cell.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ function TableCell:addClass(class)
return self
end

---@param injector WidgetInjector?
---@return {[1]: Html}
function TableCell:make()
function TableCell:make(injector)
local cell = mw.html.create('div'):addClass('csstable-widget-cell')
cell:css{
['grid-row'] = self.rowSpan and 'span ' .. self.rowSpan or nil,
Expand Down
5 changes: 3 additions & 2 deletions components/widget/widget_table_row.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ function TableRow:getCellCount()
return #self.cells
end

---@param injector WidgetInjector?
---@return {[1]: Html}
function TableRow:make()
function TableRow:make(injector)
local row = mw.html.create('div'):addClass('csstable-widget-row')

for _, class in ipairs(self.classes) do
Expand All @@ -61,7 +62,7 @@ function TableRow:make()
row:css(self.css)

for _, cell in ipairs(self.cells) do
for _, node in ipairs(WidgetFactory.work(cell, self.injector)) do
for _, node in ipairs(WidgetFactory.work(cell, injector)) do
row:node(node)
end
end
Expand Down

0 comments on commit beaa47a

Please sign in to comment.