summaryrefslogtreecommitdiffstats
path: root/lib/redmine/menu_manager.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/redmine/menu_manager.rb')
-rw-r--r--lib/redmine/menu_manager.rb192
1 files changed, 100 insertions, 92 deletions
diff --git a/lib/redmine/menu_manager.rb b/lib/redmine/menu_manager.rb
index 70fa157fd..10b7a8ef9 100644
--- a/lib/redmine/menu_manager.rb
+++ b/lib/redmine/menu_manager.rb
@@ -15,93 +15,6 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
-require 'tree' # gem install rubytree
-
-# Monkey patch the TreeNode to add on a few more methods :nodoc:
-module TreeNodePatch
- def self.included(base)
- base.class_eval do
- attr_reader :last_items_count
-
- alias :old_initilize :initialize
- def initialize(name, content = nil)
- old_initilize(name, content)
- @childrenHash ||= {}
- @last_items_count = 0
- extend(InstanceMethods)
- end
- end
- end
-
- module InstanceMethods
- # 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.
- 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
-
- # 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.
- 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
-
- 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
-
- # 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.
- 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
-
- # Wrapp remove! making sure to decrement the last_items counter if
- # the removed child was a last item
- def remove!(child)
- @last_items_count -= +1 if child && child.last
- super
- end
-
-
- # Will return the position (zero-based) of the current child in
- # it's parent
- def position
- self.parent.children.index(self)
- end
- end
-end
-Tree::TreeNode.send(:include, TreeNodePatch)
-
module Redmine
module MenuManager
class MenuError < StandardError #:nodoc:
@@ -169,7 +82,7 @@ module Redmine
def display_main_menu?(project)
menu_name = project && !project.new_record? ? :project_menu : :application_menu
- Redmine::MenuManager.items(menu_name).size > 1 # 1 element is the root
+ Redmine::MenuManager.items(menu_name).children.present?
end
def render_menu(menu, project=nil)
@@ -181,7 +94,7 @@ module Redmine
end
def render_menu_node(node, project=nil)
- if node.hasChildren? || !node.child_menus.nil?
+ if node.children.present? || !node.child_menus.nil?
return render_menu_node_with_children(node, project)
else
caption, url, selected = extract_node_details(node, project)
@@ -306,13 +219,13 @@ module Redmine
end
def items(menu_name)
- @items[menu_name.to_sym] || Tree::TreeNode.new(:root, {})
+ @items[menu_name.to_sym] || MenuNode.new(:root, {})
end
end
class Mapper
def initialize(menu, items)
- items[menu] ||= Tree::TreeNode.new(:root, {})
+ items[menu] ||= MenuNode.new(:root, {})
@menu = menu
@menu_items = items[menu]
end
@@ -398,7 +311,102 @@ module Redmine
end
end
- class MenuItem < Tree::TreeNode
+ class MenuNode
+ include Enumerable
+ attr_accessor :parent
+ attr_reader :last_items_count, :name
+
+ def initialize(name, content = nil)
+ @name = name
+ @childrenHash ||= {}
+ @children = []
+ @last_items_count = 0
+ end
+
+ def children
+ if block_given?
+ @children.each {|child| yield child}
+ else
+ @children
+ end
+ end
+
+ # Returns the number of descendants + 1
+ def size
+ @children.inject(1) {|sum, node| sum + node.size}
+ end
+
+ def each &block
+ yield self
+ children { |child| child.each(&block) }
+ end
+
+ # Adds a child at first position
+ 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
+
+ # Adds a child at given position
+ 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
+
+ # Adds a child as last child
+ 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
+
+ # Adds a child
+ 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
+ alias :<< :add
+
+ # Removes a child
+ def remove!(child)
+ @childrenHash.delete(child.name)
+ @children.delete(child)
+ @last_items_count -= +1 if child && child.last
+ child.parent = nil
+ child
+ end
+
+ # Returns the position for this node in it's parent
+ def position
+ self.parent.children.index(self)
+ end
+
+ # Returns the root for this node
+ def root
+ root = self
+ root = root.parent while root.parent
+ root
+ end
+ end
+
+ class MenuItem < MenuNode
include Redmine::I18n
attr_reader :name, :url, :param, :condition, :parent, :child_menus, :last