diff options
| author | s-ol <s-ol@users.noreply.github.com> | 2018-11-13 08:32:47 +0000 |
|---|---|---|
| committer | s-ol <s-ol@users.noreply.github.com> | 2018-11-13 08:32:47 +0000 |
| commit | 38c8a3ad5dd9b39fe03c1cdcd1f64ea9d0fc42d2 (patch) | |
| tree | e550b0128b97147d9d02a829785cf2e7b0c7cec4 | |
| parent | begin documenting mmm.component (diff) | |
| download | mmm-38c8a3ad5dd9b39fe03c1cdcd1f64ea9d0fc42d2.tar.gz mmm-38c8a3ad5dd9b39fe03c1cdcd1f64ea9d0fc42d2.zip | |
only pre-compile moonscript if no Lua variant available
| -rw-r--r-- | bundle_fileder.moon | 25 | ||||
| -rw-r--r-- | mmm/mmmfs/fileder.moon | 10 | ||||
| -rw-r--r-- | root/meta/mmm.component/text$moonscript -> fn -> mmm$dom.moon | 121 | ||||
| -rw-r--r-- | root/meta/mmm.component/todoMVC/text$lua -> mmm$component.lua | 45 |
4 files changed, 188 insertions, 13 deletions
diff --git a/bundle_fileder.moon b/bundle_fileder.moon index 01aa0a4..c0067cb 100644 --- a/bundle_fileder.moon +++ b/bundle_fileder.moon @@ -23,18 +23,20 @@ do continue table.insert addto, file - -readfile = (name) -> - file = io.open name, 'r' - with file\read '*all' - file\close! +-- compile a moonscript facet to lua +compile = (old, new, val) -> "-- this facet has been transpiled from '#{old}' +-- to '#{new}' for execution in the browser. +-- refer to the original facet as the source. +#{to_lua val}" -- load a fs file as a fileder facet load_facet = (filename) -> key = (filename\match '(.*)%.%w+') or filename key = Key key\gsub '%$', '/' - value = readfile filename + file = io.open filename, 'r' + value = file\read '*all' + file\close! key, value @@ -90,10 +92,13 @@ with io.open '$bundle.lua', 'w' key, value = load_facet facet .facets[key] = value - if key.type\match '^text/moonscript %->' - new_key = Key key.name, key.type\gsub '^text/moonscript %-> (.*)', 'text/lua -> %1' - new_key.original = key - .facets[new_key] = compile key, new_key, value + for key, value in pairs .facets + continue unless key.type\match '^text/moonscript %->' + built_key = Key key.name, key.type\gsub '^text/moonscript %-> (.*)', 'text/lua -> %1' + built_key.original = key + + continue if \has built_key + .facets[built_key] = compile key, built_key, value for child in *children_bundles -- @BUG: child bundles are malformed due to Tup bug ($ symbol) diff --git a/mmm/mmmfs/fileder.moon b/mmm/mmmfs/fileder.moon index d08d80c..559e08b 100644 --- a/mmm/mmmfs/fileder.moon +++ b/mmm/mmmfs/fileder.moon @@ -115,6 +115,16 @@ class Fileder [name for name in pairs names] + -- check whether a facet is directly available + has: (...) => + want = Key ... + + for key in pairs @facets + continue if key.original + + if key.name == want.name and key.type == want.type + return key + -- find facet and type according to criteria, nil if no value or conversion path -- * ... - arguments like Key find: (...) => diff --git a/root/meta/mmm.component/text$moonscript -> fn -> mmm$dom.moon b/root/meta/mmm.component/text$moonscript -> fn -> mmm$dom.moon index d5acc1e..6840e42 100644 --- a/root/meta/mmm.component/text$moonscript -> fn -> mmm$dom.moon +++ b/root/meta/mmm.component/text$moonscript -> fn -> mmm$dom.moon @@ -1,4 +1,5 @@ import article, section, h1, h2, h3, p, a, div, ul, li, pre, code from require 'mmm.dom' +import tohtml from require 'mmm.component' import lua, moonscript from (require 'mmm.highlighting').languages mmmcomp = -> code 'mmm.component' @@ -10,7 +11,7 @@ source = do return the_code unless demo example = assert load lua_src - div the_code, div example!, class: 'example' + div the_code, div (tohtml example!), class: 'example' => article { h1 mmmcomp! @@ -25,7 +26,7 @@ source = do content can still be generated from the same code that powers the reactive interface." h2 "Examples" - p "Feel free to read the API documentation below, or take a look at the following examples using the ", + p "Feel free to read the documentation below, or take a look at the following examples using the ", (code 'mmmfs'), " inspect mode:" ul for child in *@children @@ -36,7 +37,7 @@ source = do BROWSER\navigate child.path } - h2 "API" + h2 "Guide" p "Begin by requiring ", mmmcomp!, ". The module returns a table containing the following:" ul { @@ -166,4 +167,118 @@ fruit:set("orange") print(loud_fruit:get()) -- prints 'ORANGE' ]], false } + + section do + relem = -> code 'ReactiveElement' + rvar = -> code 'ReactiveVar' + + { + id: 'ReactiveElement' + + h3 relem!, "s" + p relem!, " is a wrapper around DOM elements that allows you to use ", rvar!, "s to specify attributes and + children of the element. Internally it ", (code ':subscribe()'), "s to each of the ", rvar!, "s so that it can + update whenever any of the values change." + + p relem!, "s can be instantiated using the ", relem!, " constructor, but usually you will want to use the + shorthand functions that you can pull out of the ", (code 'elements'), " 'magic table', as they will make your + code much more legible. This table provides functions for creating any HTML element based on it's name. + The elements you use are automatically cached so you can either pull out only the ones you want to use into + your local namespace or use the table itself." + + p "Each of these node creation functions accept any number of nodes or strings as arguments and will attach these + as their children:" + + source [[ +import elements from require 'mmm.component' +import h1, code from elements + +h1 "Hello from ", code 'mmm.component' + +-- or if you want to keep your namespace clean: +e = elements +e.h1 "Hello from ", e.code 'mmm.component' + ]], [[ +local elements = require'mmm.component'.elements +local h1, code = elements.h1, elements.code + +h1("Hello from ", code 'mmm.component') + +-- or if you want to keep your namespace clean: +local e = elements +return e.h1("Hello from ", e.code 'mmm.component') + ]] + + source [[ +import text, elements from require 'mmm.component' +import div, button from elements + +count = ReactiveVar 0 + +div { + button '-', onclick: () -> count\transform (c) -> c - 1 + " count is: " + count\map (num) -> text num + " " + button '+', onclick: () -> count\transform (c) -> c + 1 +} + ]], [[ +local comp = require 'mmm.component' +local div, button = comp.elements.div, comp.elements.button + +local count = comp.ReactiveVar(0) + +return div { + button { + '-', + onclick = function () count:set(count:get() - 1) end + }, + " count is: ", + count:map(function (num) return comp.text(num) end), + " ", + button { + '+', + onclick = function () count:set(count:get() + 1) end + }, +} + ]] + + source [[ +import text, elements from require 'mmm.component' +import select, option from elements + +color = ReactiveVar 'white' + +div { + div 'test', style: color\map (background) -> + { padding: '1em', :background } + + select { + onchange: (e) => color\set e.target.value + + option 'white' + option 'red' + option 'green' + } +} + ]], [[ +local comp = require 'mmm.component' +local e = comp.elements + +local color = comp.ReactiveVar 'red' + +return e.div { + e.div { 'test', style = color:map(function (bg) + return { padding = '1em', background = bg } + end) }, + e.select { + onchange = function (_, e) color:set(e.target.value) end, + + e.option 'red', + e.option 'green', + e.option 'blue', + }, +} + ]] + } } diff --git a/root/meta/mmm.component/todoMVC/text$lua -> mmm$component.lua b/root/meta/mmm.component/todoMVC/text$lua -> mmm$component.lua new file mode 100644 index 0000000..04dae47 --- /dev/null +++ b/root/meta/mmm.component/todoMVC/text$lua -> mmm$component.lua @@ -0,0 +1,45 @@ +local component = require 'mmm.component' +local ReactiveVar, text, e = component.ReactiveVar, component.text, component.elements + +local parent = e.div() +local function todoItem(desc, done) + -- convert into reactive data sources + local desc, done = ReactiveVar(desc), ReactiveVar(done) + local me = e.div{ + style = { + margin = '8px', + padding = '8px', + background = '#eeeeee', + }, + e.h3{ desc:map(text), style = 'margin: 0;' }, + e.span(done:map(function (done) + if done then + return text 'done' + else + return text 'not done yet' + end + end)), + e.input{ type = 'checkbox', checked = done, onchange = function(_, e) done:set(e.target.checked) end }, + e.a{ text 'delete', href = '#', onclick = function() parent:remove(me) end } + } + return me +end + +parent:append(todoItem('write a Component System', true)) +parent:append(todoItem('eat Lasagna', true)) +parent:append(todoItem('do other things')) + +local desc = ReactiveVar 'start' +local form = e.form{ + action = '', + style = { margin = '2px' }, + onsubmit = function(_, e) + e:preventDefault() + parent:append(todoItem(desc:get())) + desc:set '' + end, +} +form:append(e.input{ type = 'text', value = desc, onchange = function(_, e) desc:set(e.target.value) end }) +form:append(e.input{ type = 'submit', value = 'add' }) + +return e.article(parent, form) |
