app/soc/views/helpers/html_menu.py
changeset 196 089a86d84067
parent 195 086282e19995
child 198 e4cbd0909520
equal deleted inserted replaced
195:086282e19995 196:089a86d84067
    63 
    63 
    64 class AHrefMenuItem(menu.MenuItem):
    64 class AHrefMenuItem(menu.MenuItem):
    65   """Provides HTML menu item properties as attributes as an <a href> link. 
    65   """Provides HTML menu item properties as attributes as an <a href> link. 
    66   """
    66   """
    67   
    67   
    68   def __init__(self, text, url=None, selected=False, help_text=None,
    68   def __init__(self, text, value=None, selected=False, annotation=None,
    69                sub_menu=None):
    69                sub_menu=None):
    70     """Initializes the menu item attributes from supplied arguments.
    70     """Initializes the menu item attributes from supplied arguments.
    71     
    71     
    72     Args:
    72     Args:
    73       text: text displayed for the menu item link anchor
    73       text: text displayed for the menu item link anchor
    74       url: optional URL to be placed in the menu item link href;
    74       value: optional URL to be placed in the menu item link href;
    75         default is None
    75         default is None
    76       selected: Boolean indicating if this menu item is selected;
    76       selected: Boolean indicating if this menu item is selected;
    77         default is False
    77         default is False
    78       help_text: optional help text associated with the menu item
    78       annotation: optional help text associated with the menu item
    79       sub_menu: see menu.MenuItem.__init__() 
    79       sub_menu: see menu.MenuItem.__init__() 
    80     """
    80     """
    81     menu.MenuItem.__init__(self, text, selected=selected, sub_menu=sub_menu)
    81     menu.MenuItem.__init__(self, text, value=value, selected=selected,
    82     self.url = url
    82                            annotation=annotation, sub_menu=sub_menu)
    83     self.help_text = help_text
       
    84 
    83 
    85   def getHtmlTags(self, indent):
    84   def getHtmlTags(self, indent):
    86     """Returns list of HTML tags for a menu item (and possibly its sub-menus).
    85     """Returns list of HTML tags for a menu item (and possibly its sub-menus).
    87     
    86     
    88     Args:
    87     Args:
    90         (usually consists entirely of spaces)
    89         (usually consists entirely of spaces)
    91         
    90         
    92     Returns:
    91     Returns:
    93       a list of strings that can be joined with '\n' into a single string
    92       a list of strings that can be joined with '\n' into a single string
    94       to produce an <a href="...">...</a> link, or just the MenuItem.name
    93       to produce an <a href="...">...</a> link, or just the MenuItem.name
    95       as plain text if there was no AHrefMenuItem.url; may also append
    94       as plain text if there was no AHrefMenuItem.value URL; may also append
    96       arbitrarily nested sub-menus
    95       arbitrarily nested sub-menus
    97     """
    96     """
    98     tags = []
    97     tags = []
    99 
    98 
   100     # TODO(tlarsen): user-supplied content *must* be escaped to prevent XSS
    99     # TODO(tlarsen): user-supplied content *must* be escaped to prevent XSS
   101 
   100 
   102     # TODO(tlarsen): implement "selected" style
   101     # TODO(tlarsen): implement "selected" style
   103     if self.url:
   102     if self.value:
       
   103       # URL supplied, so make an <a href="self.value">self.name</a> link
   104       tags.append('%s<a href=' % indent)
   104       tags.append('%s<a href=' % indent)
   105       tags.append('"%s">%s</a>' % (self.url, self.name))
   105       tags.append('"%s">%s</a>' % (self.value, self.name))
   106     else:
   106     else:
   107       # if no URL, then not a link, so just display text
   107       # if no URL, then not a link, so just display self.name as text
   108       tags.append(item.name)
   108       tags.append(self.name)
   109 
   109 
   110     # TODO(tlarsen): implement the mouse-over support for item.help_text
   110     # TODO(tlarsen): implement the mouse-over support for item.annotation
   111 
   111 
   112     if self.sub_menu:
   112     if self.sub_menu:
   113       tags.extend(self.sub_menu.getHtmlTags(indent + ' '))
   113       tags.extend(self.sub_menu.getHtmlTags(indent + ' '))
   114           
   114           
   115     return tags
   115     return tags
   120 
   120 
   121 class LiMenuItem(AHrefMenuItem):
   121 class LiMenuItem(AHrefMenuItem):
   122   """Provides HTML menu item properties as attributes as an <li> list item.
   122   """Provides HTML menu item properties as attributes as an <li> list item.
   123   """
   123   """
   124   
   124   
   125   def __init__(self, text, url=None, selected=False, help_text=None,
   125   def __init__(self, text, value=None, selected=False, annotation=None,
   126                sub_menu=None):
   126                sub_menu=None):
   127     """Initializes the menu item attributes from supplied arguments.
   127     """Initializes the menu item attributes from supplied arguments.
   128     
   128     
   129     Args:
   129     Args:
   130       text, url, selected, help_text, sub_menu:
   130       text, value, selected, annotation, sub_menu:
   131         see AHrefMenuItem.__init__() 
   131         see AHrefMenuItem.__init__() 
   132     """
   132     """
   133     AHrefMenuItem.__init__(self, text, url=url, selected=selected,
   133     AHrefMenuItem.__init__(self, text, value=value, selected=selected,
   134                            help_text=help_text, sub_menu=sub_menu)
   134                            annotation=annotation, sub_menu=sub_menu)
   135 
   135 
   136   def getHtmlTags(self, indent):
   136   def getHtmlTags(self, indent):
   137     """Returns <a href> link wrapped as an <li> list item.
   137     """Returns <a href> link wrapped as an <li> list item.
   138     
   138     
   139     See also AHrefMenuItem.getHtmlTags().
   139     See also AHrefMenuItem.getHtmlTags().