Add component instance with general methods. New component constructor

This commit is contained in:
Insality
2020-01-30 01:46:02 +03:00
parent 0542ec4e69
commit ae47bcee8f
22 changed files with 156 additions and 114 deletions

View File

@@ -0,0 +1,75 @@
local const = require("druid.const")
local M = {}
local instance = {}
function instance.get_style(self)
if not self._meta.style then
return const.EMPTY_TABLE
end
return self._meta.style[self._component.name] or const.EMPTY_TABLE
end
function instance.set_style(self, component_style)
self._meta.style = component_style
end
function instance.set_template(self, template)
self._meta.template = template
end
function instance.set_nodes(self, nodes)
self._meta.nodes = nodes
end
function instance.get_context(self, context)
return self._meta.context
end
function instance.set_context(self, context)
self._meta.context = context
end
function instance.get_druid(self)
local context = { _context = self }
return setmetatable(context, { __index = self:get_context().druid })
end
function instance.setup_component(self, context, style)
self._meta = {
template = nil,
context = nil,
nodes = nil,
style = nil,
}
self:set_context(context)
self:set_style(style)
return self
end
function M.new(name, interest)
local mt = {
_component = {
name = name,
interest = interest
}
}
local component = setmetatable(mt, { __index = instance })
return component
end
return M

View File

@@ -16,11 +16,11 @@ end
local function create(self, module)
local instance = setmetatable({}, { __index = module })
-- Component context, self from component creation
instance.context = self._context
instance.druid_style = self._style
instance:setup_component(self._context, self._style)
table.insert(self, instance)
local register_to = module.interest
local register_to = module._component.interest
if register_to then
local v
for i = 1, #register_to do
@@ -58,7 +58,7 @@ function M.remove(self, instance)
end
end
local interest = instance.interest
local interest = instance._component.interest
if interest then
local v
for i = 1, #interest do