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_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
- #initialize(controllers: nil, actions: nil, targets: nil, outlets: nil, outlet_host: nil, named_classes: nil, data_maps: 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_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"
46 47 48 49 |
# File 'lib/vident/root_component.rb', line 46 def action(*args) part1, part2, part3 = args (args.size == 1) ? parse_action_arg(part1) : parse_multiple_action_args(part1, part2, part3) end |
#action_data_attribute(*actions) ⇒ Object
51 52 53 |
# File 'lib/vident/root_component.rb', line 51 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
100 101 102 103 |
# File 'lib/vident/root_component.rb', line 100 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
72 73 74 75 |
# File 'lib/vident/root_component.rb', line 72 def build_outlet_selector(outlet_selector) prefix = @id ? "##{@id} " : "" "#{prefix}[data-controller~=#{outlet_selector}]" end |
#connect_outlet(outlet) ⇒ Object
31 32 33 34 |
# File 'lib/vident/root_component.rb', line 31 def connect_outlet(outlet) @outlets ||= [] @outlets << outlet end |
#initialize(controllers: nil, actions: nil, targets: nil, outlets: nil, outlet_host: nil, named_classes: nil, data_maps: 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 29 |
# 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 data_maps: 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 @data_map_kvs = {} @data_maps = data_maps 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.
88 89 90 |
# File 'lib/vident/root_component.rb', line 88 def named_classes(*names) names.map { |name| convert_classes_list_to_string(@named_classes[name]) }.join(" ") end |
#outlet(css_selector: nil) ⇒ Object
77 78 79 80 81 82 83 84 |
# File 'lib/vident/root_component.rb', line 77 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'}
60 61 62 63 64 65 66 |
# File 'lib/vident/root_component.rb', line 60 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_data_attribute(name) ⇒ Object
68 69 70 |
# File 'lib/vident/root_component.rb', line 68 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
107 108 109 |
# File 'lib/vident/root_component.rb', line 107 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
95 96 97 |
# File 'lib/vident/root_component.rb', line 95 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
113 114 115 116 |
# File 'lib/vident/root_component.rb', line 113 def with_outlets(*outlets) attrs = build_outlet_data_attributes(outlets) attrs.map { |dt, n| "data-#{dt}=\"#{n}\"" }.join(" ").html_safe end |