Module: GlimmerHelper

Defined in:
lib/glimmer/helpers/glimmer_helper.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.next_id_numberObject



3
4
5
6
# File 'lib/glimmer/helpers/glimmer_helper.rb', line 3

def next_id_number
  @next_id_number ||= 0
  @next_id_number += 1
end

Instance Method Details

#glimmer_component(component_asset_path, *component_args) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/glimmer/helpers/glimmer_helper.rb', line 9

def glimmer_component(component_asset_path, *component_args)
  component_file = component_asset_path.split('/').last # TODO support namespaced components
  component_class_name = component_file.classify # TODO support namespaced components
  next_id_number = GlimmerHelper.next_id_number
  component_id = "glimmer_component_#{next_id_number}"
  component_script_container_id = "glimmer_component_script_container_#{next_id_number}"
  component_args_json = JSON.dump(component_args)
  opal_script = <<~OPAL
    require 'glimmer-dsl-web'
    component_args_json = '#{component_args_json}'
    component_args = JSON.parse(component_args_json)
    component_args << {} if !component_args.last.is_a?(Hash)
    component_args.last[:parent] = "##{component_id}"
    #{component_class_name}.render(*component_args)
  OPAL
  js_script = <<~JAVASCRIPT
    Opal.eval(`#{opal_script}`)
  JAVASCRIPT
  (:div, id: component_script_container_id, class: ['glimmer_component_script_container', "#{component_file}_script_container"], 'data-turbo': 'false') do
    (:div, '', id: component_id, class: ['glimmer_component', component_file]) +
    javascript_include_tag(component_asset_path, "data-turbolinks-track": "reload") +
    (:script, raw(js_script), type: 'application/javascript', "data-turbo-eval": "false")
  end
end