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