annotate .svn/pristine/16/163200e35f1d8c0484dc12a3ebb2256afa7733a8.svn-base @ 1494:e248c7af89ec redmine-2.4

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