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