Changeset 5083


Ignore:
Timestamp:
25 Mar 2010, 15:47:43 (15 years ago)
Author:
uli
Message:

Make ActionButton? viewlets more or less independent from the package
they're contained in. Setting the used template file explicitly and
mangling the static attribute, we can define inheriting classes in
packages other than w.s.browser, which would as fallback still use the
local templates and action icons.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.sirp/trunk/src/waeup/sirp/browser/viewlets.py

    r5005 r5083  
    44
    55from hurry import yui
    6 from zope.component import getMultiAdapter
     6from zope.component import getMultiAdapter, queryAdapter
     7from zope.interface import Interface
    78from zope.location.interfaces import ISite
    89from waeup.sirp.browser.pages import (
     
    5758    grok.baseclass()
    5859    grok.context(IWAeUPObject)
    59     grok.template('actionbutton')
    6060    grok.viewletmanager(ActionBar)
    6161    icon = 'actionicon_modify.png' # File must exist in static/
     
    6363    text = 'Edit settings' # Text to display on the button
    6464
     65    # We set the template file explicitly (instead of using
     66    # ``grok.template('actionbutton')``) to stick with this template
     67    # also in derived classes in other packages. If we didn't, those
     68    # derived ActionButton viewlets had to provide an own template,
     69    # which would not be updated automatically, when the local
     70    # template ``templates/actionbutton.pt`` changes.
     71    #
     72    # Inheriting viewlets that wish to use their own template anyway
     73    # can do so by setting their local ``grok.template(<mytemplate>)``
     74    # and setting ``template`` to ``None`` for the class::
     75    #
     76    # class DerivedActionButton(ActionButton):
     77    #   ...
     78    #   grok.template('overriding_template')
     79    #   template = None
     80    #   ...
     81    #
     82    template = grok.PageTemplateFile('templates/actionbutton.pt')
     83
    6584    @property
    6685    def alt(self):
     
    7392        """Get the icon URL.
    7493        """
    75         return self.view.static[self.icon]()
     94        static = self.view.static
     95        if static is None or static.get(self.icon, None) is None:
     96            # In derived classes defined in other modules/packages
     97            # than w.s.browser, ``static`` might refer to a static dir
     98            # local to the derived class' module. As we often like to
     99            # get the icons from here
     100            # (i.e. waeup.sirp.browser/static), we set the directory
     101            # resource appropiately.
     102            #
     103            # XXX: The hardcoding of 'w.s.browser' should be replaced
     104            #      by something smarter.
     105            #
     106            # TODO: notes in here should go to general documentation.
     107            static = queryAdapter(
     108                self.request, Interface, name='waeup.sirp.browser')
     109        return static[self.icon]()
    76110
    77111    @property
Note: See TracChangeset for help on using the changeset viewer.