Class: Redmine::MenuManager::TreeNode

Inherits:
Tree::TreeNode
  • Object
show all
Defined in:
lib/redmine/menu_manager/tree_node.rb

Direct Known Subclasses

MenuItem

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, content = nil) ⇒ TreeNode

Returns a new instance of TreeNode.


20
21
22
23
# File 'lib/redmine/menu_manager/tree_node.rb', line 20

def initialize(name, content = nil)
  @last_items_count = 0
  super
end

Instance Attribute Details

#last_items_countObject (readonly)

Returns the value of attribute last_items_count


18
19
20
# File 'lib/redmine/menu_manager/tree_node.rb', line 18

def last_items_count
  @last_items_count
end

Instance Method Details

#add(child) ⇒ Object

Adds the specified child node to the receiver node. The child node's parent is set to be the receiver. The child is added as the last child in the current list of children for the receiver node.


65
66
67
68
69
70
71
72
73
74
# File 'lib/redmine/menu_manager/tree_node.rb', line 65

def add(child)
  raise "Child already added" if @childrenHash.has_key?(child.name)

  @childrenHash[child.name]  = child
  position = @children.size - @last_items_count
  @children.insert(position, child)
  child.parent = self
  return child

end

#add_at(child, position) ⇒ Object

Adds the specified child node to the receiver node. The child node's parent is set to be the receiver. The child is added at the position into the current list of children for the receiver node.


41
42
43
44
45
46
47
48
49
# File 'lib/redmine/menu_manager/tree_node.rb', line 41

def add_at(child, position)
  raise "Child already added" if @childrenHash.has_key?(child.name)

  @childrenHash[child.name]  = child
  @children = @children.insert(position, child)
  child.parent = self
  return child

end

#add_last(child) ⇒ Object


51
52
53
54
55
56
57
58
59
60
# File 'lib/redmine/menu_manager/tree_node.rb', line 51

def add_last(child)
  raise "Child already added" if @childrenHash.has_key?(child.name)

  @childrenHash[child.name]  = child
  @children <<  child
  @last_items_count += 1
  child.parent = self
  return child

end

#positionObject

Will return the position (zero-based) of the current child in it's parent


86
87
88
# File 'lib/redmine/menu_manager/tree_node.rb', line 86

def position
  self.parent.children.index(self)
end

#prepend(child) ⇒ Object

Adds the specified child node to the receiver node. The child node's parent is set to be the receiver. The child is added as the first child in the current list of children for the receiver node.


28
29
30
31
32
33
34
35
36
# File 'lib/redmine/menu_manager/tree_node.rb', line 28

def prepend(child)
  raise "Child already added" if @childrenHash.has_key?(child.name)

  @childrenHash[child.name]  = child
  @children = [child] + @children
  child.parent = self
  return child

end

#remove!(child) ⇒ Object

Wrapp remove! making sure to decrement the last_items counter if the removed child was a last item


78
79
80
81
# File 'lib/redmine/menu_manager/tree_node.rb', line 78

def remove!(child)
  @last_items_count -= +1 if child && child.last
  super
end