X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=unfold%2Fplugin.py;h=f9e1ea41b07f380fbc5b3645c2f514fb316af2d8;hb=2d8d2976d8dba26159f42d10152715160c0c1770;hp=17931b6754cce5d6520668e8214a1b02ff173c9e;hpb=d2ab222245b6311d81f9465cde3f48761c077168;p=myslice.git diff --git a/unfold/plugin.py b/unfold/plugin.py index 17931b67..f9e1ea41 100644 --- a/unfold/plugin.py +++ b/unfold/plugin.py @@ -16,8 +16,8 @@ from unfold.prelude import Prelude # . True : to debug all plugin DEBUG= False -#DEBUG= [ 'SliceList' ] -DEBUG=True +#DEBUG= [ 'SimpleList' ] +#DEBUG=True # decorator to deflect calls on Plugin to its Prelude through self.page.prelude def to_prelude (method): @@ -43,10 +43,19 @@ class Plugin: # . page: the context of the request being served # . title: is used visually for displaying the widget #### optional - # . togglable: whether it can be turned on and off (like PleKitToggle) - # . toggled: if togglable, what's the initial status # . visible: if not set the plugin does not show up at all # (not quite sure what this was for) + # . togglable: whether it can be turned on and off by clicking on the title (like PleKitToggle) + # . toggled: whether the plugin should startup open/shown or closed/hidden + # possible values are + # .. True : start up open/hidden + # .. False : start up closed/shown + # .. 'persistent' : start up as it was the last time that browser showed it (based on 'domid') + # NOTE that it is required for you to set a domid if you want to use persistent mode + # since domid is the key for storing that data in the browser storage space + # .. None : if not passed to __init__ at all, then the default_toggled() method is called + # .. : anything else, defaults to True + # #### internal data # . domid: created internally, but can be set at creation time if needed # useful for hand-made css, or for selecting an active plugin in a composite @@ -57,7 +66,7 @@ class Plugin: # which will result in 'foo' being accessible to the template engine # def __init__ (self, page, title, domid=None, - visible=True, togglable=True, toggled=True, **settings): + visible=True, togglable=None, toggled=None, **settings): self.page = page self.title=title # callers can provide their domid for css'ing @@ -66,8 +75,10 @@ class Plugin: self.classname=self._py_classname() self.plugin_classname=self._js_classname() self.visible=visible - self.togglable=togglable - self.toggled=toggled + if togglable is None: self.togglable=self.default_togglable() + else: self.togglable=togglable + if toggled is None: self.toggled=self.default_toggled() + else: self.toggled=toggled # what comes from subclasses for (k,v) in settings.iteritems(): setattr(self,k,v) @@ -80,6 +91,9 @@ class Plugin: # do this only once the structure is fine self.page.record_plugin(self) + def __repr__ (self): + return "[%s]:%s"%(self.classname,self.domid) + def _py_classname (self): try: return self.__class__.__name__ except: return 'Plugin' @@ -103,7 +117,7 @@ class Plugin: except: return '%s:"undefined"'%setting else: value=getattr(self,setting,None) - if not value: value = "unknown-setting-%s"%setting + if value is None: value = "unknown-setting-%s"%setting # first try to use to_json method (json.dumps not working on class instances) try: value_json=value.to_json() except: value_json=json.dumps(value,separators=(',',':')) @@ -142,15 +156,26 @@ class Plugin: # need_spin is used in plugin.html self.need_spin=self.start_with_spin() env.update(self.__dict__) + # translate high-level 'toggled' into 4 different booleans + if self.toggled=='persistent': + # start with everything turned off and let the js callback do its job + env.update({'persistent_toggle':True,'display_hide_button':False,'display_show_button':False,'display_body':False}) + elif self.toggled==False: + env.update({'persistent_toggle':False,'display_hide_button':False,'display_show_button':True,'display_body':False}) + else: + env.update({'persistent_toggle':False,'display_hide_button':True,'display_show_button':False,'display_body':True}) if self.need_debug(): print "rendering plugin.html with env keys %s"%env.keys() + print "rendering plugin.html with env" + for (k,v) in env.items(): + if "display" in k or "persistent" in k: print k,'->',v result = render_to_string ('plugin.html',env) # export this only for relevant plugins if self.export_json_settings(): env ['settings_json' ] = self.settings_json() # compute plugin-specific initialization - js_init = render_to_string ( 'plugin-setenv.js', env ) + js_init = render_to_string ( 'plugin-init.js', env ) self.add_js_chunks (js_init) # interpret the result of requirements () @@ -164,7 +189,12 @@ class Plugin: def render_content (self, request): """Should return an HTML fragment""" template = self.template_file() - env=self.template_env(request) + # start with a fresh one + env={} + # add our own settings as defaults + env.update(self.__dict__) + # then the things explicitly defined in template_env() + env.update(self.template_env(request)) if not isinstance (env,dict): raise Exception, "%s.template_env returns wrong type"%self.classname result=render_to_string (template, env) @@ -221,6 +251,9 @@ class Plugin: def template_file (self): return "undefined_template" def template_env (self, request): return {} + def default_togglable (self): return True + def default_toggled (self): return 'persistent' + # # tell the framework about requirements (for the document
) # # the notion of 'Media' in django provides for medium-dependant # # selection of css files