Module: Ferro::Elementary

Included in:
BaseElement, Document
Defined in:
opal/opal-ferro/ferro_elementary.js.rb

Overview

Module defines element creation methods and child management. Note that there are no private methods in Opal. Methods that should be private are marked in the docs with ‘Internal method’.

Constant Summary collapse

RESERVED_NAMES =

Array of reseved names, child element should not have a name that is included in this list

%i[
  initialize factory root router page404
  creation _before_create before_create create _after_create
  after_create style _stylize cascade
  add_child forget_children remove_child method_missing destroy
  value set_text html parent children element domtype options
  add_states add_state update_state toggle_state state_active
]

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object

Getter for children.



121
122
123
124
125
126
127
# File 'opal/opal-ferro/ferro_elementary.js.rb', line 121

def method_missing(method_name, *args, &block)
  if @children.has_key?(method_name)
    @children[method_name]
  else
    super
  end
end

Instance Method Details

#_after_createObject

Internal method.



42
# File 'opal/opal-ferro/ferro_elementary.js.rb', line 42

def _after_create;end

#_before_createObject

Internal method.



31
# File 'opal/opal-ferro/ferro_elementary.js.rb', line 31

def _before_create;end

#_stylizeObject

Internal method.



52
53
54
55
56
57
58
59
60
61
# File 'opal/opal-ferro/ferro_elementary.js.rb', line 52

def _stylize
  styles = style

  if styles.class == Hash
    set_attribute(
      'style',
      styles.map { |k, v| "#{k}:#{v};" }.join
    )
  end
end

#add_child(name, element_class, options = {}) ⇒ Object

Add a child element.

Parameters:

  • name (String)

    A unique name for the element that is not in RESERVED_NAMES

  • element_class (String)

    Ruby class name for the new element

  • options (Hash) (defaults to: {})

    Options to pass to the element. Any option key that is not recognized is set as an attribute on the DOM element. Recognized keys are:

    prepend Prepend the new element before this DOM element
    content Add the value of content as a textnode to the DOM element
    


76
77
78
79
80
81
# File 'opal/opal-ferro/ferro_elementary.js.rb', line 76

def add_child(name, element_class, options = {})
  sym = symbolize(name)
  raise "Child '#{sym}' already defined" if @children.has_key?(sym)
  raise "Illegal name (#{sym})" if RESERVED_NAMES.include?(sym)
  @children[sym] = element_class.new(self, sym, options)
end

#after_createObject

Internal method.



45
# File 'opal/opal-ferro/ferro_elementary.js.rb', line 45

def after_create;end

#before_createObject

Internal method.



34
# File 'opal/opal-ferro/ferro_elementary.js.rb', line 34

def before_create;end

#cascadeObject

Override this method to continue the MOM creation process.



64
# File 'opal/opal-ferro/ferro_elementary.js.rb', line 64

def cascade;end

#createObject

Calls the factory to create the DOM element.



37
38
39
# File 'opal/opal-ferro/ferro_elementary.js.rb', line 37

def create
  @element = factory.create_element(self, @domtype, @parent, @options) if @domtype 
end

#creationObject

Create DOM element and children elements. Calls before- and after create hooks.



20
21
22
23
24
25
26
27
28
# File 'opal/opal-ferro/ferro_elementary.js.rb', line 20

def creation
  _before_create
  before_create
  create
  _after_create
  after_create
  _stylize
  cascade
end

#destroyObject

Remove a DOM element.



115
116
117
118
# File 'opal/opal-ferro/ferro_elementary.js.rb', line 115

def destroy
  `#{parent.element}.removeChild(#{element})`
  parent.remove_child(@sym)
end

#each_child(&block) ⇒ Object

Recursively iterate all child elements

param [Block] block A block to execute for every child element

and the element itself


104
105
106
107
108
109
110
111
112
# File 'opal/opal-ferro/ferro_elementary.js.rb', line 104

def each_child(&block)
  if block_given?
    block.call self

    @children.each do |_, child|
      child.each_child(&block)
    end
  end
end

#forget_childrenObject

Remove all child elements.



89
90
91
# File 'opal/opal-ferro/ferro_elementary.js.rb', line 89

def forget_children
  children = {}
end

#remove_child(sym) ⇒ Object

Remove a specific child element.

param [Symbol] sym The element to remove



96
97
98
# File 'opal/opal-ferro/ferro_elementary.js.rb', line 96

def remove_child(sym)
  @children.delete(sym)
end

#styleObject

Override this method to return a Hash of styles. Hash-key is the CSS style name, hash-value is the CSS style value.



49
# File 'opal/opal-ferro/ferro_elementary.js.rb', line 49

def style;end

#symbolize(name) ⇒ Object

Convert a string containing a variable name to a symbol.



84
85
86
# File 'opal/opal-ferro/ferro_elementary.js.rb', line 84

def symbolize(name)
  name.downcase.to_sym
end