app/soc/logic/menu.py
changeset 517 661ab830e921
parent 516 ec1dcd70b97e
child 518 d9d31d316a74
equal deleted inserted replaced
516:ec1dcd70b97e 517:661ab830e921
     1 #!/usr/bin/python2.5
       
     2 #
       
     3 # Copyright 2008 the Melange authors.
       
     4 #
       
     5 # Licensed under the Apache License, Version 2.0 (the "License");
       
     6 # you may not use this file except in compliance with the License.
       
     7 # You may obtain a copy of the License at
       
     8 #
       
     9 #   http://www.apache.org/licenses/LICENSE-2.0
       
    10 #
       
    11 # Unless required by applicable law or agreed to in writing, software
       
    12 # distributed under the License is distributed on an "AS IS" BASIS,
       
    13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    14 # See the License for the specific language governing permissions and
       
    15 # limitations under the License.
       
    16 
       
    17 """Representations and manipulation of arbitrarily nested menus.
       
    18 """
       
    19 
       
    20 __authors__ = [
       
    21   '"Todd Larsen" <tlarsen@google.com>',
       
    22   ]
       
    23 
       
    24 
       
    25 from django.utils import datastructures
       
    26 
       
    27 
       
    28 class Menu:
       
    29   """Ordered collection of MenuItem objects.
       
    30   
       
    31   MenuItems are retrievable as an ordered list or individually by their
       
    32   MenuItem.text key.
       
    33   """
       
    34   
       
    35   def __init__(self, items=None):
       
    36     """Initializes ordered list of MenuItems.
       
    37     
       
    38     Args:
       
    39       items: list of MenuItem objects in display order
       
    40     """
       
    41     if not items:
       
    42       items = []
       
    43     
       
    44     items = [(i.name, i) for i in items]
       
    45     self._items = datastructures.SortedDict(data=items)
       
    46 
       
    47   def getItem(self, name):
       
    48     """Returns a MenuItem retrieved by its MenuItem.text."""
       
    49     return self._items.get(name)
       
    50 
       
    51   def setItem(self, item):
       
    52     """Overwrites an existing MenuItem, or appends a new one."""
       
    53     self._items[item.name] = item
       
    54 
       
    55   def delItem(self, name):
       
    56     """Removes an existing MenuItem."""
       
    57     del self._items[name]
       
    58 
       
    59   def _getItems(self):
       
    60     """Returns an ordered list of the MenuItems."""
       
    61     return self._items.values()
       
    62 
       
    63   items = property(_getItems, doc=
       
    64     """Read-only list of MenuItems, for use in templates.""")
       
    65 
       
    66 
       
    67 class MenuItem:
       
    68   """Provides menu item properties as easily-accessible attributes.
       
    69   """
       
    70   
       
    71   def __init__(self, name, value=None, selected=False, annotation=None,
       
    72                 sub_menu=None):
       
    73     """Initializes the menu item attributes from supplied arguments.
       
    74     
       
    75     Args:
       
    76       name: name of the menu item
       
    77       value: optional value associated with the menu item;
       
    78         default is None
       
    79       selected: Boolean indicating if this menu item is selected;
       
    80         default is False
       
    81       annotation: optional annotation associated with the menu item;
       
    82         default is None
       
    83       sub_menu: a Menu of sub-items to display below this menu item;
       
    84         default is None, indicating no sub-menu
       
    85     """
       
    86     self.name = name
       
    87     self.value = value
       
    88     self.selected = selected
       
    89     self.annotation = annotation
       
    90     self.sub_menu = sub_menu