annotate .svn/pristine/69/69bc5c1c53ea4e9d8ad8c537b1338f42510d7272.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

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