X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=engine%2Fplugin.py;h=fc68c48a7ea656e0471f31e8b5987bb42f7dc1e6;hb=e5c7d07dbd241c1bf8ffe87ae7e1ccb64f113f32;hp=fa01567e890fc204c6a4dacc8a984b4736514ce2;hpb=68e0811883d0790679f0539300a230677f7b9743;p=unfold.git diff --git a/engine/plugin.py b/engine/plugin.py index fa01567e..fc68c48a 100644 --- a/engine/plugin.py +++ b/engine/plugin.py @@ -14,48 +14,47 @@ from engine.prelude import Prelude # . [ 'SliceList', 'TabbedView' ] : to debug these classes # . True : to debug all plugin -DEBUG= [ 'Tabs' ] +#DEBUG= [ 'Tabs' ] +DEBUG= False class Plugin: - # using a simple incremental scheme to generate uuids for now - uuid=0 + # using a simple incremental scheme to generate domids for now + # we just need this to be unique in a page + domid=0 - # xxx should generate some random id @staticmethod - def newuuid(): - Plugin.uuid += 1 - return Plugin.uuid + def newdomid(): + Plugin.domid += 1 + return "plugin-%d"%Plugin.domid ########## # Constructor #### mandatory # . title: is used visually for displaying the widget - # . name: a simple id suitable for forging css names #### optional - # . togglable: whether it can be turned on and off from the UI - # like e.g. PleKitToggle + # . 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 + # . visible: if not set the plugin does not show up at all + # (not quite sure what this was for) #### internal data - # . uuid: created internally + # . 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 # . rank: this is for plugins sons of a composite plugin #### custom # any other setting can also be set when creating the object, like # p=Plugin(foo='bar') # which will result in 'foo' being accessible to the template engine # - def __init__ (self, title, name, + def __init__ (self, title, domid=None, visible=True, togglable=True, toggled=True, **settings): # what is in this dictionary will get exposed to template and to javascript self._settings=settings self.title=title - self.name=name - self.add_to_settings ( ['title','name'] ) - self.uuid=Plugin.newuuid() + if not domid: domid=Plugin.newdomid() + self.domid=domid self.classname=self._classname() - self.add_to_settings ( [ 'uuid', 'classname' ] ) + self.add_to_settings ( ['title', 'domid', 'classname'] ) self.visible=visible self.togglable=togglable self.toggled=toggled @@ -94,7 +93,6 @@ class Plugin: # returns the html code for that plugin # in essence, wraps the results of self.render_content () def render (self, request): - uuid = self.uuid # initialize prelude placeholder if needed self._init_prelude (request) # call render_content @@ -105,14 +103,16 @@ class Plugin: env.update(self._settings) result = render_to_string ('plugin.html',env) - # expose _settings in json format to js, and add plugin_uuid: uuid in the mix - js_env = { 'plugin_uuid' : self.uuid } + # expose _settings in json format to js, and add plugin_uuid: domid in the mix + # NOTE this plugin_uuid thing might occur in js files, ** do not rename ** + js_env = { 'plugin_uuid' : self.domid } js_env.update (self._settings) + for k in self.exclude_from_json(): + if k in js_env: del js_env[k] settings_json = json.dumps (js_env, separators=(',',':')) env ['settings_json' ] = settings_json # compute plugin-specific initialization - js_init = render_to_string ( 'plugin_setenv.js', env ) - print 'js_init',js_init + js_init = render_to_string ( 'plugin-setenv.js', env ) self.add_js_chunks (request, js_init) # interpret the result of requirements () @@ -213,3 +213,6 @@ class Plugin: # 'css_chunk': [], # likewise for css scripts # } + # for better performance + # you can specify a list of keys that won't be exposed as json attributes + def exclude_from_json (self): return []