class Rubyvis::Dom::Node

Represents a Node in the W3C Document Object Model.

Attributes

_p[RW]
ancestor[RW]

Created for Tree

angle[RW]
breadth[RW]
change[RW]

Created for Tree

child_nodes[RW]

The array of child nodes. This array is empty for leaf nodes. An easy way to check if child nodes exist is to query firstChild.

depth[RW]
dx[RW]
dy[RW]
first_child[RW]

The first child, which is null for leaf nodes.

index[RW]
inner_radius[RW]
last_child[RW]
max_breadth[RW]
max_depth[RW]
mid_angle[RW]
min_breadth[RW]
min_depth[RW]
mod[RW]

Created for Tree

n[RW]
next_sibling[RW]
node_name[RW]

The node name. When generated from a map, the node name corresponds to the key at the given level in the map. Note that the root node has no associated key, and thus has an undefined node name (and no parentNode).

node_value[RW]

The node value. When generated from a map, node value corresponds to the leaf value for leaf nodes, and is undefined for internal nodes.

number[RW]

Created for Tree

outer_radius[RW]
parent_node[RW]
prelim[RW]

Created for Tree

previous_sibling[RW]
radius[RW]
shift[RW]

Created for Tree

size[RW]
start_angle[RW]
thread[RW]

Created for Tree

x[RW]
y[RW]

Public Class Methods

new(value=nil) click to toggle source

Constructs a DOM node for the specified value. Instances of this class are not typically created directly; instead they are generated from a JavaScript map using the {@link pv.Dom} operator.

# File lib/rubyvis/dom.rb, line 127
def initialize(value=nil)
  @node_value = value
  @child_nodes=[]
  @parent_node=nil
  @first_child=nil
  @last_child=nil
  @previous_sibling=nil
  @next_sibling=nil
  
end

Public Instance Methods

append_child(n) click to toggle source

Appends the specified child node to this node. If the specified child is already part of the DOM, the child is first removed before being added to this node.

# File lib/rubyvis/dom.rb, line 161
def append_child(n)
  if n.parent_node
   n.parent_node.remove_child(n)
  end
  n.parent_node=self
  n.previous_sibling=last_child
  if self.last_child
    @last_child.next_sibling = n
  else
    @first_child=n
  end
  @last_child=n
  child_nodes.push(n)
  n
end
each_child() { |c| ... } click to toggle source

Yield block on each child Replaces the javascript formula

for (var c = o.first_child; c; c = c.nextSibling)
# File lib/rubyvis/dom.rb, line 240
def each_child
  c=@first_child
  while c
    yield c
    c=c.next_sibling
  end
end
insert_before(n, r) click to toggle source

Inserts the specified child n before the given reference child r of this node. If r is null, this method is equivalent to {@link appendChild}. If n is already part of the DOM, it is first removed before being inserted.

@throws Error if r is non-null and not a child of this node. @returns {pv.Dom.Node} the inserted child.

# File lib/rubyvis/dom.rb, line 184
def insert_before(n, r)
  return append_child(n) if !r
  i=@child_nodes.index r
  raise "child not found" if i.nil?
  n.parent_node.remove_child(n) if n.parent_node
  n.parent_node=self
  n.next_sibling=r
  n.previous_sibling = r.previous_sibling
  if r.previous_sibling
    r.previous_sibling.next_sibling=n
    r.previous_sibling=n
  else
    @last_child=n if r==@last_child
    @first_child=n
  end
  @child_nodes = @child_nodes[0,i] + [n] + @child_nodes[i, child_nodes.size-i]
  n
end
inspect() click to toggle source

toggle missing

# File lib/rubyvis/dom.rb, line 327
def inspect
  childs=@child_nodes.map{|e| e.inspect}.join(",")
  "#<#{self.class} #{object_id.to_s(16)} (#{}), name: #{@node_name}, value: #{@node_value} child_nodes: [#{childs}]>"
end
nodes() click to toggle source
# File lib/rubyvis/dom.rb, line 321
def nodes
  array=[]
  nodes_flatten(self,array)
  array
end
remove_child(n) click to toggle source

Removes the specified child node from this node.

# File lib/rubyvis/dom.rb, line 138
def remove_child(n)
  i=@child_nodes.index n
  raise "child not found" if i.nil?
  @child_nodes.delete_at i
  if n.previous_sibling
   n.previous_sibling.next_sibling=n.next_sibling 
  else
    @first_child=n.next_sibling
  end
  if n.next_sibling
   n.next_sibling.previous_sibling=n.previous_sibling 
  else
   @last_child=n.previous_sibling
  end
  n.next_sibling=nil
  n.previous_sibling=nil
  n.parent_node=nil
  n
end
replace_child(n,r) click to toggle source

Replaces the specified child r of this node with the node n. If n is already part of the DOM, it is first removed before being added.

# File lib/rubyvis/dom.rb, line 205
def replace_child(n,r)
  i=child_nodes.index r
  raise "child not found" if i.nil?
  n.parent_node.remove_child(n) if n.parent_node
  n.parent_node=self
  n.next_sibling=r.next_sibling
  n.previous_sibling=r.previous_sibling
  if r.previous_sibling
    r.previous_sibling.next_sibling=n
  else
    @first_child=n
  end
  if r.next_sibling
    r.next_sibling.previous_sibling=n
  else
    @last_child=n
  end
  
  @child_nodes[i]=n
  r
end
reverse() click to toggle source

Reverses all sibling nodes.

# File lib/rubyvis/dom.rb, line 301
def reverse
  child_nodes=[]
  visit_after {|n,dummy|
  while(n.last_child) do
    child_nodes.push(n.remove_child(n.last_child)) 
  end
  c=nil
  while(c=child_nodes.pop)
    n.insert_before(c,n.first_child)
  end
  }
  self
end
sort(f_a=nil,&f) click to toggle source

Sorts child nodes of this node, and all descendent nodes recursively, using the specified comparator function f. The comparator function is passed two nodes to compare.

<p>Note: during the sort operation, the comparator function should not rely on the tree being well-formed; the values of previousSibling and nextSibling for the nodes being compared are not defined during the sort operation.

@param {function} f a comparator function. @returns this.

# File lib/rubyvis/dom.rb, line 281
def sort(f_a=nil,&f)
  f=f_a unless f_a.nil?
  raise "Should pass a Proc" if f.nil?
  if @first_child
    @child_nodes.sort!(&f)
    _p=@first_child = child_nodes[0]
    _p.previous_sibling=nil
    (1...@child_nodes.size).each {|i|
      _p.sort(&f)
      c=@child_nodes[i]
      c.previous_sibling=_p
      _p=_p.next_sibling=c
    }
    @last_child=_p
    _p.next_sibling=nil
    _p.sort(f)
  end
  self
end
visit_after(f=nil,&block) click to toggle source

Visits each node in the tree in postorder traversal, applying the specified function f. The arguments to the function are:<ol>

<li>The current node. <li>The current depth, starting at 0 for the root node.</ol>

# File lib/rubyvis/dom.rb, line 263
def visit_after(f=nil,&block)
  block=f unless f.nil?
  raise "Should pass a Proc" if block.nil?
  visit_visit(self,0,block, :after)
end
visit_before(f=nil,&block) click to toggle source

Visits each node in the tree in preorder traversal, applying the specified proc block. The arguments to the function are:<ol>

<li>The current node. <li>The current depth, starting at 0 for the root node.</ol>

# File lib/rubyvis/dom.rb, line 253
def visit_before(f=nil,&block)
  block=f unless f.nil?
  raise "Should pass a Proc" if block.nil?
  visit_visit(self,0,block, :before)
end