Class: Volt::ComponentNode
Overview
Component nodes contain an array of both HtmlNodes and ComponentNodes. Instead of providing a full DOM API, component nodes are the branch nodes and html nodes are the leafs. This is all we need to produce the html from templates outside of a normal dom.
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Eventable
#on, #remove_listener, #trigger!
Constructor Details
#initialize(binding_id = nil, parent = nil, root = nil) ⇒ ComponentNode
Returns a new instance of ComponentNode.
14
15
16
17
18
19
|
# File 'lib/volt/page/targets/binding_document/component_node.rb', line 14
def initialize(binding_id = nil, parent = nil, root = nil)
@nodes = []
@binding_id = binding_id
@parent = parent
@root = root
end
|
Instance Attribute Details
#binding_id ⇒ Object
Returns the value of attribute binding_id.
12
13
14
|
# File 'lib/volt/page/targets/binding_document/component_node.rb', line 12
def binding_id
@binding_id
end
|
Returns the value of attribute nodes.
12
13
14
|
# File 'lib/volt/page/targets/binding_document/component_node.rb', line 12
def nodes
@nodes
end
|
Returns the value of attribute parent.
12
13
14
|
# File 'lib/volt/page/targets/binding_document/component_node.rb', line 12
def parent
@parent
end
|
Returns the value of attribute root.
12
13
14
|
# File 'lib/volt/page/targets/binding_document/component_node.rb', line 12
def root
@root
end
|
Instance Method Details
64
65
66
|
# File 'lib/volt/page/targets/binding_document/component_node.rb', line 64
def <<(node)
@nodes << node
end
|
21
22
23
24
25
26
27
|
# File 'lib/volt/page/targets/binding_document/component_node.rb', line 21
def changed!
if @root
@root.changed!
else
trigger!('changed')
end
end
|
#find_by_binding_id(binding_id) ⇒ Object
82
83
84
85
86
87
88
89
90
91
92
93
|
# File 'lib/volt/page/targets/binding_document/component_node.rb', line 82
def find_by_binding_id(binding_id)
return self if @binding_id == binding_id
@nodes.each do |node|
if node.is_a?(ComponentNode)
val = node.find_by_binding_id(binding_id)
return val if val
end
end
nil
end
|
#html=(html) ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/volt/page/targets/binding_document/component_node.rb', line 33
def html=(html)
parts = html.split(/(\<\!\-\- \$\/?[0-9]+ \-\-\>)/).reject { |v| v == '' }
@nodes = []
current_node = self
parts.each do |part|
case part
when /\<\!\-\- \$[0-9]+ \-\-\>/
binding_id = part.match(/\<\!\-\- \$([0-9]+) \-\-\>/)[1].to_i
sub_node = ComponentNode.new(binding_id, current_node, @root || self)
current_node << sub_node
current_node = sub_node
when /\<\!\-\- \$\/[0-9]+ \-\-\>/
current_node = current_node.parent
else
current_node << HtmlNode.new(part)
end
end
changed!
end
|
#insert(index, node) ⇒ Object
68
69
70
71
|
# File 'lib/volt/page/targets/binding_document/component_node.rb', line 68
def insert(index, node)
@nodes.insert(index, node)
changed!
end
|
111
112
113
|
# File 'lib/volt/page/targets/binding_document/component_node.rb', line 111
def inspect
"<ComponentNode:#{@binding_id} #{@nodes.inspect}>"
end
|
95
96
97
98
99
|
# File 'lib/volt/page/targets/binding_document/component_node.rb', line 95
def remove
@nodes = []
changed!
end
|
#remove_anchors ⇒ Object
101
102
103
104
105
106
107
108
109
|
# File 'lib/volt/page/targets/binding_document/component_node.rb', line 101
def remove_anchors
fail 'not implemented'
@parent.nodes.delete(self)
changed!
@parent = nil
@binding_id = nil
end
|
#text=(text) ⇒ Object
29
30
31
|
# File 'lib/volt/page/targets/binding_document/component_node.rb', line 29
def text=(text)
self.html = text
end
|
73
74
75
76
77
78
79
80
|
# File 'lib/volt/page/targets/binding_document/component_node.rb', line 73
def to_html
str = []
@nodes.each do |node|
str << node.to_html
end
str.join('')
end
|