Remove getters and setters
This commit is contained in:
@@ -2,35 +2,27 @@ local XMLNode = {}
|
||||
|
||||
XMLNode.new = function(name)
|
||||
local node = {}
|
||||
node.___value = nil
|
||||
node.___name = name
|
||||
node.___children = {}
|
||||
node.___props = {}
|
||||
node.___reactiveProps = {}
|
||||
node.value = nil
|
||||
node.name = name
|
||||
node.children = {}
|
||||
node.attributes = {}
|
||||
node.computedAttributes = {}
|
||||
|
||||
function node:value() return self.___value end
|
||||
function node:setValue(val) self.___value = val end
|
||||
function node:name() return self.___name end
|
||||
function node:setName(name) self.___name = name end
|
||||
function node:children() return self.___children end
|
||||
function node:numChildren() return #self.___children end
|
||||
function node:addChild(child)
|
||||
if self[child:name()] ~= nil then
|
||||
if type(self[child:name()].name) == "function" then
|
||||
if self[child.name] ~= nil then
|
||||
if type(self[child.name].name) == "function" then
|
||||
local tempTable = {}
|
||||
table.insert(tempTable, self[child:name()])
|
||||
self[child:name()] = tempTable
|
||||
table.insert(tempTable, self[child.name])
|
||||
self[child.name] = tempTable
|
||||
end
|
||||
table.insert(self[child:name()], child)
|
||||
table.insert(self[child.name], child)
|
||||
else
|
||||
self[child:name()] = child
|
||||
self[child.name] = child
|
||||
end
|
||||
table.insert(self.___children, child)
|
||||
table.insert(self.children, child)
|
||||
end
|
||||
|
||||
function node:properties() return self.___props end
|
||||
function node:numProperties() return #self.___props end
|
||||
function node:addProperty(name, value)
|
||||
function node:addAttribute(name, value)
|
||||
local lName = "@" .. name
|
||||
if self[lName] ~= nil then
|
||||
if type(self[lName]) == "string" then
|
||||
@@ -42,12 +34,11 @@ XMLNode.new = function(name)
|
||||
else
|
||||
self[lName] = value
|
||||
end
|
||||
table.insert(self.___props, { name = name, value = self[lName] })
|
||||
table.insert(self.attributes, { name = name, value = self[lName] })
|
||||
end
|
||||
|
||||
function node:reactiveProperties() return self.___reactiveProps end
|
||||
function node:addReactiveProperty(name, value)
|
||||
self.___reactiveProps[name] = value
|
||||
function node:addComputedAttribute(name, value)
|
||||
self.computedAttributes[name] = value
|
||||
end
|
||||
|
||||
return node
|
||||
@@ -61,7 +52,7 @@ function XMLParser.XmlValue(name, tab)
|
||||
if(tab[name]~=nil)then
|
||||
if(type(tab[name])=="table")then
|
||||
if(tab[name].value~=nil)then
|
||||
var = tab[name]:value()
|
||||
var = tab[name].value
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -106,15 +97,15 @@ function XMLParser:FromXmlString(value)
|
||||
return value;
|
||||
end
|
||||
|
||||
function XMLParser:ParseProps(node, s)
|
||||
function XMLParser:ParseAttributes(node, s)
|
||||
string.gsub(s, "(%w+)=([\"'])(.-)%2", function(w, _, a)
|
||||
node:addProperty(w, self:FromXmlString(a))
|
||||
node:addAttribute(w, self:FromXmlString(a))
|
||||
end)
|
||||
end
|
||||
|
||||
function XMLParser:ParseReactiveProps(node, s)
|
||||
function XMLParser:ParseComputedAttributes(node, s)
|
||||
string.gsub(s, "(%w+)={(.-)}", function(w, a)
|
||||
node:addReactiveProperty(w, a)
|
||||
node:addComputedAttribute(w, a)
|
||||
end)
|
||||
end
|
||||
|
||||
@@ -129,18 +120,18 @@ function XMLParser:ParseXmlText(xmlText)
|
||||
if not ni then break end
|
||||
local text = string.sub(xmlText, i, ni - 1);
|
||||
if not string.find(text, "^%s*$") then
|
||||
local lVal = (top:value() or "") .. self:FromXmlString(text)
|
||||
stack[#stack]:setValue(lVal)
|
||||
local lVal = (top.value or "") .. self:FromXmlString(text)
|
||||
stack[#stack].value = lVal
|
||||
end
|
||||
if empty == "/" then -- empty element tag
|
||||
local lNode = XMLNode.new(label)
|
||||
self:ParseProps(lNode, xarg)
|
||||
self:ParseReactiveProps(lNode, xarg)
|
||||
self:ParseAttributes(lNode, xarg)
|
||||
self:ParseComputedAttributes(lNode, xarg)
|
||||
top:addChild(lNode)
|
||||
elseif c == "" then -- start tag
|
||||
local lNode = XMLNode.new(label)
|
||||
self:ParseProps(lNode, xarg)
|
||||
self:ParseReactiveProps(lNode, xarg)
|
||||
self:ParseAttributes(lNode, xarg)
|
||||
self:ParseComputedAttributes(lNode, xarg)
|
||||
table.insert(stack, lNode)
|
||||
top = lNode
|
||||
else -- end tag
|
||||
@@ -150,7 +141,7 @@ function XMLParser:ParseXmlText(xmlText)
|
||||
if #stack < 1 then
|
||||
error("XMLParser: nothing to close with " .. label)
|
||||
end
|
||||
if toclose:name() ~= label then
|
||||
if toclose.name ~= label then
|
||||
error("XMLParser: trying to close " .. toclose.name .. " with " .. label)
|
||||
end
|
||||
top:addChild(toclose)
|
||||
@@ -159,7 +150,7 @@ function XMLParser:ParseXmlText(xmlText)
|
||||
end
|
||||
local text = string.sub(xmlText, i);
|
||||
if #stack > 1 then
|
||||
error("XMLParser: unclosed " .. stack[#stack]:name())
|
||||
error("XMLParser: unclosed " .. stack[#stack].name)
|
||||
end
|
||||
return top
|
||||
end
|
||||
|
||||
@@ -27,7 +27,7 @@ end
|
||||
|
||||
local function registerFunctionEvents(self, data, events, renderContext)
|
||||
for _, event in pairs(events) do
|
||||
local expression = data:reactiveProperties()[event]
|
||||
local expression = data.computedAttributes[event]
|
||||
if (expression ~= nil) then
|
||||
registerFunctionEvent(self, expression .. "()", self[event], renderContext)
|
||||
end
|
||||
@@ -116,14 +116,14 @@ return {
|
||||
local object = {
|
||||
setValuesByXMLData = function(self, data, renderContext)
|
||||
renderContext.env[self:getName()] = self
|
||||
for prop, expression in pairs(data:reactiveProperties()) do
|
||||
for prop, expression in pairs(data.computedAttributes) do
|
||||
local update = function()
|
||||
local value = load("return " .. expression, nil, "t", renderContext.env)()
|
||||
self:setProperty(prop, value)
|
||||
end
|
||||
basalt.effect(update)
|
||||
end
|
||||
for _, prop in ipairs(data:properties()) do
|
||||
for _, prop in ipairs(data.attributes) do
|
||||
self:setProperty(prop.name, prop.value)
|
||||
end
|
||||
registerFunctionEvents(self, data, {
|
||||
@@ -173,7 +173,7 @@ return {
|
||||
|
||||
local function addXMLObjectType(node, addFn, self, renderContext)
|
||||
if (node ~= nil) then
|
||||
if (node.properties ~= nil) then
|
||||
if (node.attributes ~= nil) then
|
||||
node = {node}
|
||||
end
|
||||
for _, v in pairs(node) do
|
||||
@@ -186,11 +186,11 @@ return {
|
||||
|
||||
local function insertChildLayout(self, layout, node, renderContext)
|
||||
local props = {}
|
||||
for _, prop in ipairs(node:properties()) do
|
||||
for _, prop in ipairs(node.attributes) do
|
||||
props[prop.name] = prop.value
|
||||
end
|
||||
local updateFns = {}
|
||||
for prop, expression in pairs(node:reactiveProperties()) do
|
||||
for prop, expression in pairs(node.computedAttributes) do
|
||||
updateFns[prop] = basalt.derived(function()
|
||||
return load("return " .. expression, nil, "t", renderContext.env)()
|
||||
end)
|
||||
@@ -207,12 +207,11 @@ return {
|
||||
setValuesByXMLData = function(self, data, renderContext)
|
||||
lastXMLReferences = {}
|
||||
base.setValuesByXMLData(self, data, renderContext)
|
||||
|
||||
local children = data:children()
|
||||
|
||||
local _OBJECTS = basalt.getObjects()
|
||||
|
||||
for _, childNode in pairs(children) do
|
||||
local tagName = childNode.___name
|
||||
for _, childNode in pairs(data.children) do
|
||||
local tagName = childNode.name
|
||||
if (tagName ~= "animation") then
|
||||
local layout = renderContext.env[tagName]
|
||||
local objectKey = tagName:gsub("^%l", string.upper)
|
||||
@@ -257,22 +256,22 @@ return {
|
||||
base.setValuesByXMLData(self, data, renderContext)
|
||||
if(data["lines"]~=nil)then
|
||||
local l = data["lines"]["line"]
|
||||
if(l.properties~=nil)then l = {l} end
|
||||
if(l.attributes~=nil)then l = {l} end
|
||||
for _,v in pairs(l)do
|
||||
self:addLine(v:value())
|
||||
self:addLine(v.value)
|
||||
end
|
||||
end
|
||||
if(data["keywords"]~=nil)then
|
||||
for k,v in pairs(data["keywords"])do
|
||||
if(colors[k]~=nil)then
|
||||
local entry = v
|
||||
if(entry.properties~=nil)then entry = {entry} end
|
||||
if(entry.attributes~=nil)then entry = {entry} end
|
||||
local tab = {}
|
||||
for a,b in pairs(entry)do
|
||||
local keywordList = b["keyword"]
|
||||
if(b["keyword"].properties~=nil)then keywordList = {b["keyword"]} end
|
||||
if(b["keyword"].attributes~=nil)then keywordList = {b["keyword"]} end
|
||||
for c,d in pairs(keywordList)do
|
||||
table.insert(tab, d:value())
|
||||
table.insert(tab, d.value)
|
||||
end
|
||||
end
|
||||
self:addKeywords(colors[k], tab)
|
||||
@@ -282,7 +281,7 @@ return {
|
||||
if(data["rules"]~=nil)then
|
||||
if(data["rules"]["rule"]~=nil)then
|
||||
local tab = data["rules"]["rule"]
|
||||
if(data["rules"]["rule"].properties~=nil)then tab = {data["rules"]["rule"]} end
|
||||
if(data["rules"]["rule"].attributes~=nil)then tab = {data["rules"]["rule"]} end
|
||||
for k,v in pairs(tab)do
|
||||
|
||||
if(XMLParser.XmlValue("pattern", v)~=nil)then
|
||||
@@ -330,7 +329,7 @@ return {
|
||||
base.setValuesByXMLData(self, data, renderContext)
|
||||
if(data["item"]~=nil)then
|
||||
local tab = data["item"]
|
||||
if(tab.properties~=nil)then tab = {tab} end
|
||||
if(tab.attributes~=nil)then tab = {tab} end
|
||||
for _,v in pairs(tab)do
|
||||
if(self:getType()~="Radio")then
|
||||
self:addItem(XMLParser.XmlValue("text", v), colors[XMLParser.XmlValue("bg", v)], colors[XMLParser.XmlValue("fg", v)])
|
||||
@@ -359,7 +358,7 @@ return {
|
||||
base.setValuesByXMLData(self, data, renderContext)
|
||||
if(data["item"]~=nil)then
|
||||
local tab = data["item"]
|
||||
if(tab.properties~=nil)then tab = {tab} end
|
||||
if(tab.attributes~=nil)then tab = {tab} end
|
||||
for _,v in pairs(tab)do
|
||||
self:addItem(XMLParser.XmlValue("text", v), XMLParser.XmlValue("x", v), XMLParser.XmlValue("y", v), colors[XMLParser.XmlValue("bg", v)], colors[XMLParser.XmlValue("fg", v)])
|
||||
end
|
||||
@@ -376,7 +375,7 @@ return {
|
||||
base.setValuesByXMLData(self, data, renderContext)
|
||||
if(data["item"]~=nil)then
|
||||
local tab = data["item"]
|
||||
if(tab.properties~=nil)then tab = {tab} end
|
||||
if(tab.attributes~=nil)then tab = {tab} end
|
||||
for _,_ in pairs(tab)do
|
||||
self:addDataPoint(XMLParser.XmlValue("value"))
|
||||
end
|
||||
@@ -394,7 +393,7 @@ return {
|
||||
local function addNode(node, data)
|
||||
if(data["node"]~=nil)then
|
||||
local tab = data["node"]
|
||||
if(tab.properties~=nil)then tab = {tab} end
|
||||
if(tab.attributes~=nil)then tab = {tab} end
|
||||
for _,v in pairs(tab)do
|
||||
local n = node:addNode(XMLParser.XmlValue("text", v), colors[XMLParser.XmlValue("bg", v)], colors[XMLParser.XmlValue("fg", v)])
|
||||
addNode(n, v)
|
||||
@@ -403,7 +402,7 @@ return {
|
||||
end
|
||||
if(data["node"]~=nil)then
|
||||
local tab = data["node"]
|
||||
if(tab.properties~=nil)then tab = {tab} end
|
||||
if(tab.attributes~=nil)then tab = {tab} end
|
||||
for _,v in pairs(tab)do
|
||||
local n = self:addNode(XMLParser.XmlValue("text", v), colors[XMLParser.XmlValue("bg", v)], colors[XMLParser.XmlValue("fg", v)])
|
||||
addNode(n, v)
|
||||
|
||||
Reference in New Issue
Block a user