|
1 #!/usr/bin/python2.5 |
|
2 # |
|
3 # Copyright 2008 the Melange authors. |
|
4 # |
|
5 # Licensed under the Apache License, Version 2.0 (the "License"); |
|
6 # you may not use this file except in compliance with the License. |
|
7 # You may obtain a copy of the License at |
|
8 # |
|
9 # http://www.apache.org/licenses/LICENSE-2.0 |
|
10 # |
|
11 # Unless required by applicable law or agreed to in writing, software |
|
12 # distributed under the License is distributed on an "AS IS" BASIS, |
|
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14 # See the License for the specific language governing permissions and |
|
15 # limitations under the License. |
|
16 |
|
17 """Custom widgets used for form fields. |
|
18 """ |
|
19 |
|
20 __authors__ = [ |
|
21 '"Pawel Solyga" <pawel.solyga@gmail.com>', |
|
22 ] |
|
23 |
|
24 import django.newforms as forms |
|
25 from django.newforms.widgets import flatatt |
|
26 from django.newforms.util import smart_unicode |
|
27 from django.utils.html import escape |
|
28 from django.utils import simplejson |
|
29 from django.utils.safestring import mark_safe |
|
30 |
|
31 class TinyMCE(forms.Textarea): |
|
32 """ |
|
33 TinyMCE widget. requires you include tiny_mce_src.js in your template |
|
34 you can customize the mce_settings by overwriting instance mce_settings, |
|
35 or add extra options using update_settings |
|
36 """ |
|
37 |
|
38 mce_settings = dict( |
|
39 mode = "exact", |
|
40 theme = "simple", |
|
41 theme_advanced_toolbar_location = "top", |
|
42 theme_advanced_toolbar_align = "center", |
|
43 ) |
|
44 |
|
45 def update_settings(self, custom): |
|
46 return_dict = self.mce_settings.copy() |
|
47 return_dict.update(custom) |
|
48 return return_dict |
|
49 |
|
50 def render(self, name, value, attrs=None): |
|
51 if value is None: |
|
52 value = '' |
|
53 value = smart_unicode(value) |
|
54 final_attrs = self.build_attrs(attrs, name=name) |
|
55 |
|
56 self.mce_settings['elements'] = "id_%s" % name |
|
57 mce_json = simplejson.JSONEncoder().encode(self.mce_settings) |
|
58 |
|
59 return mark_safe(u'<textarea%s>%s</textarea> \ |
|
60 <script type="text/javascript">\ |
|
61 tinyMCE.init(%s)</script>' % (flatatt(final_attrs), |
|
62 escape(value), |
|
63 mce_json)) |