Add children list to the component

This commit is contained in:
Insality 2020-09-23 01:28:54 +03:00
parent 54a1eda738
commit 71b393b2c2
2 changed files with 42 additions and 8 deletions

View File

@ -150,25 +150,57 @@ function Component:get_name()
end 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 --- Setup component context and his style table
-- @function component:setup_component -- @function component:setup_component
-- @tparam druid_instance table The parent druid instance -- @tparam druid_instance table The parent druid instance
-- @tparam context table Druid context. Usually it is self of script -- @tparam context table Druid context. Usually it is self of script
-- @tparam style table Druid style module -- @tparam style table Druid style module
-- @treturn Component Component itself -- @treturn Component Component itself
function Component.setup_component(self, druid_instance, context, style) function Component:setup_component(druid_instance, context, style)
self._meta = { self._meta = {
template = nil, template = nil,
context = nil, context = nil,
nodes = nil, nodes = nil,
style = nil, style = nil,
druid = druid_instance, druid = druid_instance,
increased_input_priority = false increased_input_priority = false,
input_enabled = true,
children = {}
} }
self:set_context(context) self:set_context(context)
self:set_style(style) self:set_style(style)
local parent = self:get_parent_component()
if parent then
parent:add_children(self)
end
return self return self
end end

View File

@ -190,16 +190,18 @@ function Druid.remove(self, component)
return return
end end
local all_components = self.components[const.ALL]
-- Recursive remove all children of component -- Recursive remove all children of component
for i = #all_components, 1, -1 do local children = component._meta.children
local inst = all_components[i] for i = 1, #children do
if inst:is_child_of(component) then self:remove(children[i])
self:remove(inst) local parent = children[i]:get_parent_component()
if parent then
parent:remove_children(children[i])
end end
end end
component._meta.children = {}
local all_components = self.components[const.ALL]
for i = #all_components, 1, -1 do for i = #all_components, 1, -1 do
if all_components[i] == component then if all_components[i] == component then
if component.on_remove then if component.on_remove then