aboutsummaryrefslogtreecommitdiffstats
path: root/lib/component.server.moon
diff options
context:
space:
mode:
authors-ol <s-ol@users.noreply.github.com>2018-11-01 10:17:55 +0000
committers-ol <s-ol@users.noreply.github.com>2018-11-01 10:17:55 +0000
commit30fb0dc40d595b9e0b7747ad8e5c4e8cf7e3023b (patch)
treedc56f20ec4d78115e15761c402589b5aec94ef55 /lib/component.server.moon
parentdefer all scripts (diff)
downloadmmm-30fb0dc40d595b9e0b7747ad8e5c4e8cf7e3023b.tar.gz
mmm-30fb0dc40d595b9e0b7747ad8e5c4e8cf7e3023b.zip
move mmm code from lib to mmm etc
Diffstat (limited to 'lib/component.server.moon')
-rw-r--r--lib/component.server.moon130
1 files changed, 0 insertions, 130 deletions
diff --git a/lib/component.server.moon b/lib/component.server.moon
deleted file mode 100644
index 0026d32..0000000
--- a/lib/component.server.moon
+++ /dev/null
@@ -1,130 +0,0 @@
-import opairs from require 'lib.ordered'
-
-void_tags = { 'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', 'track', 'wbr' }
-void_tags = { t,t for t in *void_tags }
-
--- convert anything to HTML string
--- val must be one of:
--- * MMMElement (have a .render method that returns a string)
--- * string
-tohtml = (val) ->
- if 'table' == type val
- assert val.render, "Table doesn't have .render"
- val = val\render!
- if 'string' == type val
- val
- else
- error "not a Node: #{val}, #{type val}"
-
--- overloaded append
--- see tohtml for acceptable values
-g_append = append
-append = (value) -> g_append tohtml value
-
--- shorthand to form a text node from strings
-text = (...) -> table.concat { ... }, ' '
-
-class ReactiveVar
- @isinstance: (val) -> 'table' == (type val) and val.subscribe
-
- new: (@value) =>
-
- set: (value) =>
- error "attempting to update ReactiveVar serverside"
-
- get: => @value
-
- subscribe: (callback) =>
- warn "attempting to subscribe to ReactiveVar serverside"
-
- map: (transform) =>
- ReactiveVar transform @value
-
-class ReactiveElement
- @isinstance: (val) -> 'table' == (type val) and val.render
-
- new: (element, ...) =>
- @element = element
- @attrs = { style: {} }
- @children = {}
-
- children = { ... }
-
- -- attributes are last arguments but mustn't be a ReactiveVar
- attributes = children[#children]
- if 'table' == (type attributes) and
- (not ReactiveElement.isinstance attributes) and
- (not ReactiveVar.isinstance attributes)
- table.remove children
- else
- attributes = {}
-
- for k,v in pairs attributes
- @set k, v if 'string' == type k
-
- -- if there is only one argument,
- -- children can be in attributes table too
- if #children == 0
- children = attributes
-
- for child in *children
- @append child
-
- destroy: =>
-
- set: (attr, value) =>
- if attr == 'style' and 'table' == type value
- for k,v in opairs value
- @attrs.style[k] = v
- return
-
- @attrs[attr] = value
-
- append: (child, last) =>
- assert not last, "last passed to append on server"
- if ReactiveVar.isinstance child
- child = child\get!
-
- child = tohtml child
- table.insert @children, child
-
- remove: (child) =>
- error "remove called serverside"
-
- render: =>
- b = "<#{@element}"
- for k,v in opairs @attrs
- if 'table' == type v
- tmp = ''
- for kk, vv in opairs v
- tmp ..= "#{kk}: #{vv}; "
- v = tmp
- b ..= " #{k}=\"#{v}\""
-
- if void_tags[@element]
- assert #@children == 0, "void tag #{element} cannot have children!"
- b .. ">"
- else
- b .. ">"
- b ..= ">" .. table.concat @children, ''
- b ..= "</#{@element}>"
- b
-
-elements = setmetatable {}, __index: (name) =>
- with val = (...) -> ReactiveElement name, ...
- @[name] = val
-
-get_or_create = (elem, id, ...) ->
- with ReactiveElement elem, ...
- \set 'id', id
-
-{
- :ReactiveVar,
- :ReactiveElement,
- :get_or_create,
- :tohtml,
- :flush,
- :append,
- :text,
- :elements,
-}