annotate .svn/pristine/59/594645e33ca990837dd2f730162a5f369b707902.svn-base @ 1327:287f201c2802 redmine-2.2-integration

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