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