To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / lib / redmine / menu_manager.rb @ 1298:4f746d8966dd
History | View | Annotate | Download (14 KB)
| 1 | 909:cbb26bc654de | Chris | # Redmine - project management software
|
|---|---|---|---|
| 2 | 1295:622f24f53b42 | Chris | # Copyright (C) 2006-2013 Jean-Philippe Lang
|
| 3 | 0:513646585e45 | Chris | #
|
| 4 | # This program is free software; you can redistribute it and/or
|
||
| 5 | # modify it under the terms of the GNU General Public License
|
||
| 6 | # as published by the Free Software Foundation; either version 2
|
||
| 7 | # of the License, or (at your option) any later version.
|
||
| 8 | 909:cbb26bc654de | Chris | #
|
| 9 | 0:513646585e45 | Chris | # This program is distributed in the hope that it will be useful,
|
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
| 12 | # GNU General Public License for more details.
|
||
| 13 | 909:cbb26bc654de | Chris | #
|
| 14 | 0:513646585e45 | Chris | # You should have received a copy of the GNU General Public License
|
| 15 | # along with this program; if not, write to the Free Software
|
||
| 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||
| 17 | |||
| 18 | module Redmine |
||
| 19 | module MenuManager |
||
| 20 | class MenuError < StandardError #:nodoc: |
||
| 21 | end
|
||
| 22 | 909:cbb26bc654de | Chris | |
| 23 | 0:513646585e45 | Chris | module MenuController |
| 24 | def self.included(base) |
||
| 25 | base.extend(ClassMethods)
|
||
| 26 | end
|
||
| 27 | |||
| 28 | module ClassMethods |
||
| 29 | @@menu_items = Hash.new {|hash, key| hash[key] = {:default => key, :actions => {}}} |
||
| 30 | mattr_accessor :menu_items
|
||
| 31 | 909:cbb26bc654de | Chris | |
| 32 | 0:513646585e45 | Chris | # Set the menu item name for a controller or specific actions
|
| 33 | # Examples:
|
||
| 34 | # * menu_item :tickets # => sets the menu name to :tickets for the whole controller
|
||
| 35 | # * menu_item :tickets, :only => :list # => sets the menu name to :tickets for the 'list' action only
|
||
| 36 | # * menu_item :tickets, :only => [:list, :show] # => sets the menu name to :tickets for 2 actions only
|
||
| 37 | 909:cbb26bc654de | Chris | #
|
| 38 | 0:513646585e45 | Chris | # The default menu item name for a controller is controller_name by default
|
| 39 | # Eg. the default menu item name for ProjectsController is :projects
|
||
| 40 | def menu_item(id, options = {}) |
||
| 41 | if actions = options[:only] |
||
| 42 | actions = [] << actions unless actions.is_a?(Array) |
||
| 43 | actions.each {|a| menu_items[controller_name.to_sym][:actions][a.to_sym] = id}
|
||
| 44 | else
|
||
| 45 | menu_items[controller_name.to_sym][:default] = id
|
||
| 46 | end
|
||
| 47 | end
|
||
| 48 | end
|
||
| 49 | 909:cbb26bc654de | Chris | |
| 50 | 0:513646585e45 | Chris | def menu_items |
| 51 | self.class.menu_items
|
||
| 52 | end
|
||
| 53 | 909:cbb26bc654de | Chris | |
| 54 | 0:513646585e45 | Chris | # Returns the menu item name according to the current action
|
| 55 | def current_menu_item |
||
| 56 | @current_menu_item ||= menu_items[controller_name.to_sym][:actions][action_name.to_sym] || |
||
| 57 | menu_items[controller_name.to_sym][:default]
|
||
| 58 | end
|
||
| 59 | 909:cbb26bc654de | Chris | |
| 60 | 0:513646585e45 | Chris | # Redirects user to the menu item of the given project
|
| 61 | # Returns false if user is not authorized
|
||
| 62 | def redirect_to_project_menu_item(project, name) |
||
| 63 | item = Redmine::MenuManager.items(:project_menu).detect {|i| i.name.to_s == name.to_s} |
||
| 64 | if item && User.current.allowed_to?(item.url, project) && (item.condition.nil? || item.condition.call(project)) |
||
| 65 | redirect_to({item.param => project}.merge(item.url))
|
||
| 66 | return true |
||
| 67 | end
|
||
| 68 | false
|
||
| 69 | end
|
||
| 70 | end
|
||
| 71 | 909:cbb26bc654de | Chris | |
| 72 | 0:513646585e45 | Chris | module MenuHelper |
| 73 | # Returns the current menu item name
|
||
| 74 | def current_menu_item |
||
| 75 | 909:cbb26bc654de | Chris | controller.current_menu_item |
| 76 | 0:513646585e45 | Chris | end
|
| 77 | 909:cbb26bc654de | Chris | |
| 78 | 0:513646585e45 | Chris | # Renders the application main menu
|
| 79 | def render_main_menu(project) |
||
| 80 | render_menu((project && !project.new_record?) ? :project_menu : :application_menu, project) |
||
| 81 | end
|
||
| 82 | 909:cbb26bc654de | Chris | |
| 83 | 0:513646585e45 | Chris | def display_main_menu?(project) |
| 84 | menu_name = project && !project.new_record? ? :project_menu : :application_menu |
||
| 85 | 1115:433d4f72a19b | Chris | Redmine::MenuManager.items(menu_name).children.present? |
| 86 | 0:513646585e45 | Chris | end
|
| 87 | |||
| 88 | def render_menu(menu, project=nil) |
||
| 89 | links = [] |
||
| 90 | menu_items_for(menu, project) do |node|
|
||
| 91 | links << render_menu_node(node, project) |
||
| 92 | end
|
||
| 93 | 909:cbb26bc654de | Chris | links.empty? ? nil : content_tag('ul', links.join("\n").html_safe) |
| 94 | 0:513646585e45 | Chris | end
|
| 95 | |||
| 96 | def render_menu_node(node, project=nil) |
||
| 97 | 1115:433d4f72a19b | Chris | if node.children.present? || !node.child_menus.nil?
|
| 98 | 0:513646585e45 | Chris | return render_menu_node_with_children(node, project)
|
| 99 | else
|
||
| 100 | caption, url, selected = extract_node_details(node, project) |
||
| 101 | return content_tag('li', |
||
| 102 | render_single_menu_node(node, caption, url, selected)) |
||
| 103 | end
|
||
| 104 | end
|
||
| 105 | |||
| 106 | def render_menu_node_with_children(node, project=nil) |
||
| 107 | caption, url, selected = extract_node_details(node, project) |
||
| 108 | |||
| 109 | 37:94944d00e43c | chris | html = [].tap do |html|
|
| 110 | 0:513646585e45 | Chris | html << '<li>'
|
| 111 | # Parent
|
||
| 112 | html << render_single_menu_node(node, caption, url, selected) |
||
| 113 | |||
| 114 | # Standard children
|
||
| 115 | 1115:433d4f72a19b | Chris | standard_children_list = "".html_safe.tap do |child_html| |
| 116 | 0:513646585e45 | Chris | node.children.each do |child|
|
| 117 | child_html << render_menu_node(child, project) |
||
| 118 | end
|
||
| 119 | end
|
||
| 120 | |||
| 121 | html << content_tag(:ul, standard_children_list, :class => 'menu-children') unless standard_children_list.empty? |
||
| 122 | |||
| 123 | # Unattached children
|
||
| 124 | unattached_children_list = render_unattached_children_menu(node, project) |
||
| 125 | html << content_tag(:ul, unattached_children_list, :class => 'menu-children unattached') unless unattached_children_list.blank? |
||
| 126 | |||
| 127 | html << '</li>'
|
||
| 128 | end
|
||
| 129 | 1115:433d4f72a19b | Chris | return html.join("\n").html_safe |
| 130 | 0:513646585e45 | Chris | end
|
| 131 | |||
| 132 | # Returns a list of unattached children menu items
|
||
| 133 | def render_unattached_children_menu(node, project) |
||
| 134 | return nil unless node.child_menus |
||
| 135 | |||
| 136 | 1115:433d4f72a19b | Chris | "".html_safe.tap do |child_html| |
| 137 | 0:513646585e45 | Chris | unattached_children = node.child_menus.call(project) |
| 138 | # Tree nodes support #each so we need to do object detection
|
||
| 139 | if unattached_children.is_a? Array |
||
| 140 | unattached_children.each do |child|
|
||
| 141 | 909:cbb26bc654de | Chris | child_html << content_tag(:li, render_unattached_menu_item(child, project))
|
| 142 | 0:513646585e45 | Chris | end
|
| 143 | else
|
||
| 144 | raise MenuError, ":child_menus must be an array of MenuItems" |
||
| 145 | end
|
||
| 146 | end
|
||
| 147 | end
|
||
| 148 | |||
| 149 | def render_single_menu_node(item, caption, url, selected) |
||
| 150 | link_to(h(caption), url, item.html_options(:selected => selected))
|
||
| 151 | end
|
||
| 152 | |||
| 153 | def render_unattached_menu_item(menu_item, project) |
||
| 154 | raise MenuError, ":child_menus must be an array of MenuItems" unless menu_item.is_a? MenuItem |
||
| 155 | |||
| 156 | if User.current.allowed_to?(menu_item.url, project) |
||
| 157 | link_to(h(menu_item.caption), |
||
| 158 | menu_item.url, |
||
| 159 | menu_item.html_options) |
||
| 160 | end
|
||
| 161 | end
|
||
| 162 | 909:cbb26bc654de | Chris | |
| 163 | 0:513646585e45 | Chris | def menu_items_for(menu, project=nil) |
| 164 | items = [] |
||
| 165 | Redmine::MenuManager.items(menu).root.children.each do |node| |
||
| 166 | if allowed_node?(node, User.current, project) |
||
| 167 | if block_given?
|
||
| 168 | yield node
|
||
| 169 | else
|
||
| 170 | items << node # TODO: not used?
|
||
| 171 | end
|
||
| 172 | end
|
||
| 173 | end
|
||
| 174 | return block_given? ? nil : items |
||
| 175 | end
|
||
| 176 | |||
| 177 | def extract_node_details(node, project=nil) |
||
| 178 | item = node |
||
| 179 | url = case item.url
|
||
| 180 | when Hash |
||
| 181 | project.nil? ? item.url : {item.param => project}.merge(item.url)
|
||
| 182 | when Symbol |
||
| 183 | send(item.url) |
||
| 184 | else
|
||
| 185 | item.url |
||
| 186 | end
|
||
| 187 | caption = item.caption(project) |
||
| 188 | return [caption, url, (current_menu_item == item.name)]
|
||
| 189 | end
|
||
| 190 | |||
| 191 | # Checks if a user is allowed to access the menu item by:
|
||
| 192 | #
|
||
| 193 | 1295:622f24f53b42 | Chris | # * Checking the url target (project only)
|
| 194 | 0:513646585e45 | Chris | # * Checking the conditions of the item
|
| 195 | def allowed_node?(node, user, project) |
||
| 196 | 1295:622f24f53b42 | Chris | if project && user && !user.allowed_to?(node.url, project)
|
| 197 | return false |
||
| 198 | end
|
||
| 199 | 0:513646585e45 | Chris | if node.condition && !node.condition.call(project)
|
| 200 | # Condition that doesn't pass
|
||
| 201 | return false |
||
| 202 | end
|
||
| 203 | 1295:622f24f53b42 | Chris | return true |
| 204 | 0:513646585e45 | Chris | end
|
| 205 | end
|
||
| 206 | 909:cbb26bc654de | Chris | |
| 207 | 0:513646585e45 | Chris | class << self |
| 208 | def map(menu_name) |
||
| 209 | @items ||= {}
|
||
| 210 | mapper = Mapper.new(menu_name.to_sym, @items) |
||
| 211 | if block_given?
|
||
| 212 | yield mapper
|
||
| 213 | else
|
||
| 214 | mapper |
||
| 215 | end
|
||
| 216 | end
|
||
| 217 | 909:cbb26bc654de | Chris | |
| 218 | 0:513646585e45 | Chris | def items(menu_name) |
| 219 | 1115:433d4f72a19b | Chris | @items[menu_name.to_sym] || MenuNode.new(:root, {}) |
| 220 | 0:513646585e45 | Chris | end
|
| 221 | end
|
||
| 222 | 909:cbb26bc654de | Chris | |
| 223 | 0:513646585e45 | Chris | class Mapper |
| 224 | def initialize(menu, items) |
||
| 225 | 1115:433d4f72a19b | Chris | items[menu] ||= MenuNode.new(:root, {}) |
| 226 | 0:513646585e45 | Chris | @menu = menu
|
| 227 | @menu_items = items[menu]
|
||
| 228 | end
|
||
| 229 | 909:cbb26bc654de | Chris | |
| 230 | 0:513646585e45 | Chris | # Adds an item at the end of the menu. Available options:
|
| 231 | # * param: the parameter name that is used for the project id (default is :id)
|
||
| 232 | # * if: a Proc that is called before rendering the item, the item is displayed only if it returns true
|
||
| 233 | # * caption that can be:
|
||
| 234 | # * a localized string Symbol
|
||
| 235 | # * a String
|
||
| 236 | # * a Proc that can take the project as argument
|
||
| 237 | # * before, after: specify where the menu item should be inserted (eg. :after => :activity)
|
||
| 238 | # * parent: menu item will be added as a child of another named menu (eg. :parent => :issues)
|
||
| 239 | # * 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.
|
||
| 240 | # eg. :children => Proc.new {|project| [Redmine::MenuManager::MenuItem.new(...)] }
|
||
| 241 | # * last: menu item will stay at the end (eg. :last => true)
|
||
| 242 | # * html_options: a hash of html options that are passed to link_to
|
||
| 243 | def push(name, url, options={}) |
||
| 244 | options = options.dup |
||
| 245 | |||
| 246 | if options[:parent] |
||
| 247 | subtree = self.find(options[:parent]) |
||
| 248 | if subtree
|
||
| 249 | target_root = subtree |
||
| 250 | else
|
||
| 251 | target_root = @menu_items.root
|
||
| 252 | end
|
||
| 253 | |||
| 254 | else
|
||
| 255 | target_root = @menu_items.root
|
||
| 256 | end
|
||
| 257 | |||
| 258 | # menu item position
|
||
| 259 | if first = options.delete(:first) |
||
| 260 | target_root.prepend(MenuItem.new(name, url, options))
|
||
| 261 | elsif before = options.delete(:before) |
||
| 262 | |||
| 263 | if exists?(before)
|
||
| 264 | target_root.add_at(MenuItem.new(name, url, options), position_of(before))
|
||
| 265 | else
|
||
| 266 | target_root.add(MenuItem.new(name, url, options))
|
||
| 267 | end
|
||
| 268 | |||
| 269 | elsif after = options.delete(:after) |
||
| 270 | |||
| 271 | if exists?(after)
|
||
| 272 | target_root.add_at(MenuItem.new(name, url, options), position_of(after) + 1) |
||
| 273 | else
|
||
| 274 | target_root.add(MenuItem.new(name, url, options))
|
||
| 275 | end
|
||
| 276 | 909:cbb26bc654de | Chris | |
| 277 | 0:513646585e45 | Chris | elsif options[:last] # don't delete, needs to be stored |
| 278 | target_root.add_last(MenuItem.new(name, url, options))
|
||
| 279 | else
|
||
| 280 | target_root.add(MenuItem.new(name, url, options))
|
||
| 281 | end
|
||
| 282 | end
|
||
| 283 | 909:cbb26bc654de | Chris | |
| 284 | 0:513646585e45 | Chris | # Removes a menu item
|
| 285 | def delete(name) |
||
| 286 | if found = self.find(name) |
||
| 287 | @menu_items.remove!(found)
|
||
| 288 | end
|
||
| 289 | end
|
||
| 290 | |||
| 291 | # Checks if a menu item exists
|
||
| 292 | def exists?(name) |
||
| 293 | @menu_items.any? {|node| node.name == name}
|
||
| 294 | end
|
||
| 295 | |||
| 296 | def find(name) |
||
| 297 | @menu_items.find {|node| node.name == name}
|
||
| 298 | end
|
||
| 299 | |||
| 300 | def position_of(name) |
||
| 301 | @menu_items.each do |node| |
||
| 302 | if node.name == name
|
||
| 303 | return node.position
|
||
| 304 | end
|
||
| 305 | end
|
||
| 306 | end
|
||
| 307 | end
|
||
| 308 | 909:cbb26bc654de | Chris | |
| 309 | 1115:433d4f72a19b | Chris | class MenuNode |
| 310 | include Enumerable
|
||
| 311 | attr_accessor :parent
|
||
| 312 | attr_reader :last_items_count, :name |
||
| 313 | |||
| 314 | def initialize(name, content = nil) |
||
| 315 | @name = name
|
||
| 316 | @children = []
|
||
| 317 | @last_items_count = 0 |
||
| 318 | end
|
||
| 319 | |||
| 320 | def children |
||
| 321 | if block_given?
|
||
| 322 | @children.each {|child| yield child} |
||
| 323 | else
|
||
| 324 | @children
|
||
| 325 | end
|
||
| 326 | end
|
||
| 327 | |||
| 328 | # Returns the number of descendants + 1
|
||
| 329 | def size |
||
| 330 | @children.inject(1) {|sum, node| sum + node.size} |
||
| 331 | end
|
||
| 332 | |||
| 333 | def each &block |
||
| 334 | yield self |
||
| 335 | children { |child| child.each(&block) }
|
||
| 336 | end
|
||
| 337 | |||
| 338 | # Adds a child at first position
|
||
| 339 | def prepend(child) |
||
| 340 | add_at(child, 0)
|
||
| 341 | end
|
||
| 342 | |||
| 343 | # Adds a child at given position
|
||
| 344 | def add_at(child, position) |
||
| 345 | raise "Child already added" if find {|node| node.name == child.name} |
||
| 346 | |||
| 347 | @children = @children.insert(position, child) |
||
| 348 | child.parent = self
|
||
| 349 | child |
||
| 350 | end
|
||
| 351 | |||
| 352 | # Adds a child as last child
|
||
| 353 | def add_last(child) |
||
| 354 | add_at(child, -1)
|
||
| 355 | @last_items_count += 1 |
||
| 356 | child |
||
| 357 | end
|
||
| 358 | |||
| 359 | # Adds a child
|
||
| 360 | def add(child) |
||
| 361 | position = @children.size - @last_items_count |
||
| 362 | add_at(child, position) |
||
| 363 | end
|
||
| 364 | alias :<< :add |
||
| 365 | |||
| 366 | # Removes a child
|
||
| 367 | def remove!(child) |
||
| 368 | @children.delete(child)
|
||
| 369 | @last_items_count -= +1 if child && child.last |
||
| 370 | child.parent = nil
|
||
| 371 | child |
||
| 372 | end
|
||
| 373 | |||
| 374 | # Returns the position for this node in it's parent
|
||
| 375 | def position |
||
| 376 | self.parent.children.index(self) |
||
| 377 | end
|
||
| 378 | |||
| 379 | # Returns the root for this node
|
||
| 380 | def root |
||
| 381 | root = self
|
||
| 382 | root = root.parent while root.parent
|
||
| 383 | root |
||
| 384 | end
|
||
| 385 | end
|
||
| 386 | |||
| 387 | class MenuItem < MenuNode |
||
| 388 | 0:513646585e45 | Chris | include Redmine::I18n |
| 389 | attr_reader :name, :url, :param, :condition, :parent, :child_menus, :last |
||
| 390 | 909:cbb26bc654de | Chris | |
| 391 | 0:513646585e45 | Chris | def initialize(name, url, options) |
| 392 | raise ArgumentError, "Invalid option :if for menu item '#{name}'" if options[:if] && !options[:if].respond_to?(:call) |
||
| 393 | raise ArgumentError, "Invalid option :html for menu item '#{name}'" if options[:html] && !options[:html].is_a?(Hash) |
||
| 394 | raise ArgumentError, "Cannot set the :parent to be the same as this item" if options[:parent] == name.to_sym |
||
| 395 | raise ArgumentError, "Invalid option :children for menu item '#{name}'" if options[:children] && !options[:children].respond_to?(:call) |
||
| 396 | @name = name
|
||
| 397 | @url = url
|
||
| 398 | @condition = options[:if] |
||
| 399 | @param = options[:param] || :id |
||
| 400 | @caption = options[:caption] |
||
| 401 | @html_options = options[:html] || {} |
||
| 402 | # Adds a unique class to each menu item based on its name
|
||
| 403 | @html_options[:class] = [@html_options[:class], @name.to_s.dasherize].compact.join(' ') |
||
| 404 | @parent = options[:parent] |
||
| 405 | @child_menus = options[:children] |
||
| 406 | @last = options[:last] || false |
||
| 407 | super @name.to_sym |
||
| 408 | end
|
||
| 409 | 909:cbb26bc654de | Chris | |
| 410 | 0:513646585e45 | Chris | def caption(project=nil) |
| 411 | if @caption.is_a?(Proc) |
||
| 412 | c = @caption.call(project).to_s
|
||
| 413 | c = @name.to_s.humanize if c.blank? |
||
| 414 | c |
||
| 415 | else
|
||
| 416 | if @caption.nil? |
||
| 417 | l_or_humanize(name, :prefix => 'label_') |
||
| 418 | else
|
||
| 419 | @caption.is_a?(Symbol) ? l(@caption) : @caption |
||
| 420 | end
|
||
| 421 | end
|
||
| 422 | end
|
||
| 423 | 909:cbb26bc654de | Chris | |
| 424 | 0:513646585e45 | Chris | def html_options(options={}) |
| 425 | if options[:selected] |
||
| 426 | o = @html_options.dup
|
||
| 427 | o[:class] += ' selected' |
||
| 428 | o |
||
| 429 | else
|
||
| 430 | @html_options
|
||
| 431 | end
|
||
| 432 | end
|
||
| 433 | 909:cbb26bc654de | Chris | end
|
| 434 | 0:513646585e45 | Chris | end
|
| 435 | end |