Class: Ruwi::Vdom
- Inherits:
-
Object
- Object
- Ruwi::Vdom
- Defined in:
- lib/ruwi/runtime/vdom.rb
Constant Summary collapse
- DOM_TYPES =
{ TEXT: 'text', ELEMENT: 'element', FRAGMENT: 'fragment', COMPONENT: 'component' }
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#component ⇒ Object
Returns the value of attribute component.
-
#el ⇒ Object
Returns the value of attribute el.
-
#listeners ⇒ Object
Returns the value of attribute listeners.
-
#props ⇒ Object
readonly
Returns the value of attribute props.
-
#tag ⇒ Object
readonly
Returns the value of attribute tag.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
- .extract_children(vdom) ⇒ Array
- .h(tag, props = {}, children = []) ⇒ Vdom
- .h_fragment(vdoms) ⇒ Vdom
- .h_string(str) ⇒ Vdom
- .is_text_node?(child) ⇒ Boolean
- .map_text_nodes(children) ⇒ Object
Instance Method Summary collapse
Constructor Details
#initialize(tag, props, type, children, value) ⇒ Vdom
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
#children ⇒ Object (readonly)
Returns the value of attribute children.
50 51 52 |
# File 'lib/ruwi/runtime/vdom.rb', line 50 def children @children end |
#component ⇒ Object
Returns the value of attribute component.
51 52 53 |
# File 'lib/ruwi/runtime/vdom.rb', line 51 def component @component end |
#el ⇒ Object
Returns the value of attribute el.
51 52 53 |
# File 'lib/ruwi/runtime/vdom.rb', line 51 def el @el end |
#listeners ⇒ Object
Returns the value of attribute listeners.
51 52 53 |
# File 'lib/ruwi/runtime/vdom.rb', line 51 def listeners @listeners end |
#props ⇒ Object (readonly)
Returns the value of attribute props.
50 51 52 |
# File 'lib/ruwi/runtime/vdom.rb', line 50 def props @props end |
#tag ⇒ Object (readonly)
Returns the value of attribute tag.
50 51 52 |
# File 'lib/ruwi/runtime/vdom.rb', line 50 def tag @tag end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
50 51 52 |
# File 'lib/ruwi/runtime/vdom.rb', line 50 def type @type end |
#value ⇒ Object (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
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
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
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
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
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
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 |