From 92f95f77d1a58cb8528b3eeefbb56cd3090e7b7d Mon Sep 17 00:00:00 2001 From: Insality Date: Fri, 8 May 2020 00:12:48 +0300 Subject: [PATCH] #41 Add recursive component removing on druid:remove --- druid/component.lua | 16 ++++++++++++++++ druid/system/druid_instance.lua | 8 ++++++++ 2 files changed, 24 insertions(+) diff --git a/druid/component.lua b/druid/component.lua index 2864aac..2b0c9d1 100644 --- a/druid/component.lua +++ b/druid/component.lua @@ -138,6 +138,22 @@ function Component.get_druid(self) end +--- Return true, if current component is child of another component +-- @function component:is_child_of +-- @treturn bool True, if current component is child of another +function Component.is_child_of(self, component) + return self:get_context() == component +end + + +--- Return component name +-- @function component:get_name +-- @treturn string The component name +function Component.get_name(self) + return self._component.name +end + + --- Setup component context and his style table -- @function component:setup_component -- @tparam druid_instance table The parent druid instance diff --git a/druid/system/druid_instance.lua b/druid/system/druid_instance.lua index dbed6ba..b3c5991 100644 --- a/druid/system/druid_instance.lua +++ b/druid/system/druid_instance.lua @@ -170,6 +170,14 @@ end function Druid.remove(self, component) local all_components = self.components[const.ALL] + -- Recursive remove all children of component + for i = 1, #all_components do + local inst = all_components[i] + if inst:is_child_of(component) then + self:remove(inst) + end + end + for i = #all_components, 1, -1 do if all_components[i] == component then if component.on_remove then