mirror of
https://github.com/Insality/druid.git
synced 2025-09-27 18:12:19 +02:00
Add component instance with general methods. New component constructor
This commit is contained in:
75
druid/system/component.lua
Normal file
75
druid/system/component.lua
Normal 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
|
@@ -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
|
||||
|
Reference in New Issue
Block a user