Class: Redmine::MenuManager::Mapper
- Inherits:
-
Object
- Object
- Redmine::MenuManager::Mapper
- Defined in:
- lib/redmine/menu_manager.rb
Constant Summary collapse
- @@last_items_count =
Hash.new {|h,k| h[k] = 0}
Instance Method Summary collapse
-
#delete(name) ⇒ Object
Removes a menu item.
-
#exists?(name) ⇒ Boolean
Checks if a menu item exists.
- #find(name) ⇒ Object
-
#initialize(menu, items) ⇒ Mapper
constructor
A new instance of Mapper.
- #position_of(name) ⇒ Object
-
#push(name, url, options = {}) ⇒ Object
Adds an item at the end of the menu.
Constructor Details
#initialize(menu, items) ⇒ Mapper
Returns a new instance of Mapper.
313 314 315 316 317 |
# File 'lib/redmine/menu_manager.rb', line 313 def initialize(, items) items[] ||= Tree::TreeNode.new(:root, {}) @menu = @menu_items = items[] end |
Instance Method Details
#delete(name) ⇒ Object
Removes a menu item
376 377 378 379 380 |
# File 'lib/redmine/menu_manager.rb', line 376 def delete(name) if found = self.find(name) @menu_items.remove!(found) end end |
#exists?(name) ⇒ Boolean
Checks if a menu item exists
383 384 385 |
# File 'lib/redmine/menu_manager.rb', line 383 def exists?(name) @menu_items.any? {|node| node.name == name} end |
#find(name) ⇒ Object
387 388 389 |
# File 'lib/redmine/menu_manager.rb', line 387 def find(name) @menu_items.find {|node| node.name == name} end |
#position_of(name) ⇒ Object
391 392 393 394 395 396 397 |
# File 'lib/redmine/menu_manager.rb', line 391 def position_of(name) @menu_items.each do |node| if node.name == name return node.position end end end |
#push(name, url, options = {}) ⇒ Object
Adds an item at the end of the menu. Available options:
-
param: the parameter name that is used for the project id (default is :id)
-
if: a Proc that is called before rendering the item, the item is displayed only if it returns true
-
caption that can be:
-
a localized string Symbol
-
a String
-
a Proc that can take the project as argument
-
-
before, after: specify where the menu item should be inserted (eg. :after => :activity)
-
parent: menu item will be added as a child of another named menu (eg. :parent => :issues)
-
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. eg. :children => Proc.new {|project| [Redmine::MenuManager::MenuItem.new(…)] }
-
last: menu item will stay at the end (eg. :last => true)
-
html_options: a hash of html options that are passed to link_to
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 |
# File 'lib/redmine/menu_manager.rb', line 334 def push(name, url, ={}) = .dup if [:parent] subtree = self.find([:parent]) if subtree target_root = subtree else target_root = @menu_items.root end else target_root = @menu_items.root end # menu item position if first = .delete(:first) target_root.prepend(MenuItem.new(name, url, )) elsif before = .delete(:before) if exists?(before) target_root.add_at(MenuItem.new(name, url, ), position_of(before)) else target_root.add(MenuItem.new(name, url, )) end elsif after = .delete(:after) if exists?(after) target_root.add_at(MenuItem.new(name, url, ), position_of(after) + 1) else target_root.add(MenuItem.new(name, url, )) end elsif [:last] # don't delete, needs to be stored target_root.add_last(MenuItem.new(name, url, )) else target_root.add(MenuItem.new(name, url, )) end end |