Skip to content

Commit a1fdb6f

Browse files
committed
First push
0 parents  commit a1fdb6f

File tree

14 files changed

+475
-0
lines changed

14 files changed

+475
-0
lines changed

flat/component.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
local module = {}
2+
local js = require "js"
3+
4+
module.components = {}
5+
6+
function module.new(name, inside)
7+
module.components[name] = inside
8+
end
9+
10+
function module.create(name)
11+
return module.components[name]()
12+
end
13+
14+
return module

flat/element.lua

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
local module = {}
2+
local js = require "js"
3+
4+
module.page = nil
5+
6+
function get_element(id)
7+
return js.global.document:getElementById(id)
8+
end
9+
10+
function get_id(number)
11+
return "flat_" .. number
12+
end
13+
14+
function module.create(kinda, innera, props)
15+
local page = module.page
16+
local result = {
17+
properties = {
18+
flat_kind = kinda,
19+
innerHTML = innera
20+
},
21+
render = function(self)
22+
if not self.render_index then
23+
self.render_index = page.render(self)
24+
end
25+
26+
self:postrender(self.render_index)
27+
end,
28+
postrender = function(self, id)
29+
local elm = get_element(get_id(id))
30+
if elm then
31+
for i, v in pairs(self.properties) do
32+
if elm[i] then
33+
elm[i] = v
34+
else
35+
elm:setAttribute(i, v)
36+
end
37+
end
38+
end
39+
40+
page.current_page_html[self.render_index] = elm.outerHTML
41+
end,
42+
render_child = function(self, child)
43+
child = get_element(child.id)
44+
local elm = get_element(get_id(self.render_index))
45+
elm:appendChild(child)
46+
end,
47+
remove = function(self)
48+
local elm = get_element(get_id(self.render_index))
49+
elm:remove()
50+
51+
table.remove(page.current_page_html, self.render_index)
52+
end,
53+
event = function(self, ev, fn)
54+
local elm = get_element(get_id(self.render_index))
55+
56+
elm:addEventListener(ev, fn)
57+
58+
return {
59+
kill = function()
60+
elm:removeEventListener(ev, fn)
61+
end
62+
}
63+
end
64+
}
65+
66+
if props then
67+
for i, v in pairs(props) do
68+
result.properties[i] = v
69+
end
70+
end
71+
72+
return setmetatable(result, {
73+
__newindex = function(table, index, value)
74+
if index ~= "render_index" then
75+
rawset(table.properties, index, value)
76+
77+
if table.render_index then
78+
table:render()
79+
end
80+
else
81+
rawset(table, index, value)
82+
end
83+
end,
84+
__index = function(table, index)
85+
if not t and index ~= "render_index" then
86+
local elm = get_element(get_id(rawget(table, "render_index")))
87+
return elm[index] or elm:getAttribute(index)
88+
end
89+
90+
return table.properties[index]
91+
end
92+
})
93+
end
94+
95+
return module

flat/http.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
local http = {}
2+
3+
function http.get(url)
4+
local xmlhttp = js.new(js.global.window.XMLHttpRequest)
5+
xmlhttp:open("GET", url, false)
6+
xmlhttp:send(nil)
7+
8+
return xmlhttp.responseText
9+
end
10+
11+
return http

flat/page.lua

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
local js = require "js"
2+
local http = require "flat.http"
3+
4+
local module = {}
5+
module.current_page_html = {}
6+
module.dom = nil
7+
8+
function module.render(element)
9+
if element then
10+
local node = js.global.document:createElement(element.properties.flat_kind)
11+
module.dom:appendChild(node)
12+
13+
table.insert(module.current_page_html, element)
14+
15+
node.id = "flat_" .. #module.current_page_html
16+
end
17+
18+
return #module.current_page_html
19+
end
20+
21+
function module.load_dom(dom)
22+
_G.flat.init()
23+
_G.flat.element.page = module
24+
25+
local index = http.get("scripts/render/index.html")
26+
27+
js.global.document:write(index)
28+
js.global.document:close()
29+
30+
module.dom = js.global.document:getElementById(dom)
31+
end
32+
33+
return module

flat/require.lua

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
local js = require "js"
2+
local window = js.global
3+
4+
local flat = {
5+
element = require"flat.element",
6+
page = require"flat.page",
7+
router = require"flat.router",
8+
styler = require"flat.styler",
9+
component = require"flat.component",
10+
init = function()
11+
function wait(delay)
12+
local co = assert(coroutine.running(), "Should be run in a coroutine")
13+
14+
window:setTimeout(function()
15+
assert(coroutine.resume(co))
16+
end, delay*1000)
17+
18+
coroutine.yield()
19+
end
20+
end
21+
}
22+
23+
_G.flat = flat
24+
25+
return flat

flat/router.lua

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
--not functional, yet.
2+
3+
local module = {}
4+
5+
local js = require "js"
6+
local window = js.global
7+
8+
function module.set_route(name)
9+
print(window.history.pushState)
10+
window.history:pushState(nil, "", name)
11+
end
12+
13+
return module

flat/styler.lua

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
local module = {}
2+
3+
module.styles = {}
4+
module.applied = {}
5+
6+
function module.new(name, params, theme)
7+
module.styles[name] = params
8+
end
9+
10+
function module.use(name, element)
11+
local style = module.styles[name]
12+
if style then
13+
for i,v in pairs(style) do
14+
element.style[i:gsub("_", "-")] = v
15+
end
16+
end
17+
18+
module.applied[element] = name
19+
end
20+
21+
return module

index.html

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<title>flat.lua framework</title>
2+
3+
<div style="margin-left: 50%;">
4+
<h1>
5+
flat.lua is loading...
6+
</h1>
7+
</div>
8+
9+
<script src="lua51.js" type="text/javascript"></script>
10+
11+
<script>
12+
function executeScript(source) {
13+
console.log(fengari.load(source, "web")())
14+
}
15+
16+
function executeFile(path) {
17+
fetch("scripts/lua/" + path)
18+
.then(Response => Response.text())
19+
.then(text => executeScript(text))
20+
}
21+
22+
executeFile("main.lua")
23+
</script>

lua51.js

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sample_sites/component_sample.lua

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
local flat = require "lua.flat.require"
2+
flat.page.load_dom("flat-dom") -- loads the custom dom from render/index.html
3+
4+
flat.component.new("alert", function()
5+
local div = flat.element.create("div", "This is my alert!")
6+
7+
flat.styler.new("alert", {
8+
border = "0.375rem",
9+
position = "relative",
10+
padding = "1rem 1rem",
11+
margin_bottom = "10px",
12+
margin_top = "10px",
13+
color = "#842029",
14+
margin_left = "10px",
15+
background_color = "#f8d7da",
16+
border_radius = "0.375rem",
17+
border_left_width = "10px",
18+
border_left_style = "solid",
19+
width = "fit-content"
20+
})
21+
22+
div:render()
23+
24+
flat.styler.use("alert", div)
25+
26+
return {
27+
text = function(update)
28+
div.innerHTML = update
29+
end
30+
}
31+
end)
32+
33+
local alert1 = flat.component.create("alert")
34+
alert1.text("You've been warned!")
35+
36+
local alert2 = flat.component.create("alert")
37+
alert2.text("This is my second warning to you.")

0 commit comments

Comments
 (0)