[#269] Instead of patching rubytree, creating a custom sub class and using that everywhere
This commit is contained in:
parent
6dd831e3fe
commit
28b7f6cc72
@ -14,96 +14,6 @@
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# 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
|
||||
|
||||
def initialize_with_redmine(name, content = nil)
|
||||
extend InstanceMethods
|
||||
@last_items_count = 0
|
||||
|
||||
initialize_without_redmine(name, content)
|
||||
end
|
||||
alias_method_chain :initialize, :redmine
|
||||
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
|
||||
unless Tree::TreeNode.included_modules.include?(TreeNodePatch)
|
||||
Tree::TreeNode.send(:include, TreeNodePatch)
|
||||
end
|
||||
|
||||
module Redmine::MenuManager
|
||||
def self.map(menu_name)
|
||||
@items ||= {}
|
||||
@ -116,6 +26,6 @@ module Redmine::MenuManager
|
||||
end
|
||||
|
||||
def self.items(menu_name)
|
||||
@items[menu_name.to_sym] || Tree::TreeNode.new(:root, {})
|
||||
@items[menu_name.to_sym] || Redmine::MenuManager::TreeNode.new(:root, {})
|
||||
end
|
||||
end
|
||||
|
@ -1,6 +1,6 @@
|
||||
class Redmine::MenuManager::Mapper
|
||||
def initialize(menu, items)
|
||||
items[menu] ||= Tree::TreeNode.new(:root, {})
|
||||
items[menu] ||= Redmine::MenuManager::TreeNode.new(:root, {})
|
||||
@menu = menu
|
||||
@menu_items = items[menu]
|
||||
end
|
||||
|
@ -1,4 +1,4 @@
|
||||
class Redmine::MenuManager::MenuItem < Tree::TreeNode
|
||||
class Redmine::MenuManager::MenuItem < Redmine::MenuManager::TreeNode
|
||||
include Redmine::I18n
|
||||
attr_reader :name, :url, :param, :condition, :parent, :child_menus, :last
|
||||
|
||||
|
75
lib/redmine/menu_manager/tree_node.rb
Normal file
75
lib/redmine/menu_manager/tree_node.rb
Normal file
@ -0,0 +1,75 @@
|
||||
require 'tree' # gem install rubytree
|
||||
|
||||
class Redmine::MenuManager::TreeNode < Tree::TreeNode
|
||||
attr_reader :last_items_count
|
||||
|
||||
def initialize(name, content = nil)
|
||||
@last_items_count = 0
|
||||
super
|
||||
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 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
|
Loading…
x
Reference in New Issue
Block a user