Make tab panes URL-addressable.

This commit is contained in:
Logan Hanks
2012-03-23 12:49:39 -07:00
parent 30fb5fcc2b
commit 80d32449eb
4 changed files with 29 additions and 9 deletions

View File

@@ -363,11 +363,15 @@ class NamedButton(NavButton):
class JsButton(NavButton):
"""A button which fires a JS event and thus has no path and cannot
be in the 'selected' state"""
def __init__(self, title, style = 'js', **kw):
NavButton.__init__(self, title, '#', style = style, **kw)
def __init__(self, title, style = 'js', tab_name = None, **kw):
NavButton.__init__(self, title, '#', style = style, tab_name = tab_name,
**kw)
def build(self, *a, **kw):
self.path = 'javascript:void(0)'
if self.tab_name:
self.path = '#' + self.tab_name
else:
self.path = 'javascript:void(0)'
def is_selected(self):
return False

View File

@@ -2506,7 +2506,7 @@ class FlairPane(Templated):
Templated.__init__(
self,
tabs=TabbedPane(tabs),
tabs=TabbedPane(tabs, linkable=True),
flair_enabled=c.site.flair_enabled,
flair_position=c.site.flair_position,
link_flair_position=c.site.link_flair_position,
@@ -3028,17 +3028,18 @@ class Roadblocks(Templated):
else g.default_sr
class TabbedPane(Templated):
def __init__(self, tabs):
def __init__(self, tabs, linkable=False):
"""Renders as tabbed area where you can choose which tab to
render. Tabs is a list of tuples (tab_name, tab_pane)."""
buttons = []
for tab_name, title, pane in tabs:
buttons.append(JsButton(title, onclick="return select_tab_menu(this, '%s');" % tab_name))
onclick = "return select_tab_menu(this, '%s')" % tab_name
buttons.append(JsButton(title, tab_name=tab_name, onclick=onclick))
self.tabmenu = JsNavMenu(buttons, type = 'tabmenu')
self.tabs = tabs
Templated.__init__(self)
Templated.__init__(self, linkable=linkable)
class LinkChild(object):
def __init__(self, link, load = False, expand = False, nofollow = False):

View File

@@ -124,9 +124,12 @@
${"id='%s'" % thing._id if thing._id else ""}>
%for i, option in enumerate(thing):
<%
option.selected = (option == thing.selected)
li_id = (
"id='tab-%s'" % option.tab_name if hasattr(option, 'tab_name')
else "")
li_class = "class='selected'" if option == thing.selected else ""
%>
<li ${"class='selected'" if option.selected else ""}>
<li ${id_class} ${li_class}>
${option}
</li>
%endfor

View File

@@ -7,3 +7,15 @@ ${thing.tabmenu}
</div>
%endfor
</div>
%if thing.linkable:
<script>
$(function() {
var target = "tab-" + $(window.location).attr("hash").substr(1);
$(".tabmenu li").each(function() {
if (this.id == target) {
$(this).find("a").click();
}
});
});
</script>
%endif