app/soc/views/helpers/html_menu.py
changeset 195 086282e19995
child 196 089a86d84067
equal deleted inserted replaced
194:8bdb1d2d0c36 195:086282e19995
       
     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 """Helpers for displaying arbitrarily nested menus as HTML lists.
       
    18 """
       
    19 
       
    20 __authors__ = [
       
    21   '"Todd Larsen" <tlarsen@google.com>',
       
    22   ]
       
    23 
       
    24 
       
    25 from soc.logic import menu
       
    26 
       
    27 
       
    28 class UlMenu(menu.Menu):
       
    29   """Ordered collection of MenuItem objects as a <ul> list.
       
    30   """
       
    31 
       
    32   def __init__(self, items=None):
       
    33     """Passes the menu items to the base class __init__().
       
    34     """
       
    35     menu.Menu.__init__(self, items=items)
       
    36 
       
    37   def getHtmlTags(self, indent):
       
    38     """Returns list of HTML tags for arbitrarily nested items in the menu.
       
    39     
       
    40     Args:
       
    41       indent: string prepended to the beginning of each line of output
       
    42         (usually consists entirely of spaces)
       
    43         
       
    44     Returns:
       
    45       a list of strings that can be joined with '\n' into a single string
       
    46       to produce an entire <ul>...</ul> list in HTML
       
    47     """
       
    48     tags = []
       
    49 
       
    50     if self.items:
       
    51       tags.append('%s<ul>' % indent)
       
    52 
       
    53       for item in self.items:
       
    54         tags.extend(item.getHtmlTags(indent + ' '))
       
    55     
       
    56       tags.append('%s</ul>' % indent)
       
    57 
       
    58     return tags
       
    59 
       
    60   def __str__(self):
       
    61     return '\n'.join(self.getHtmlTags(''))
       
    62 
       
    63 
       
    64 class AHrefMenuItem(menu.MenuItem):
       
    65   """Provides HTML menu item properties as attributes as an <a href> link. 
       
    66   """
       
    67   
       
    68   def __init__(self, text, url=None, selected=False, help_text=None,
       
    69                sub_menu=None):
       
    70     """Initializes the menu item attributes from supplied arguments.
       
    71     
       
    72     Args:
       
    73       text: text displayed for the menu item link anchor
       
    74       url: optional URL to be placed in the menu item link href;
       
    75         default is None
       
    76       selected: Boolean indicating if this menu item is selected;
       
    77         default is False
       
    78       help_text: optional help text associated with the menu item
       
    79       sub_menu: see menu.MenuItem.__init__() 
       
    80     """
       
    81     menu.MenuItem.__init__(self, text, selected=selected, sub_menu=sub_menu)
       
    82     self.url = url
       
    83     self.help_text = help_text
       
    84 
       
    85   def getHtmlTags(self, indent):
       
    86     """Returns list of HTML tags for a menu item (and possibly its sub-menus).
       
    87     
       
    88     Args:
       
    89       indent: string prepended to the beginning of each line of output
       
    90         (usually consists entirely of spaces)
       
    91         
       
    92     Returns:
       
    93       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
       
    95       as plain text if there was no AHrefMenuItem.url; may also append
       
    96       arbitrarily nested sub-menus
       
    97     """
       
    98     tags = []
       
    99 
       
   100     # TODO(tlarsen): user-supplied content *must* be escaped to prevent XSS
       
   101 
       
   102     # TODO(tlarsen): implement "selected" style
       
   103     if self.url:
       
   104       tags.append('%s<a href=' % indent)
       
   105       tags.append('"%s">%s</a>' % (self.url, self.name))
       
   106     else:
       
   107       # if no URL, then not a link, so just display text
       
   108       tags.append(item.name)
       
   109 
       
   110     # TODO(tlarsen): implement the mouse-over support for item.help_text
       
   111 
       
   112     if self.sub_menu:
       
   113       tags.extend(self.sub_menu.getHtmlTags(indent + ' '))
       
   114           
       
   115     return tags
       
   116 
       
   117   def __str__(self):
       
   118     return '\n'.join(self.getHtmlTags(''))
       
   119 
       
   120 
       
   121 class LiMenuItem(AHrefMenuItem):
       
   122   """Provides HTML menu item properties as attributes as an <li> list item.
       
   123   """
       
   124   
       
   125   def __init__(self, text, url=None, selected=False, help_text=None,
       
   126                sub_menu=None):
       
   127     """Initializes the menu item attributes from supplied arguments.
       
   128     
       
   129     Args:
       
   130       text, url, selected, help_text, sub_menu:
       
   131         see AHrefMenuItem.__init__() 
       
   132     """
       
   133     AHrefMenuItem.__init__(self, text, url=url, selected=selected,
       
   134                            help_text=help_text, sub_menu=sub_menu)
       
   135 
       
   136   def getHtmlTags(self, indent):
       
   137     """Returns <a href> link wrapped as an <li> list item.
       
   138     
       
   139     See also AHrefMenuItem.getHtmlTags().
       
   140     """
       
   141     return (['%s<li>' % indent]
       
   142             + AHrefMenuItem.getHtmlTags(self, indent + ' ')
       
   143             + ['%s</li>' % indent])