From beaa47a77f3cf209e8024bd734b354e87db7bd35 Mon Sep 17 00:00:00 2001 From: Rikard Blixt Date: Thu, 4 Apr 2024 11:59:42 +0200 Subject: [PATCH] refactor(widget): improve injector handling (#4129) * refactor: clean up widget code * fix merge conflict --- components/infobox/commons/infobox.lua | 1 - components/infobox/commons/infobox_widget.lua | 27 +++++-------------- .../commons/infobox_widget_breakdown.lua | 3 ++- .../commons/infobox_widget_builder.lua | 11 +++----- .../infobox/commons/infobox_widget_cell.lua | 3 ++- .../infobox/commons/infobox_widget_center.lua | 3 ++- .../commons/infobox_widget_chronology.lua | 3 ++- .../commons/infobox_widget_customizable.lua | 15 ++++------- .../commons/infobox_widget_factory.lua | 5 ++-- .../infobox/commons/infobox_widget_header.lua | 3 ++- .../commons/infobox_widget_highlights.lua | 3 ++- .../commons/infobox_widget_injector.lua | 7 ----- .../infobox/commons/infobox_widget_links.lua | 3 ++- .../infobox/commons/infobox_widget_title.lua | 3 ++- .../wikis/esports/infobox_game_custom.lua | 2 +- .../wikis/wildrift/infobox_team_custom.lua | 2 +- .../prize_pool/commons/prize_pool_base.lua | 1 - components/widget/widget_table.lua | 5 ++-- components/widget/widget_table_cell.lua | 3 ++- components/widget/widget_table_row.lua | 5 ++-- 20 files changed, 44 insertions(+), 64 deletions(-) diff --git a/components/infobox/commons/infobox.lua b/components/infobox/commons/infobox.lua index f995fac9717..c77b9ef35ca 100644 --- a/components/infobox/commons/infobox.lua +++ b/components/infobox/commons/infobox.lua @@ -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) diff --git a/components/infobox/commons/infobox_widget.lua b/components/infobox/commons/infobox_widget.lua index 7f78dacd777..2d2578f7b46 100644 --- a/components/infobox/commons/infobox_widget.lua +++ b/components/infobox/commons/infobox_widget.lua @@ -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()-----') @@ -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 diff --git a/components/infobox/commons/infobox_widget_breakdown.lua b/components/infobox/commons/infobox_widget_breakdown.lua index a9246a5dcce..91d8cbf87b8 100644 --- a/components/infobox/commons/infobox_widget_breakdown.lua +++ b/components/infobox/commons/infobox_widget_breakdown.lua @@ -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 diff --git a/components/infobox/commons/infobox_widget_builder.lua b/components/infobox/commons/infobox_widget_builder.lua index b0f8adc849c..ce0764f1d86 100644 --- a/components/infobox/commons/infobox_widget_builder.lua +++ b/components/infobox/commons/infobox_widget_builder.lua @@ -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') @@ -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 diff --git a/components/infobox/commons/infobox_widget_cell.lua b/components/infobox/commons/infobox_widget_cell.lua index 7034555ca04..72ad4dd0114 100644 --- a/components/infobox/commons/infobox_widget_cell.lua +++ b/components/infobox/commons/infobox_widget_cell.lua @@ -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)) diff --git a/components/infobox/commons/infobox_widget_center.lua b/components/infobox/commons/infobox_widget_center.lua index 0a75a48cf0d..6c7a5b214e3 100644 --- a/components/infobox/commons/infobox_widget_center.lua +++ b/components/infobox/commons/infobox_widget_center.lua @@ -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 diff --git a/components/infobox/commons/infobox_widget_chronology.lua b/components/infobox/commons/infobox_widget_chronology.lua index ec9dcfb3abf..0ec3ddbb96e 100644 --- a/components/infobox/commons/infobox_widget_chronology.lua +++ b/components/infobox/commons/infobox_widget_chronology.lua @@ -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 diff --git a/components/infobox/commons/infobox_widget_customizable.lua b/components/infobox/commons/infobox_widget_customizable.lua index bd974467813..ec6c7f52ccf 100644 --- a/components/infobox/commons/infobox_widget_customizable.lua +++ b/components/infobox/commons/infobox_widget_customizable.lua @@ -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') @@ -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 diff --git a/components/infobox/commons/infobox_widget_factory.lua b/components/infobox/commons/infobox_widget_factory.lua index fcab2d831d5..73f863b8782 100644 --- a/components/infobox/commons/infobox_widget_factory.lua +++ b/components/infobox/commons/infobox_widget_factory.lua @@ -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[] @@ -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 diff --git a/components/infobox/commons/infobox_widget_header.lua b/components/infobox/commons/infobox_widget_header.lua index e748a00c8db..4497a2a2cf2 100644 --- a/components/infobox/commons/infobox_widget_header.lua +++ b/components/infobox/commons/infobox_widget_header.lua @@ -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( diff --git a/components/infobox/commons/infobox_widget_highlights.lua b/components/infobox/commons/infobox_widget_highlights.lua index 717c1ac3c73..1bd18311cd0 100644 --- a/components/infobox/commons/infobox_widget_highlights.lua +++ b/components/infobox/commons/infobox_widget_highlights.lua @@ -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 diff --git a/components/infobox/commons/infobox_widget_injector.lua b/components/infobox/commons/infobox_widget_injector.lua index 2a6cdf36de5..3efdbad7258 100644 --- a/components/infobox/commons/infobox_widget_injector.lua +++ b/components/infobox/commons/infobox_widget_injector.lua @@ -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 diff --git a/components/infobox/commons/infobox_widget_links.lua b/components/infobox/commons/infobox_widget_links.lua index 088c3d7e0ad..966be46edf2 100644 --- a/components/infobox/commons/infobox_widget_links.lua +++ b/components/infobox/commons/infobox_widget_links.lua @@ -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') diff --git a/components/infobox/commons/infobox_widget_title.lua b/components/infobox/commons/infobox_widget_title.lua index a1c94b42129..6ba2a0ae880 100644 --- a/components/infobox/commons/infobox_widget_title.lua +++ b/components/infobox/commons/infobox_widget_title.lua @@ -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 diff --git a/components/infobox/wikis/esports/infobox_game_custom.lua b/components/infobox/wikis/esports/infobox_game_custom.lua index 41cf5f2a658..bb2cc8d7ea6 100644 --- a/components/infobox/wikis/esports/infobox_game_custom.lua +++ b/components/infobox/wikis/esports/infobox_game_custom.lua @@ -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, diff --git a/components/infobox/wikis/wildrift/infobox_team_custom.lua b/components/infobox/wikis/wildrift/infobox_team_custom.lua index 0b5478d424c..a759b6dc5b2 100644 --- a/components/infobox/wikis/wildrift/infobox_team_custom.lua +++ b/components/infobox/wikis/wildrift/infobox_team_custom.lua @@ -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 diff --git a/components/prize_pool/commons/prize_pool_base.lua b/components/prize_pool/commons/prize_pool_base.lua index 165c05e9b0d..47a427e2e93 100644 --- a/components/prize_pool/commons/prize_pool_base.lua +++ b/components/prize_pool/commons/prize_pool_base.lua @@ -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) diff --git a/components/widget/widget_table.lua b/components/widget/widget_table.lua index f5218a0f6be..5e9a746c99f 100644 --- a/components/widget/widget_table.lua +++ b/components/widget/widget_table.lua @@ -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)', @@ -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 diff --git a/components/widget/widget_table_cell.lua b/components/widget/widget_table_cell.lua index 00da6fadfe9..f675e810a15 100644 --- a/components/widget/widget_table_cell.lua +++ b/components/widget/widget_table_cell.lua @@ -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, diff --git a/components/widget/widget_table_row.lua b/components/widget/widget_table_row.lua index 46088672969..b26e3e03513 100644 --- a/components/widget/widget_table_row.lua +++ b/components/widget/widget_table_row.lua @@ -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 @@ -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