web/views/boxes.py
branchtls-sprint
changeset 1263 01152fffd593
parent 1181 620ec8e6ae19
parent 1247 3332c92d950c
child 1285 d5ce82d65c2b
equal deleted inserted replaced
1246:76b3cd5d4f31 1263:01152fffd593
    21 from cubicweb.web.htmlwidgets import BoxWidget, BoxMenu, BoxHtml, RawBoxItem
    21 from cubicweb.web.htmlwidgets import BoxWidget, BoxMenu, BoxHtml, RawBoxItem
    22 from cubicweb.view import EntityView
    22 from cubicweb.view import EntityView
    23 from cubicweb.web.box import BoxTemplate
    23 from cubicweb.web.box import BoxTemplate
    24 
    24 
    25 _ = unicode
    25 _ = unicode
    26 
    26     
    27 
       
    28 class EditBox(BoxTemplate):
    27 class EditBox(BoxTemplate):
    29     """
    28     """
    30     box with all actions impacting the entity displayed: edit, copy, delete
    29     box with all actions impacting the entity displayed: edit, copy, delete
    31     change state, add related entities
    30     change state, add related entities
    32     """
    31     """
    89         to a new entity ('create' mode) or to an existant entity ('link' mode)
    88         to a new entity ('create' mode) or to an existant entity ('link' mode)
    90         """
    89         """
    91         return cls.rmode.rtag(rtype, role, etype, targettype)
    90         return cls.rmode.rtag(rtype, role, etype, targettype)
    92 
    91 
    93 
    92 
    94     def call(self, **kwargs):
    93     def call(self, view=None, **kwargs):
    95         _ = self.req._
    94         _ = self.req._
    96         title = _(self.title)
    95         title = _(self.title)
    97         if self.rset:
    96         if self.rset:
    98             etypes = self.rset.column_types(0)
    97             etypes = self.rset.column_types(0)
    99             if len(etypes) == 1:
    98             if len(etypes) == 1:
   100                 plural = self.rset.rowcount > 1 and 'plural' or ''
    99                 plural = self.rset.rowcount > 1 and 'plural' or ''
   101                 etypelabel = display_name(self.req, iter(etypes).next(), plural)
   100                 etypelabel = display_name(self.req, iter(etypes).next(), plural)
   102                 title = u'%s - %s' % (title, etypelabel.lower())
   101                 title = u'%s - %s' % (title, etypelabel.lower())
   103         box = BoxWidget(title, self.id, _class="greyBoxFrame")
   102         box = BoxWidget(title, self.id, _class="greyBoxFrame")
   104         # build list of actions
   103         # build list of actions
   105         actions = self.vreg.possible_actions(self.req, self.rset)
   104         actions = self.vreg.possible_actions(self.req, self.rset, view=view)
   106         add_menu = BoxMenu(_('add')) # 'addrelated' category
   105         add_menu = BoxMenu(_('add')) # 'addrelated' category
   107         other_menu = BoxMenu(_('more actions')) # 'moreactions' category
   106         other_menu = BoxMenu(_('more actions')) # 'moreactions' category
   108         searchstate = self.req.search_state[0]
   107         searchstate = self.req.search_state[0]
   109         for category, menu in (('mainactions', box),
   108         for category, menu in (('mainactions', box),
   110                                ('addrelated', add_menu),
   109                                ('addrelated', add_menu),
   132             other_menu.items = []
   131             other_menu.items = []
   133         self.add_submenu(box, add_menu, _('add'))
   132         self.add_submenu(box, add_menu, _('add'))
   134         self.add_submenu(box, other_menu)
   133         self.add_submenu(box, other_menu)
   135         if not box.is_empty():
   134         if not box.is_empty():
   136             box.render(self.w)
   135             box.render(self.w)
   137 
   136             
   138     def add_submenu(self, box, submenu, label_prefix=None):
   137     def add_submenu(self, box, submenu, label_prefix=None):
   139         if len(submenu.items) == 1:
   138         if len(submenu.items) == 1:
   140             boxlink = submenu.items[0]
   139             boxlink = submenu.items[0]
   141             if label_prefix:
   140             if label_prefix:
   142                 boxlink.label = u'%s %s' % (label_prefix, boxlink.label)
   141                 boxlink.label = u'%s %s' % (label_prefix, boxlink.label)
   193             state = entity.in_state[0]
   192             state = entity.in_state[0]
   194             transitions = list(state.transitions(entity))
   193             transitions = list(state.transitions(entity))
   195             if transitions:
   194             if transitions:
   196                 menu_title = u'%s: %s' % (_('state'), state.view('text'))
   195                 menu_title = u'%s: %s' % (_('state'), state.view('text'))
   197                 menu_items = []
   196                 menu_items = []
   198                 for tr in state.transitions(entity):
   197                 for tr in transitions:
   199                     url = entity.absolute_url(vid='statuschange', treid=tr.eid)
   198                     url = entity.absolute_url(vid='statuschange', treid=tr.eid)
   200                     menu_items.append(self.mk_action(_(tr.name), url))
   199                     menu_items.append(self.mk_action(_(tr.name), url))
   201                 box.append(BoxMenu(menu_title, menu_items))
   200                 box.append(BoxMenu(menu_title, menu_items))
   202             # when there are no possible transition, put state if the menu if
   201             # when there are no possible transition, put state if the menu if
   203             # there are some other actions
   202             # there are some other actions
   270 
   269 
   271 
   270 
   272 class StartupViewsBox(BoxTemplate):
   271 class StartupViewsBox(BoxTemplate):
   273     """display a box containing links to all startup views"""
   272     """display a box containing links to all startup views"""
   274     id = 'startup_views_box'
   273     id = 'startup_views_box'
   275 
       
   276     visible = False # disabled by default
   274     visible = False # disabled by default
   277     title = _('startup views')
   275     title = _('startup views')
   278     order = 70
   276     order = 70
   279 
   277 
   280     def call(self, **kwargs):
   278     def call(self, **kwargs):
   283             if view.category == 'startupview':
   281             if view.category == 'startupview':
   284                 box.append(self.box_action(view))
   282                 box.append(self.box_action(view))
   285         
   283         
   286         if not box.is_empty():
   284         if not box.is_empty():
   287             box.render(self.w)
   285             box.render(self.w)
       
   286 
   288 
   287 
   289 # helper classes ##############################################################
   288 # helper classes ##############################################################
   290 
   289 
   291 class SideBoxView(EntityView):
   290 class SideBoxView(EntityView):
   292     """helper view class to display some entities in a sidebox"""
   291     """helper view class to display some entities in a sidebox"""