Commits

Denis Costa committed 1aeecc2

closes #4 Adicionado class active no menu ativo.

  • Participants
  • Parent commits cb32a39

Comments (0)

Files changed (2)

menu/context_processors.py

 from django.conf import settings
 
-def _prepare_menu(raw_menu):
+def _prepare_menu(path, raw_menu):
     menu = []
 
     for i in raw_menu:
+        active = False
         i = list(i)
         if isinstance(i[1], tuple):
-            i[1] = _prepare_menu(i[1])
+            i[1] = _prepare_menu(path, i[1])
             i[1] = list(i[1])
         if len(i) == 2:
             i.append(None)
+        if path.startswith(i[0].lower()):
+            active = True
+        i.append(active)
         menu.append(i)
     return menu
 
     menu = []
 
     for item in raw_menu:
-        label, uri, permission = item
+        label, uri, permission, active = item
         if permission == None or request.user.has_perm(permission):
             if isinstance(uri, list):
                 uri = tuple(_check_permissions(request, uri))
-            menu.append((label, uri))
+            menu.append((label, uri, active))
     return menu
 
 def menu(request):
     raw_menu = [list(i) for i in settings.MENU]
-    return {'menu': _check_permissions(request, _prepare_menu(raw_menu))}
+    path = request.path[1:].lower()
+
+    return {'menu': _check_permissions(request, _prepare_menu(path, raw_menu))}
 
 def submenu(request):
     raw_menu = [list(i) for i in settings.MENU]
     submenu = []
+    path = request.path[1:].lower()
+
     for item in raw_menu:
         if request.path[1:].startswith(item[0].lower()) and isinstance(item[1], tuple):
             submenu = item[1]
-    return {'submenu': _check_permissions(request, _prepare_menu(submenu))}
+    return {'submenu': _check_permissions(request, _prepare_menu(path, submenu))}

menu/templatetags/menu.py

 
 register = template.Library()
 
-def _menuing(raw_menu, old_label=''):
+def _menuing(raw_menu, old_link=''):
     menu = ''
 
-    for label, link in raw_menu:
+    for label, link, active in raw_menu:
+
+        if active:
+            act = ' class="active"'
+        else:
+            act = ''
+
         if isinstance(link, tuple):
-            if old_label:
-                menu += '<li><a href="%s">%s</a><ul>%s</ul></li>' %\
-                    (label.lower(), label, _menuing(link, old_label + '/' + label.lower()))
-            else:
-                menu += '<li><a href="%s">%s</a><ul>%s</ul></li>' %\
-                    (label.lower(), label, _menuing(link, label.lower()))
+            # is submenu, so, I use label in link field
+            old_link = '%s/%s' % (old_link, label.lower())
+            menu += '<li%s><a href="%s">%s</a><ul>%s</ul></li>' % (act, old_link, label, _menuing(link, old_link))
         else:
-            if old_label:
-                menu += '<li><a href="%s">%s</a></li>' % (old_label + '/' + link, label)
-            else:
-                menu += '<li><a href="%s">%s</a></li>' % (link, label)
+            menu += '<li%s><a href="%s">%s</a></li>' % (act, '%s/%s' % (old_link, link), label)
     return menu
 
 def do_menu(parser, token):