class Volt::ComponentNode

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.

Attributes

binding_id[RW]
nodes[RW]
parent[RW]
root[RW]

Public Class Methods

new(binding_id = nil, parent = nil, root = nil) click to toggle source
# 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

Public Instance Methods

<<(node) click to toggle source
# File lib/volt/page/targets/binding_document/component_node.rb, line 64
def <<(node)
  @nodes << node
end
changed!() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/volt/page/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, @root || self)
        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

  changed!
end
insert(index, node) click to toggle source
# File lib/volt/page/targets/binding_document/component_node.rb, line 68
def insert(index, node)
  @nodes.insert(index, node)
  changed!
end
inspect() click to toggle source
# File lib/volt/page/targets/binding_document/component_node.rb, line 111
def inspect
  "<ComponentNode:#{@binding_id} #{@nodes.inspect}>"
end
remove() click to toggle source
# File lib/volt/page/targets/binding_document/component_node.rb, line 95
def remove
  @nodes = []

  changed!
end
remove_anchors() click to toggle source
# 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) click to toggle source
# File lib/volt/page/targets/binding_document/component_node.rb, line 29
def text=(text)
  self.html = text
end
to_html() click to toggle source
# 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