Class: Ruwi::Vdom

Inherits:
Object
  • Object
show all
Defined in:
lib/ruwi/runtime/vdom.rb

Constant Summary collapse

DOM_TYPES =
{
  TEXT: 'text',
  ELEMENT: 'element',
  FRAGMENT: 'fragment',
  COMPONENT: 'component'
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag, props, type, children, value) ⇒ Vdom

Parameters:

  • type (Symbol)
  • props (Hash)
  • children (Array)
  • value (Object)


39
40
41
42
43
44
45
46
47
48
# File 'lib/ruwi/runtime/vdom.rb', line 39

def initialize(tag, props, type, children, value)
  @tag = tag
  @props = props
  @children = self.class.map_text_nodes(Ruwi::Utils::Arrays.without_nulls(children))
  @type = type
  @value = value.to_s
  @el = nil
  @listeners = {}
  @component = nil
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



50
51
52
# File 'lib/ruwi/runtime/vdom.rb', line 50

def children
  @children
end

#componentObject

Returns the value of attribute component.



51
52
53
# File 'lib/ruwi/runtime/vdom.rb', line 51

def component
  @component
end

#elObject

Returns the value of attribute el.



51
52
53
# File 'lib/ruwi/runtime/vdom.rb', line 51

def el
  @el
end

#listenersObject

Returns the value of attribute listeners.



51
52
53
# File 'lib/ruwi/runtime/vdom.rb', line 51

def listeners
  @listeners
end

#propsObject (readonly)

Returns the value of attribute props.



50
51
52
# File 'lib/ruwi/runtime/vdom.rb', line 50

def props
  @props
end

#tagObject (readonly)

Returns the value of attribute tag.



50
51
52
# File 'lib/ruwi/runtime/vdom.rb', line 50

def tag
  @tag
end

#typeObject (readonly)

Returns the value of attribute type.



50
51
52
# File 'lib/ruwi/runtime/vdom.rb', line 50

def type
  @type
end

#valueObject (readonly)

Returns the value of attribute value.



50
51
52
# File 'lib/ruwi/runtime/vdom.rb', line 50

def value
  @value
end

Class Method Details

.extract_children(vdom) ⇒ Array

Parameters:

Returns:

  • (Array)


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ruwi/runtime/vdom.rb', line 68

def self.extract_children(vdom)
  return [] if vdom.children.nil?

  children = []

  vdom.children.each do |child|
    if child.type == DOM_TYPES[:FRAGMENT]
      children.concat(extract_children(child))
    else
      children << child
    end
  end

  children
end

.h(tag, props = {}, children = []) ⇒ Vdom

Parameters:

  • tag (String)
  • props (Hash) (defaults to: {})
  • children (Array) (defaults to: [])
  • type (Symbol)
  • value (Object)

Returns:



16
17
18
19
# File 'lib/ruwi/runtime/vdom.rb', line 16

def self.h(tag, props = {}, children = [])
  type = tag.is_a?(String) ? DOM_TYPES[:ELEMENT] : DOM_TYPES[:COMPONENT]
  new(tag, props, type, children, nil)
end

.h_fragment(vdoms) ⇒ Vdom

Parameters:

  • vdoms (Array)

Returns:



30
31
32
# File 'lib/ruwi/runtime/vdom.rb', line 30

def self.h_fragment(vdoms)
  new('', {}, DOM_TYPES[:FRAGMENT], map_text_nodes(Ruwi::Utils::Arrays.without_nulls(vdoms)), nil)
end

.h_string(str) ⇒ Vdom

Parameters:

  • str (String)

Returns:



23
24
25
26
# File 'lib/ruwi/runtime/vdom.rb', line 23

def self.h_string(str)
  vdom = new('', {}, DOM_TYPES[:TEXT], [], str.to_s)
  vdom
end

.is_text_node?(child) ⇒ Boolean

Parameters:

  • child (Object)

Returns:

  • (Boolean)


62
63
64
# File 'lib/ruwi/runtime/vdom.rb', line 62

def self.is_text_node?(child)
  child.is_a?(String) || child.is_a?(Integer) || child.is_a?(JS::Object)
end

.map_text_nodes(children) ⇒ Object

Parameters:

  • children (Array)


56
57
58
# File 'lib/ruwi/runtime/vdom.rb', line 56

def self.map_text_nodes(children)
  children.map { |child| is_text_node?(child) ? h_string(child) : child }
end