aboutsummaryrefslogtreecommitdiffstats
path: root/lib/dom.client.moon
blob: a5a319b6ebf472655621dd3c69d2431481499ed1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
element = (element) -> (...) ->
  children = { ... }

  -- attributes are last arguments but mustn't be a ReactiveVar
  attributes = children[#children]
  if 'table' == (type attributes) and not attributes.node
    table.remove children
  else
    attributes = {}

  with e = document\createElement element
    for k,v in pairs attributes
      k = 'className' if k == 'class'
      if k == 'style' and 'table' == type v
        for kk,vv in pairs v
          e.style[kk] = vv
      elseif 'string' == type k
        e[k] = v

    -- if there is only one argument,
    -- children can be in attributes table too
    if #children == 0
      children = attributes

    for child in *children
      if 'string' == type child
        e.innerHTML ..= child
      else
        e\appendChild child

setmetatable {}, __index: (name) =>
  with val = element name
    @[name] = val