boto-rb-web 0.34.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +17 -0
- data/assets/boto-web/boot.js +16 -0
- data/assets/boto-web/boto-web.css +32 -0
- data/assets/boto-web/dom/animate.js +49 -0
- data/assets/boto-web/dom/flip.js +41 -0
- data/assets/boto-web/dom/keyed.js +15 -0
- data/assets/boto-web/dom/morph.js +112 -0
- data/assets/boto-web/dom/navigation.js +72 -0
- data/assets/boto-web/dom/sync.js +42 -0
- data/assets/boto-web/net/base.js +5 -0
- data/assets/boto-web/net/protocol.js +46 -0
- data/assets/boto-web/net/sse.js +45 -0
- data/assets/boto-web/net/sse_stream.js +84 -0
- data/assets/boto-web/net/token.js +9 -0
- data/assets/boto-web/net/transport.js +99 -0
- data/assets/boto-web/runtime/component.js +138 -0
- data/assets/boto-web/runtime/events.js +54 -0
- data/assets/boto-web/runtime/hooks.js +82 -0
- data/assets/boto-web/runtime/live.js +12 -0
- data/assets/boto-web/runtime/on.js +36 -0
- data/assets/boto-web/runtime/runtime.js +100 -0
- data/assets/boto-web/runtime/scope.js +112 -0
- data/assets/boto-web/runtime/signals.js +123 -0
- data/assets/boto-web/template/decimal.js +93 -0
- data/assets/boto-web/template/helpers.js +31 -0
- data/assets/boto-web/template/html.js +61 -0
- data/assets/boto-web/template/hydrate.js +25 -0
- data/assets/boto-web/template/ruby/core.js +72 -0
- data/assets/boto-web/template/ruby/lists.js +63 -0
- data/assets/boto-web/template/ruby.js +5 -0
- data/lib/boto/component/arguments.rb +58 -0
- data/lib/boto/component/definition.rb +114 -0
- data/lib/boto/component.rb +107 -0
- data/lib/boto/helper.rb +14 -0
- data/lib/boto/view.rb +63 -0
- data/lib/boto/web/app_extension.rb +78 -0
- data/lib/boto/web/assets.rb +59 -0
- data/lib/boto/web/child_path.rb +38 -0
- data/lib/boto/web/code_class.rb +16 -0
- data/lib/boto/web/compiler/analysis/deps.rb +46 -0
- data/lib/boto/web/compiler/analysis/handlers.rb +47 -0
- data/lib/boto/web/compiler/analysis/walker.rb +174 -0
- data/lib/boto/web/compiler/analysis.rb +44 -0
- data/lib/boto/web/compiler/client_helper.rb +34 -0
- data/lib/boto/web/compiler/computed_body.rb +40 -0
- data/lib/boto/web/compiler/directives.rb +86 -0
- data/lib/boto/web/compiler/expression/calls.rb +149 -0
- data/lib/boto/web/compiler/expression/collections.rb +116 -0
- data/lib/boto/web/compiler/expression/literals.rb +75 -0
- data/lib/boto/web/compiler/expression/logic.rb +53 -0
- data/lib/boto/web/compiler/expression/operators.rb +85 -0
- data/lib/boto/web/compiler/expression/strings.rb +31 -0
- data/lib/boto/web/compiler/expression.rb +93 -0
- data/lib/boto/web/compiler/module_writer/handlers_js.rb +75 -0
- data/lib/boto/web/compiler/module_writer/js.rb +24 -0
- data/lib/boto/web/compiler/module_writer/tag_js.rb +132 -0
- data/lib/boto/web/compiler/module_writer/template_js.rb +89 -0
- data/lib/boto/web/compiler/module_writer/type_shape.rb +28 -0
- data/lib/boto/web/compiler/module_writer.rb +79 -0
- data/lib/boto/web/compiler/template.rb +136 -0
- data/lib/boto/web/compiler/type_map.rb +43 -0
- data/lib/boto/web/compiler/type_scope.rb +68 -0
- data/lib/boto/web/compiler.rb +9 -0
- data/lib/boto/web/dispatcher.rb +113 -0
- data/lib/boto/web/events_endpoint.rb +43 -0
- data/lib/boto/web/failure.rb +30 -0
- data/lib/boto/web/head.rb +24 -0
- data/lib/boto/web/helpers.rb +40 -0
- data/lib/boto/web/hooks.rb +39 -0
- data/lib/boto/web/html.rb +65 -0
- data/lib/boto/web/layouts/context.rb +22 -0
- data/lib/boto/web/layouts/template.rb +31 -0
- data/lib/boto/web/layouts.rb +69 -0
- data/lib/boto/web/modules.rb +37 -0
- data/lib/boto/web/page_endpoint.rb +65 -0
- data/lib/boto/web/project_helpers.rb +54 -0
- data/lib/boto/web/protocol.rb +85 -0
- data/lib/boto/web/registry.rb +86 -0
- data/lib/boto/web/registry_error.rb +8 -0
- data/lib/boto/web/renderer/erb_source.rb +199 -0
- data/lib/boto/web/renderer/render_context.rb +37 -0
- data/lib/boto/web/renderer.rb +94 -0
- data/lib/boto/web/runtime.rb +45 -0
- data/lib/boto/web/scoped_css.rb +113 -0
- data/lib/boto/web/secret_key.rb +8 -0
- data/lib/boto/web/settings.rb +27 -0
- data/lib/boto/web/snapshot/changes.rb +26 -0
- data/lib/boto/web/snapshot/inputs.rb +50 -0
- data/lib/boto/web/snapshot/token.rb +42 -0
- data/lib/boto/web/snapshot.rb +111 -0
- data/lib/boto/web/static_file.rb +35 -0
- data/lib/boto/web/stylesheet.rb +53 -0
- data/lib/boto/web/tailwind.rb +69 -0
- data/lib/boto/web/template_preview.rb +31 -0
- data/lib/boto/web/transports/http.rb +36 -0
- data/lib/boto/web/transports/sse.rb +45 -0
- data/lib/boto/web/transports.rb +21 -0
- data/lib/boto/web/version.rb +7 -0
- data/lib/boto/web/view_loader.rb +21 -0
- data/lib/boto/web.rb +17 -0
- metadata +172 -0
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
module Web
|
|
5
|
+
module Compiler
|
|
6
|
+
class Expression
|
|
7
|
+
# String methods that have a runtime helper (R.strip, R.startWith...).
|
|
8
|
+
module Strings
|
|
9
|
+
NULLARY = { strip: "R.strip", upcase: "R.upcase", downcase: "R.downcase" }.freeze
|
|
10
|
+
AFFIX = { start_with?: "R.startWith", end_with?: "R.endWith" }.freeze
|
|
11
|
+
|
|
12
|
+
private
|
|
13
|
+
|
|
14
|
+
def string_nullary(base, name)
|
|
15
|
+
helper = NULLARY[name]
|
|
16
|
+
return SERVER unless helper
|
|
17
|
+
|
|
18
|
+
result("#{helper}(#{base.js})", :string, base.deps)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def string_affix(base, name, arg)
|
|
22
|
+
helper = AFFIX[name]
|
|
23
|
+
return SERVER unless helper && arg.type == :string
|
|
24
|
+
|
|
25
|
+
result("#{helper}(#{base.js}, #{arg.js})", :boolean, deps_of(base, arg))
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "prism"
|
|
5
|
+
|
|
6
|
+
module Boto
|
|
7
|
+
module Web
|
|
8
|
+
module Compiler
|
|
9
|
+
# A Ruby expression as JavaScript for the browser. Anything outside the subset is SERVER, never an error.
|
|
10
|
+
class Expression
|
|
11
|
+
include Literals
|
|
12
|
+
include Logic
|
|
13
|
+
include Calls
|
|
14
|
+
include Strings
|
|
15
|
+
include Operators
|
|
16
|
+
include Collections
|
|
17
|
+
|
|
18
|
+
# js: the JavaScript; deps: state, input and computed names it reads; type: the internal type.
|
|
19
|
+
Result = Data.define(:js, :deps, :type)
|
|
20
|
+
SERVER = Object.new.freeze
|
|
21
|
+
# A Ruby block param with one of these names would be a JavaScript syntax error.
|
|
22
|
+
JS_RESERVED = %w[
|
|
23
|
+
await break case catch class const continue debugger default delete do else enum export extends
|
|
24
|
+
false finally for function if import in instanceof new null return static super switch this throw
|
|
25
|
+
true try typeof var void while with yield let implements interface package private protected public
|
|
26
|
+
].freeze
|
|
27
|
+
|
|
28
|
+
def self.compile(source, scope, assignment: false)
|
|
29
|
+
program = Prism.parse(source)
|
|
30
|
+
statements = program.value.statements.body
|
|
31
|
+
return SERVER if program.errors.any? || statements.size != 1
|
|
32
|
+
|
|
33
|
+
compile_node(statements.first, scope, assignment:)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def self.compile_node(node, scope, assignment: false) = new(scope, assignment:).visit(node)
|
|
37
|
+
|
|
38
|
+
def initialize(scope, assignment: false)
|
|
39
|
+
@scope = scope
|
|
40
|
+
@assignment = assignment
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def visit(node)
|
|
44
|
+
case node
|
|
45
|
+
when Prism::StatementsNode then single_statement(node)
|
|
46
|
+
when Prism::ParenthesesNode then visit(node.body)
|
|
47
|
+
when Prism::IntegerNode, Prism::FloatNode then number(node)
|
|
48
|
+
when Prism::StringNode, Prism::SymbolNode then text(node)
|
|
49
|
+
when Prism::InterpolatedStringNode then interpolated(node)
|
|
50
|
+
when Prism::NilNode then result("null", :nil)
|
|
51
|
+
when Prism::TrueNode then result("true", :boolean)
|
|
52
|
+
when Prism::FalseNode then result("false", :boolean)
|
|
53
|
+
when Prism::ArrayNode then array_literal(node.elements)
|
|
54
|
+
when Prism::HashNode, Prism::KeywordHashNode then object_literal(node.elements)
|
|
55
|
+
when Prism::LocalVariableReadNode then read_name(node.name)
|
|
56
|
+
when Prism::ItLocalVariableReadNode then read_name(:it)
|
|
57
|
+
when Prism::LocalVariableWriteNode then assignment(node)
|
|
58
|
+
when Prism::CallNode then call(node)
|
|
59
|
+
when Prism::AndNode then logical(node, "R.and")
|
|
60
|
+
when Prism::OrNode then logical(node, "R.or")
|
|
61
|
+
when Prism::IfNode then ternary(node)
|
|
62
|
+
else SERVER
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
private
|
|
67
|
+
|
|
68
|
+
def read_name(name)
|
|
69
|
+
entry = @scope.lookup(name)
|
|
70
|
+
return SERVER unless entry
|
|
71
|
+
return result(name.to_s, entry[:type]) if entry[:kind] == :local
|
|
72
|
+
return SERVER if entry[:kind] == :computed && !TypeMap.operable?(entry[:type])
|
|
73
|
+
|
|
74
|
+
result("$.#{name}", entry[:type], [name])
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def single_statement(node)
|
|
78
|
+
node.body.size == 1 ? visit(node.body.first) : SERVER
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# The results of the nodes, in order; nil when one of them is SERVER.
|
|
82
|
+
def visit_all(nodes)
|
|
83
|
+
results = nodes.map { |node| visit(node) }
|
|
84
|
+
results.any? { |item| item.equal?(SERVER) } ? nil : results
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def result(js, type, deps = []) = Result.new(js:, deps:, type:)
|
|
88
|
+
|
|
89
|
+
def deps_of(*results) = results.flat_map(&:deps).uniq
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "prism"
|
|
5
|
+
|
|
6
|
+
module Boto
|
|
7
|
+
module Web
|
|
8
|
+
module Compiler
|
|
9
|
+
class ModuleWriter
|
|
10
|
+
# The handlers export: an assignment runs in the browser, an action sends its keyword arguments to the server.
|
|
11
|
+
class HandlersJs
|
|
12
|
+
def initialize(result)
|
|
13
|
+
@result = result
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def call
|
|
17
|
+
entries = @result.handlers.map { |id, handler| " #{id}: #{entry(handler)}," }
|
|
18
|
+
"{\n#{entries.join("\n")}\n}"
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
private
|
|
22
|
+
|
|
23
|
+
def entry(handler)
|
|
24
|
+
handler.kind == :assign ? assign(handler) : action(handler)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def assign(handler)
|
|
28
|
+
result = Expression.compile(handler.source, handler.scope, assignment: true)
|
|
29
|
+
raise Error, "event handlers run in the browser: #{handler.source}" if result.equal?(Expression::SERVER)
|
|
30
|
+
|
|
31
|
+
"{ assign: ($, event, L) => { #{destructure(handler)}#{result.js}; } }"
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def action(handler)
|
|
35
|
+
call = Prism.parse(handler.source).value.statements.body.first
|
|
36
|
+
"{ action: #{JSON.generate(call.name.to_s)}, " \
|
|
37
|
+
"args: ($, event, L) => { #{destructure(handler)}return #{arguments(call, handler)}; } }"
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# `**event` sends the DOM event's data (or the emit of a child) as it is; other keywords are compiled.
|
|
41
|
+
def arguments(call, handler)
|
|
42
|
+
keywords = call.arguments&.arguments&.first
|
|
43
|
+
return "({})" if keywords.nil?
|
|
44
|
+
return "event" if event_splat?(keywords)
|
|
45
|
+
raise Error, "action arguments are keywords: #{handler.source}" unless keywords.is_a?(Prism::KeywordHashNode)
|
|
46
|
+
|
|
47
|
+
result = Expression.compile_node(keywords, handler.scope)
|
|
48
|
+
raise Error, "event handler arguments run in the browser: #{handler.source}" if result.equal?(Expression::SERVER)
|
|
49
|
+
|
|
50
|
+
"(#{result.js})"
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def event_splat?(node)
|
|
54
|
+
return false unless node.is_a?(Prism::KeywordHashNode) && node.elements.size == 1
|
|
55
|
+
|
|
56
|
+
splat = node.elements.first
|
|
57
|
+
splat.is_a?(Prism::AssocSplatNode) && name_of(splat.value) == :event
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def name_of(node)
|
|
61
|
+
return node.name if node.is_a?(Prism::LocalVariableReadNode) || node.is_a?(Prism::CallNode)
|
|
62
|
+
|
|
63
|
+
nil
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def destructure(handler)
|
|
67
|
+
return "" if handler.locals.empty?
|
|
68
|
+
|
|
69
|
+
"const { #{handler.locals.join(", ")} } = L; "
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
module Web
|
|
5
|
+
module Compiler
|
|
6
|
+
class ModuleWriter
|
|
7
|
+
# What the writers share: the text of a template literal, and an expression that must run in the browser.
|
|
8
|
+
module Js
|
|
9
|
+
module_function
|
|
10
|
+
|
|
11
|
+
# Backslashes, backticks and ${ in static HTML would change the template literal, so they are escaped.
|
|
12
|
+
def text(html) = html.gsub(/[\\`]|\$\{/) { "\\#{_1}" }
|
|
13
|
+
|
|
14
|
+
def value(source, scope)
|
|
15
|
+
result = Expression.compile(source, scope)
|
|
16
|
+
raise Error, "#{source} runs on the server, so it has no browser code" if result.equal?(Expression::SERVER)
|
|
17
|
+
|
|
18
|
+
result
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
5
|
+
module Boto
|
|
6
|
+
module Web
|
|
7
|
+
module Compiler
|
|
8
|
+
class ModuleWriter
|
|
9
|
+
# One element of the template as template-literal text, with the attributes in the server's order.
|
|
10
|
+
class TagJs
|
|
11
|
+
BOOLEAN_PROPERTIES = Renderer::ErbSource::BOOLEAN_PROPERTIES
|
|
12
|
+
|
|
13
|
+
def initialize(result, scope, loops)
|
|
14
|
+
@result = result
|
|
15
|
+
@scope = scope
|
|
16
|
+
@loops = loops
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def element(tag)
|
|
20
|
+
return child(tag) if tag.child
|
|
21
|
+
|
|
22
|
+
"<#{tag.name}#{parts(tag).join}>"
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
private
|
|
26
|
+
|
|
27
|
+
def parts(tag)
|
|
28
|
+
statics = tag.attributes.grep(Directives::Static)
|
|
29
|
+
[*scope_parts, *static_parts(statics), *event_parts(tag), *locals_part(tag), *class_part(tag, statics),
|
|
30
|
+
*style_part(tag, statics), *value_parts(tag, statics)]
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# A child is the element the server renders in its place: the browser keeps its content, so it is empty here.
|
|
34
|
+
def child(tag)
|
|
35
|
+
path = [tag.slot.to_s, *@loops.map { "${#{_1}}" }].join(".")
|
|
36
|
+
attrs = [*event_parts(tag), *locals_part(tag), *inputs_part(tag)].join
|
|
37
|
+
|
|
38
|
+
%(<#{tag.name} data-component="#{tag.child.name}" data-path="#{tag.name}:#{path}"#{attrs}></#{tag.name}>)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# The data-inputs the server writes on a child: every declared input in order, null when the parent does not pass it.
|
|
42
|
+
def inputs_part(tag)
|
|
43
|
+
bound = tag.attributes.grep(Directives::Directive).select { _1.kind == :property }.to_h { [_1.name.to_s, _1] }
|
|
44
|
+
entries = Snapshot::Inputs.items(tag.child).map { input_entry(_1, bound[_1.name.to_s]) }
|
|
45
|
+
|
|
46
|
+
[%(${attr("data-inputs", JSON.stringify({ #{entries.join(", ")} }))})]
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def input_entry(item, binding)
|
|
50
|
+
name = JSON.generate(item.name.to_s)
|
|
51
|
+
return "#{name}: null" if binding.nil?
|
|
52
|
+
|
|
53
|
+
shape = JSON.generate(TypeShape.describe(TypeMap.of(item.declared_type)))
|
|
54
|
+
"#{name}: dehydrate(#{shape}, (#{js_of(binding)}))"
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def scope_parts = @result.scope ? [Js.text(" #{@result.scope}")] : []
|
|
58
|
+
|
|
59
|
+
def static_parts(statics)
|
|
60
|
+
statics.reject { %w[class style].include?(_1.name) }.map do |static|
|
|
61
|
+
Js.text(static.value == true ? " #{static.name}" : %( #{static.name}="#{static.value}"))
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def event_parts(tag)
|
|
66
|
+
events = directives(tag).select { _1.kind == :event }
|
|
67
|
+
return [] if events.empty?
|
|
68
|
+
|
|
69
|
+
entries = events.map { "#{[_1.name, *_1.modifiers].join(".")}:#{_1.handler}" }
|
|
70
|
+
[%( data-on="#{entries.join(" ")}")]
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def locals_part(tag)
|
|
74
|
+
names = directives(tag).select { _1.kind == :event }.flat_map { @result.handlers.fetch(_1.handler).locals }.uniq
|
|
75
|
+
return [] if names.empty?
|
|
76
|
+
|
|
77
|
+
["${localsAttr({ #{names.map { "#{_1}: #{_1}" }.join(", ")} })}"]
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def class_part(tag, statics) = bound_part("classAttr", "class", %i[class class_map], tag, statics)
|
|
81
|
+
|
|
82
|
+
def style_part(tag, statics) = bound_part("styleAttr", "style", %i[style], tag, statics)
|
|
83
|
+
|
|
84
|
+
# The static text keeps its own names; the bindings add theirs (or their declarations) after it.
|
|
85
|
+
def bound_part(helper, name, kinds, tag, statics)
|
|
86
|
+
bindings = directives(tag).select { kinds.include?(_1.kind) }
|
|
87
|
+
static = static_text(statics, name)
|
|
88
|
+
return [] if static.nil? && bindings.empty?
|
|
89
|
+
|
|
90
|
+
["${#{helper}(#{JSON.generate(static)}, [#{bindings.map { pair(_1) }.join(", ")}])}"]
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def pair(binding)
|
|
94
|
+
js = Js.value(binding.source, @scope).js
|
|
95
|
+
return "...Object.entries((#{js}))" if binding.kind == :class_map
|
|
96
|
+
|
|
97
|
+
"[#{JSON.generate(binding.name)}, (#{js})]"
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def static_text(statics, name)
|
|
101
|
+
value = statics.find { _1.name == name }&.value
|
|
102
|
+
value.is_a?(String) ? value : nil
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def value_parts(tag, statics)
|
|
106
|
+
directives(tag).flat_map do |dir|
|
|
107
|
+
case dir.kind
|
|
108
|
+
when :property then ["${#{property_helper(dir.name)}(#{JSON.generate(dir.name)}, (#{js_of(dir)}))}"]
|
|
109
|
+
when :attribute then ["${attr(#{JSON.generate(dir.name)}, (#{js_of(dir)}))}"]
|
|
110
|
+
when :track then ["${attr(\"data-key\", (#{js_of(dir)}))}"]
|
|
111
|
+
when :model then model_parts(dir, statics)
|
|
112
|
+
else []
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def property_helper(name) = BOOLEAN_PROPERTIES.include?(name) ? "boolAttr" : "attr"
|
|
118
|
+
|
|
119
|
+
def model_parts(dir, statics)
|
|
120
|
+
checkbox = statics.any? { _1.name == "type" && _1.value == "checkbox" }
|
|
121
|
+
value = checkbox ? %(${boolAttr("checked", (#{js_of(dir)}))}) : %(${attr("value", (#{js_of(dir)}))})
|
|
122
|
+
[Js.text(%( data-model="#{dir.source}")), value]
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def js_of(dir) = Js.value(dir.source, @scope).js
|
|
126
|
+
|
|
127
|
+
def directives(tag) = tag.attributes.grep(Directives::Directive)
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
end
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
module Web
|
|
5
|
+
module Compiler
|
|
6
|
+
class ModuleWriter
|
|
7
|
+
# The body of render: the HTML as a template literal. Server blocks stay as empty <boto-block> tags.
|
|
8
|
+
class TemplateJs
|
|
9
|
+
def initialize(result, scope)
|
|
10
|
+
@result = result
|
|
11
|
+
@scope = scope
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def call
|
|
15
|
+
root = @result.blocks[0]
|
|
16
|
+
return block_tag(root) if root
|
|
17
|
+
|
|
18
|
+
nodes(@result.tree, @scope, [])
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
private
|
|
22
|
+
|
|
23
|
+
def nodes(list, scope, loops) = list.map { node(_1, scope, loops) }.join
|
|
24
|
+
|
|
25
|
+
def node(item, scope, loops)
|
|
26
|
+
case item
|
|
27
|
+
when Analysis::Markup then markup(item.pieces, scope, loops)
|
|
28
|
+
when Template::Output then wrapped(item) { "${esc(#{Js.value(item.source, scope).js})}" }
|
|
29
|
+
when Template::If then wrapped(item) { "${#{branch_chain(item, 0, scope, loops)}}" }
|
|
30
|
+
when Template::Each then wrapped(item) { each(item, scope, loops) }
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def wrapped(item)
|
|
35
|
+
return yield if item.block.nil?
|
|
36
|
+
|
|
37
|
+
block_tag(@result.blocks.fetch(item.block))
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def block_tag(block)
|
|
41
|
+
%(<boto-block data-block="#{block.id}" data-deps="#{block.deps.join(" ")}" style="display:contents"></boto-block>)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def markup(pieces, scope, loops)
|
|
45
|
+
pieces.each_with_index.map do |piece, index|
|
|
46
|
+
next Js.text(after_child(piece, index.zero? ? nil : pieces[index - 1])) if piece.is_a?(String)
|
|
47
|
+
|
|
48
|
+
TagJs.new(@result, scope, loops).element(piece)
|
|
49
|
+
end.join
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# The template keeps the child's closing tag; the child's own element already writes it.
|
|
53
|
+
def after_child(text, previous)
|
|
54
|
+
return text unless previous.is_a?(Directives::Tag) && previous.child
|
|
55
|
+
|
|
56
|
+
text.delete_prefix("</#{previous.name}>")
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Each branch is a template literal; the last one without a condition is the else (or "").
|
|
60
|
+
def branch_chain(node, index, scope, loops)
|
|
61
|
+
condition, children = node.branches[index]
|
|
62
|
+
body = "`#{nodes(children, scope, loops)}`"
|
|
63
|
+
return body if condition.nil?
|
|
64
|
+
|
|
65
|
+
test = "R.t(#{Js.value(condition, scope).js})"
|
|
66
|
+
test = "!#{test}" if index.zero? && node.negate
|
|
67
|
+
following = index + 1 < node.branches.size ? branch_chain(node, index + 1, scope, loops) : '""'
|
|
68
|
+
"#{test} ? #{body} : #{following}"
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# Walks with the index in every each, as the server's ERB does, so the paths of the children match.
|
|
72
|
+
def each(node, scope, loops)
|
|
73
|
+
collection = Js.value(node.collection, scope)
|
|
74
|
+
index = node.with_index ? node.vars.last.to_s : "__boto_i#{loops.size}"
|
|
75
|
+
params = node.with_index ? node.vars.join(", ") : "#{node.vars.first}, #{index}"
|
|
76
|
+
body = nodes(node.children, local_scope(node, collection.type, scope), [*loops, index])
|
|
77
|
+
|
|
78
|
+
"${R.map(#{collection.js}, (#{params}) => `#{body}`).join(\"\")}"
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def local_scope(node, collection_type, scope)
|
|
82
|
+
element = TypeMap.element(collection_type)
|
|
83
|
+
node.vars.zip([element, :integer]).reduce(scope) { |inner, (name, type)| inner.with_local(name, type) }
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
module Web
|
|
5
|
+
module Compiler
|
|
6
|
+
class ModuleWriter
|
|
7
|
+
# The shape of a type as the runtime converts it: scalars by name, lists and schemas recursively.
|
|
8
|
+
# The module's types export and the child inputs of a template both use it.
|
|
9
|
+
module TypeShape
|
|
10
|
+
SCALAR_NAMES = TypeMap::SCALARS.values.freeze
|
|
11
|
+
|
|
12
|
+
module_function
|
|
13
|
+
|
|
14
|
+
def describe(type)
|
|
15
|
+
return { "list" => describe(type[1]) } if TypeMap.list_type?(type)
|
|
16
|
+
return { "schema" => schema_fields(type[1]) } if TypeMap.schema_type?(type)
|
|
17
|
+
|
|
18
|
+
SCALAR_NAMES.include?(type) ? type.to_s : "any"
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def schema_fields(schema)
|
|
22
|
+
schema.fields.to_h { [_1.key, describe(TypeMap.of(_1.declared_type))] }
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
5
|
+
module Boto
|
|
6
|
+
module Web
|
|
7
|
+
module Compiler
|
|
8
|
+
# The browser module of a component: render produces the HTML the server renders, and the other exports
|
|
9
|
+
# are what the runtime needs to keep it in step (handlers, computeds, server blocks, deps and types).
|
|
10
|
+
class ModuleWriter
|
|
11
|
+
# scope is the CSS attribute of the class (nil: none); client_helpers maps each project helper that runs in
|
|
12
|
+
# the browser to its compiled arrow (see ClientHelper).
|
|
13
|
+
# base is relative to the module's URL: components are served at <app>/boto-web/components/<Class>.js.
|
|
14
|
+
def initialize(component_class, registry: Registry.new, scope: nil, base: "../", client_helpers: {})
|
|
15
|
+
@component = component_class
|
|
16
|
+
@base = base
|
|
17
|
+
@client_helpers = client_helpers
|
|
18
|
+
@helper_names = client_helpers.keys.freeze
|
|
19
|
+
@scope = TypeScope.new(component_class, helpers: @helper_names)
|
|
20
|
+
@result = Analysis.new(component_class, registry:, scope:, helpers: @helper_names).call
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def source
|
|
24
|
+
[imports, helpers_export, render_export, handlers_export, computeds_export, blocks_export, deps_export,
|
|
25
|
+
types_export, live_export].join("\n") + "\n"
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
private
|
|
29
|
+
|
|
30
|
+
def imports
|
|
31
|
+
<<~JS
|
|
32
|
+
import { D } from "#{@base}template/decimal.js";
|
|
33
|
+
import { R } from "#{@base}template/ruby.js";
|
|
34
|
+
import { H } from "#{@base}template/helpers.js";
|
|
35
|
+
import { esc, attr, boolAttr, classAttr, styleAttr, localsAttr } from "#{@base}template/html.js";
|
|
36
|
+
import { dehydrate } from "#{@base}template/hydrate.js";
|
|
37
|
+
JS
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# P is the project's client helpers, called by the template as P.name(...), as H holds the series ones.
|
|
41
|
+
def helpers_export
|
|
42
|
+
entries = @client_helpers.map { |name, js| "#{name}: #{js}" }
|
|
43
|
+
"const P = Object.freeze({ #{entries.join(", ")} });\n"
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def render_export = "export const render = ($) => `#{TemplateJs.new(@result, @scope).call}`;\n"
|
|
47
|
+
|
|
48
|
+
def handlers_export = "export const handlers = #{HandlersJs.new(@result).call};\n"
|
|
49
|
+
|
|
50
|
+
def computeds_export
|
|
51
|
+
entries = @component.definition.computeds.filter_map do |name|
|
|
52
|
+
node = ComputedBody.of(@component, name) or next
|
|
53
|
+
|
|
54
|
+
result = Expression.compile_node(node, TypeScope.new(@component, helpers: @helper_names))
|
|
55
|
+
"#{name}: ($) => #{result.js}" unless result.equal?(Expression::SERVER)
|
|
56
|
+
end
|
|
57
|
+
"export const computeds = { #{entries.join(", ")} };\n"
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def blocks_export
|
|
61
|
+
blocks = @result.blocks.values.to_h { [_1.id, _1.deps.map(&:to_s)] }
|
|
62
|
+
"export const blocks = #{JSON.generate(blocks)};\n"
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def deps_export = "export const deps = #{JSON.generate(@result.deps.map(&:to_s))};\n"
|
|
66
|
+
|
|
67
|
+
def types_export
|
|
68
|
+
declared = [*@component.definition.inputs.values, *@component.definition.states.values]
|
|
69
|
+
types = declared.to_h { [_1.name.to_s, TypeShape.describe(TypeMap.of(_1.declared_type))] }
|
|
70
|
+
params = @component.declared_params.to_h { |name, type| [name.to_s, TypeShape.describe(TypeMap.of(type))] }
|
|
71
|
+
"export const types = #{JSON.generate(types.merge(params))};\n"
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# A view that subscribes is live: the runtime asks it to refresh on each channel event.
|
|
75
|
+
def live_export = "export const live = #{!@component.definition.channels_block.nil?};\n"
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|