##// END OF EJS Templates
Code cleanup....
Jean-Philippe Lang -
r8109:f6dd3c548494
parent child
Show More
@@ -316,7 +316,6 module Redmine
316
316
317 def initialize(name, content = nil)
317 def initialize(name, content = nil)
318 @name = name
318 @name = name
319 @childrenHash ||= {}
320 @children = []
319 @children = []
321 @last_items_count = 0
320 @last_items_count = 0
322 end
321 end
@@ -341,50 +340,34 module Redmine
341
340
342 # Adds a child at first position
341 # Adds a child at first position
343 def prepend(child)
342 def prepend(child)
344 raise "Child already added" if @childrenHash.has_key?(child.name)
343 add_at(child, 0)
345
346 @childrenHash[child.name] = child
347 @children = [child] + @children
348 child.parent = self
349 return child
350 end
344 end
351
345
352 # Adds a child at given position
346 # Adds a child at given position
353 def add_at(child, position)
347 def add_at(child, position)
354 raise "Child already added" if @childrenHash.has_key?(child.name)
348 raise "Child already added" if find {|node| node.name == child.name}
355
349
356 @childrenHash[child.name] = child
357 @children = @children.insert(position, child)
350 @children = @children.insert(position, child)
358 child.parent = self
351 child.parent = self
359 return child
352 child
360 end
353 end
361
354
362 # Adds a child as last child
355 # Adds a child as last child
363 def add_last(child)
356 def add_last(child)
364 raise "Child already added" if @childrenHash.has_key?(child.name)
357 add_at(child, -1)
365
366 @childrenHash[child.name] = child
367 @children << child
368 @last_items_count += 1
358 @last_items_count += 1
369 child.parent = self
359 child
370 return child
371 end
360 end
372
361
373 # Adds a child
362 # Adds a child
374 def add(child)
363 def add(child)
375 raise "Child already added" if @childrenHash.has_key?(child.name)
376
377 @childrenHash[child.name] = child
378 position = @children.size - @last_items_count
364 position = @children.size - @last_items_count
379 @children.insert(position, child)
365 add_at(child, position)
380 child.parent = self
381 return child
382 end
366 end
383 alias :<< :add
367 alias :<< :add
384
368
385 # Removes a child
369 # Removes a child
386 def remove!(child)
370 def remove!(child)
387 @childrenHash.delete(child.name)
388 @children.delete(child)
371 @children.delete(child)
389 @last_items_count -= +1 if child && child.last
372 @last_items_count -= +1 if child && child.last
390 child.parent = nil
373 child.parent = nil
General Comments 0
You need to be logged in to leave comments. Login now