Class: Rubyvis::Dom::Node

Inherits:
Object show all
Defined in:
lib/rubyvis/dom.rb

Overview

Represents a Node in the W3C Document Object Model.

Instance Attribute Summary collapse

Instance Method Summary collapse

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.



119
120
121
122
123
124
125
126
127
128
# File 'lib/rubyvis/dom.rb', line 119

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

#angleObject

Returns the value of attribute angle.



110
111
112
# File 'lib/rubyvis/dom.rb', line 110

def angle
  @angle
end

#breadthObject

Returns the value of attribute breadth.



105
106
107
# File 'lib/rubyvis/dom.rb', line 105

def breadth
  @breadth
end

#child_nodesObject

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

#depthObject

Returns the value of attribute depth.



96
97
98
# File 'lib/rubyvis/dom.rb', line 96

def depth
  @depth
end

#dxObject

Returns the value of attribute dx.



97
98
99
# File 'lib/rubyvis/dom.rb', line 97

def dx
  @dx
end

#dyObject

Returns the value of attribute dy.



98
99
100
# File 'lib/rubyvis/dom.rb', line 98

def dy
  @dy
end

#first_childObject

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

#indexObject

Returns the value of attribute index.



94
95
96
# File 'lib/rubyvis/dom.rb', line 94

def index
  @index
end

#inner_radiusObject

Returns the value of attribute inner_radius.



113
114
115
# File 'lib/rubyvis/dom.rb', line 113

def inner_radius
  @inner_radius
end

#last_childObject

Returns the value of attribute last_child.



90
91
92
# File 'lib/rubyvis/dom.rb', line 90

def last_child
  @last_child
end

Returns the value of attribute link_degree.



95
96
97
# File 'lib/rubyvis/dom.rb', line 95

def link_degree
  @link_degree
end

#max_breadthObject

Returns the value of attribute max_breadth.



104
105
106
# File 'lib/rubyvis/dom.rb', line 104

def max_breadth
  @max_breadth
end

#max_depthObject

Returns the value of attribute max_depth.



107
108
109
# File 'lib/rubyvis/dom.rb', line 107

def max_depth
  @max_depth
end

#mid_angleObject

Returns the value of attribute mid_angle.



109
110
111
# File 'lib/rubyvis/dom.rb', line 109

def mid_angle
  @mid_angle
end

#min_breadthObject

Returns the value of attribute min_breadth.



103
104
105
# File 'lib/rubyvis/dom.rb', line 103

def min_breadth
  @min_breadth
end

#min_depthObject

Returns the value of attribute min_depth.



106
107
108
# File 'lib/rubyvis/dom.rb', line 106

def min_depth
  @min_depth
end

#next_siblingObject

Returns the value of attribute next_sibling.



92
93
94
# File 'lib/rubyvis/dom.rb', line 92

def next_sibling
  @next_sibling
end

#node_nameObject

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_valueObject (readonly)

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

#outer_radiusObject

Returns the value of attribute outer_radius.



112
113
114
# File 'lib/rubyvis/dom.rb', line 112

def outer_radius
  @outer_radius
end

#parent_nodeObject

Returns the value of attribute parent_node.



86
87
88
# File 'lib/rubyvis/dom.rb', line 86

def parent_node
  @parent_node
end

#previous_siblingObject

Returns the value of attribute previous_sibling.



91
92
93
# File 'lib/rubyvis/dom.rb', line 91

def previous_sibling
  @previous_sibling
end

#sizeObject

Returns the value of attribute size.



101
102
103
# File 'lib/rubyvis/dom.rb', line 101

def size
  @size
end

#start_angleObject

Returns the value of attribute start_angle.



111
112
113
# File 'lib/rubyvis/dom.rb', line 111

def start_angle
  @start_angle
end

#xObject

Returns the value of attribute x.



99
100
101
# File 'lib/rubyvis/dom.rb', line 99

def x
  @x
end

#yObject

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.



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/rubyvis/dom.rb', line 153

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

#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.



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/rubyvis/dom.rb', line 176

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

#inspectObject

toggle missing



310
311
312
313
# File 'lib/rubyvis/dom.rb', line 310

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

#nodesObject



304
305
306
307
308
# File 'lib/rubyvis/dom.rb', line 304

def nodes
  array=[]
  nodes_flatten(self,array)
  array
end

#remove_child(n) ⇒ Object

Removes the specified child node from this node.



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/rubyvis/dom.rb', line 130

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.



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/rubyvis/dom.rb', line 197

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

#reverseObject

Reverses all sibling nodes.



284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/rubyvis/dom.rb', line 284

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.

Parameters:

  • f (function)

    a comparator function.



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/rubyvis/dom.rb', line 264

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>



246
247
248
249
250
# File 'lib/rubyvis/dom.rb', line 246

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>



236
237
238
239
240
# File 'lib/rubyvis/dom.rb', line 236

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