From d3d0d102ca1043c4f70b83da9abd0702a7b11f74 Mon Sep 17 00:00:00 2001 From: Arcady Usov Date: Sun, 13 Apr 2014 23:51:09 +0600 Subject: [PATCH] Remove hard container relation with initialization CKEditor (initial CKEditor in all DOM-elements with class "CMS_CKEditor"). --- djangocms_text_ckeditor/fields.py | 4 +- .../cms/plugins/widgets/ckeditor.html | 71 +++++++++---------- 2 files changed, 35 insertions(+), 40 deletions(-) diff --git a/djangocms_text_ckeditor/fields.py b/djangocms_text_ckeditor/fields.py index 7685a28..b4382ee 100644 --- a/djangocms_text_ckeditor/fields.py +++ b/djangocms_text_ckeditor/fields.py @@ -11,12 +11,12 @@ except ImportError: class HTMLField(models.TextField): def formfield(self, **kwargs): - defaults = {'widget': TextEditorWidget} + defaults = {'widget': TextEditorWidget(attrs={'class': 'CMS_CKEditor'})} defaults.update(kwargs) # override the admin widget if defaults['widget'] == admin_widgets.AdminTextareaWidget: - defaults['widget'] = TextEditorWidget + defaults['widget'] = TextEditorWidget(attrs={'class': 'CMS_CKEditor'}) return super(HTMLField, self).formfield(**defaults) diff --git a/djangocms_text_ckeditor/templates/cms/plugins/widgets/ckeditor.html b/djangocms_text_ckeditor/templates/cms/plugins/widgets/ckeditor.html index 6bda9bd..969d87a 100644 --- a/djangocms_text_ckeditor/templates/cms/plugins/widgets/ckeditor.html +++ b/djangocms_text_ckeditor/templates/cms/plugins/widgets/ckeditor.html @@ -14,48 +14,43 @@ (function($) { // CMS.$ will be passed for $ $(document).ready(function () { - function initCKEditor(container) { - CMS.CKEditor.init(container, {{ settings|safe }}, { - 'static_url': '{{ STATIC_URL }}',{% if placeholder %} - 'add_plugin_url': '{{ placeholder.get_add_url }}', - 'placeholder_id': {{ placeholder.pk|unlocalize|default:"''" }}, - 'plugin_id': {{ plugin_pk|unlocalize }}, - 'plugin_language': '{{ plugin_language }}', - 'lang': { - 'toolbar': '{% filter escapejs %}{% trans "CMS Plugins" %}{% endfilter %}', - 'add': '{% filter escapejs %}{% trans "Add CMS Plugin" %}{% endfilter %}', - 'edit': '{% filter escapejs %}{% trans "Edit CMS Plugin" %}{% endfilter %}', - 'aria': '{% filter escapejs %}{% trans "CMS Plugins" %}{% endfilter %}' - }, - 'plugins': [ - {% regroup installed_plugins by module as module_list %} - {% for module in module_list %} - { group: '{% filter escapejs %}{% if module.grouper %}{{ module.grouper|capfirst }}{% else %}{% trans "Standard Plugins" %}{% endif %}{% endfilter %}', items: [ - {% for plugin in module.list %} - { 'title': '{% filter escapejs %}{{ plugin.name }}{% endfilter %}', 'type': '{% filter escapejs %}{{ plugin.value }}{% endfilter %}' }{% if not forloop.last %},{% endif %} - {% endfor %} - ]}{% if not forloop.last %},{% endif %} - {% endfor %} - ]{% else %} - 'toolbar': 'HTMLField' - {% endif %} - }); - } + function initCMSCKEditor() { + if ($('.CMS_CKEditor:visible').length > 0) { + $('.CMS_CKEditor:visible').each(function() { + $(this).hide(); - // get the container id - var container = 'id_{{ name }}'; - var prefixPos = container.indexOf('-__prefix__'); - if (prefixPos != - 1) { - // in case the textarea is in an inline, we need to perform some replacements - var name = container.substring(0, prefixPos); - var replacement = $('#' + name + "-TOTAL_FORMS").val(); - container = container.replace('__prefix__', replacement); + var container = $(this).attr('id'); + CMS.CKEditor.init(container, {{ settings|safe }}, { + 'static_url': '{{ STATIC_URL }}',{% if placeholder %} + 'add_plugin_url': '{{ placeholder.get_add_url }}', + 'placeholder_id': {{ placeholder.pk|unlocalize|default:"''" }}, + 'plugin_id': {{ plugin_pk|unlocalize }}, + 'plugin_language': '{{ plugin_language }}', + 'lang': { + 'toolbar': '{% filter escapejs %}{% trans "CMS Plugins" %}{% endfilter %}', + 'add': '{% filter escapejs %}{% trans "Add CMS Plugin" %}{% endfilter %}', + 'edit': '{% filter escapejs %}{% trans "Edit CMS Plugin" %}{% endfilter %}', + 'aria': '{% filter escapejs %}{% trans "CMS Plugins" %}{% endfilter %}' + }, + 'plugins': [ + {% regroup installed_plugins by module as module_list %} + {% for module in module_list %} + { group: '{% filter escapejs %}{% if module.grouper %}{{ module.grouper|capfirst }}{% else %}{% trans "Standard Plugins" %}{% endif %}{% endfilter %}', items: [ + {% for plugin in module.list %} + { 'title': '{% filter escapejs %}{{ plugin.name }}{% endfilter %}', 'type': '{% filter escapejs %}{{ plugin.value }}{% endfilter %}' }{% if not forloop.last %},{% endif %} + {% endfor %} + ]}{% if not forloop.last %},{% endif %} + {% endfor %} + ]{% else %} + 'toolbar': 'HTMLField' + {% endif %} + }); + }); + } } // initialize ckeditor only if the container exists and is ready - $('#'+container).ready(function() { - initCKEditor(container); - }); + initCMSCKEditor(); }); })(CMS.$);