author | Pawel Solyga <Pawel.Solyga@gmail.com> |
Tue, 23 Sep 2008 19:20:57 +0000 | |
changeset 188 | 27ed1a09c98d |
parent 187 | 2c0a497b2b2a |
child 189 | 1cf3e7531382 |
--- 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 %} + <script type="text/javascript" src="/tiny_mce/tiny_mce_src.js"></script> + {% 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'''<textarea %(attrs)s>%(value)s</textarea> + <script type="text/javascript"> + tinyMCE.init(%(settings_json)s)</script>''' + 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'<textarea%s>%s</textarea> \ - <script type="text/javascript">\ - tinyMCE.init(%s)</script>' % (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})