Class: Rubyvis::Dom::Node
Overview
Represents a Node
in the W3C Document Object Model.
Instance Attribute Summary collapse
-
#_p ⇒ Object
Returns the value of attribute _p.
-
#ancestor ⇒ Object
Created for Tree.
-
#angle ⇒ Object
Returns the value of attribute angle.
-
#breadth ⇒ Object
Returns the value of attribute breadth.
-
#change ⇒ Object
Created for Tree.
-
#child_nodes ⇒ Object
The array of child nodes.
-
#depth ⇒ Object
Returns the value of attribute depth.
-
#dx ⇒ Object
Returns the value of attribute dx.
-
#dy ⇒ Object
Returns the value of attribute dy.
-
#first_child ⇒ Object
The first child, which is null for leaf nodes.
-
#index ⇒ Object
Returns the value of attribute index.
-
#inner_radius ⇒ Object
Returns the value of attribute inner_radius.
-
#last_child ⇒ Object
Returns the value of attribute last_child.
-
#link_degree ⇒ Object
Returns the value of attribute link_degree.
-
#max_breadth ⇒ Object
Returns the value of attribute max_breadth.
-
#max_depth ⇒ Object
Returns the value of attribute max_depth.
-
#mid_angle ⇒ Object
Returns the value of attribute mid_angle.
-
#min_breadth ⇒ Object
Returns the value of attribute min_breadth.
-
#min_depth ⇒ Object
Returns the value of attribute min_depth.
-
#mod ⇒ Object
Created for Tree.
-
#n ⇒ Object
Returns the value of attribute n.
-
#next_sibling ⇒ Object
Returns the value of attribute next_sibling.
-
#node_name ⇒ Object
The node name.
-
#node_value ⇒ Object
The node value.
-
#number ⇒ Object
Created for Tree.
-
#outer_radius ⇒ Object
Returns the value of attribute outer_radius.
-
#parent_node ⇒ Object
Returns the value of attribute parent_node.
-
#prelim ⇒ Object
Created for Tree.
-
#previous_sibling ⇒ Object
Returns the value of attribute previous_sibling.
-
#radius ⇒ Object
Returns the value of attribute radius.
-
#shift ⇒ Object
Created for Tree.
-
#size ⇒ Object
Returns the value of attribute size.
-
#start_angle ⇒ Object
Returns the value of attribute start_angle.
-
#thread ⇒ Object
Created for Tree.
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
Instance Method Summary collapse
-
#append_child(n) ⇒ Object
Appends the specified child node to this node.
-
#each_child ⇒ Object
Yield block on each child Replaces the javascript formula for (var c = o.first_child; c; c = c.nextSibling).
-
#initialize(value = nil) ⇒ Node
constructor
Constructs a DOM node for the specified value.
-
#insert_before(n, r) ⇒ Object
Inserts the specified child n before the given reference child r of this node.
-
#inspect ⇒ Object
toggle missing.
- #nodes ⇒ Object
-
#remove_child(n) ⇒ Object
Removes the specified child node from this node.
-
#replace_child(n, r) ⇒ Object
Replaces the specified child r of this node with the node n.
-
#reverse ⇒ Object
Reverses all sibling nodes.
-
#sort(f_a = nil, &f) ⇒ Object
Sorts child nodes of this node, and all descendent nodes recursively, using the specified comparator function
f
. -
#visit_after(f = nil, &block) ⇒ Object
Visits each node in the tree in postorder traversal, applying the specified function f.
-
#visit_before(f = nil, &block) ⇒ Object
Visits each node in the tree in preorder traversal, applying the specified proc block.
Constructor Details
#initialize(value = nil) ⇒ Node
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 pv.Dom operator.
127 128 129 130 131 132 133 134 135 136 |
# 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 |
Instance Attribute Details
#_p ⇒ Object
Returns the value of attribute _p.
115 116 117 |
# File 'lib/rubyvis/dom.rb', line 115 def _p @_p end |
#ancestor ⇒ Object
Created for Tree
121 122 123 |
# File 'lib/rubyvis/dom.rb', line 121 def ancestor @ancestor end |
#angle ⇒ Object
Returns the value of attribute angle.
110 111 112 |
# File 'lib/rubyvis/dom.rb', line 110 def angle @angle end |
#breadth ⇒ Object
Returns the value of attribute breadth.
105 106 107 |
# File 'lib/rubyvis/dom.rb', line 105 def breadth @breadth end |
#change ⇒ Object
Created for Tree
121 122 123 |
# File 'lib/rubyvis/dom.rb', line 121 def change @change end |
#child_nodes ⇒ Object
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
.
85 86 87 |
# File 'lib/rubyvis/dom.rb', line 85 def child_nodes @child_nodes end |
#depth ⇒ Object
Returns the value of attribute depth.
96 97 98 |
# File 'lib/rubyvis/dom.rb', line 96 def depth @depth end |
#dx ⇒ Object
Returns the value of attribute dx.
97 98 99 |
# File 'lib/rubyvis/dom.rb', line 97 def dx @dx end |
#dy ⇒ Object
Returns the value of attribute dy.
98 99 100 |
# File 'lib/rubyvis/dom.rb', line 98 def dy @dy end |
#first_child ⇒ Object
The first child, which is null for leaf nodes.
89 90 91 |
# File 'lib/rubyvis/dom.rb', line 89 def first_child @first_child end |
#index ⇒ Object
Returns the value of attribute index.
94 95 96 |
# File 'lib/rubyvis/dom.rb', line 94 def index @index end |
#inner_radius ⇒ Object
Returns the value of attribute inner_radius.
113 114 115 |
# File 'lib/rubyvis/dom.rb', line 113 def inner_radius @inner_radius end |
#last_child ⇒ Object
Returns the value of attribute last_child.
90 91 92 |
# File 'lib/rubyvis/dom.rb', line 90 def last_child @last_child end |
#link_degree ⇒ Object
Returns the value of attribute link_degree.
95 96 97 |
# File 'lib/rubyvis/dom.rb', line 95 def link_degree @link_degree end |
#max_breadth ⇒ Object
Returns the value of attribute max_breadth.
104 105 106 |
# File 'lib/rubyvis/dom.rb', line 104 def max_breadth @max_breadth end |
#max_depth ⇒ Object
Returns the value of attribute max_depth.
107 108 109 |
# File 'lib/rubyvis/dom.rb', line 107 def max_depth @max_depth end |
#mid_angle ⇒ Object
Returns the value of attribute mid_angle.
109 110 111 |
# File 'lib/rubyvis/dom.rb', line 109 def mid_angle @mid_angle end |
#min_breadth ⇒ Object
Returns the value of attribute min_breadth.
103 104 105 |
# File 'lib/rubyvis/dom.rb', line 103 def min_breadth @min_breadth end |
#min_depth ⇒ Object
Returns the value of attribute min_depth.
106 107 108 |
# File 'lib/rubyvis/dom.rb', line 106 def min_depth @min_depth end |
#n ⇒ Object
Returns the value of attribute n.
116 117 118 |
# File 'lib/rubyvis/dom.rb', line 116 def n @n end |
#next_sibling ⇒ Object
Returns the value of attribute next_sibling.
92 93 94 |
# File 'lib/rubyvis/dom.rb', line 92 def next_sibling @next_sibling end |
#node_name ⇒ Object
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
).
77 78 79 |
# File 'lib/rubyvis/dom.rb', line 77 def node_name @node_name end |
#node_value ⇒ Object
The node value. When generated from a map, node value corresponds to the leaf value for leaf nodes, and is undefined for internal nodes.
81 82 83 |
# File 'lib/rubyvis/dom.rb', line 81 def node_value @node_value end |
#number ⇒ Object
Created for Tree
121 122 123 |
# File 'lib/rubyvis/dom.rb', line 121 def number @number end |
#outer_radius ⇒ Object
Returns the value of attribute outer_radius.
112 113 114 |
# File 'lib/rubyvis/dom.rb', line 112 def outer_radius @outer_radius end |
#parent_node ⇒ Object
Returns the value of attribute parent_node.
86 87 88 |
# File 'lib/rubyvis/dom.rb', line 86 def parent_node @parent_node end |
#prelim ⇒ Object
Created for Tree
121 122 123 |
# File 'lib/rubyvis/dom.rb', line 121 def prelim @prelim end |
#previous_sibling ⇒ Object
Returns the value of attribute previous_sibling.
91 92 93 |
# File 'lib/rubyvis/dom.rb', line 91 def previous_sibling @previous_sibling end |
#radius ⇒ Object
Returns the value of attribute radius.
114 115 116 |
# File 'lib/rubyvis/dom.rb', line 114 def radius @radius end |
#shift ⇒ Object
Created for Tree
121 122 123 |
# File 'lib/rubyvis/dom.rb', line 121 def shift @shift end |
#size ⇒ Object
Returns the value of attribute size.
101 102 103 |
# File 'lib/rubyvis/dom.rb', line 101 def size @size end |
#start_angle ⇒ Object
Returns the value of attribute start_angle.
111 112 113 |
# File 'lib/rubyvis/dom.rb', line 111 def start_angle @start_angle end |
#thread ⇒ Object
Created for Tree
121 122 123 |
# File 'lib/rubyvis/dom.rb', line 121 def thread @thread end |
#x ⇒ Object
Returns the value of attribute x.
99 100 101 |
# File 'lib/rubyvis/dom.rb', line 99 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
100 101 102 |
# File 'lib/rubyvis/dom.rb', line 100 def y @y end |
Instance Method Details
#append_child(n) ⇒ Object
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.
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# 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 ⇒ Object
Yield block on each child Replaces the javascript formula
for (var c = o.first_child; c; c = c.nextSibling)
240 241 242 243 244 245 246 |
# 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) ⇒ Object
Inserts the specified child n before the given reference child r of this node. If r is null, this method is equivalent to #appendChild. If n is already part of the DOM, it is first removed before being inserted.
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# 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 ⇒ Object
toggle missing
327 328 329 330 |
# 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 ⇒ Object
321 322 323 324 325 |
# File 'lib/rubyvis/dom.rb', line 321 def nodes array=[] nodes_flatten(self,array) array end |
#remove_child(n) ⇒ Object
Removes the specified child node from this node.
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# 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) ⇒ Object
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.
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# 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 ⇒ Object
Reverses all sibling nodes.
301 302 303 304 305 306 307 308 309 310 311 312 313 |
# 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) ⇒ Object
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.
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
# 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) ⇒ Object
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>
263 264 265 266 267 |
# 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) ⇒ Object
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>
253 254 255 256 257 |
# 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 |