Author: eallen
Date: 2009-08-14 16:03:08 -0400 (Fri, 14 Aug 2009)
New Revision: 3580
Modified:
mgmt/trunk/cumin/python/cumin/widgets.py
Log:
Added DynamicSwitch class to handle cases where the states are not fixed.
Modified: mgmt/trunk/cumin/python/cumin/widgets.py
===================================================================
--- mgmt/trunk/cumin/python/cumin/widgets.py 2009-08-14 19:00:10 UTC (rev 3579)
+++ mgmt/trunk/cumin/python/cumin/widgets.py 2009-08-14 20:03:08 UTC (rev 3580)
@@ -496,8 +496,7 @@
return self.param.get(session)
def set(self, session, value):
- foo = self.param.set(session, value)
- return foo
+ return self.param.set(session, value)
def get_items(self, session):
return self.__states
@@ -536,6 +535,67 @@
return fmt_link(branch.marshal(), title, class_, link_title=hover, bm=bm,
click=click, attribs=attribs)
+class DynamicSwitch(StateSwitch):
+ """ Used when the states are not fixed.
+ They vary each request """
+
+ def __init__(self, app, name):
+ super(DynamicSwitch, self).__init__(app, name)
+
+ self.states = self.StatesAttribute(app, "states")
+ self.add_attribute(self.states)
+
+ def add_state(self, session, state, title, hover=""):
+ state_attrib = self.states.get(session)
+ state_attrib['states'].append(state)
+ state_attrib['titles'][state] = title
+ state_attrib['hover'][state] = hover
+
+ if self.param.default is None:
+ self.param.default = state
+
+ def get_items(self, session):
+ state_attrib = self.states.get(session)
+ return state_attrib['states']
+
+ def get_title(self, session, state):
+ state_attrib = self.states.get(session)
+ return state in state_attrib['titles'] and
state_attrib['titles'][state]
+
+ def get_hover(self, session, state):
+ state_attrib = self.states.get(session)
+ return state in state_attrib['hover'] and
state_attrib['hover'][state]
+
+ def get_bookmark(self, session, state):
+ state_attrib = self.states.get(session)
+ return state in state_attrib['bookmark'] and
state_attrib['bookmark'][state]
+
+ def get_click(self, session, state):
+ return ""
+
+ def get_attributes(self, session, state):
+ return dict()
+
+ def render_item_link(self, session, state):
+ branch = session.branch()
+ self.set(branch, state)
+
+ title = self.get_title(session, state)
+ hover = self.get_hover(session, state)
+ class_ = self.get(session) == state and "selected"
+ bm = self.get_bookmark(session, state)
+ click = self.get_click(session, state)
+ attribs = self.get_attributes(session, state)
+
+ return fmt_link(branch.marshal(), title, class_, link_title=hover, bm=bm,
click=click, attribs=attribs)
+
+ class StatesAttribute(Attribute):
+ def get_default(self, session):
+ return {'states': [],
+ 'titles': {},
+ 'hover': {},
+ 'bookmark': {}}
+
class GroupSwitch(StateSwitch):
def __init__(self, app, name):
super(GroupSwitch, self).__init__(app, name)