Class: ComponentNode

Inherits:
BaseNode show all
Includes:
Events
Defined in:
lib/volt/templates/targets/binding_document/component_node.rb

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.

Direct Known Subclasses

AttributeTarget

Instance Attribute Summary collapse

Attributes included from Events

#scope

Instance Method Summary collapse

Methods included from Events

#add_event_follower, #add_event_to_chains, #add_following, #all_listeners_for, #event_chain, #event_followers, #event_followings, #listeners, #on, #remove_event_follower, #remove_event_from_chains, #remove_following, #remove_listener, #trigger!, #trigger_by_scope!

Constructor Details

#initialize(binding_id = nil, parent = nil) ⇒ ComponentNode

Returns a new instance of ComponentNode.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/volt/templates/targets/binding_document/component_node.rb', line 12

def initialize(binding_id=nil, parent=nil)
  @nodes = []
  @binding_id = binding_id
  @parent = parent
  
  @change_listener = on('changed') do
    if @parent
      @parent.trigger!('changed')
    end
  end
end

Instance Attribute Details

#binding_idObject

Returns the value of attribute binding_id.



11
12
13
# File 'lib/volt/templates/targets/binding_document/component_node.rb', line 11

def binding_id
  @binding_id
end

#nodesObject

Returns the value of attribute nodes.



11
12
13
# File 'lib/volt/templates/targets/binding_document/component_node.rb', line 11

def nodes
  @nodes
end

#parentObject

Returns the value of attribute parent.



11
12
13
# File 'lib/volt/templates/targets/binding_document/component_node.rb', line 11

def parent
  @parent
end

Instance Method Details

#<<(node) ⇒ Object



64
65
66
# File 'lib/volt/templates/targets/binding_document/component_node.rb', line 64

def <<(node)
  @nodes << node
end

#find_by_binding_id(binding_id) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/volt/templates/targets/binding_document/component_node.rb', line 77

def find_by_binding_id(binding_id)
  if @binding_id == binding_id
    return self
  end
  
  @nodes.each do |node|
    if node.cur.is_a?(ComponentNode)
      val = node.find_by_binding_id(binding_id)
      return val if val
    end
  end
  
  return 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/templates/targets/binding_document/component_node.rb', line 33

def html=(html)
  parts = html.split(/(\<\!\-\- \$\/?[0-9]+ \-\-\>)/).reject {|v| v == '' }

  # Clear current nodes
  @nodes = []
  
  current_node = self

  parts.each do |part|
    case part
    when /\<\!\-\- \$[0-9]+ \-\-\>/
      # Open
      binding_id = part.match(/\<\!\-\- \$([0-9]+) \-\-\>/)[1].to_i
    
      sub_node = ComponentNode.new(binding_id, current_node)
      current_node << sub_node
      current_node = sub_node
    when /\<\!\-\- \$\/[0-9]+ \-\-\>/
      # Close
      # binding_id = part.match(/\<\!\-\- \$\/([0-9]+) \-\-\>/)[1].to_i
    
      current_node = current_node.parent
    else
      # html string
      current_node << HtmlNode.new(part)
    end
  end
  
  trigger!('changed')
end

#inspectObject



109
110
111
# File 'lib/volt/templates/targets/binding_document/component_node.rb', line 109

def inspect
  "<ComponentNode:#{@binding_id} #{@nodes.inspect}>"
end

#removeObject



92
93
94
95
96
# File 'lib/volt/templates/targets/binding_document/component_node.rb', line 92

def remove
  @nodes = []
  
  trigger!('changed')
end

#remove_anchorsObject



98
99
100
101
102
103
104
105
106
107
# File 'lib/volt/templates/targets/binding_document/component_node.rb', line 98

def remove_anchors
  raise "not implemented"

  @parent.nodes.delete(self)
      
  @change_listener.remove
  @change_listener = nil
  
  trigger!('changed')
end

#skip_current_queue_flushObject

TODO: improve



25
26
27
# File 'lib/volt/templates/targets/binding_document/component_node.rb', line 25

def skip_current_queue_flush
  true
end

#text=(text) ⇒ Object



29
30
31
# File 'lib/volt/templates/targets/binding_document/component_node.rb', line 29

def text=(text)
  self.html = text
end

#to_htmlObject



68
69
70
71
72
73
74
75
# File 'lib/volt/templates/targets/binding_document/component_node.rb', line 68

def to_html
  str = []
  @nodes.each do |node|
    str << node.to_html
  end
  
  return str.join('')
end