Class: Fron::Component
- Inherits:
-
DOM::Element
- Object
- DOM::NODE
- DOM::Element
- Fron::Component
- 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
-
.defaults(data = nil) ⇒ Object
readonly
Returns the value of attribute defaults.
-
.registry ⇒ Array
readonly
The registry of behaviors.
-
.styles ⇒ Array
readonly
The styles for this component.
-
.tagname ⇒ String
readonly
The tagname of the component.
Attributes inherited from DOM::Element
Attributes included from DOM::Events
Class Method Summary collapse
-
.create(tag) ⇒ Fron::Component
Creates a new class with the specific tag.
-
.inherited(subclass) ⇒ Object
Handles inheritance.
-
.register(behavior, methods) ⇒ Object
Register a behavior.
-
.tag(tag) ⇒ Object
Sets the tag name of the component.
Instance Method Summary collapse
-
#initialize(tagname = nil) ⇒ Component
constructor
Initalizs the component.
Methods included from Behaviors::Style
Methods included from Behaviors::Events
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
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 |
.registry ⇒ Array (readonly)
Returns The registry of behaviors.
15 16 17 |
# File 'opal/fron/core/component.rb', line 15 def registry @registry end |
.styles ⇒ Array (readonly)
Returns The styles for this component.
18 19 20 |
# File 'opal/fron/core/component.rb', line 18 def styles @styles end |
.tagname ⇒ String (readonly)
Returns 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
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
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
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| 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
67 68 69 |
# File 'opal/fron/core/component.rb', line 67 def tag(tag) @tagname = tag end |