From d708608645cb4904964e624116ffa944fc06891e Mon Sep 17 00:00:00 2001 From: Thierry Parmentelat Date: Tue, 18 Dec 2012 11:26:47 +0100 Subject: [PATCH] pass python settings to the js layer - from plugin.php --- engine/plugin.py | 14 ++++++++++++-- templates/widget-plugin.html | 11 +++++++---- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/engine/plugin.py b/engine/plugin.py index 7a8e7ece..f5e04de5 100644 --- a/engine/plugin.py +++ b/engine/plugin.py @@ -2,6 +2,8 @@ # so it should be specialized in real plugin classes # like e.g. plugins.simplelist.SimpleList +import json + from django.template.loader import render_to_string class Plugin: @@ -33,6 +35,9 @@ class Plugin: title = self.get_class() plugin_content = self.render_content (request) + # expose _settings in json format to js + settings_json = json.dumps(self._settings, separators=(',',':')) + # xxx missing from the php version # compute an 'optionstr' from the set of available settings/options as a json string # that gets passed to jquery somehow @@ -43,12 +48,17 @@ class Plugin: 'hidable':self.is_hidable(), 'hidden':self.is_hidden_by_default(), 'plugin_content' : plugin_content, - 'optionstr' : 'xxx-todo', + 'settings' : settings_json, }) return result ### abstract interface + # you may redefine this completely, but if you don't we'll just use method + # template() to find out which template to use, and env() to find out which + # dictionary to pass the templating system def render_content (self, request): """Should return an HTML fragment""" - return "Your plugin needs to redefine 'render_content(self, request)'" + template = self.template() + env=self.env() + return render_to_string (template, env) diff --git a/templates/widget-plugin.html b/templates/widget-plugin.html index fe9dcbaa..540cb4a6 100644 --- a/templates/widget-plugin.html +++ b/templates/widget-plugin.html @@ -19,13 +19,12 @@ {% endif %} +{% insert prelude_js %} {% if visible and hidable %} {# xxx sounds like this could be optimized by a single call performed on document.ready #} {# that would do that on all DOM elements that require it #} -{% insert prelude_js %} /* Show / hide plugin */ - jQuery('#show_{{ uuid }}').click(function() { jQuery('#{{ uuid }}').show(); if (typeof jQuery('#{{ uuid }}').{{ title }} != 'undefined') { @@ -44,6 +43,10 @@ event.preventDefault(); {% if hidden %} jQuery('#{{ uuid }}').hide() {% endif %} -/* {{ optionstr }} optionstr needs more work */ -{% endinsert %} {% endif %} +/* Plugin initialization (if the plugin has the right structure) - from plugin.php */ +if (typeof jQuery('#{{ uuid }}').$title != 'undefined') { + jQuery('#{{ uuid }}').$title({ {{ settings }} plugin_uuid: '{{ uuid }}' }); + //jQuery('#{{ uuid }}').$title('show'); + }; +{% endinsert %} -- 2.43.0