| | 409 | |
| | 410 | == Manipulating Menus == |
| | 411 | |
| | 412 | You can use the call-method later to append more items: |
| | 413 | {{{ |
| | 414 | # Append another item to the menu |
| | 415 | my_menu(MyMenuLayout("Third Item")) |
| | 416 | }}} |
| | 417 | |
| | 418 | If you want to insert an item at a particular position, use the insert method: |
| | 419 | |
| | 420 | {{{ |
| | 421 | my_menu.insert(0, MyMenuLayout("New First Item")) |
| | 422 | }}} |
| | 423 | |
| | 424 | You can also remove items from a certain position: |
| | 425 | |
| | 426 | {{{ |
| | 427 | my_menu.pop(0) # Remove "New First Item" again |
| | 428 | }}} |
| | 429 | |
| | 430 | Note that every item can only ever belong to exactly one parent item. If you append or insert the same item to a different parent item, it will automatically be removed from the original parent: |
| | 431 | |
| | 432 | {{{ |
| | 433 | # CLI example |
| | 434 | >>> menu_1 = M("Menu 1") # define menu 1 |
| | 435 | >>> menu_2 = M("Menu 2") # define menu 2 |
| | 436 | >>> item_1 = M("Item 1") # define a menu item |
| | 437 | >>> menu_1.append(item_1) # append the menu item to menu 1 |
| | 438 | <S3OptionsMenuLayout:Menu 1 {<S3OptionsMenuLayout:Item 1>}> |
| | 439 | >>> menu_2 |
| | 440 | <S3OptionsMenuLayout:Menu 2> |
| | 441 | >>> menu_2.append(item_1) # append the menu item to menu 2 |
| | 442 | <S3OptionsMenuLayout:Menu 2 {<S3OptionsMenuLayout:Item 1>}> |
| | 443 | >>> menu_1 |
| | 444 | <S3OptionsMenuLayout:Menu 1> # => menu item no longer component of menu 1 |
| | 445 | }}} |
| | 446 | |
| | 447 | Thus, if you want to re-use a sequence of menu items, make their definition a function and call it at each place you need the sequence. |
| | 448 | |
| | 449 | NB: at the CLI, you can also see the item rendered as HTML, by calling: |
| | 450 | |
| | 451 | {{{ |
| | 452 | >>> menu_2.xml() |
| | 453 | '<ul id="subnav"><li><div class="hoverable"><a href="/vita/default/index">Item 1</a></div></li><li><div class="hoverable"><a href="/vita/default/index">Item 1</a></div></li></ul>' |
| | 454 | }}} |
| | 455 | |
| | 456 | Note that this performs status checks, i.e.: |
| | 457 | {{{ |
| | 458 | >>> menu_3 = M("Menu 3", c="pr") |
| | 459 | >>> menu_3.xml() |
| | 460 | '' # <- CLI has controller="default", function="index", i.e. this menu item is inactive |
| | 461 | }}} |
| | 462 | |
| | 463 | It does also perform authorization checks, i.e. |
| | 464 | {{{ |
| | 465 | >>> current.request.controller = "pr" |
| | 466 | >>> menu_3 = M("Menu 3", c="pr") |
| | 467 | >>> menu_3.xml() |
| | 468 | '' # <- Item is active, but unauthorized |
| | 469 | >>> menu_3.check_permission() |
| | 470 | False |
| | 471 | >>> auth.override = True |
| | 472 | >>> menu_3.xml() |
| | 473 | '<ul id="subnav"></ul>' # <- Now we're allowed to access this item |
| | 474 | >>> auth.override = False |
| | 475 | >>> menu_3.xml() |
| | 476 | '' |
| | 477 | }}} |