Class: Fron::Component

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

Overview

Component

Constant Summary

Constants inherited from DOM::Element

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

Class Attribute Summary collapse

Instance 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::Events

included, on

Methods included from Behaviors::Components

#component, component, included

Methods inherited from DOM::Element

#[], #[]=, #attribute?, #checked, #checked=, #disabled, #disabled=, #files, #find, #findAll, #focus, #hide, #html, #html=, #id, #matches, #next, #removeAttribute, #show, #tag, #value, #value=, #visible?

Methods included from DOM::Dimensions

#bottom, #clientRect, #cover?, #height, #left, #right, #scrollLeft, #scrollTop, #top, #width

Methods included from DOM::ClassList

#addClass, #hasClass, #removeClass, #toggleClass

Methods inherited from DOM::NODE

#<<, #<=>, #==, #>>, #children, #dup, #dup!, #empty, #empty?, fromNode, getElement, #index, #insertBefore, #normalize!, #parent, #parentNode, #remove, #remove!, #text, #text=

Methods included from DOM::Events

#addListener, #delegate, #off, #on, #on!, #removeListeners, #trigger

Constructor Details

#initialize(tag = nil) ⇒ Component

Initalizs the component


60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'opal/fron/core/component.rb', line 60

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

  super tag || klass.tagname || klass.name.split('::').last

  klass.behaviors.each do |mod, methods|
    methods.each do |name|
      next unless mod.respond_to?(name)
      registry = self.class.instance_variable_get("@#{name}")
      instance_exec registry, &mod.method(name)
    end
  end
end

Class Attribute Details

.behaviorsObject (readonly)

Returns the value of attribute behaviors


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

def behaviors
  @behaviors
end

.tagnameObject (readonly)

Returns the value of attribute tagname


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

def tagname
  @tagname
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model


8
9
10
# File 'opal/fron/core/component.rb', line 8

def model
  @model
end

Class Method Details

.inherited(subclass) ⇒ Object

Handles inheritance


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

def inherited(subclass)
  # Copy behaviours
  subclass.instance_variable_set '@behaviors', @behaviors.dup

  # Copy registries
  @behaviors.values.reduce(&:+).each do |type|
    next unless (var = instance_variable_get("@#{type}"))
    instVar = subclass.instance_variable_get("@#{type}") || []
    subclass.instance_variable_set("@#{type}", instVar.concat(var))
  end
end

.register(behavior, methods) ⇒ Object

Register a behavior


18
19
20
21
22
23
24
25
26
27
28
29
# File 'opal/fron/core/component.rb', line 18

def register(behavior, methods)
  @behaviors ||= {}
  @behaviors[behavior] = methods

  methods.each do |name|
    instance_variable_set "@#{name}", []
    metaDef name do |*args, &block|
      args << block if block_given?
      instance_variable_get("@#{name}") << args
    end
  end
end

.tag(tag) ⇒ Object

Sets the tag name of the component


49
50
51
# File 'opal/fron/core/component.rb', line 49

def tag(tag)
  @tagname = tag
end