diff --git a/druid/component.lua b/druid/component.lua index ab59b63..1dc359c 100644 --- a/druid/component.lua +++ b/druid/component.lua @@ -150,25 +150,57 @@ function Component:get_name() end +function Component:get_parent_component() + local context = self:get_context() + + if context.isInstanceOf and context:isInstanceOf(Component) then + return context + end + + return nil +end + + +function Component:add_children(children) + table.insert(self._meta.children, children) +end + + +function Component:remove_children(children) + for i = #self._meta.children, 1, -1 do + if self._meta.children[i] == children then + table.remove(self._meta.children, i) + end + end +end + + --- Setup component context and his style table -- @function component:setup_component -- @tparam druid_instance table The parent druid instance -- @tparam context table Druid context. Usually it is self of script -- @tparam style table Druid style module -- @treturn Component Component itself -function Component.setup_component(self, druid_instance, context, style) +function Component:setup_component(druid_instance, context, style) self._meta = { template = nil, context = nil, nodes = nil, style = nil, druid = druid_instance, - increased_input_priority = false + increased_input_priority = false, + input_enabled = true, + children = {} } self:set_context(context) self:set_style(style) + local parent = self:get_parent_component() + if parent then + parent:add_children(self) + end + return self end diff --git a/druid/system/druid_instance.lua b/druid/system/druid_instance.lua index 259f49b..102cff9 100644 --- a/druid/system/druid_instance.lua +++ b/druid/system/druid_instance.lua @@ -190,16 +190,18 @@ function Druid.remove(self, component) return end - local all_components = self.components[const.ALL] - -- Recursive remove all children of component - for i = #all_components, 1, -1 do - local inst = all_components[i] - if inst:is_child_of(component) then - self:remove(inst) + local children = component._meta.children + for i = 1, #children do + self:remove(children[i]) + local parent = children[i]:get_parent_component() + if parent then + parent:remove_children(children[i]) end end + component._meta.children = {} + local all_components = self.components[const.ALL] for i = #all_components, 1, -1 do if all_components[i] == component then if component.on_remove then