26 from django.newforms.util import smart_unicode |
26 from django.newforms.util import smart_unicode |
27 from django.utils.html import escape |
27 from django.utils.html import escape |
28 from django.utils import simplejson |
28 from django.utils import simplejson |
29 from django.utils.safestring import mark_safe |
29 from django.utils.safestring import mark_safe |
30 |
30 |
|
31 |
|
32 class ReadOnlyInput(forms.widgets.Input): |
|
33 """Read only input widget. |
|
34 """ |
|
35 input_type = 'text' |
|
36 |
|
37 def render(self, name, value, attrs=None): |
|
38 """Render ReadOnlyInput widget as HTML. |
|
39 """ |
|
40 attrs['readonly'] = 'readonly' |
|
41 return super(ReadOnlyInput, self).render(name, value, attrs) |
|
42 |
|
43 |
31 class TinyMCE(forms.widgets.Textarea): |
44 class TinyMCE(forms.widgets.Textarea): |
32 """TinyMCE widget. |
45 """TinyMCE widget. |
33 |
46 |
34 Requires to include tiny_mce_src.js in your template. Widget can be |
47 Requires to include tiny_mce_src.js in your template. Widget can be |
35 customized by overwriting or adding extra options to mce_settings |
48 customized by overwriting or adding extra options to mce_settings |
36 dictionary |
49 dictionary |
37 |
50 |
38 You can set TinyMCE widget for particular form field using code below: |
51 You can set TinyMCE widget for particular form field using code below: |
39 class ExampleForm(forms_helpers.DbModelForm): |
52 class ExampleForm(forms_helpers.DbModelForm): |
40 content = forms.fields.CharField(widget=custom_widgets.TinyMCE()) |
53 content = forms.fields.CharField(widget=custom_widgets.TinyMCE()) |
41 |
54 |
42 You can include tiny_mce_src.js in your template using: |
55 You can include tiny_mce_src.js in your template using: |
43 {% block scripts %} |
56 {% block scripts %} |
44 <script type="text/javascript" src="/tiny_mce/tiny_mce_src.js"></script> |
57 <script type="text/javascript" src="/tiny_mce/tiny_mce_src.js"></script> |
45 {% endblock %} |
58 {% endblock %} |
46 """ |
59 """ |
47 DEF_MCE_SETTINGS = { 'mode': "exact", |
60 DEF_MCE_SETTINGS = { 'mode': "exact", |
48 'theme': "simple", |
61 'theme': "simple", |
49 'theme_advanced_toolbar_location': "top", |
62 'theme_advanced_toolbar_location': "top", |
50 'theme_advanced_toolbar_align': "center"} |
63 'theme_advanced_toolbar_align': "center"} |
51 |
64 |
52 mce_settings = DEF_MCE_SETTINGS.copy() |
65 mce_settings = DEF_MCE_SETTINGS.copy() |
53 |
66 |
54 TINY_MCE_HTML_FMT = u'''\ |
67 TINY_MCE_HTML_FMT = u'''\ |
55 <textarea %(attrs)s>%(value)s</textarea> |
68 <textarea %(attrs)s>%(value)s</textarea> |
56 <script type="text/javascript"> |
69 <script type="text/javascript"> |
57 tinyMCE.init(%(settings_json)s) |
70 tinyMCE.init(%(settings_json)s) |
58 </script>''' |
71 </script>''' |
|
72 |
|
73 def render(self, name, value, attrs=None): |
|
74 """Render TinyMCE widget as HTML. |
|
75 """ |
|
76 if value is None: |
|
77 value = '' |
|
78 value = smart_unicode(value) |
|
79 final_attrs = self.build_attrs(attrs, name=name) |
59 |
80 |
60 def render(self, name, value, attrs=None): |
81 self.mce_settings['elements'] = "id_%s" % name |
61 """Render TinyMCE widget as HTML. |
|
62 """ |
|
63 if value is None: |
|
64 value = '' |
|
65 value = smart_unicode(value) |
|
66 final_attrs = self.build_attrs(attrs, name=name) |
|
67 |
82 |
68 self.mce_settings['elements'] = "id_%s" % name |
83 # convert mce_settings from dict to JSON |
69 |
84 mce_json = simplejson.JSONEncoder().encode(self.mce_settings) |
70 # convert mce_settings from dict to JSON |
|
71 mce_json = simplejson.JSONEncoder().encode(self.mce_settings) |
|
72 |
85 |
73 return mark_safe(self.TINY_MCE_HTML_FMT % |
86 return mark_safe(self.TINY_MCE_HTML_FMT % |
74 {'attrs': flatatt(final_attrs), |
87 {'attrs': flatatt(final_attrs), |
75 'value': escape(value), |
88 'value': escape(value), |
76 'settings_json': mce_json}) |
89 'settings_json': mce_json}) |