-# redMine - project management software
-# Copyright (C) 2006-2007 Jean-Philippe Lang
+# Redmine - project management software
+# Copyright (C) 2006-2011 Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
-#
+#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
-#
+#
# 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.
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)
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
module MenuManager
class MenuError < StandardError #:nodoc:
end
-
+
module MenuController
def self.included(base)
base.extend(ClassMethods)
module ClassMethods
@@menu_items = Hash.new {|hash, key| hash[key] = {:default => key, :actions => {}}}
mattr_accessor :menu_items
-
+
# Set the menu item name for a controller or specific actions
# Examples:
# * menu_item :tickets # => sets the menu name to :tickets for the whole controller
# * menu_item :tickets, :only => :list # => sets the menu name to :tickets for the 'list' action only
# * menu_item :tickets, :only => [:list, :show] # => sets the menu name to :tickets for 2 actions only
- #
+ #
# The default menu item name for a controller is controller_name by default
# Eg. the default menu item name for ProjectsController is :projects
def menu_item(id, options = {})
end
end
end
-
+
def menu_items
self.class.menu_items
end
-
+
# Returns the menu item name according to the current action
def current_menu_item
@current_menu_item ||= menu_items[controller_name.to_sym][:actions][action_name.to_sym] ||
menu_items[controller_name.to_sym][:default]
end
-
+
# Redirects user to the menu item of the given project
# Returns false if user is not authorized
def redirect_to_project_menu_item(project, name)
false
end
end
-
+
module MenuHelper
# Returns the current menu item name
def current_menu_item
@controller.current_menu_item
end
-
+
# Renders the application main menu
def render_main_menu(project)
render_menu((project && !project.new_record?) ? :project_menu : :application_menu, project)
end
-
+
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
# Tree nodes support #each so we need to do object detection
if unattached_children.is_a? Array
unattached_children.each do |child|
- child_html << content_tag(:li, render_unattached_menu_item(child, project))
+ child_html << content_tag(:li, render_unattached_menu_item(child, project))
end
else
raise MenuError, ":child_menus must be an array of MenuItems"
menu_item.html_options)
end
end
-
+
def menu_items_for(menu, project=nil)
items = []
Redmine::MenuManager.items(menu).root.children.each do |node|
end
end
end
-
+
class << self
def map(menu_name)
@items ||= {}
mapper
end
end
-
+
def items(menu_name)
@items[menu_name.to_sym] || Tree::TreeNode.new(:root, {})
end
end
-
+
class Mapper
def initialize(menu, items)
items[menu] ||= Tree::TreeNode.new(:root, {})
@menu = menu
@menu_items = items[menu]
end
-
+
@@last_items_count = Hash.new {|h,k| h[k] = 0}
-
+
# Adds an item at the end of the menu. Available options:
# * param: the parameter name that is used for the project id (default is :id)
# * if: a Proc that is called before rendering the item, the item is displayed only if it returns true
else
target_root.add(MenuItem.new(name, url, options))
end
-
+
elsif options[:last] # don't delete, needs to be stored
target_root.add_last(MenuItem.new(name, url, options))
else
target_root.add(MenuItem.new(name, url, options))
end
end
-
+
# Removes a menu item
def delete(name)
if found = self.find(name)
end
end
end
-
+
class MenuItem < Tree::TreeNode
include Redmine::I18n
attr_reader :name, :url, :param, :condition, :parent, :child_menus, :last
-
+
def initialize(name, url, options)
raise ArgumentError, "Invalid option :if for menu item '#{name}'" if options[:if] && !options[:if].respond_to?(:call)
raise ArgumentError, "Invalid option :html for menu item '#{name}'" if options[:html] && !options[:html].is_a?(Hash)
@last = options[:last] || false
super @name.to_sym
end
-
+
def caption(project=nil)
if @caption.is_a?(Proc)
c = @caption.call(project).to_s
end
end
end
-
+
def html_options(options={})
if options[:selected]
o = @html_options.dup
@html_options
end
end
- end
+ end
end
end