Merge pull request #119 from itcrab/bugfix/many-editors-in-inline-admin

fix for admin inline when on page many ckeditors.
This commit is contained in:
Iacopo Spalletti 2014-05-18 11:33:28 +02:00
commit c37187ac11
3 changed files with 70 additions and 47 deletions

View File

@ -48,22 +48,24 @@ $(document).ready(function () {
},
init: function (container, options, settings) {
this.container = $('#' + container);
if ($('#' + container).length > 0) {
this.container = $('#' + container);
// add additional settings to options
this.options.toolbar = settings.toolbar;
this.options = $.extend(true, {
'settings': settings
}, this.options, options);
// add additional settings to options
this.options.toolbar = settings.toolbar;
this.options = $.extend(true, {
'settings': settings
}, this.options, options);
// add additional plugins (autoloads plugins.js)
CKEDITOR.plugins.addExternal('cmsplugins', settings.static_url + 'ckeditor_plugins/cmsplugins/');
// add additional plugins (autoloads plugins.js)
CKEDITOR.plugins.addExternal('cmsplugins', settings.static_url + 'ckeditor_plugins/cmsplugins/');
// render ckeditor
this.editor = CKEDITOR.replace(container, this.options);
// render ckeditor
this.editor = CKEDITOR.replace(container, this.options);
// add additional styling
CKEDITOR.on('instanceReady', $.proxy(CMS.CKEditor, 'setup'));
// add additional styling
CKEDITOR.on('instanceReady', $.proxy(CMS.CKEditor, 'setup'));
}
},
// setup is called after ckeditor has been initialized

View File

@ -14,44 +14,54 @@
(function($) {
// CMS.$ will be passed for $
$(document).ready(function () {
// 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);
function initCMSCKEditor() {
if ($('.{{ ckeditor_class }}:visible').length > 0) {
$('.{{ ckeditor_class }}:visible').each(function() {
$(this).hide();
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() {
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 %}
});
});
initCMSCKEditor();
// initialize ckeditor in admin inline for "add-row" link
var add_buttons = $('.add-row a');
if (add_buttons.length > 0) {
if (add_buttons.data('isInitCMSCKEditor') !== 'yes') {
add_buttons.click(function () {
initCMSCKEditor();
});
add_buttons.data('isInitCMSCKEditor', 'yes');
}
}
});
})(CMS.$);
</script>

View File

@ -16,6 +16,16 @@ class TextEditorWidget(Textarea):
installed_plugins is a list of plugins to display that are text_enabled
"""
if attrs is None:
attrs = {}
self.ckeditor_class = 'CMS_CKEditor'
if self.ckeditor_class not in attrs.get('class', '').join(' '):
new_class = attrs.get('class', '') + ' %s' % self.ckeditor_class
attrs.update({
'class': new_class.strip()
})
super(TextEditorWidget, self).__init__(attrs)
self.installed_plugins = installed_plugins
self.pk = pk
@ -28,6 +38,7 @@ class TextEditorWidget(Textarea):
def render_additions(self, name, value, attrs=None):
language = get_language().split('-')[0]
context = {
'ckeditor_class': self.ckeditor_class,
'name': name,
'language': language,
'settings': language.join(json.dumps(text_settings.CKEDITOR_SETTINGS).split("{{ language }}")),