summaryrefslogtreecommitdiff
path: root/chimere/widgets.py
diff options
context:
space:
mode:
Diffstat (limited to 'chimere/widgets.py')
-rw-r--r--chimere/widgets.py88
1 files changed, 18 insertions, 70 deletions
diff --git a/chimere/widgets.py b/chimere/widgets.py
index bd25054..a12d364 100644
--- a/chimere/widgets.py
+++ b/chimere/widgets.py
@@ -36,6 +36,11 @@ from django.utils.translation import ugettext as _
from django.forms.utils import flatatt
from django.template.loader import render_to_string
+if "tinymce" in settings.INSTALLED_APPS:
+ from tinymce.widgets import TinyMCE as Textarea
+else:
+ Textarea = forms.Textarea
+
import re
@@ -211,92 +216,35 @@ class ImporterChoicesWidget(forms.Select):
class Media:
js = ["%schimere/js/importer_interface.js" % settings.STATIC_URL]
-TINYMCE_JS, FULL_TINY_JS, ADMIN_TINY_JS = [], [], []
-
-if settings.ENABLE_TINYMCE:
- TINYMCE_JS = ["{}tinymce/tinymce.min.js".format(settings.STATIC_URL)]
- FULL_TINY_JS = TINYMCE_JS[:] + \
- ["%schimere/js/textareas.js" % settings.STATIC_URL]
- ADMIN_TINY_JS = TINYMCE_JS[:] + \
- ["%schimere/js/textareas_admin.js" % settings.STATIC_URL]
-
-TINYMCE_LANGUAGES = ['fr_FR']
-
-class TextareaWidgetBase(forms.Textarea):
+class TextareaWidgetBase(Textarea):
"""
Manage the edition of a text using TinyMCE
"""
+
+ def __init__(self, *args, **kwargs):
+ if "tinymce" in settings.INSTALLED_APPS and 'mce_attrs' not in kwargs:
+ kwargs['mce_attrs'] = {'width': 400, 'height': 150}
+ super(TextareaWidgetBase, self).__init__(*args, **kwargs)
+
def render(self, *args, **kwargs):
- if not TINYMCE_JS:
- rendered = super(TextareaWidgetBase, self).render(*args, **kwargs)
- return mark_safe(rendered)
- if 'attrs' not in kwargs:
- kwargs['attrs'] = {}
- if 'class' not in kwargs['attrs']:
- kwargs['attrs']['class'] = ''
- else:
- kwargs['attrs']['class'] += ' '
- kwargs['attrs']['class'] += 'mceEditor'
rendered = super(TextareaWidgetBase, self).render(*args, **kwargs)
- rendered += """
-<style>
-.mce-tinymce{
- position: initial;
-}
-.field-%s label{
- padding: 0.9em 1em;
-}
-</style>
-""" % args[0]
- current_language = ""
- if settings.LANGUAGE_CODE.replace('-', '_') in TINYMCE_LANGUAGES:
- current_language = settings.LANGUAGE_CODE.replace('-', '_')
- elif settings.LANGUAGE_CODE.split('-')[0] in TINYMCE_LANGUAGES:
- current_language = settings.LANGUAGE_CODE.split('-')[0]
- if current_language:
- rendered += """
- <script type='text/javascript'>
- window.tinymce_lang = "{}";
- </script>
- """.format(current_language)
return mark_safe(rendered)
class FullTextareaWidget(TextareaWidgetBase):
- """
- Manage the edition of a text using TinyMCE
- """
- class Media:
- js = TINYMCE_JS
-
- def render(self, *args, **kwargs):
- if not TINYMCE_JS:
- rendered = super(FullTextareaWidget, self).render(*args, **kwargs)
- return mark_safe(rendered)
-
- if 'attrs' not in kwargs:
- kwargs['attrs'] = {}
- if 'class' not in kwargs['attrs']:
- kwargs['attrs']['class'] = ''
- else:
- kwargs['attrs']['class'] += ' '
- kwargs['attrs']['class'] += 'mceEditor'
- rendered = super(FullTextareaWidget, self).render(*args, **kwargs)
- return mark_safe(rendered)
+ def __init__(self, *args, **kwargs):
+ if "tinymce" in settings.INSTALLED_APPS and 'mce_attrs' not in kwargs:
+ kwargs['mce_attrs'] = {'height': 150}
+ super(TextareaWidgetBase, self).__init__(*args, **kwargs)
class TextareaWidget(TextareaWidgetBase):
- """
- Manage the edition of a text using TinyMCE
- """
- class Media:
- js = FULL_TINY_JS
+ pass
class TextareaAdminWidget(TextareaWidgetBase):
- class Media:
- js = ADMIN_TINY_JS
+ pass
class DatePickerWidget(forms.TextInput):