Class: Fron::Component

Inherits:
DOM::Element show all
Includes:
Behaviors::Components, Behaviors::Events, Behaviors::Style
Defined in:
opal/fron/core/component.rb

Overview

Base class for components.

Constant Summary collapse

TAGNAME_REGEXP =
/^\w([\w\d-]+)*$/

Constants inherited from DOM::Element

DOM::Element::ATTRIBUTE_REGEXP, DOM::Element::MODIFIER_REGEXP, DOM::Element::TAG_REGEXP

Class Attribute Summary collapse

Attributes inherited from DOM::Element

#style

Attributes included from DOM::Events

#listeners

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Behaviors::Style

included

Methods included from Behaviors::Events

included, on

Methods included from Behaviors::Components

#component, component, included

Methods inherited from DOM::Element

#active?, #apply_attributes, #apply_modifiers, #click, #files, #find, #find_all, #hide, #id, #include?, #matches, #next, #path, #previous, #scroll_into_view, #scroll_into_view_if_needed, #show, #tag, #z_index

Methods included from DOM::ElementAccessor

#attribute_accessor, #element_accessor, #element_method

Methods included from DOM::Dimensions

#bottom, #client_rect, #cover?, #height, #left, #right, #top, #visible?, #width

Methods included from DOM::ClassList

#add_class, #has_class, #remove_class, #toggle_class

Methods included from DOM::Attributes

#[], #[]=, #attribute?, #remove_attribute

Methods inherited from DOM::NODE

#<<, #<=>, #==, #>>, #children, #dup, #dup!, #empty, #empty?, from_node, get_element, #index, #insert_before, #normalize!, #parent, #parent_node, #remove, #remove!, #text, #text=

Methods included from DOM::Events

#add_listener, #delegate, #off, #old_trigger, #on, #on!, #remove_listeners, #trigger

Constructor Details

#initialize(tagname = nil) ⇒ Component

Initalizs the component

Parameters:

  • tag (String)

    The tagname



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'opal/fron/core/component.rb', line 85

def initialize(tagname = nil)
  klass = self.class

  tag = tagname || klass.tagname

  raise "Invalid tag '#{tag}' for #{self}!" unless tag =~ TAGNAME_REGEXP

  super tag

  klass.registry.each do |item|
    instance_exec item, &item[:method].unbind.bind(self)
  end

  klass.defaults.to_h.each do |key, value|
    if respond_to?("#{key}=")
      send "#{key}=", value
    else
      self[key] = value
    end
  end
end

Class Attribute Details

.defaults(data = nil) ⇒ Object (readonly)

Returns the value of attribute defaults.



20
21
22
# File 'opal/fron/core/component.rb', line 20

def defaults
  @defaults
end

.registryArray (readonly)

Returns The registry of behaviors.

Returns:

  • (Array)

    The registry of behaviors



15
16
17
# File 'opal/fron/core/component.rb', line 15

def registry
  @registry
end

.stylesArray (readonly)

Returns The styles for this component.

Returns:

  • (Array)

    The styles for this component



18
19
20
# File 'opal/fron/core/component.rb', line 18

def styles
  @styles
end

.tagnameString (readonly)

Returns The tagname of the component.

Returns:

  • (String)

    The tagname of the component



12
13
14
# File 'opal/fron/core/component.rb', line 12

def tagname
  @tagname
end

Class Method Details

.create(tag) ⇒ Fron::Component

Creates a new class with the specific tag

Parameters:

Returns:



27
28
29
30
31
# File 'opal/fron/core/component.rb', line 27

def create(tag)
  klass = Class.new self
  klass.tag tag
  klass
end

.inherited(subclass) ⇒ Object

Handles inheritance

Parameters:

  • subclass (Class)

    The subclass



57
58
59
60
61
62
# File 'opal/fron/core/component.rb', line 57

def inherited(subclass)
  # Copy behaviours
  subclass.instance_variable_set '@registry', @registry.dup
  subclass.instance_variable_set '@styles', @styles.dup
  subclass.instance_variable_set '@defaults', (@defaults || {}).dup
end

.register(behavior, methods) ⇒ Object

Register a behavior

Parameters:

  • behavior (Module)

    The behavior

  • methods (Array)

    The methods to register



37
38
39
40
41
42
43
44
45
46
# File 'opal/fron/core/component.rb', line 37

def register(behavior, methods)
  @registry ||= []
  @styles ||= []

  methods.each do |name|
    meta_def name do |*args, &block|
      @registry << { method: behavior.method(name), args: args, block: block, id: SecureRandom.uuid }
    end
  end
end

.tag(tag) ⇒ Object

Sets the tag name of the component

Parameters:

  • tag (String)

    The tag name



67
68
69
# File 'opal/fron/core/component.rb', line 67

def tag(tag)
  @tagname = tag
end