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