command_tower 0.21.1 → 0.22.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 +4 -4
- data/app/controllers/concerns/command_tower/execution/http_boundary.rb +2 -1
- data/docs/api_reference.md +7 -3
- data/docs/bootstrap/11-auth-client-path.md +2 -0
- data/docs/extending.md +1 -1
- data/docs/messaging_integration_guide.md +2 -0
- data/docs/ui.md +138 -0
- data/docs/upgrades/0.22.0.md +29 -0
- data/docs/upgrades/README.md +1 -0
- data/lib/command_tower/configuration/config.rb +6 -0
- data/lib/command_tower/configuration/ui/config.rb +47 -0
- data/lib/command_tower/current.rb +1 -0
- data/lib/command_tower/execution.rb +2 -1
- data/lib/command_tower/ui/builder/frame.rb +19 -0
- data/lib/command_tower/ui/builder/item_builder.rb +27 -0
- data/lib/command_tower/ui/builder/token_resolver.rb +54 -0
- data/lib/command_tower/ui/builder.rb +157 -0
- data/lib/command_tower/ui/catalog.rb +18 -0
- data/lib/command_tower/ui/composer.rb +45 -0
- data/lib/command_tower/ui/document.rb +167 -0
- data/lib/command_tower/ui/email/layout.rb +95 -0
- data/lib/command_tower/ui/email/renderer/containers.rb +72 -0
- data/lib/command_tower/ui/email/renderer/inline_nodes.rb +70 -0
- data/lib/command_tower/ui/email/renderer/styling.rb +91 -0
- data/lib/command_tower/ui/email/renderer/text_nodes.rb +78 -0
- data/lib/command_tower/ui/email/renderer.rb +89 -0
- data/lib/command_tower/ui/email/styles.rb +77 -0
- data/lib/command_tower/ui/fragment.rb +54 -0
- data/lib/command_tower/ui/fragments/cta_group.rb +60 -0
- data/lib/command_tower/ui/fragments/fact_row.rb +20 -0
- data/lib/command_tower/ui/fragments/facts.rb +35 -0
- data/lib/command_tower/ui/fragments/header.rb +51 -0
- data/lib/command_tower/ui/fragments/notice_frame.rb +36 -0
- data/lib/command_tower/ui/hardening.rb +73 -0
- data/lib/command_tower/ui/hostname.rb +124 -0
- data/lib/command_tower/ui/legacy_mapping.rb +66 -0
- data/lib/command_tower/ui/limits.rb +19 -0
- data/lib/command_tower/ui/nodes/action.rb +53 -0
- data/lib/command_tower/ui/nodes/avatar.rb +35 -0
- data/lib/command_tower/ui/nodes/badge.rb +17 -0
- data/lib/command_tower/ui/nodes/banner.rb +17 -0
- data/lib/command_tower/ui/nodes/base.rb +125 -0
- data/lib/command_tower/ui/nodes/button.rb +34 -0
- data/lib/command_tower/ui/nodes/card.rb +19 -0
- data/lib/command_tower/ui/nodes/code.rb +28 -0
- data/lib/command_tower/ui/nodes/component.rb +39 -0
- data/lib/command_tower/ui/nodes/divider.rb +11 -0
- data/lib/command_tower/ui/nodes/facts.rb +14 -0
- data/lib/command_tower/ui/nodes/image.rb +41 -0
- data/lib/command_tower/ui/nodes/instant.rb +59 -0
- data/lib/command_tower/ui/nodes/key_value.rb +63 -0
- data/lib/command_tower/ui/nodes/span.rb +76 -0
- data/lib/command_tower/ui/nodes/stack.rb +24 -0
- data/lib/command_tower/ui/nodes/stat_tile.rb +20 -0
- data/lib/command_tower/ui/nodes/text.rb +24 -0
- data/lib/command_tower/ui/text_derivation.rb +89 -0
- data/lib/command_tower/ui/time_formatter.rb +59 -0
- data/lib/command_tower/ui/tokens/ui_token_mapping.json +277 -0
- data/lib/command_tower/ui/tokens.rb +56 -0
- data/lib/command_tower/ui/zone_abbreviation.rb +130 -0
- data/lib/command_tower/ui.rb +80 -0
- data/lib/command_tower/version.rb +1 -1
- data/lib/command_tower.rb +1 -0
- data/lib/tasks/ui.rake +16 -0
- metadata +67 -1
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module UI
|
|
5
|
+
# Document size limits. Mirrored by the shared fixture
|
|
6
|
+
# spec/fixtures/ui/wire_hardening.json (design section 8.1) so the frontend
|
|
7
|
+
# normalizer enforces the same numbers.
|
|
8
|
+
module Limits
|
|
9
|
+
MAX_NODES = 200
|
|
10
|
+
MAX_DEPTH = 8
|
|
11
|
+
MAX_BYTES = 64 * 1024
|
|
12
|
+
MAX_STRING_LENGTH = 2_000
|
|
13
|
+
MAX_TEXT_FALLBACK_LENGTH = 8_000
|
|
14
|
+
MAX_SPANS = 50
|
|
15
|
+
MAX_KEY_VALUE_ITEMS = 50
|
|
16
|
+
MAX_FACTS_NODES = 100
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module UI
|
|
5
|
+
module Nodes
|
|
6
|
+
module Action
|
|
7
|
+
# `navigate` action. Exactly one destination:
|
|
8
|
+
# path: internal, "/" not "//" (see Hardening.internal_path?)
|
|
9
|
+
# href: external, https (plus mailto and tel), no userinfo
|
|
10
|
+
# Wire: { type: "navigate", destination: { kind: "internal", path: } | { kind: "external", href: } }
|
|
11
|
+
class Navigate < Base
|
|
12
|
+
attribute :path, :string
|
|
13
|
+
attribute :href, :string
|
|
14
|
+
|
|
15
|
+
validate :single_valid_destination
|
|
16
|
+
|
|
17
|
+
def wire_type
|
|
18
|
+
"navigate"
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def as_json(_options = nil)
|
|
22
|
+
{ "type" => wire_type, "destination" => destination_json }
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
private
|
|
26
|
+
|
|
27
|
+
def destination_json
|
|
28
|
+
return { "kind" => "external", "href" => href } if path.nil?
|
|
29
|
+
|
|
30
|
+
{ "kind" => "internal", "path" => path }
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def single_valid_destination
|
|
34
|
+
return errors.add(:base, :destination_required) if path.nil? && href.nil?
|
|
35
|
+
return errors.add(:base, :ambiguous_destination) if path && href
|
|
36
|
+
|
|
37
|
+
validate_path if path
|
|
38
|
+
validate_href if href
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def validate_path
|
|
42
|
+
errors.add(:path, :invalid_path) unless Hardening.internal_path?(path)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def validate_href
|
|
46
|
+
code = Hardening.external_href_code(href)
|
|
47
|
+
errors.add(:href, code) if code
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module UI
|
|
5
|
+
module Nodes
|
|
6
|
+
# Needs `src` (same https and host allowlist as image) or `initials`.
|
|
7
|
+
# `alt` is required when `src` is present.
|
|
8
|
+
class Avatar < Node
|
|
9
|
+
TYPE = "avatar"
|
|
10
|
+
|
|
11
|
+
field :src, :string
|
|
12
|
+
field :initials, :string
|
|
13
|
+
field :size, :string, default: "md"
|
|
14
|
+
field :alt, :string
|
|
15
|
+
|
|
16
|
+
validates :size, inclusion: { in: Tokens::AVATAR_SIZE }
|
|
17
|
+
validates :initials, length: { maximum: 3 }, allow_nil: true
|
|
18
|
+
validate :src_or_initials_present
|
|
19
|
+
validate :src_is_allowed_image, if: -> { src.present? }
|
|
20
|
+
validates :alt, presence: true, if: -> { src.present? }
|
|
21
|
+
|
|
22
|
+
private
|
|
23
|
+
|
|
24
|
+
def src_or_initials_present
|
|
25
|
+
errors.add(:base, :src_or_initials_required) if src.blank? && initials.blank?
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def src_is_allowed_image
|
|
29
|
+
code = Hardening.image_src_code(src, hosts: CommandTower.config.ui.image_hosts)
|
|
30
|
+
errors.add(:src, code) if code
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module UI
|
|
5
|
+
module Nodes
|
|
6
|
+
class Badge < Node
|
|
7
|
+
TYPE = "badge"
|
|
8
|
+
|
|
9
|
+
field :label, :string
|
|
10
|
+
field :tone, :string, default: "neutral"
|
|
11
|
+
|
|
12
|
+
validates :label, presence: true
|
|
13
|
+
validates :tone, inclusion: { in: Tokens::TONE }
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module UI
|
|
5
|
+
module Nodes
|
|
6
|
+
class Banner < Node
|
|
7
|
+
TYPE = "banner"
|
|
8
|
+
|
|
9
|
+
field :tone, :string, default: "neutral"
|
|
10
|
+
field :title, :string
|
|
11
|
+
include Spanned
|
|
12
|
+
|
|
13
|
+
validates :tone, inclusion: { in: Tokens::TONE }
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "active_model"
|
|
4
|
+
|
|
5
|
+
module CommandTower
|
|
6
|
+
module UI
|
|
7
|
+
module Nodes
|
|
8
|
+
ICON_FORMAT = /\A[a-z0-9][a-z0-9_-]{0,63}\z/
|
|
9
|
+
|
|
10
|
+
# Shared machinery for every wire object: node classes, spans, actions and
|
|
11
|
+
# key_value items. ActiveModel attributes and validations; `as_json` emits
|
|
12
|
+
# camelCase keys with snake_case token values.
|
|
13
|
+
#
|
|
14
|
+
# Validation errors are read through `errors` types only (symbols such as
|
|
15
|
+
# :inclusion). Never interpolate a value into a message: values can be
|
|
16
|
+
# sensitive (`code`) or PII. `inspect` and `to_s` therefore show the class
|
|
17
|
+
# name only.
|
|
18
|
+
class Base
|
|
19
|
+
include ActiveModel::Model
|
|
20
|
+
include ActiveModel::Attributes
|
|
21
|
+
|
|
22
|
+
class_attribute :wire_fields, instance_writer: false, default: []
|
|
23
|
+
|
|
24
|
+
class << self
|
|
25
|
+
# Declares an attribute that appears on the wire (nil values are omitted).
|
|
26
|
+
def field(name, type = nil, **)
|
|
27
|
+
attribute(name, type, **)
|
|
28
|
+
self.wire_fields += [name]
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
validate :wire_strings_within_limit
|
|
33
|
+
|
|
34
|
+
def node?
|
|
35
|
+
false
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def wire_type
|
|
39
|
+
nil
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# Yields [relative_path, object] for each nested wire object, in document order.
|
|
43
|
+
def each_child; end
|
|
44
|
+
|
|
45
|
+
def as_json(_options = nil)
|
|
46
|
+
json = {}
|
|
47
|
+
json["type"] = wire_type if wire_type
|
|
48
|
+
self.class.wire_fields.each do |name|
|
|
49
|
+
value = public_send(name)
|
|
50
|
+
json[name.to_s.camelize(:lower)] = serialize(value) unless value.nil?
|
|
51
|
+
end
|
|
52
|
+
json
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def inspect
|
|
56
|
+
"#<#{self.class.name}>"
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def to_s
|
|
60
|
+
inspect
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def pretty_print(printer)
|
|
64
|
+
printer.text(inspect)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
private
|
|
68
|
+
|
|
69
|
+
def serialize(value)
|
|
70
|
+
case value
|
|
71
|
+
when Array then value.map { |item| serialize(item) }
|
|
72
|
+
else value.as_json
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def wire_strings_within_limit
|
|
77
|
+
self.class.wire_fields.each do |name|
|
|
78
|
+
value = public_send(name)
|
|
79
|
+
next unless value.is_a?(String) && value.length > Limits::MAX_STRING_LENGTH
|
|
80
|
+
|
|
81
|
+
errors.add(name, :too_long, count: Limits::MAX_STRING_LENGTH)
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def nested_objects(name, list)
|
|
86
|
+
return unless list.is_a?(Array)
|
|
87
|
+
|
|
88
|
+
list.each_with_index do |item, index|
|
|
89
|
+
yield "#{name}[#{index}]", item if item.is_a?(Base)
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# A catalog primitive (counts toward the document node and depth limits).
|
|
95
|
+
class Node < Base
|
|
96
|
+
def node?
|
|
97
|
+
true
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def wire_type
|
|
101
|
+
self.class::TYPE
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
# Node with `children` (stack, card, facts).
|
|
106
|
+
class Container < Node
|
|
107
|
+
field :children, default: -> { [] }
|
|
108
|
+
|
|
109
|
+
validate :children_are_nodes
|
|
110
|
+
|
|
111
|
+
def each_child(&)
|
|
112
|
+
nested_objects("children", children, &)
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
private
|
|
116
|
+
|
|
117
|
+
def children_are_nodes
|
|
118
|
+
return if children.is_a?(Array) && children.all?(Node)
|
|
119
|
+
|
|
120
|
+
errors.add(:children, :invalid_child)
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module UI
|
|
5
|
+
module Nodes
|
|
6
|
+
# A document holds at most one `primary` button (enforced by Document).
|
|
7
|
+
class Button < Node
|
|
8
|
+
TYPE = "button"
|
|
9
|
+
|
|
10
|
+
field :label, :string
|
|
11
|
+
field :variant, :string, default: "secondary"
|
|
12
|
+
field :action
|
|
13
|
+
|
|
14
|
+
validates :label, presence: true
|
|
15
|
+
validates :variant, inclusion: { in: Tokens::BUTTON_VARIANT }
|
|
16
|
+
validate :action_is_navigate
|
|
17
|
+
|
|
18
|
+
def primary?
|
|
19
|
+
variant == "primary"
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def each_child
|
|
23
|
+
yield "action", action if action.is_a?(Base)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
private
|
|
27
|
+
|
|
28
|
+
def action_is_navigate
|
|
29
|
+
errors.add(:action, :invalid_action) unless action.is_a?(Action::Navigate)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module UI
|
|
5
|
+
module Nodes
|
|
6
|
+
class Card < Container
|
|
7
|
+
TYPE = "card"
|
|
8
|
+
|
|
9
|
+
field :surface, :string, default: "raised"
|
|
10
|
+
field :tone, :string
|
|
11
|
+
field :padding, :string, default: "md"
|
|
12
|
+
|
|
13
|
+
validates :surface, inclusion: { in: Tokens::SURFACE }
|
|
14
|
+
validates :tone, inclusion: { in: Tokens::TONE }, allow_nil: true
|
|
15
|
+
validates :padding, inclusion: { in: Tokens::GAP }
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module UI
|
|
5
|
+
module Nodes
|
|
6
|
+
# Sensitive node (a check-in code). `value` is excluded from derived text,
|
|
7
|
+
# legacy mapping and email, and never appears in `inspect`, `to_s`, `pp`,
|
|
8
|
+
# validation errors or logs. The wire form (`as_json`) is the one place it
|
|
9
|
+
# is emitted, because the client renders it. Validation adds error types
|
|
10
|
+
# only, never a `value:` option.
|
|
11
|
+
class Code < Node
|
|
12
|
+
TYPE = "code"
|
|
13
|
+
FORMAT = %w[qr text].freeze
|
|
14
|
+
|
|
15
|
+
field :value, :string
|
|
16
|
+
field :format, :string
|
|
17
|
+
field :label, :string
|
|
18
|
+
|
|
19
|
+
validates :value, :label, presence: true
|
|
20
|
+
validates :format, inclusion: { in: FORMAT }
|
|
21
|
+
|
|
22
|
+
def inspect
|
|
23
|
+
"#<#{self.class.name} value=[REDACTED]>"
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module UI
|
|
5
|
+
module Nodes
|
|
6
|
+
# Registered native component with a required `fallback` tree. A document
|
|
7
|
+
# holds at most one `component` (enforced by Document).
|
|
8
|
+
class Component < Node
|
|
9
|
+
TYPE = "component"
|
|
10
|
+
KEY_FORMAT = /\A[a-z0-9_]+(?:\.[a-z0-9_]+)*\z/
|
|
11
|
+
VARIANT_FORMAT = /\A[a-z0-9_]+\z/
|
|
12
|
+
|
|
13
|
+
field :key, :string
|
|
14
|
+
field :variant, :string
|
|
15
|
+
field :data, default: -> { {} }
|
|
16
|
+
field :fallback
|
|
17
|
+
|
|
18
|
+
validates :key, format: { with: KEY_FORMAT }
|
|
19
|
+
validates :variant, format: { with: VARIANT_FORMAT }, allow_nil: true
|
|
20
|
+
validate :data_is_hash
|
|
21
|
+
validate :fallback_is_node
|
|
22
|
+
|
|
23
|
+
def each_child
|
|
24
|
+
yield "fallback", fallback if fallback.is_a?(Node)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
private
|
|
28
|
+
|
|
29
|
+
def data_is_hash
|
|
30
|
+
errors.add(:data, :invalid) unless data.is_a?(Hash)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def fallback_is_node
|
|
34
|
+
errors.add(:fallback, :invalid_child) unless fallback.is_a?(Node)
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module UI
|
|
5
|
+
module Nodes
|
|
6
|
+
# Structural wrapper for the shared fact block so renderers and checks can
|
|
7
|
+
# find it. Renders as a plain stack. Capped by Limits::MAX_FACTS_NODES
|
|
8
|
+
# descendants (enforced by Document).
|
|
9
|
+
class Facts < Container
|
|
10
|
+
TYPE = "facts"
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module UI
|
|
5
|
+
module Nodes
|
|
6
|
+
# `src` must be https on a host listed in config.ui.image_hosts (exact
|
|
7
|
+
# match, fail closed). `alt` is required unless `decorative` is true
|
|
8
|
+
# (council A12). `aspect` is width / height and is required (no server
|
|
9
|
+
# measurement).
|
|
10
|
+
class Image < Node
|
|
11
|
+
TYPE = "image"
|
|
12
|
+
FIT = %w[cover contain].freeze
|
|
13
|
+
|
|
14
|
+
field :src, :string
|
|
15
|
+
field :alt, :string
|
|
16
|
+
field :decorative, :boolean, default: false
|
|
17
|
+
field :aspect, :float
|
|
18
|
+
field :animated, :boolean, default: false
|
|
19
|
+
field :fit, :string, default: "cover"
|
|
20
|
+
|
|
21
|
+
validate :src_is_allowed_image
|
|
22
|
+
validate :alt_present_unless_decorative
|
|
23
|
+
validates :aspect, numericality: { greater_than_or_equal_to: 0.1, less_than_or_equal_to: 10 }
|
|
24
|
+
validates :fit, inclusion: { in: FIT }
|
|
25
|
+
|
|
26
|
+
private
|
|
27
|
+
|
|
28
|
+
def src_is_allowed_image
|
|
29
|
+
return errors.add(:src, :blank) if src.blank?
|
|
30
|
+
|
|
31
|
+
code = Hardening.image_src_code(src, hosts: CommandTower.config.ui.image_hosts)
|
|
32
|
+
errors.add(:src, code) if code
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def alt_present_unless_decorative
|
|
36
|
+
errors.add(:alt, :blank) if alt.blank? && !decorative
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "time"
|
|
4
|
+
require "tzinfo"
|
|
5
|
+
|
|
6
|
+
module CommandTower
|
|
7
|
+
module UI
|
|
8
|
+
module Nodes
|
|
9
|
+
# ActiveModel type for `at`: Time-like values become UTC ISO-8601 strings.
|
|
10
|
+
class InstantAtType < ActiveModel::Type::Value
|
|
11
|
+
private
|
|
12
|
+
|
|
13
|
+
def cast_value(value)
|
|
14
|
+
value.respond_to?(:utc) && value.respond_to?(:iso8601) ? value.utc.iso8601 : value
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Wire type `time`. Named Instant so it never shadows ::Time inside Nodes.
|
|
19
|
+
class Instant < Node
|
|
20
|
+
TYPE = "time"
|
|
21
|
+
FORMAT = %w[short long date time countdown].freeze
|
|
22
|
+
LABEL = %w[auto always never].freeze
|
|
23
|
+
UTC_ISO8601 = /\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?Z\z/
|
|
24
|
+
|
|
25
|
+
field :at, InstantAtType.new
|
|
26
|
+
field :zone, :string
|
|
27
|
+
field :format, :string, default: "short"
|
|
28
|
+
field :label, :string, default: "auto"
|
|
29
|
+
field :variant, :string, default: "body"
|
|
30
|
+
field :text_tone, :string, default: "primary"
|
|
31
|
+
|
|
32
|
+
validate :at_is_utc_iso8601
|
|
33
|
+
validate :zone_is_known
|
|
34
|
+
validates :format, inclusion: { in: FORMAT }
|
|
35
|
+
validates :label, inclusion: { in: LABEL }
|
|
36
|
+
validates :variant, inclusion: { in: Tokens::VARIANT }
|
|
37
|
+
validates :text_tone, inclusion: { in: Tokens::TEXT_TONE }
|
|
38
|
+
|
|
39
|
+
private
|
|
40
|
+
|
|
41
|
+
def at_is_utc_iso8601
|
|
42
|
+
return errors.add(:at, :invalid) unless at.is_a?(String) && UTC_ISO8601.match?(at)
|
|
43
|
+
|
|
44
|
+
Time.iso8601(at)
|
|
45
|
+
rescue ArgumentError
|
|
46
|
+
errors.add(:at, :invalid)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def zone_is_known
|
|
50
|
+
return errors.add(:zone, :blank) if zone.blank?
|
|
51
|
+
|
|
52
|
+
TZInfo::Timezone.get(zone)
|
|
53
|
+
rescue TZInfo::InvalidTimezoneIdentifier
|
|
54
|
+
errors.add(:zone, :invalid)
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module UI
|
|
5
|
+
module Nodes
|
|
6
|
+
# ActiveModel type for `items`: hashes become KeyValue::Item.
|
|
7
|
+
class KeyValueItemListType < ActiveModel::Type::Value
|
|
8
|
+
private
|
|
9
|
+
|
|
10
|
+
def cast_value(value)
|
|
11
|
+
return value unless value.is_a?(Array)
|
|
12
|
+
|
|
13
|
+
value.map { |item| item.is_a?(Hash) ? KeyValue::Item.new(item) : item }
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# Label/value rows. Needs at least one item (UX decision G7).
|
|
18
|
+
class KeyValue < Node
|
|
19
|
+
TYPE = "key_value"
|
|
20
|
+
|
|
21
|
+
# One row. `value` is a String or an Instant node.
|
|
22
|
+
class Item < Base
|
|
23
|
+
field :icon, :string
|
|
24
|
+
field :label, :string
|
|
25
|
+
field :value
|
|
26
|
+
|
|
27
|
+
validates :icon, format: { with: ICON_FORMAT }, allow_nil: true
|
|
28
|
+
validate :value_is_text_or_instant
|
|
29
|
+
|
|
30
|
+
def each_child
|
|
31
|
+
yield "value", value if value.is_a?(Instant)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
private
|
|
35
|
+
|
|
36
|
+
def value_is_text_or_instant
|
|
37
|
+
return errors.add(:value, :blank) if value.nil? || value == ""
|
|
38
|
+
return if value.is_a?(String) || value.is_a?(Instant)
|
|
39
|
+
|
|
40
|
+
errors.add(:value, :invalid)
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
field :items, KeyValueItemListType.new, default: -> { [] }
|
|
45
|
+
|
|
46
|
+
validate :items_are_valid_list
|
|
47
|
+
|
|
48
|
+
def each_child(&)
|
|
49
|
+
nested_objects("items", items, &)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
private
|
|
53
|
+
|
|
54
|
+
def items_are_valid_list
|
|
55
|
+
return errors.add(:items, :invalid_child) unless items.is_a?(Array) && items.all?(Item)
|
|
56
|
+
|
|
57
|
+
errors.add(:items, :blank) if items.empty?
|
|
58
|
+
errors.add(:items, :too_many_items) if items.length > Limits::MAX_KEY_VALUE_ITEMS
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module UI
|
|
5
|
+
module Nodes
|
|
6
|
+
# Inline run of text inside `text` and `banner`. `text` spans may be
|
|
7
|
+
# strong; `link` spans carry a navigate action. No HTML or markdown.
|
|
8
|
+
class Span < Base
|
|
9
|
+
TYPES = %w[text link].freeze
|
|
10
|
+
|
|
11
|
+
attribute :type, :string, default: "text"
|
|
12
|
+
field :text, :string
|
|
13
|
+
field :strong, :boolean
|
|
14
|
+
field :action
|
|
15
|
+
|
|
16
|
+
validates :type, inclusion: { in: TYPES }
|
|
17
|
+
validates :text, presence: true
|
|
18
|
+
validate :action_matches_type
|
|
19
|
+
|
|
20
|
+
def self.link(text, action:)
|
|
21
|
+
new(type: "link", text:, action:)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def wire_type
|
|
25
|
+
type
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def each_child
|
|
29
|
+
yield "action", action if action.is_a?(Base)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
private
|
|
33
|
+
|
|
34
|
+
def action_matches_type
|
|
35
|
+
if type == "link"
|
|
36
|
+
errors.add(:action, :invalid_action) unless action.is_a?(Action::Navigate)
|
|
37
|
+
elsif !action.nil?
|
|
38
|
+
errors.add(:action, :not_allowed)
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# ActiveModel type for `spans`: plain strings become text spans.
|
|
44
|
+
class SpanListType < ActiveModel::Type::Value
|
|
45
|
+
private
|
|
46
|
+
|
|
47
|
+
def cast_value(value)
|
|
48
|
+
return value unless value.is_a?(Array)
|
|
49
|
+
|
|
50
|
+
value.map { |item| item.is_a?(String) ? Span.new(text: item) : item }
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Shared by `text` and `banner`.
|
|
55
|
+
module Spanned
|
|
56
|
+
def self.included(base)
|
|
57
|
+
base.field :spans, SpanListType.new, default: -> { [] }
|
|
58
|
+
base.validate :spans_are_valid_list
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def each_child(&)
|
|
62
|
+
nested_objects("spans", spans, &)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
private
|
|
66
|
+
|
|
67
|
+
def spans_are_valid_list
|
|
68
|
+
return errors.add(:spans, :invalid_child) unless spans.is_a?(Array) && spans.all?(Span)
|
|
69
|
+
|
|
70
|
+
errors.add(:spans, :blank) if spans.empty?
|
|
71
|
+
errors.add(:spans, :too_many_spans) if spans.length > Limits::MAX_SPANS
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module UI
|
|
5
|
+
module Nodes
|
|
6
|
+
# Vertical stack (the catalog has no row or direction, UX decision G1).
|
|
7
|
+
#
|
|
8
|
+
# `align` (UX decision G3):
|
|
9
|
+
# absent = stretch children across the cross axis
|
|
10
|
+
# start / center / end = cross-axis alignment
|
|
11
|
+
# between = main-axis space-between
|
|
12
|
+
# `badge` and `avatar` children always hug their content.
|
|
13
|
+
class Stack < Container
|
|
14
|
+
TYPE = "stack"
|
|
15
|
+
|
|
16
|
+
field :gap, :string, default: "md"
|
|
17
|
+
field :align, :string
|
|
18
|
+
|
|
19
|
+
validates :gap, inclusion: { in: Tokens::GAP }
|
|
20
|
+
validates :align, inclusion: { in: Tokens::ALIGN }, allow_nil: true
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|