Module: Vident::RootComponent
- Defined in:
- lib/vident/root_component.rb
Instance Method Summary collapse
-
#action(*args) ⇒ Object
TODO: rename Create a Stimulus action string, and returns it examples: action(:my_thing) => “current_controller#myThing” action(:click, :my_thing) => “click->current_controller#myThing” action(“click->current_controller#myThing”) => “click->current_controller#myThing” action(“path/to/current”, :my_thing) => “path–to–current_controller#myThing” action(:click, “path/to/current”, :my_thing) => “click->path–to–current_controller#myThing”.
- #action_attribute(*actions_to_set) ⇒ Object
- #action_data_attribute(*actions) ⇒ Object
-
#as_targets(*targets) ⇒ Object
(also: #as_target)
Return the HTML ‘data-target` attribute for the given targets.
- #build_outlet_selector(outlet_selector) ⇒ Object
- #connect_outlet(outlet) ⇒ Object
-
#controller_attribute(*controllers_to_set) ⇒ Object
The view component’s helpers for setting stimulus data-* attributes on this component.
- #initialize(controllers: nil, actions: nil, targets: nil, outlets: nil, outlet_host: nil, named_classes: nil, values: nil, element_tag: nil, id: nil, html_options: nil) ⇒ Object
-
#named_classes(*names) ⇒ Object
Getter for a named classes list so can be used in view to set initial state on SSR Returns a String of classes that can be used in a ‘class` attribute.
- #outlet(css_selector: nil) ⇒ Object
-
#target(name, part2 = nil) ⇒ Object
TODO: rename & make stimulus Target class instance and returns it, which can convert to String Create a Stimulus Target and returns it examples: target(:my_target) => ‘current_controller’ name: ‘myTarget’ target(“path/to/current”, :my_target) => ‘path–to–current_controller’, name: ‘myTarget’.
- #target_attributes(*targets) ⇒ Object
- #target_data_attribute(name) ⇒ Object
-
#with_actions(*actions_to_set) ⇒ Object
(also: #with_action)
Return the HTML ‘data-action` attribute for the given actions.
-
#with_controllers(*controllers_to_set) ⇒ Object
Return the HTML ‘data-controller` attribute for the given controllers.
-
#with_outlets(*outlets) ⇒ Object
(also: #with_outlet)
Return the HTML ‘data-` attribute for the given outlets.
Instance Method Details
#action(*args) ⇒ Object
TODO: rename Create a Stimulus action string, and returns it
examples:
action(:my_thing) => "current_controller#myThing"
action(:click, :my_thing) => "click->current_controller#myThing"
action("click->current_controller#myThing") => "click->current_controller#myThing"
action("path/to/current", :my_thing) => "path--to--current_controller#myThing"
action(:click, "path/to/current", :my_thing) => "click->path--to--current_controller#myThing"
58 59 60 61 |
# File 'lib/vident/root_component.rb', line 58 def action(*args) part1, part2, part3 = args (args.size == 1) ? parse_action_arg(part1) : parse_multiple_action_args(part1, part2, part3) end |
#action_attribute(*actions_to_set) ⇒ Object
46 47 48 |
# File 'lib/vident/root_component.rb', line 46 def action_attribute(*actions_to_set) {"data-action" => parse_actions(actions_to_set).join(" ").html_safe} end |
#action_data_attribute(*actions) ⇒ Object
63 64 65 |
# File 'lib/vident/root_component.rb', line 63 def action_data_attribute(*actions) {action: parse_actions(actions).join(" ")} end |
#as_targets(*targets) ⇒ Object Also known as: as_target
Return the HTML ‘data-target` attribute for the given targets
112 113 114 115 |
# File 'lib/vident/root_component.rb', line 112 def as_targets(*targets) attrs = build_target_data_attributes(parse_targets(targets)) attrs.map { |dt, n| "data-#{dt}=\"#{n}\"" }.join(" ").html_safe end |
#build_outlet_selector(outlet_selector) ⇒ Object
84 85 86 87 |
# File 'lib/vident/root_component.rb', line 84 def build_outlet_selector(outlet_selector) prefix = @id ? "##{@id} " : "" "#{prefix}[data-controller~=#{outlet_selector}]" end |
#connect_outlet(outlet) ⇒ Object
30 31 32 33 |
# File 'lib/vident/root_component.rb', line 30 def connect_outlet(outlet) @outlets ||= [] @outlets << outlet end |
#controller_attribute(*controllers_to_set) ⇒ Object
The view component’s helpers for setting stimulus data-* attributes on this component.
37 38 39 |
# File 'lib/vident/root_component.rb', line 37 def controller_attribute(*controllers_to_set) {"data-controller" => controller_list(controllers_to_set)&.html_safe} end |
#initialize(controllers: nil, actions: nil, targets: nil, outlets: nil, outlet_host: nil, named_classes: nil, values: nil, element_tag: nil, id: nil, html_options: nil) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/vident/root_component.rb', line 5 def initialize( controllers: nil, actions: nil, targets: nil, outlets: nil, outlet_host: nil, named_classes: nil, # https://stimulus.hotwired.dev/reference/css-classes values: nil, element_tag: nil, id: nil, html_options: nil ) @element_tag = element_tag @html_options = @id = id @controllers = Array.wrap(controllers) @actions = actions @targets = targets @outlets = outlets @named_classes = named_classes @values = values outlet_host.connect_outlet(self) if outlet_host.respond_to?(:connect_outlet) end |
#named_classes(*names) ⇒ Object
Getter for a named classes list so can be used in view to set initial state on SSR Returns a String of classes that can be used in a ‘class` attribute.
100 101 102 |
# File 'lib/vident/root_component.rb', line 100 def named_classes(*names) names.map { |name| convert_classes_list_to_string(@named_classes[name]) }.join(" ") end |
#outlet(css_selector: nil) ⇒ Object
89 90 91 92 93 94 95 96 |
# File 'lib/vident/root_component.rb', line 89 def outlet(css_selector: nil) controller = implied_controller_name if css_selector.nil? [controller, build_outlet_selector(controller)] else [controller, css_selector] end end |
#target(name, part2 = nil) ⇒ Object
TODO: rename & make stimulus Target class instance and returns it, which can convert to String Create a Stimulus Target and returns it
examples:
target(:my_target) => {controller: 'current_controller' name: 'myTarget'}
target("path/to/current", :my_target) => {controller: 'path--to--current_controller', name: 'myTarget'}
72 73 74 75 76 77 78 |
# File 'lib/vident/root_component.rb', line 72 def target(name, part2 = nil) if part2.nil? {controller: implied_controller_name, name: js_name(name)} else {controller: stimulize_path(name), name: js_name(part2)} end end |
#target_attributes(*targets) ⇒ Object
41 42 43 44 |
# File 'lib/vident/root_component.rb', line 41 def target_attributes(*targets) attrs = build_target_data_attributes(parse_targets(targets)) attrs.transform_keys { |dt| "data-#{dt}" } end |
#target_data_attribute(name) ⇒ Object
80 81 82 |
# File 'lib/vident/root_component.rb', line 80 def target_data_attribute(name) build_target_data_attributes([target(name)]) end |
#with_actions(*actions_to_set) ⇒ Object Also known as: with_action
Return the HTML ‘data-action` attribute for the given actions
119 120 121 |
# File 'lib/vident/root_component.rb', line 119 def with_actions(*actions_to_set) "data-action='#{parse_actions(actions_to_set).join(" ")}'".html_safe end |
#with_controllers(*controllers_to_set) ⇒ Object
Return the HTML ‘data-controller` attribute for the given controllers
107 108 109 |
# File 'lib/vident/root_component.rb', line 107 def with_controllers(*controllers_to_set) "data-controller=\"#{controller_list(controllers_to_set)}\"".html_safe end |
#with_outlets(*outlets) ⇒ Object Also known as: with_outlet
Return the HTML ‘data-` attribute for the given outlets
125 126 127 128 |
# File 'lib/vident/root_component.rb', line 125 def with_outlets(*outlets) attrs = build_outlet_data_attributes(outlets) attrs.map { |dt, n| "data-#{dt}=\"#{n}\"" }.join(" ").html_safe end |