class UiBibz::Ui::Core::Component

Creates a component of the given name using options created by the set of options.

Attributes

Options

You can add HTML attributes using the html_options. You can pass arguments in options attribute:

Signatures

UiBibz::Ui::Core::Component.new(content, options = nil, html_options = nil)

UiBibz::Ui::Core::Component.new(options = nil, html_options = nil) do
  content
end

Examples

UiBibz::Ui::Core::Component.new(content, { type: :success, glyph: 'eye' }, { class: 'test' })
# or
UiBibz::Ui::Core::Component.new({glyph: { name: 'eye', size: 3}, { class: 'test' }) do
  content
end

Constants

BREAKPOINTS
SIZES
STATUSES

Constants

Attributes

content[RW]
html_options[RW]
options[RW]

Public Class Methods

new(content = nil, options = nil, html_options = nil, &block) click to toggle source

Use link_to system in rails

  • Content can be send by content variable or by block if a block is sent, variable 'content' does not exit.

  • Options of component is defined in hash options

  • Html options is defined in hash html_options

# File lib/ui_bibz/ui/core/component.rb, line 61
def initialize(content = nil, options = nil, html_options = nil, &block)
  if !block.nil?
    @tapped = tapped?(block)
    @html_options = options
    @options = content
    read_cache = Rails.cache.read(@options.try(:[], :cache))
    if read_cache.nil?
      context  = eval('self', block.binding) # rubocop:disable Style/EvalWithLocation
      @content = context.capture(&block)
    else
      @content = read_cache
    end
  elsif content.is_a?(Hash)
    @html_options = options
    @options = content
  else
    @html_options = html_options
    @options = options
    @content = content
  end
  @html_options = (@html_options || {}).with_indifferent_access
  @options      = (@options || {}).with_indifferent_access
  init_options
  init_component_html_options
end

Public Instance Methods

pre_render() click to toggle source

Render without cache

# File lib/ui_bibz/ui/core/component.rb, line 93
def pre_render
  glyph_and_content_html
end
render() click to toggle source

Render html tag with or without cache

# File lib/ui_bibz/ui/core/component.rb, line 88
def render
  render_with_or_without_cache
end
tapped?(block) click to toggle source

Know if component is tapped or not

# File lib/ui_bibz/ui/core/component.rb, line 98
def tapped?(block)
  UiBibz::Utils::Screwdriver.tapped?(block)
end

Protected Instance Methods

add_html_data(name, value: true) click to toggle source

Add html data arguments

# File lib/ui_bibz/ui/core/component.rb, line 149
def add_html_data(name, value: true)
  html_options[:data] = {} if html_options[:data].nil?
  value = value.strip if value.is_a?(String)
  html_options[:data].update({ name => value })
end
component_html_classes() click to toggle source

Override this method to add html classes Accept Array or String

# File lib/ui_bibz/ui/core/component.rb, line 106
def component_html_classes
  []
end
component_html_data() click to toggle source

Override this method to add html data

# File lib/ui_bibz/ui/core/component.rb, line 111
def component_html_data
  # To stimulusjs
  data_target = html_options.try(:[], :data).try(:[], :target) || options.try(:delete, :target)
  add_html_data(:target, value: data_target) unless data_target.nil?

  data_controller = html_options.try(:[], :data).try(:[], :controller) || options.try(:delete, :controller)
  add_html_data(:controller, value: data_controller) unless data_controller.nil?

  data_action = html_options.try(:[], :data).try(:[], :action) || options.try(:delete, :action)
  add_html_data(:action, value: data_action) unless data_action.nil?

  # To turbolinks
  data_turbolinks = html_options.try(:[], :data).try(:[], :turbolinks) || options.try(:delete, :turbolinks)
  add_html_data(:turbolinks, value: data_turbolinks) unless data_turbolinks.nil?

  # To Turbo
  data_turbo = html_options.try(:[], :data).try(:[], :turbo) || options.try(:delete, :turbo)
  add_html_data(:turbo, value: data_turbo) unless data_turbo.nil?
end
component_html_options() click to toggle source

Override this method to add html Options Accept Hash

# File lib/ui_bibz/ui/core/component.rb, line 133
def component_html_options
  {}
end
component_options() click to toggle source

Override this method to add Options to the component Accept Hash

# File lib/ui_bibz/ui/core/component.rb, line 139
def component_options
  {}
end
component_wrapper_html_classes() click to toggle source

Override this method to add html classes to wrapper

# File lib/ui_bibz/ui/core/component.rb, line 144
def component_wrapper_html_classes
  []
end
disabled?() click to toggle source
# File lib/ui_bibz/ui/core/component.rb, line 155
def disabled?
  options[:state] == :disabled || html_options[:disabled]
end
sanitize_text(text) click to toggle source
# File lib/ui_bibz/ui/core/component.rb, line 159
def sanitize_text(text)
  sanitize(text, tags: [], attributes: [])
end

Private Instance Methods

init_component_html_options() click to toggle source
# File lib/ui_bibz/ui/core/component.rb, line 194
def init_component_html_options
  initialize_component_html_data
  initialize_component_html_classes
  initialize_component_html_options
end
init_options() click to toggle source
# File lib/ui_bibz/ui/core/component.rb, line 180
def init_options
  @options = component_options.merge(@options).with_indifferent_access
end
initialize_component_html_data() click to toggle source
# File lib/ui_bibz/ui/core/component.rb, line 184
def initialize_component_html_data
  component_html_data
  popover_data_html
  tooltip_data_html
end
initialize_component_html_options() click to toggle source
# File lib/ui_bibz/ui/core/component.rb, line 190
def initialize_component_html_options
  html_options.merge!(component_html_options)
end
render_with_or_without_cache() click to toggle source
# File lib/ui_bibz/ui/core/component.rb, line 165
def render_with_or_without_cache
  if options[:cache]
    cache      = Rails.cache
    read_cache = cache.read(options[:cache])
    if read_cache
      read_cache
    else
      cache.write(options[:cache], pre_render)
      pre_render
    end
  else
    pre_render
  end
end