explicitly defines the selected menu items for the wiki controller

* index, new_child and the rest is handled separately
* index and new_child are now scoped (by their symbol) under the wiki_menu_entry, so that the correct index/new_child page is selected. The symbols are still somewhat magical.
* implements a method to add a dynamically evaluated method to determine the currently selected menu item
pull/41/head
Jens Ulferts 12 years ago
parent 03edba5dbc
commit ce9b860b3f
  1. 22
      app/controllers/wiki_controller.rb
  2. 27
      lib/redmine/menu_manager/menu_controller.rb
  3. 14
      lib/redmine/menu_manager/menu_helper.rb

@ -39,6 +39,28 @@ class WikiController < ApplicationController
include AttachmentsHelper
attr_reader :page, :related_page
current_menu_item :index do |controller|
menu_item = controller.related_page.nearest_menu_item
:"#{menu_item.item_class}_toc"
end
current_menu_item :new_child do |controller|
menu_item = controller.page.nearest_menu_item
:"#{menu_item.item_class}_new_page"
end
current_menu_item do |controller|
menu_item = controller.page.nearest_menu_item
menu_item.present? ?
menu_item.item_class.to_sym :
nil
end
# List of pages, sorted alphabetically and by parent (hierarchy)
def index
@related_page = WikiPage.find_by_wiki_id_and_title(@wiki.id, params[:id])

@ -37,6 +37,17 @@ module Redmine::MenuManager::MenuController
menu_items[controller_name.to_sym][:default] = id
end
end
def current_menu_item(actions = :default, &block)
raise ArgumentError "#current_menu_item requires a block" unless block_given?
if actions == :default
menu_items[controller_name.to_sym][:default] = block
else
actions = [] << actions unless actions.is_a?(Array)
actions.each {|a| menu_items[controller_name.to_sym][:actions][a.to_sym] = block}
end
end
end
def menu_items
@ -45,8 +56,22 @@ module Redmine::MenuManager::MenuController
# 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] ||
return @current_menu_item if @current_menu_item_determined
@current_menu_item = menu_items[controller_name.to_sym][:actions][action_name.to_sym] ||
menu_items[controller_name.to_sym][:default]
@current_menu_item = if @current_menu_item.is_a?(Symbol)
@current_menu_item
elsif @current_menu_item.is_a?(Proc)
@current_menu_item.call(self)
else
raise ArgumentError "Invalid"
end
@current_menu_item_determined = true
@current_menu_item
end
# Redirects user to the menu item of the given project

@ -35,12 +35,12 @@ module Redmine::MenuManager::MenuHelper
{ :controller => 'wiki', :action => 'show', :id => h(main_item.title) },
:param => :project_id, :caption => main_item.name
menu.push :wiki_create_new_page, {:action=>"new_child", :controller=>"wiki", :id => h(main_item.title) },
menu.push :"#{main_item.item_class}_new_page", {:action=>"new_child", :controller=>"wiki", :id => h(main_item.title) },
:param => :project_id, :caption => :create_child_page,
:parent => "#{main_item.item_class}".to_sym if main_item.new_wiki_page and
WikiPage.find_by_wiki_id_and_title(project_wiki.id, main_item.title)
menu.push :table_of_contents, {:action => 'index', :controller => 'wiki', :id => h(main_item.title)}, :param => :project_id, :caption => :label_table_of_contents, :parent => "#{main_item.item_class}".to_sym if main_item.index_page
menu.push :"#{main_item.item_class}_toc", {:action => 'index', :controller => 'wiki', :id => h(main_item.title)}, :param => :project_id, :caption => :label_table_of_contents, :parent => "#{main_item.item_class}".to_sym if main_item.index_page
main_item.children.each do |child|
menu.push "#{child.item_class}".to_sym,
@ -185,15 +185,7 @@ module Redmine::MenuManager::MenuHelper
end
caption = item.caption(project)
if @page and @page.instance_of?(WikiPage) and !@page.new_record? and current_menu_item == :wiki
menu_item = @page.nearest_menu_item
selected = node.name.to_sym == menu_item.title.dasherize.to_sym if menu_item
elsif current_menu_item == :wiki and related_page = params[:id]
selected = related_page.dasherize == item.name.to_s.dasherize
else
selected = current_menu_item == item.name
end
selected = current_menu_item == item.name
return [caption, url, selected]
end

Loading…
Cancel
Save