##// END OF EJS Templates
Hide the main menu div if there isn't any items for it. #3259...
Eric Davis -
r3425:7514e12d331b
parent child
Show More
@@ -1,74 +1,76
1 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2 2 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
3 3 <head>
4 4 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
5 5 <title><%=h html_title %></title>
6 6 <meta name="description" content="<%= Redmine::Info.app_name %>" />
7 7 <meta name="keywords" content="issue,bug,tracker" />
8 8 <%= stylesheet_link_tag 'application', :media => 'all' %>
9 9 <%= javascript_include_tag :defaults %>
10 10 <%= heads_for_wiki_formatter %>
11 11 <!--[if IE]>
12 12 <style type="text/css">
13 13 * html body{ width: expression( document.documentElement.clientWidth < 900 ? '900px' : '100%' ); }
14 14 body {behavior: url(<%= stylesheet_path "csshover.htc" %>);}
15 15 </style>
16 16 <![endif]-->
17 17 <%= call_hook :view_layouts_base_html_head %>
18 18 <!-- page specific tags -->
19 19 <%= yield :header_tags -%>
20 20 </head>
21 21 <body>
22 22 <div id="wrapper">
23 23 <div id="wrapper2">
24 24 <div id="top-menu">
25 25 <div id="account">
26 26 <%= render_menu :account_menu -%>
27 27 </div>
28 28 <%= content_tag('div', "#{l(:label_logged_as)} #{link_to_user(User.current, :format => :username)}", :id => 'loggedas') if User.current.logged? %>
29 29 <%= render_menu :top_menu -%>
30 30 </div>
31 31
32 32 <div id="header">
33 33 <div id="quick-search">
34 34 <% form_tag({:controller => 'search', :action => 'index', :id => @project}, :method => :get ) do %>
35 35 <%= hidden_field_tag(controller.default_search_scope, 1, :id => nil) if controller.default_search_scope %>
36 36 <%= link_to l(:label_search), {:controller => 'search', :action => 'index', :id => @project}, :accesskey => accesskey(:search) %>:
37 37 <%= text_field_tag 'q', @question, :size => 20, :class => 'small', :accesskey => accesskey(:quick_search) %>
38 38 <% end %>
39 39 <%= render_project_jump_box %>
40 40 </div>
41 41
42 42 <h1><%= page_header_title %></h1>
43 43
44 <% if display_main_menu?(@project) %>
44 45 <div id="main-menu">
45 46 <%= render_main_menu(@project) %>
46 47 </div>
48 <% end %>
47 49 </div>
48 50
49 51 <%= tag('div', {:id => 'main', :class => (has_content?(:sidebar) ? '' : 'nosidebar')}, true) %>
50 52 <div id="sidebar">
51 53 <%= yield :sidebar %>
52 54 <%= call_hook :view_layouts_base_sidebar %>
53 55 </div>
54 56
55 57 <div id="content">
56 58 <%= render_flash_messages %>
57 59 <%= yield %>
58 60 <%= call_hook :view_layouts_base_content %>
59 61 <div style="clear:both;"></div>
60 62 </div>
61 63 </div>
62 64
63 65 <div id="ajax-indicator" style="display:none;"><span><%= l(:label_loading) %></span></div>
64 66
65 67 <div id="footer">
66 68 <div class="bgl"><div class="bgr">
67 69 Powered by <%= link_to Redmine::Info.app_name, Redmine::Info.url %> &copy; 2006-2010 Jean-Philippe Lang
68 70 </div></div>
69 71 </div>
70 72 </div>
71 73 </div>
72 74 <%= call_hook :view_layouts_base_body_bottom %>
73 75 </body>
74 76 </html>
@@ -1,443 +1,448
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 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 @last_items_count = 0
30 30 extend(InstanceMethods)
31 31 end
32 32 end
33 33 end
34 34
35 35 module InstanceMethods
36 36 # Adds the specified child node to the receiver node. The child node's
37 37 # parent is set to be the receiver. The child is added as the first child in
38 38 # the current list of children for the receiver node.
39 39 def prepend(child)
40 40 raise "Child already added" if @childrenHash.has_key?(child.name)
41 41
42 42 @childrenHash[child.name] = child
43 43 @children = [child] + @children
44 44 child.parent = self
45 45 return child
46 46
47 47 end
48 48
49 49 # Adds the specified child node to the receiver node. The child node's
50 50 # parent is set to be the receiver. The child is added at the position
51 51 # into the current list of children for the receiver node.
52 52 def add_at(child, position)
53 53 raise "Child already added" if @childrenHash.has_key?(child.name)
54 54
55 55 @childrenHash[child.name] = child
56 56 @children = @children.insert(position, child)
57 57 child.parent = self
58 58 return child
59 59
60 60 end
61 61
62 62 def add_last(child)
63 63 raise "Child already added" if @childrenHash.has_key?(child.name)
64 64
65 65 @childrenHash[child.name] = child
66 66 @children << child
67 67 @last_items_count += 1
68 68 child.parent = self
69 69 return child
70 70
71 71 end
72 72
73 73 # Adds the specified child node to the receiver node. The child node's
74 74 # parent is set to be the receiver. The child is added as the last child in
75 75 # the current list of children for the receiver node.
76 76 def add(child)
77 77 raise "Child already added" if @childrenHash.has_key?(child.name)
78 78
79 79 @childrenHash[child.name] = child
80 80 position = @children.size - @last_items_count
81 81 @children.insert(position, child)
82 82 child.parent = self
83 83 return child
84 84
85 85 end
86 86
87 87 # Wrapp remove! making sure to decrement the last_items counter if
88 88 # the removed child was a last item
89 89 def remove!(child)
90 90 @last_items_count -= +1 if child && child.last
91 91 super
92 92 end
93 93
94 94
95 95 # Will return the position (zero-based) of the current child in
96 96 # it's parent
97 97 def position
98 98 self.parent.children.index(self)
99 99 end
100 100 end
101 101 end
102 102 Tree::TreeNode.send(:include, TreeNodePatch)
103 103
104 104 module Redmine
105 105 module MenuManager
106 106 class MenuError < StandardError #:nodoc:
107 107 end
108 108
109 109 module MenuController
110 110 def self.included(base)
111 111 base.extend(ClassMethods)
112 112 end
113 113
114 114 module ClassMethods
115 115 @@menu_items = Hash.new {|hash, key| hash[key] = {:default => key, :actions => {}}}
116 116 mattr_accessor :menu_items
117 117
118 118 # Set the menu item name for a controller or specific actions
119 119 # Examples:
120 120 # * menu_item :tickets # => sets the menu name to :tickets for the whole controller
121 121 # * menu_item :tickets, :only => :list # => sets the menu name to :tickets for the 'list' action only
122 122 # * menu_item :tickets, :only => [:list, :show] # => sets the menu name to :tickets for 2 actions only
123 123 #
124 124 # The default menu item name for a controller is controller_name by default
125 125 # Eg. the default menu item name for ProjectsController is :projects
126 126 def menu_item(id, options = {})
127 127 if actions = options[:only]
128 128 actions = [] << actions unless actions.is_a?(Array)
129 129 actions.each {|a| menu_items[controller_name.to_sym][:actions][a.to_sym] = id}
130 130 else
131 131 menu_items[controller_name.to_sym][:default] = id
132 132 end
133 133 end
134 134 end
135 135
136 136 def menu_items
137 137 self.class.menu_items
138 138 end
139 139
140 140 # Returns the menu item name according to the current action
141 141 def current_menu_item
142 142 @current_menu_item ||= menu_items[controller_name.to_sym][:actions][action_name.to_sym] ||
143 143 menu_items[controller_name.to_sym][:default]
144 144 end
145 145
146 146 # Redirects user to the menu item of the given project
147 147 # Returns false if user is not authorized
148 148 def redirect_to_project_menu_item(project, name)
149 149 item = Redmine::MenuManager.items(:project_menu).detect {|i| i.name.to_s == name.to_s}
150 150 if item && User.current.allowed_to?(item.url, project) && (item.condition.nil? || item.condition.call(project))
151 151 redirect_to({item.param => project}.merge(item.url))
152 152 return true
153 153 end
154 154 false
155 155 end
156 156 end
157 157
158 158 module MenuHelper
159 159 # Returns the current menu item name
160 160 def current_menu_item
161 161 @controller.current_menu_item
162 162 end
163 163
164 164 # Renders the application main menu
165 165 def render_main_menu(project)
166 166 render_menu((project && !project.new_record?) ? :project_menu : :application_menu, project)
167 167 end
168 168
169 def display_main_menu?(project)
170 menu_name = project && !project.new_record? ? :project_menu : :application_menu
171 Redmine::MenuManager.items(menu_name).size > 1 # 1 element is the root
172 end
173
169 174 def render_menu(menu, project=nil)
170 175 links = []
171 176 menu_items_for(menu, project) do |node|
172 177 links << render_menu_node(node, project)
173 178 end
174 179 links.empty? ? nil : content_tag('ul', links.join("\n"))
175 180 end
176 181
177 182 def render_menu_node(node, project=nil)
178 183 if node.hasChildren? || !node.child_menus.nil?
179 184 return render_menu_node_with_children(node, project)
180 185 else
181 186 caption, url, selected = extract_node_details(node, project)
182 187 return content_tag('li',
183 188 render_single_menu_node(node, caption, url, selected))
184 189 end
185 190 end
186 191
187 192 def render_menu_node_with_children(node, project=nil)
188 193 caption, url, selected = extract_node_details(node, project)
189 194
190 195 html = returning [] do |html|
191 196 html << '<li>'
192 197 # Parent
193 198 html << render_single_menu_node(node, caption, url, selected)
194 199
195 200 # Standard children
196 201 standard_children_list = returning "" do |child_html|
197 202 node.children.each do |child|
198 203 child_html << render_menu_node(child, project)
199 204 end
200 205 end
201 206
202 207 html << content_tag(:ul, standard_children_list, :class => 'menu-children') unless standard_children_list.empty?
203 208
204 209 # Unattached children
205 210 unattached_children_list = render_unattached_children_menu(node, project)
206 211 html << content_tag(:ul, unattached_children_list, :class => 'menu-children unattached') unless unattached_children_list.blank?
207 212
208 213 html << '</li>'
209 214 end
210 215 return html.join("\n")
211 216 end
212 217
213 218 # Returns a list of unattached children menu items
214 219 def render_unattached_children_menu(node, project)
215 220 return nil unless node.child_menus
216 221
217 222 returning "" do |child_html|
218 223 unattached_children = node.child_menus.call(project)
219 224 # Tree nodes support #each so we need to do object detection
220 225 if unattached_children.is_a? Array
221 226 unattached_children.each do |child|
222 227 child_html << content_tag(:li, render_unattached_menu_item(child, project))
223 228 end
224 229 else
225 230 raise MenuError, ":child_menus must be an array of MenuItems"
226 231 end
227 232 end
228 233 end
229 234
230 235 def render_single_menu_node(item, caption, url, selected)
231 236 link_to(h(caption), url, item.html_options(:selected => selected))
232 237 end
233 238
234 239 def render_unattached_menu_item(menu_item, project)
235 240 raise MenuError, ":child_menus must be an array of MenuItems" unless menu_item.is_a? MenuItem
236 241
237 242 if User.current.allowed_to?(menu_item.url, project)
238 243 link_to(h(menu_item.caption),
239 244 menu_item.url,
240 245 menu_item.html_options)
241 246 end
242 247 end
243 248
244 249 def menu_items_for(menu, project=nil)
245 250 items = []
246 251 Redmine::MenuManager.items(menu).root.children.each do |node|
247 252 if allowed_node?(node, User.current, project)
248 253 if block_given?
249 254 yield node
250 255 else
251 256 items << node # TODO: not used?
252 257 end
253 258 end
254 259 end
255 260 return block_given? ? nil : items
256 261 end
257 262
258 263 def extract_node_details(node, project=nil)
259 264 item = node
260 265 url = case item.url
261 266 when Hash
262 267 project.nil? ? item.url : {item.param => project}.merge(item.url)
263 268 when Symbol
264 269 send(item.url)
265 270 else
266 271 item.url
267 272 end
268 273 caption = item.caption(project)
269 274 return [caption, url, (current_menu_item == item.name)]
270 275 end
271 276
272 277 # Checks if a user is allowed to access the menu item by:
273 278 #
274 279 # * Checking the conditions of the item
275 280 # * Checking the url target (project only)
276 281 def allowed_node?(node, user, project)
277 282 if node.condition && !node.condition.call(project)
278 283 # Condition that doesn't pass
279 284 return false
280 285 end
281 286
282 287 if project
283 288 return user && user.allowed_to?(node.url, project)
284 289 else
285 290 # outside a project, all menu items allowed
286 291 return true
287 292 end
288 293 end
289 294 end
290 295
291 296 class << self
292 297 def map(menu_name)
293 298 @items ||= {}
294 299 mapper = Mapper.new(menu_name.to_sym, @items)
295 300 if block_given?
296 301 yield mapper
297 302 else
298 303 mapper
299 304 end
300 305 end
301 306
302 307 def items(menu_name)
303 308 @items[menu_name.to_sym] || Tree::TreeNode.new(:root, {})
304 309 end
305 310 end
306 311
307 312 class Mapper
308 313 def initialize(menu, items)
309 314 items[menu] ||= Tree::TreeNode.new(:root, {})
310 315 @menu = menu
311 316 @menu_items = items[menu]
312 317 end
313 318
314 319 @@last_items_count = Hash.new {|h,k| h[k] = 0}
315 320
316 321 # Adds an item at the end of the menu. Available options:
317 322 # * param: the parameter name that is used for the project id (default is :id)
318 323 # * if: a Proc that is called before rendering the item, the item is displayed only if it returns true
319 324 # * caption that can be:
320 325 # * a localized string Symbol
321 326 # * a String
322 327 # * a Proc that can take the project as argument
323 328 # * before, after: specify where the menu item should be inserted (eg. :after => :activity)
324 329 # * parent: menu item will be added as a child of another named menu (eg. :parent => :issues)
325 330 # * 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.
326 331 # eg. :children => Proc.new {|project| [Redmine::MenuManager::MenuItem.new(...)] }
327 332 # * last: menu item will stay at the end (eg. :last => true)
328 333 # * html_options: a hash of html options that are passed to link_to
329 334 def push(name, url, options={})
330 335 options = options.dup
331 336
332 337 if options[:parent]
333 338 subtree = self.find(options[:parent])
334 339 if subtree
335 340 target_root = subtree
336 341 else
337 342 target_root = @menu_items.root
338 343 end
339 344
340 345 else
341 346 target_root = @menu_items.root
342 347 end
343 348
344 349 # menu item position
345 350 if first = options.delete(:first)
346 351 target_root.prepend(MenuItem.new(name, url, options))
347 352 elsif before = options.delete(:before)
348 353
349 354 if exists?(before)
350 355 target_root.add_at(MenuItem.new(name, url, options), position_of(before))
351 356 else
352 357 target_root.add(MenuItem.new(name, url, options))
353 358 end
354 359
355 360 elsif after = options.delete(:after)
356 361
357 362 if exists?(after)
358 363 target_root.add_at(MenuItem.new(name, url, options), position_of(after) + 1)
359 364 else
360 365 target_root.add(MenuItem.new(name, url, options))
361 366 end
362 367
363 368 elsif options[:last] # don't delete, needs to be stored
364 369 target_root.add_last(MenuItem.new(name, url, options))
365 370 else
366 371 target_root.add(MenuItem.new(name, url, options))
367 372 end
368 373 end
369 374
370 375 # Removes a menu item
371 376 def delete(name)
372 377 if found = self.find(name)
373 378 @menu_items.remove!(found)
374 379 end
375 380 end
376 381
377 382 # Checks if a menu item exists
378 383 def exists?(name)
379 384 @menu_items.any? {|node| node.name == name}
380 385 end
381 386
382 387 def find(name)
383 388 @menu_items.find {|node| node.name == name}
384 389 end
385 390
386 391 def position_of(name)
387 392 @menu_items.each do |node|
388 393 if node.name == name
389 394 return node.position
390 395 end
391 396 end
392 397 end
393 398 end
394 399
395 400 class MenuItem < Tree::TreeNode
396 401 include Redmine::I18n
397 402 attr_reader :name, :url, :param, :condition, :parent, :child_menus, :last
398 403
399 404 def initialize(name, url, options)
400 405 raise ArgumentError, "Invalid option :if for menu item '#{name}'" if options[:if] && !options[:if].respond_to?(:call)
401 406 raise ArgumentError, "Invalid option :html for menu item '#{name}'" if options[:html] && !options[:html].is_a?(Hash)
402 407 raise ArgumentError, "Cannot set the :parent to be the same as this item" if options[:parent] == name.to_sym
403 408 raise ArgumentError, "Invalid option :children for menu item '#{name}'" if options[:children] && !options[:children].respond_to?(:call)
404 409 @name = name
405 410 @url = url
406 411 @condition = options[:if]
407 412 @param = options[:param] || :id
408 413 @caption = options[:caption]
409 414 @html_options = options[:html] || {}
410 415 # Adds a unique class to each menu item based on its name
411 416 @html_options[:class] = [@html_options[:class], @name.to_s.dasherize].compact.join(' ')
412 417 @parent = options[:parent]
413 418 @child_menus = options[:children]
414 419 @last = options[:last] || false
415 420 super @name.to_sym
416 421 end
417 422
418 423 def caption(project=nil)
419 424 if @caption.is_a?(Proc)
420 425 c = @caption.call(project).to_s
421 426 c = @name.to_s.humanize if c.blank?
422 427 c
423 428 else
424 429 if @caption.nil?
425 430 l_or_humanize(name, :prefix => 'label_')
426 431 else
427 432 @caption.is_a?(Symbol) ? l(@caption) : @caption
428 433 end
429 434 end
430 435 end
431 436
432 437 def html_options(options={})
433 438 if options[:selected]
434 439 o = @html_options.dup
435 440 o[:class] += ' selected'
436 441 o
437 442 else
438 443 @html_options
439 444 end
440 445 end
441 446 end
442 447 end
443 448 end
General Comments 0
You need to be logged in to leave comments. Login now