From e5491bc7fbd867812500c845db99b55b35db0666 Mon Sep 17 00:00:00 2001 From: s-ol Date: Mon, 29 Oct 2018 19:37:40 +1100 Subject: rename lib.html to lib.dom --- lib/canvasapp.client.moon | 2 +- lib/component.client.moon | 1 - lib/dom.client.moon | 32 ++++++++++++++++++++++++++++++++ lib/dom.server.moon | 33 +++++++++++++++++++++++++++++++++ lib/html.client.moon | 32 -------------------------------- lib/html.server.moon | 33 --------------------------------- 6 files changed, 66 insertions(+), 67 deletions(-) create mode 100644 lib/dom.client.moon create mode 100644 lib/dom.server.moon delete mode 100644 lib/html.client.moon delete mode 100644 lib/html.server.moon (limited to 'lib') diff --git a/lib/canvasapp.client.moon b/lib/canvasapp.client.moon index 38cc307..71ffa59 100644 --- a/lib/canvasapp.client.moon +++ b/lib/canvasapp.client.moon @@ -1,7 +1,7 @@ window = js.global js = require 'js' -import a, canvas, div, button, script from require 'lib.html' +import a, canvas, div, button, script from require 'lib.dom' class CanvasApp width: 500 diff --git a/lib/component.client.moon b/lib/component.client.moon index af0b38a..617deb5 100644 --- a/lib/component.client.moon +++ b/lib/component.client.moon @@ -102,7 +102,6 @@ class ReactiveElement @node.style[k] = v return - print "setting attr #{attr}" @node[attr] = value append: (child, last) => diff --git a/lib/dom.client.moon b/lib/dom.client.moon new file mode 100644 index 0000000..b8f2bd5 --- /dev/null +++ b/lib/dom.client.moon @@ -0,0 +1,32 @@ +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 + 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 diff --git a/lib/dom.server.moon b/lib/dom.server.moon new file mode 100644 index 0000000..3a7b537 --- /dev/null +++ b/lib/dom.server.moon @@ -0,0 +1,33 @@ +import opairs from require 'lib.ordered' + +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 = {} + + b = "<#{element}" + for k,v in opairs attributes + if k == 'style' and 'table' == type v + tmp = '' + for kk, vv in opairs v + tmp ..= "#{kk}: #{vv}; " + v = tmp + b ..= " #{k}=\"#{v}\"" + + -- if there is only one argument, + -- children can be in attributes table too + if #children == 0 + children = attributes + + b ..= ">" .. table.concat children, '' + b ..= "" + b + +setmetatable {}, __index: (name) => + with val = element name + @[name] = val diff --git a/lib/html.client.moon b/lib/html.client.moon deleted file mode 100644 index b8f2bd5..0000000 --- a/lib/html.client.moon +++ /dev/null @@ -1,32 +0,0 @@ -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 - 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 diff --git a/lib/html.server.moon b/lib/html.server.moon deleted file mode 100644 index 3a7b537..0000000 --- a/lib/html.server.moon +++ /dev/null @@ -1,33 +0,0 @@ -import opairs from require 'lib.ordered' - -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 = {} - - b = "<#{element}" - for k,v in opairs attributes - if k == 'style' and 'table' == type v - tmp = '' - for kk, vv in opairs v - tmp ..= "#{kk}: #{vv}; " - v = tmp - b ..= " #{k}=\"#{v}\"" - - -- if there is only one argument, - -- children can be in attributes table too - if #children == 0 - children = attributes - - b ..= ">" .. table.concat children, '' - b ..= "" - b - -setmetatable {}, __index: (name) => - with val = element name - @[name] = val -- cgit v1.2.3