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,69 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
module Web
|
|
5
|
+
# The layouts of a project (src/views/_layouts/<name>.html.erb), compiled at boot. A view names its layout
|
|
6
|
+
# (`layout :admin`; :application by default; false for none). :application falls back to the gem's minimal one.
|
|
7
|
+
class Layouts
|
|
8
|
+
Error = Class.new(StandardError)
|
|
9
|
+
|
|
10
|
+
EXTENSION = ".html.erb"
|
|
11
|
+
DEFAULT = Boto::View::DEFAULT_LAYOUT
|
|
12
|
+
# The page's HTML goes where the layout yields; boto_head brings the stylesheets and the boot script.
|
|
13
|
+
MINIMAL = <<~HTML
|
|
14
|
+
<!DOCTYPE html>
|
|
15
|
+
<html>
|
|
16
|
+
<head>
|
|
17
|
+
<meta charset="utf-8">
|
|
18
|
+
<title><%= title %></title>
|
|
19
|
+
<%== boto_head %>
|
|
20
|
+
</head>
|
|
21
|
+
<body><%== yield %></body>
|
|
22
|
+
</html>
|
|
23
|
+
HTML
|
|
24
|
+
|
|
25
|
+
class << self
|
|
26
|
+
# dir is the _layouts folder; nil or a missing folder means the project has no layouts of its own.
|
|
27
|
+
def load(dir:, helpers: [])
|
|
28
|
+
files = dir && File.directory?(dir) ? Dir.glob("*#{EXTENSION}", base: dir) : []
|
|
29
|
+
new(files.to_h { |file| [file.delete_suffix(EXTENSION).to_sym, read(File.join(dir, file))] }, helpers)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
private
|
|
33
|
+
|
|
34
|
+
def read(path) = Template.compile(File.read(path, encoding: Encoding::UTF_8))
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# The context class is built here, once, with the project's helpers. helpers is positional so that a
|
|
38
|
+
# braceless `new(name: template)` still reads as the templates.
|
|
39
|
+
def initialize(templates, helpers = [])
|
|
40
|
+
templates = { DEFAULT => Template.compile(MINIMAL), **templates }.freeze
|
|
41
|
+
@methods = templates.keys.each_with_index.to_h { |name, index| [name, :"__layout_#{index}__"] }.freeze
|
|
42
|
+
codes = templates.to_h { |name, template| [@methods.fetch(name), template.code] }
|
|
43
|
+
@context_class = CodeClass.build(Context, helpers:, codes:, file: "boto layouts")
|
|
44
|
+
freeze
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# A context for one page: the title and boto_head of the request, on the class built at boot.
|
|
48
|
+
def context(title:, head:) = @context_class.new(title:, head:)
|
|
49
|
+
|
|
50
|
+
# Refused at boot: a view whose layout the project does not have.
|
|
51
|
+
def check!(views)
|
|
52
|
+
views.each do |view|
|
|
53
|
+
name = view.layout
|
|
54
|
+
next if name == false || @methods.key?(name)
|
|
55
|
+
|
|
56
|
+
raise Error, "#{view.name || view.inspect} uses layout #{name.inspect}, " \
|
|
57
|
+
"but _layouts/#{name}#{EXTENSION} does not exist"
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# The page's HTML inside its layout; the page alone for a view with layout false.
|
|
62
|
+
def render(name, context, html)
|
|
63
|
+
return html unless name
|
|
64
|
+
|
|
65
|
+
context.public_send(@methods.fetch(name)) { html }
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
module Web
|
|
5
|
+
# The browser module of each component and view of a project: compiled on the first request, kept per class.
|
|
6
|
+
class Modules
|
|
7
|
+
# client_helpers: the project's client helpers, name => compiled arrow (ProjectHelpers#client_helpers).
|
|
8
|
+
def initialize(registry:, scope: nil, client_helpers: {})
|
|
9
|
+
@registry = registry
|
|
10
|
+
@scope = scope
|
|
11
|
+
@client_helpers = client_helpers
|
|
12
|
+
@classes = (registry.components + registry.views).to_h { [_1.name, _1] }.freeze
|
|
13
|
+
@sources = {}
|
|
14
|
+
@mutex = Mutex.new
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# nil for a name the registry does not hold: a URL never reaches the compiler with an arbitrary constant.
|
|
18
|
+
def fetch(class_name)
|
|
19
|
+
klass = @classes[class_name] or return nil
|
|
20
|
+
|
|
21
|
+
@mutex.synchronize { @sources[klass] ||= compile(klass) }
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Forgets every compiled module, for the reload of development.
|
|
25
|
+
def clear
|
|
26
|
+
@mutex.synchronize { @sources.clear }
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
private
|
|
30
|
+
|
|
31
|
+
def compile(klass)
|
|
32
|
+
Compiler::ModuleWriter.new(klass, registry: @registry, scope: @scope&.call(klass),
|
|
33
|
+
client_helpers: @client_helpers).source
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
module Web
|
|
5
|
+
# The endpoint of one view: its route, params and plugin declarations are the view's, so the core runs it like any
|
|
6
|
+
# endpoint. The call renders the page, or, when the transport set an envelope, runs the action (see Transports::Http).
|
|
7
|
+
class PageEndpoint < Boto::Endpoint
|
|
8
|
+
ENVELOPE_KEY = "boto.web.envelope"
|
|
9
|
+
HTML_TYPE = "text/html; charset=utf-8"
|
|
10
|
+
# The declarations a View keeps in the same instance variables as an endpoint (see Endpoint::Dsl).
|
|
11
|
+
DECLARATIONS = %i[@route @params @skipped_plugins @authentication @authorization @rate_limit_rule @middlewares].freeze
|
|
12
|
+
|
|
13
|
+
class << self
|
|
14
|
+
attr_accessor :view, :runtime
|
|
15
|
+
|
|
16
|
+
def name = view ? view.name : super
|
|
17
|
+
|
|
18
|
+
# A subclass per view, built at boot; the core compiles and routes it like a discovered endpoint.
|
|
19
|
+
def for(view, runtime)
|
|
20
|
+
klass = Class.new(self)
|
|
21
|
+
klass.view = view
|
|
22
|
+
klass.runtime = runtime
|
|
23
|
+
DECLARATIONS.each { |ivar| klass.instance_variable_set(ivar, view.instance_variable_get(ivar)) }
|
|
24
|
+
klass
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def call(**args)
|
|
29
|
+
return action_response if env[ENVELOPE_KEY]
|
|
30
|
+
|
|
31
|
+
page_response(args)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
private
|
|
35
|
+
|
|
36
|
+
def page_response(args)
|
|
37
|
+
runtime = self.class.runtime
|
|
38
|
+
page = self.class.view.build(**args)
|
|
39
|
+
html = runtime.renderer.render_root(page, tag: "boto-view", path: "")
|
|
40
|
+
body = runtime.layouts.render(self.class.view.layout, layout_context(page), html)
|
|
41
|
+
App::RackResponse.new(status: 200, headers: { "content-type" => HTML_TYPE }, body: [body])
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def layout_context(page)
|
|
45
|
+
head = Head.html(script_name: env["SCRIPT_NAME"].to_s, csrf_token: csrf_token)
|
|
46
|
+
self.class.runtime.layouts.context(title: page_title(page), head:)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# The view's own title when it defines one, else the project's name.
|
|
50
|
+
def page_title(page) = page.respond_to?(:title) ? page.title : self.class.runtime.project_name
|
|
51
|
+
|
|
52
|
+
def action_response
|
|
53
|
+
status, body = self.class.runtime.dispatcher.call(env[ENVELOPE_KEY])
|
|
54
|
+
Transports.json(status, body)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# The masked token of the session, for the meta tag; only when the sessions plugin has run for this request.
|
|
58
|
+
def csrf_token
|
|
59
|
+
return unless defined?(Boto::Plugins::Csrf) && env[Boto::Plugins::Sessions::SESSION_ENV]
|
|
60
|
+
|
|
61
|
+
Boto::Plugins::Csrf.token(env)
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
module Web
|
|
5
|
+
# The helper modules of a project (src/helpers/), checked at boot: a method name is defined once (not in two
|
|
6
|
+
# modules, not as a series helper), and each client helper compiles to JavaScript.
|
|
7
|
+
class ProjectHelpers
|
|
8
|
+
class Error < StandardError; end
|
|
9
|
+
|
|
10
|
+
SERIES_OWNER = "Boto::Web::Helpers"
|
|
11
|
+
|
|
12
|
+
attr_reader :modules, :client_helper_names, :client_helpers
|
|
13
|
+
|
|
14
|
+
# The module constants of a namespace (Projeto::Helpers), loaded by Zeitwerk.
|
|
15
|
+
def self.from(namespace) = new(namespace.constants.map { namespace.const_get(_1) }.grep(Module))
|
|
16
|
+
|
|
17
|
+
def initialize(modules)
|
|
18
|
+
@modules = modules.freeze
|
|
19
|
+
check_extended
|
|
20
|
+
check_names
|
|
21
|
+
@client_helper_names = @modules.flat_map(&:client_helper_names).freeze
|
|
22
|
+
@client_helpers = compile_client_helpers
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
private
|
|
26
|
+
|
|
27
|
+
def check_extended
|
|
28
|
+
plain = @modules.find { !_1.is_a?(Boto::Helper) }
|
|
29
|
+
raise Error, "#{label(plain)} must extend Boto::Helper" if plain
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def check_names
|
|
33
|
+
owners = Boto::Web::Helpers::NAMES.to_h { [_1, SERIES_OWNER] }
|
|
34
|
+
@modules.each do |mod|
|
|
35
|
+
mod.instance_methods(false).each do |name|
|
|
36
|
+
raise Error, "helper #{name} is defined in #{owners[name]} and in #{label(mod)}" if owners.key?(name)
|
|
37
|
+
|
|
38
|
+
owners[name] = label(mod)
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def compile_client_helpers
|
|
44
|
+
@modules.each_with_object({}) do |mod, compiled|
|
|
45
|
+
mod.client_helper_names.each do |name|
|
|
46
|
+
compiled[name] = Compiler::ClientHelper.compile(mod, name, @client_helper_names)
|
|
47
|
+
end
|
|
48
|
+
end.freeze
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def label(mod) = mod.name || mod.inspect
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
5
|
+
module Boto
|
|
6
|
+
module Web
|
|
7
|
+
# The v1 envelopes between the browser runtime and the server. Every envelope carries "v": 1.
|
|
8
|
+
#
|
|
9
|
+
# Request (POST <base>/_boto/web), by "kind":
|
|
10
|
+
# "action" view, path, component (when path is not empty), changed, action, args
|
|
11
|
+
# "refresh" view, path, component (when path is not empty), changed, inputs (a child's inputs, optional)
|
|
12
|
+
# "block" view, path, component (when path is not empty), changed, block (the id, e.g. "b3")
|
|
13
|
+
#
|
|
14
|
+
# Response:
|
|
15
|
+
# "result" view, component (a child only), states (what changed), events, blocks
|
|
16
|
+
# "error" status, reason, errors (each { path, message }, filled for "invalid_input")
|
|
17
|
+
# reasons: invalid_snapshot (400), expired (400), bad_request (400), unknown_action (400),
|
|
18
|
+
# unsupported_version (400), invalid_input (422)
|
|
19
|
+
#
|
|
20
|
+
# The live channel is not an envelope: its events are named by SSE, and the transport owns them.
|
|
21
|
+
module Protocol
|
|
22
|
+
VERSION = 1
|
|
23
|
+
KINDS = %w[action refresh block].freeze
|
|
24
|
+
|
|
25
|
+
class BadRequest < Boto::Error::BadRequest; end
|
|
26
|
+
|
|
27
|
+
class UnsupportedVersion < BadRequest; end
|
|
28
|
+
|
|
29
|
+
module_function
|
|
30
|
+
|
|
31
|
+
def parse(json)
|
|
32
|
+
envelope = load_object(json)
|
|
33
|
+
raise UnsupportedVersion, "version #{envelope["v"].inspect} is not #{VERSION}" unless envelope["v"] == VERSION
|
|
34
|
+
|
|
35
|
+
check!(envelope)
|
|
36
|
+
envelope
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def result(view:, states:, events:, blocks:, component: nil)
|
|
40
|
+
body = { "v" => VERSION, "kind" => "result", "view" => view, "states" => states, "events" => events, "blocks" => blocks }
|
|
41
|
+
body["component"] = component if component
|
|
42
|
+
body
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def error(status, reason, errors = [])
|
|
46
|
+
{ "v" => VERSION, "kind" => "error", "status" => status, "reason" => reason, "errors" => errors }
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def load_object(json)
|
|
50
|
+
value = JSON.parse(json)
|
|
51
|
+
raise BadRequest, "envelope is not an object" unless value.is_a?(Hash)
|
|
52
|
+
|
|
53
|
+
value
|
|
54
|
+
rescue JSON::ParserError
|
|
55
|
+
raise BadRequest, "envelope is not JSON"
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def check!(envelope)
|
|
59
|
+
raise BadRequest, "kind must be one of #{KINDS.join(", ")}" unless KINDS.include?(envelope["kind"])
|
|
60
|
+
raise BadRequest, "view must be a string" unless envelope["view"].is_a?(String)
|
|
61
|
+
raise BadRequest, "changed must be an object" unless envelope["changed"].is_a?(Hash)
|
|
62
|
+
raise BadRequest, "inputs must be an object" unless envelope.fetch("inputs", {}).is_a?(Hash)
|
|
63
|
+
|
|
64
|
+
check_path!(envelope)
|
|
65
|
+
check_kind!(envelope)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def check_path!(envelope)
|
|
69
|
+
path = envelope["path"]
|
|
70
|
+
raise BadRequest, "path must be an array of strings" unless path.is_a?(Array) && path.all?(String)
|
|
71
|
+
raise BadRequest, "component is missing" if !path.empty? && !envelope["component"].is_a?(String)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def check_kind!(envelope)
|
|
75
|
+
case envelope["kind"]
|
|
76
|
+
when "action"
|
|
77
|
+
raise BadRequest, "action must be a string" unless envelope["action"].is_a?(String)
|
|
78
|
+
raise BadRequest, "args must be an object" unless envelope["args"].is_a?(Hash)
|
|
79
|
+
when "block"
|
|
80
|
+
raise BadRequest, "block must be a string" unless envelope["block"].is_a?(String)
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
module Web
|
|
5
|
+
# The components and views of one project, indexed once at boot. Refuses what the browser could not
|
|
6
|
+
# resolve: a selector used twice, a component with a route, a component without a selector.
|
|
7
|
+
class Registry
|
|
8
|
+
COMPONENTS_FOLDER = :Components
|
|
9
|
+
VIEWS_FOLDER = :Views
|
|
10
|
+
|
|
11
|
+
class << self
|
|
12
|
+
# Components are collected as any Boto::Component (a View found there is refused for its route).
|
|
13
|
+
def from_namespace(namespace)
|
|
14
|
+
new(components: classes_in(namespace, COMPONENTS_FOLDER, Boto::Component),
|
|
15
|
+
views: classes_in(namespace, VIEWS_FOLDER, Boto::View))
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
private
|
|
19
|
+
|
|
20
|
+
def classes_in(namespace, folder, kind)
|
|
21
|
+
return [] unless namespace.const_defined?(folder, false)
|
|
22
|
+
|
|
23
|
+
collect(namespace.const_get(folder, false), kind)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def collect(mod, kind)
|
|
27
|
+
mod.constants(false).flat_map { |name| found_in(mod, mod.const_get(name, false), kind) }
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def found_in(mod, value, kind)
|
|
31
|
+
case value
|
|
32
|
+
when Class then value < kind ? [value] : []
|
|
33
|
+
when Module then nested?(mod, value) ? collect(value, kind) : []
|
|
34
|
+
else []
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Only modules named under mod are walked, so an alias to an outer module cannot loop.
|
|
39
|
+
def nested?(mod, value)
|
|
40
|
+
value.name&.start_with?("#{mod.name}::")
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
attr_reader :components, :views
|
|
45
|
+
|
|
46
|
+
def initialize(components: [], views: [])
|
|
47
|
+
@components = components.dup.freeze
|
|
48
|
+
@views = views.dup.freeze
|
|
49
|
+
@by_selector = index_components(@components)
|
|
50
|
+
freeze
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def component_for(selector) = @by_selector[selector]
|
|
54
|
+
|
|
55
|
+
private
|
|
56
|
+
|
|
57
|
+
def index_components(components)
|
|
58
|
+
components.each_with_object({}) do |klass, index|
|
|
59
|
+
check_component!(klass)
|
|
60
|
+
check_selector!(index, klass)
|
|
61
|
+
index[klass.definition.selector] = klass
|
|
62
|
+
end.freeze
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def check_component!(klass)
|
|
66
|
+
raise RegistryError, "#{describe(klass)} has no selector" unless klass.definition.selector
|
|
67
|
+
return unless klass.respond_to?(:route) && klass.route
|
|
68
|
+
|
|
69
|
+
raise RegistryError, "#{describe(klass)} has a route (#{klass.route.join(" ")}); components have none, use a view"
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def check_selector!(index, klass)
|
|
73
|
+
selector = klass.definition.selector
|
|
74
|
+
return unless index.key?(selector)
|
|
75
|
+
|
|
76
|
+
raise RegistryError, "selector \"#{selector}\" is declared twice: " \
|
|
77
|
+
"#{describe(index.fetch(selector))} and #{describe(klass)}"
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def describe(klass)
|
|
81
|
+
file = klass.name && Object.const_source_location(klass.name).first
|
|
82
|
+
"#{klass.name || klass.inspect} (#{file || "unknown file"})"
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
end
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
module Web
|
|
5
|
+
class Renderer
|
|
6
|
+
# The ERB of a component, or of one of its blocks, built from the analyzed tree. The output is the HTML
|
|
7
|
+
# the browser gets: directives become attributes, and each server block gets its <boto-block> wrapper.
|
|
8
|
+
class ErbSource
|
|
9
|
+
BOOLEAN_PROPERTIES = %w[disabled checked selected hidden readonly required open].freeze
|
|
10
|
+
|
|
11
|
+
# The ERB of a block's node, without the wrapper of that block itself.
|
|
12
|
+
def self.block(result, block)
|
|
13
|
+
skip = block.node.respond_to?(:block) ? block.node.block : nil
|
|
14
|
+
new(result, skip:).nodes(Array(block.node))
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def initialize(result, skip: nil)
|
|
18
|
+
@result = result
|
|
19
|
+
@skip = skip
|
|
20
|
+
@loops = []
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def component
|
|
24
|
+
html = nodes(@result.tree)
|
|
25
|
+
root = @result.blocks[0]
|
|
26
|
+
root ? block_tag(root, html) : html
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def nodes(list) = list.map { node(_1) }.join
|
|
30
|
+
|
|
31
|
+
private
|
|
32
|
+
|
|
33
|
+
def node(item)
|
|
34
|
+
case item
|
|
35
|
+
when Compiler::Analysis::Markup then markup(item.pieces)
|
|
36
|
+
when Compiler::Template::Output then wrapped(item) { "<%= Boto::Web::Html.escape((#{item.source})) %>" }
|
|
37
|
+
when Compiler::Template::If then wrapped(item) { if_erb(item) }
|
|
38
|
+
when Compiler::Template::Each then wrapped(item) { each_erb(item) }
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def wrapped(node)
|
|
43
|
+
return yield if node.block.nil? || node.block == @skip
|
|
44
|
+
|
|
45
|
+
block_tag(@result.blocks.fetch(node.block), yield)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def block_tag(block, body)
|
|
49
|
+
%(<boto-block data-block="#{block.id}" data-deps="#{block.deps.join(" ")}" style="display:contents">#{body}</boto-block>)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def markup(pieces)
|
|
53
|
+
pieces.each_with_index.map do |piece, index|
|
|
54
|
+
if piece.is_a?(String)
|
|
55
|
+
after_child(piece, index.zero? ? nil : pieces[index - 1])
|
|
56
|
+
else
|
|
57
|
+
tag(piece)
|
|
58
|
+
end
|
|
59
|
+
end.join
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# The template keeps the child's closing tag; the child's render already writes it.
|
|
63
|
+
def after_child(text, previous)
|
|
64
|
+
return text unless previous.is_a?(Compiler::Directives::Tag) && previous.child
|
|
65
|
+
|
|
66
|
+
text.delete_prefix("</#{previous.name}>")
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def tag(tag)
|
|
70
|
+
return child(tag) if tag.child
|
|
71
|
+
|
|
72
|
+
"<#{tag.name}<%= Boto::Web::Html.attrs(#{tag_parts(tag).join(", ")}) %>>"
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def child(tag)
|
|
76
|
+
inputs = directives(tag).select { _1.kind == :property }.map { "#{_1.name.to_sym.inspect} => (#{_1.source})" }
|
|
77
|
+
"<%= child(#{tag.name.inspect}, { #{inputs.join(", ")} }, " \
|
|
78
|
+
"Boto::Web::Html.attrs(#{event_parts(tag).concat(locals_part(tag)).join(", ")}), #{slot_code(tag)}) %>"
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# The data-path index: the static slot, then the index of each each around the tag.
|
|
82
|
+
def slot_code(tag)
|
|
83
|
+
parts = [tag.slot.to_s, *@loops.map { "\#{#{_1}}" }]
|
|
84
|
+
'"' + parts.join(".") + '"'
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def tag_parts(tag)
|
|
88
|
+
statics = tag.attributes.grep(Compiler::Directives::Static)
|
|
89
|
+
[*scope_parts, *static_parts(statics), *event_parts(tag), *locals_part(tag), *class_part(statics, tag),
|
|
90
|
+
*style_part(statics, tag), *value_parts(tag, statics)]
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# The scope attribute goes first, as a bare attribute, in the same place as the browser's TagJs.
|
|
94
|
+
def scope_parts = @result.scope ? [literal(" #{@result.scope}")] : []
|
|
95
|
+
|
|
96
|
+
def static_parts(statics)
|
|
97
|
+
statics.reject { %w[class style].include?(_1.name) }.map do |static|
|
|
98
|
+
literal(static.value == true ? " #{static.name}" : %( #{static.name}="#{static.value}"))
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def event_parts(tag)
|
|
103
|
+
events = directives(tag).select { _1.kind == :event }
|
|
104
|
+
return [] if events.empty?
|
|
105
|
+
|
|
106
|
+
entries = events.map { "#{[_1.name, *_1.modifiers].join(".")}:#{_1.handler}" }
|
|
107
|
+
[literal(%( data-on="#{entries.join(" ")}"))]
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def locals_part(tag)
|
|
111
|
+
names = directives(tag).select { _1.kind == :event }.flat_map { @result.handlers.fetch(_1.handler).locals }.uniq
|
|
112
|
+
return [] if names.empty?
|
|
113
|
+
|
|
114
|
+
entries = names.map { "#{_1.to_s.inspect} => #{_1}" }
|
|
115
|
+
["Boto::Web::Html.locals_attr({ #{entries.join(", ")} })"]
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def class_part(statics, tag)
|
|
119
|
+
bindings = directives(tag).select { %i[class class_map].include?(_1.kind) }
|
|
120
|
+
static = static_text(statics, "class")
|
|
121
|
+
return [] if static.nil? && bindings.empty?
|
|
122
|
+
|
|
123
|
+
["Boto::Web::Html.class_attr(#{static.inspect}, [#{pairs(bindings).join(", ")}])"]
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def style_part(statics, tag)
|
|
127
|
+
bindings = directives(tag).select { _1.kind == :style }
|
|
128
|
+
static = static_text(statics, "style")
|
|
129
|
+
return [] if static.nil? && bindings.empty?
|
|
130
|
+
|
|
131
|
+
["Boto::Web::Html.style_attr(#{static.inspect}, [#{pairs(bindings).join(", ")}])"]
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def static_text(statics, name)
|
|
135
|
+
value = statics.find { _1.name == name }&.value
|
|
136
|
+
value.is_a?(String) ? value : nil
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
def pairs(bindings)
|
|
140
|
+
bindings.map do |binding|
|
|
141
|
+
binding.kind == :class_map ? "*(#{binding.source}).to_a" : "[#{binding.name.inspect}, (#{binding.source})]"
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def value_parts(tag, statics)
|
|
146
|
+
directives(tag).flat_map do |dir|
|
|
147
|
+
case dir.kind
|
|
148
|
+
when :property then [property_part(dir)]
|
|
149
|
+
when :attribute then ["Boto::Web::Html.attr(#{dir.name.inspect}, (#{dir.source}))"]
|
|
150
|
+
when :track then ["Boto::Web::Html.attr(\"data-key\", (#{dir.source}))"]
|
|
151
|
+
when :model then model_parts(dir, statics)
|
|
152
|
+
else []
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
def property_part(dir)
|
|
158
|
+
helper = BOOLEAN_PROPERTIES.include?(dir.name) ? "bool_attr" : "attr"
|
|
159
|
+
"Boto::Web::Html.#{helper}(#{dir.name.inspect}, (#{dir.source}))"
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
def model_parts(dir, statics)
|
|
163
|
+
checkbox = statics.any? { _1.name == "type" && _1.value == "checkbox" }
|
|
164
|
+
value = checkbox ? "Boto::Web::Html.bool_attr(\"checked\", (#{dir.source}))" : "Boto::Web::Html.attr(\"value\", (#{dir.source}))"
|
|
165
|
+
[literal(%( data-model="#{dir.source}")), value]
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
def directives(tag) = tag.attributes.grep(Compiler::Directives::Directive)
|
|
169
|
+
|
|
170
|
+
# A literal inside <%= %> must not contain the ERB closing tag, so %> is written as an escape.
|
|
171
|
+
def literal(text) = text.inspect.gsub("%>") { '\u0025>' }
|
|
172
|
+
|
|
173
|
+
def if_erb(node)
|
|
174
|
+
body = node.branches.each_with_index.map do |(condition, children), index|
|
|
175
|
+
"<% #{if_head(node, condition, index)} %>#{nodes(children)}"
|
|
176
|
+
end
|
|
177
|
+
"#{body.join}<% end %>"
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
def if_head(node, condition, index)
|
|
181
|
+
return "else" unless condition
|
|
182
|
+
|
|
183
|
+
keyword = index.zero? ? (node.negate ? "unless" : "if") : "elsif"
|
|
184
|
+
"#{keyword} (#{condition})"
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
# Every each walks with its index, so the children inside know their iteration.
|
|
188
|
+
def each_erb(node)
|
|
189
|
+
index = node.with_index ? node.vars.last.to_s : "__boto_i#{@loops.size}"
|
|
190
|
+
params = node.with_index ? node.vars.join(", ") : "#{node.vars.first}, #{index}"
|
|
191
|
+
@loops.push(index)
|
|
192
|
+
body = nodes(node.children)
|
|
193
|
+
@loops.pop
|
|
194
|
+
"<% (#{node.collection}).each_with_index do |#{params}| %>#{body}<% end %>"
|
|
195
|
+
end
|
|
196
|
+
end
|
|
197
|
+
end
|
|
198
|
+
end
|
|
199
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
module Web
|
|
5
|
+
class Renderer
|
|
6
|
+
# Where a component's ERB runs: the component's own names, the helpers, and the children of this render.
|
|
7
|
+
# The class is built once per component (see .subclass); each request only instantiates it.
|
|
8
|
+
class RenderContext
|
|
9
|
+
include Helpers
|
|
10
|
+
|
|
11
|
+
# Names are defined on the subclass, so a state called open wins over Kernel#open.
|
|
12
|
+
def self.subclass(component, helpers:, codes:, file:)
|
|
13
|
+
CodeClass.build(self, helpers:, codes:, file:).tap do |context|
|
|
14
|
+
names(component).each { |name| context.define_method(name) { @instance.public_send(name) } }
|
|
15
|
+
context.define_method(:pending?) { |name| @instance.pending?(name) }
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def self.names(component)
|
|
20
|
+
definition = component.definition
|
|
21
|
+
[*definition.state_names, *definition.input_names, *definition.computeds, *component.declared_params.keys]
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def initialize(instance, renderer)
|
|
25
|
+
@instance = instance
|
|
26
|
+
@renderer = renderer
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# The index comes from the template (see ErbSource#slot_code), so a block renders the same paths.
|
|
30
|
+
def child(selector, inputs, attrs, index)
|
|
31
|
+
klass = @renderer.component_for(selector)
|
|
32
|
+
@renderer.render_child(klass, inputs:, tag: selector, path: "#{selector}:#{index}", attrs:)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|