# HG changeset patch # User Pawel Solyga # Date 1222197657 0 # Node ID 27ed1a09c98d3ddd8e16cbd87a8ee517b82b34f2 # Parent 2c0a497b2b2a8be0a91ea1ab0a520ca99a3d46c4 Changes in TinyMCE widget in custom_widgets.py based on review comments. Patch by: Pawel Solyga Review url: http://code.google.com/p/soc/source/detail?r=550 diff -r 2c0a497b2b2a -r 27ed1a09c98d app/soc/views/helpers/custom_widgets.py --- a/app/soc/views/helpers/custom_widgets.py Tue Sep 23 17:55:54 2008 +0000 +++ b/app/soc/views/helpers/custom_widgets.py Tue Sep 23 19:20:57 2008 +0000 @@ -29,35 +29,46 @@ from django.utils.safestring import mark_safe class TinyMCE(forms.widgets.Textarea): - """ - TinyMCE widget. requires you include tiny_mce_src.js in your template - you can customize the mce_settings by overwriting instance mce_settings, - or add extra options using update_settings - """ + """TinyMCE widget. + + Requires to include tiny_mce_src.js in your template. Widget can be + customized by overwriting or adding extra options to mce_settings + dictionary - mce_settings = dict( - mode = "exact", - theme = "simple", - theme_advanced_toolbar_location = "top", - theme_advanced_toolbar_align = "center", - ) + You can set TinyMCE widget for particular form field using code below: + class ExampleForm(forms_helpers.DbModelForm): + content = forms.fields.CharField(widget=custom_widgets.TinyMCE()) + + You can include tiny_mce_src.js in your template using: + {% block scripts %} + + {% endblock %} + """ + MCE_DEF_SETTINGS = { 'mode': "exact", + 'theme': "simple", + 'theme_advanced_toolbar_location': "top", + 'theme_advanced_toolbar_align': "center"} - def update_settings(self, custom): - return_dict = self.mce_settings.copy() - return_dict.update(custom) - return return_dict + mce_settings = MCE_DEF_SETTINGS + TINY_MCE_HTML_FMT = u''' + ''' + def render(self, name, value, attrs=None): + """Render TinyMCE widget as HTML. + """ if value is None: value = '' value = smart_unicode(value) final_attrs = self.build_attrs(attrs, name=name) self.mce_settings['elements'] = "id_%s" % name + + # convert mce_settings from dict to JSON mce_json = simplejson.JSONEncoder().encode(self.mce_settings) - return mark_safe(u'%s \ - ' % (flatatt(final_attrs), - escape(value), - mce_json)) + return mark_safe( self.TINY_MCE_HTML_FMT % + { 'attrs': flatatt(final_attrs), + 'value': escape(value), + 'settings_json': mce_json})