Chris@909: # Redmine - project management software Chris@909: # Copyright (C) 2006-2011 Jean-Philippe Lang Chris@909: # Chris@909: # This program is free software; you can redistribute it and/or Chris@909: # modify it under the terms of the GNU General Public License Chris@909: # as published by the Free Software Foundation; either version 2 Chris@909: # of the License, or (at your option) any later version. Chris@909: # Chris@909: # This program is distributed in the hope that it will be useful, Chris@909: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@909: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@909: # GNU General Public License for more details. Chris@909: # Chris@909: # You should have received a copy of the GNU General Public License Chris@909: # along with this program; if not, write to the Free Software Chris@909: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@909: Chris@909: require 'tree' # gem install rubytree Chris@909: Chris@909: # Monkey patch the TreeNode to add on a few more methods :nodoc: Chris@909: module TreeNodePatch Chris@909: def self.included(base) Chris@909: base.class_eval do Chris@909: attr_reader :last_items_count Chris@909: Chris@909: alias :old_initilize :initialize Chris@909: def initialize(name, content = nil) Chris@909: old_initilize(name, content) Chris@909: @childrenHash ||= {} Chris@909: @last_items_count = 0 Chris@909: extend(InstanceMethods) Chris@909: end Chris@909: end Chris@909: end Chris@909: Chris@909: module InstanceMethods Chris@909: # Adds the specified child node to the receiver node. The child node's Chris@909: # parent is set to be the receiver. The child is added as the first child in Chris@909: # the current list of children for the receiver node. Chris@909: def prepend(child) Chris@909: raise "Child already added" if @childrenHash.has_key?(child.name) Chris@909: Chris@909: @childrenHash[child.name] = child Chris@909: @children = [child] + @children Chris@909: child.parent = self Chris@909: return child Chris@909: Chris@909: end Chris@909: Chris@909: # Adds the specified child node to the receiver node. The child node's Chris@909: # parent is set to be the receiver. The child is added at the position Chris@909: # into the current list of children for the receiver node. Chris@909: def add_at(child, position) Chris@909: raise "Child already added" if @childrenHash.has_key?(child.name) Chris@909: Chris@909: @childrenHash[child.name] = child Chris@909: @children = @children.insert(position, child) Chris@909: child.parent = self Chris@909: return child Chris@909: Chris@909: end Chris@909: Chris@909: def add_last(child) Chris@909: raise "Child already added" if @childrenHash.has_key?(child.name) Chris@909: Chris@909: @childrenHash[child.name] = child Chris@909: @children << child Chris@909: @last_items_count += 1 Chris@909: child.parent = self Chris@909: return child Chris@909: Chris@909: end Chris@909: Chris@909: # Adds the specified child node to the receiver node. The child node's Chris@909: # parent is set to be the receiver. The child is added as the last child in Chris@909: # the current list of children for the receiver node. Chris@909: def add(child) Chris@909: raise "Child already added" if @childrenHash.has_key?(child.name) Chris@909: Chris@909: @childrenHash[child.name] = child Chris@909: position = @children.size - @last_items_count Chris@909: @children.insert(position, child) Chris@909: child.parent = self Chris@909: return child Chris@909: Chris@909: end Chris@909: Chris@909: # Wrapp remove! making sure to decrement the last_items counter if Chris@909: # the removed child was a last item Chris@909: def remove!(child) Chris@909: @last_items_count -= +1 if child && child.last Chris@909: super Chris@909: end Chris@909: Chris@909: Chris@909: # Will return the position (zero-based) of the current child in Chris@909: # it's parent Chris@909: def position Chris@909: self.parent.children.index(self) Chris@909: end Chris@909: end Chris@909: end Chris@909: Tree::TreeNode.send(:include, TreeNodePatch) Chris@909: Chris@909: module Redmine Chris@909: module MenuManager Chris@909: class MenuError < StandardError #:nodoc: Chris@909: end Chris@909: Chris@909: module MenuController Chris@909: def self.included(base) Chris@909: base.extend(ClassMethods) Chris@909: end Chris@909: Chris@909: module ClassMethods Chris@909: @@menu_items = Hash.new {|hash, key| hash[key] = {:default => key, :actions => {}}} Chris@909: mattr_accessor :menu_items Chris@909: Chris@909: # Set the menu item name for a controller or specific actions Chris@909: # Examples: Chris@909: # * menu_item :tickets # => sets the menu name to :tickets for the whole controller Chris@909: # * menu_item :tickets, :only => :list # => sets the menu name to :tickets for the 'list' action only Chris@909: # * menu_item :tickets, :only => [:list, :show] # => sets the menu name to :tickets for 2 actions only Chris@909: # Chris@909: # The default menu item name for a controller is controller_name by default Chris@909: # Eg. the default menu item name for ProjectsController is :projects Chris@909: def menu_item(id, options = {}) Chris@909: if actions = options[:only] Chris@909: actions = [] << actions unless actions.is_a?(Array) Chris@909: actions.each {|a| menu_items[controller_name.to_sym][:actions][a.to_sym] = id} Chris@909: else Chris@909: menu_items[controller_name.to_sym][:default] = id Chris@909: end Chris@909: end Chris@909: end Chris@909: Chris@909: def menu_items Chris@909: self.class.menu_items Chris@909: end Chris@909: Chris@909: # Returns the menu item name according to the current action Chris@909: def current_menu_item Chris@909: @current_menu_item ||= menu_items[controller_name.to_sym][:actions][action_name.to_sym] || Chris@909: menu_items[controller_name.to_sym][:default] Chris@909: end Chris@909: Chris@909: # Redirects user to the menu item of the given project Chris@909: # Returns false if user is not authorized Chris@909: def redirect_to_project_menu_item(project, name) Chris@909: item = Redmine::MenuManager.items(:project_menu).detect {|i| i.name.to_s == name.to_s} Chris@909: if item && User.current.allowed_to?(item.url, project) && (item.condition.nil? || item.condition.call(project)) Chris@909: redirect_to({item.param => project}.merge(item.url)) Chris@909: return true Chris@909: end Chris@909: false Chris@909: end Chris@909: end Chris@909: Chris@909: module MenuHelper Chris@909: # Returns the current menu item name Chris@909: def current_menu_item Chris@909: controller.current_menu_item Chris@909: end Chris@909: Chris@909: # Renders the application main menu Chris@909: def render_main_menu(project) Chris@909: render_menu((project && !project.new_record?) ? :project_menu : :application_menu, project) Chris@909: end Chris@909: Chris@909: def display_main_menu?(project) Chris@909: menu_name = project && !project.new_record? ? :project_menu : :application_menu Chris@909: Redmine::MenuManager.items(menu_name).size > 1 # 1 element is the root Chris@909: end Chris@909: Chris@909: def render_menu(menu, project=nil) Chris@909: links = [] Chris@909: menu_items_for(menu, project) do |node| Chris@909: links << render_menu_node(node, project) Chris@909: end Chris@909: links.empty? ? nil : content_tag('ul', links.join("\n").html_safe) Chris@909: end Chris@909: Chris@909: def render_menu_node(node, project=nil) Chris@909: if node.hasChildren? || !node.child_menus.nil? Chris@909: return render_menu_node_with_children(node, project) Chris@909: else Chris@909: caption, url, selected = extract_node_details(node, project) Chris@909: return content_tag('li', Chris@909: render_single_menu_node(node, caption, url, selected)) Chris@909: end Chris@909: end Chris@909: Chris@909: def render_menu_node_with_children(node, project=nil) Chris@909: caption, url, selected = extract_node_details(node, project) Chris@909: Chris@909: html = [].tap do |html| Chris@909: html << '
  • ' Chris@909: # Parent Chris@909: html << render_single_menu_node(node, caption, url, selected) Chris@909: Chris@909: # Standard children Chris@909: standard_children_list = "".tap do |child_html| Chris@909: node.children.each do |child| Chris@909: child_html << render_menu_node(child, project) Chris@909: end Chris@909: end Chris@909: Chris@909: html << content_tag(:ul, standard_children_list, :class => 'menu-children') unless standard_children_list.empty? Chris@909: Chris@909: # Unattached children Chris@909: unattached_children_list = render_unattached_children_menu(node, project) Chris@909: html << content_tag(:ul, unattached_children_list, :class => 'menu-children unattached') unless unattached_children_list.blank? Chris@909: Chris@909: html << '
  • ' Chris@909: end Chris@909: return html.join("\n") Chris@909: end Chris@909: Chris@909: # Returns a list of unattached children menu items Chris@909: def render_unattached_children_menu(node, project) Chris@909: return nil unless node.child_menus Chris@909: Chris@909: "".tap do |child_html| Chris@909: unattached_children = node.child_menus.call(project) Chris@909: # Tree nodes support #each so we need to do object detection Chris@909: if unattached_children.is_a? Array Chris@909: unattached_children.each do |child| Chris@909: child_html << content_tag(:li, render_unattached_menu_item(child, project)) Chris@909: end Chris@909: else Chris@909: raise MenuError, ":child_menus must be an array of MenuItems" Chris@909: end Chris@909: end Chris@909: end Chris@909: Chris@909: def render_single_menu_node(item, caption, url, selected) Chris@909: link_to(h(caption), url, item.html_options(:selected => selected)) Chris@909: end Chris@909: Chris@909: def render_unattached_menu_item(menu_item, project) Chris@909: raise MenuError, ":child_menus must be an array of MenuItems" unless menu_item.is_a? MenuItem Chris@909: Chris@909: if User.current.allowed_to?(menu_item.url, project) Chris@909: link_to(h(menu_item.caption), Chris@909: menu_item.url, Chris@909: menu_item.html_options) Chris@909: end Chris@909: end Chris@909: Chris@909: def menu_items_for(menu, project=nil) Chris@909: items = [] Chris@909: Redmine::MenuManager.items(menu).root.children.each do |node| Chris@909: if allowed_node?(node, User.current, project) Chris@909: if block_given? Chris@909: yield node Chris@909: else Chris@909: items << node # TODO: not used? Chris@909: end Chris@909: end Chris@909: end Chris@909: return block_given? ? nil : items Chris@909: end Chris@909: Chris@909: def extract_node_details(node, project=nil) Chris@909: item = node Chris@909: url = case item.url Chris@909: when Hash Chris@909: project.nil? ? item.url : {item.param => project}.merge(item.url) Chris@909: when Symbol Chris@909: send(item.url) Chris@909: else Chris@909: item.url Chris@909: end Chris@909: caption = item.caption(project) Chris@909: return [caption, url, (current_menu_item == item.name)] Chris@909: end Chris@909: Chris@909: # Checks if a user is allowed to access the menu item by: Chris@909: # Chris@909: # * Checking the conditions of the item Chris@909: # * Checking the url target (project only) Chris@909: def allowed_node?(node, user, project) Chris@909: if node.condition && !node.condition.call(project) Chris@909: # Condition that doesn't pass Chris@909: return false Chris@909: end Chris@909: Chris@909: if project Chris@909: return user && user.allowed_to?(node.url, project) Chris@909: else Chris@909: # outside a project, all menu items allowed Chris@909: return true Chris@909: end Chris@909: end Chris@909: end Chris@909: Chris@909: class << self Chris@909: def map(menu_name) Chris@909: @items ||= {} Chris@909: mapper = Mapper.new(menu_name.to_sym, @items) Chris@909: if block_given? Chris@909: yield mapper Chris@909: else Chris@909: mapper Chris@909: end Chris@909: end Chris@909: Chris@909: def items(menu_name) Chris@909: @items[menu_name.to_sym] || Tree::TreeNode.new(:root, {}) Chris@909: end Chris@909: end Chris@909: Chris@909: class Mapper Chris@909: def initialize(menu, items) Chris@909: items[menu] ||= Tree::TreeNode.new(:root, {}) Chris@909: @menu = menu Chris@909: @menu_items = items[menu] Chris@909: end Chris@909: Chris@909: @@last_items_count = Hash.new {|h,k| h[k] = 0} Chris@909: Chris@909: # Adds an item at the end of the menu. Available options: Chris@909: # * param: the parameter name that is used for the project id (default is :id) Chris@909: # * if: a Proc that is called before rendering the item, the item is displayed only if it returns true Chris@909: # * caption that can be: Chris@909: # * a localized string Symbol Chris@909: # * a String Chris@909: # * a Proc that can take the project as argument Chris@909: # * before, after: specify where the menu item should be inserted (eg. :after => :activity) Chris@909: # * parent: menu item will be added as a child of another named menu (eg. :parent => :issues) Chris@909: # * children: a Proc that is called before rendering the item. The Proc should return an array of MenuItems, which will be added as children to this item. Chris@909: # eg. :children => Proc.new {|project| [Redmine::MenuManager::MenuItem.new(...)] } Chris@909: # * last: menu item will stay at the end (eg. :last => true) Chris@909: # * html_options: a hash of html options that are passed to link_to Chris@909: def push(name, url, options={}) Chris@909: options = options.dup Chris@909: Chris@909: if options[:parent] Chris@909: subtree = self.find(options[:parent]) Chris@909: if subtree Chris@909: target_root = subtree Chris@909: else Chris@909: target_root = @menu_items.root Chris@909: end Chris@909: Chris@909: else Chris@909: target_root = @menu_items.root Chris@909: end Chris@909: Chris@909: # menu item position Chris@909: if first = options.delete(:first) Chris@909: target_root.prepend(MenuItem.new(name, url, options)) Chris@909: elsif before = options.delete(:before) Chris@909: Chris@909: if exists?(before) Chris@909: target_root.add_at(MenuItem.new(name, url, options), position_of(before)) Chris@909: else Chris@909: target_root.add(MenuItem.new(name, url, options)) Chris@909: end Chris@909: Chris@909: elsif after = options.delete(:after) Chris@909: Chris@909: if exists?(after) Chris@909: target_root.add_at(MenuItem.new(name, url, options), position_of(after) + 1) Chris@909: else Chris@909: target_root.add(MenuItem.new(name, url, options)) Chris@909: end Chris@909: Chris@909: elsif options[:last] # don't delete, needs to be stored Chris@909: target_root.add_last(MenuItem.new(name, url, options)) Chris@909: else Chris@909: target_root.add(MenuItem.new(name, url, options)) Chris@909: end Chris@909: end Chris@909: Chris@909: # Removes a menu item Chris@909: def delete(name) Chris@909: if found = self.find(name) Chris@909: @menu_items.remove!(found) Chris@909: end Chris@909: end Chris@909: Chris@909: # Checks if a menu item exists Chris@909: def exists?(name) Chris@909: @menu_items.any? {|node| node.name == name} Chris@909: end Chris@909: Chris@909: def find(name) Chris@909: @menu_items.find {|node| node.name == name} Chris@909: end Chris@909: Chris@909: def position_of(name) Chris@909: @menu_items.each do |node| Chris@909: if node.name == name Chris@909: return node.position Chris@909: end Chris@909: end Chris@909: end Chris@909: end Chris@909: Chris@909: class MenuItem < Tree::TreeNode Chris@909: include Redmine::I18n Chris@909: attr_reader :name, :url, :param, :condition, :parent, :child_menus, :last Chris@909: Chris@909: def initialize(name, url, options) Chris@909: raise ArgumentError, "Invalid option :if for menu item '#{name}'" if options[:if] && !options[:if].respond_to?(:call) Chris@909: raise ArgumentError, "Invalid option :html for menu item '#{name}'" if options[:html] && !options[:html].is_a?(Hash) Chris@909: raise ArgumentError, "Cannot set the :parent to be the same as this item" if options[:parent] == name.to_sym Chris@909: raise ArgumentError, "Invalid option :children for menu item '#{name}'" if options[:children] && !options[:children].respond_to?(:call) Chris@909: @name = name Chris@909: @url = url Chris@909: @condition = options[:if] Chris@909: @param = options[:param] || :id Chris@909: @caption = options[:caption] Chris@909: @html_options = options[:html] || {} Chris@909: # Adds a unique class to each menu item based on its name Chris@909: @html_options[:class] = [@html_options[:class], @name.to_s.dasherize].compact.join(' ') Chris@909: @parent = options[:parent] Chris@909: @child_menus = options[:children] Chris@909: @last = options[:last] || false Chris@909: super @name.to_sym Chris@909: end Chris@909: Chris@909: def caption(project=nil) Chris@909: if @caption.is_a?(Proc) Chris@909: c = @caption.call(project).to_s Chris@909: c = @name.to_s.humanize if c.blank? Chris@909: c Chris@909: else Chris@909: if @caption.nil? Chris@909: l_or_humanize(name, :prefix => 'label_') Chris@909: else Chris@909: @caption.is_a?(Symbol) ? l(@caption) : @caption Chris@909: end Chris@909: end Chris@909: end Chris@909: Chris@909: def html_options(options={}) Chris@909: if options[:selected] Chris@909: o = @html_options.dup Chris@909: o[:class] += ' selected' Chris@909: o Chris@909: else Chris@909: @html_options Chris@909: end Chris@909: end Chris@909: end Chris@909: end Chris@909: end