{% load i18n l10n admin_static %}

{% block extrastyle %}
<link rel="stylesheet" href="{% static "djangocms_text_ckeditor/css/cms.ckeditor.css" %}" />
{% endblock %}

{% block extrahead %}
<script src="{% static "cms/js/modules/cms.base.js" %}"></script>
<script src="{% static "djangocms_text_ckeditor/ckeditor/ckeditor.js" %}"></script>
<script src="{% static "djangocms_text_ckeditor/js/cms.ckeditor.js" %}"></script>
{% endblock %}

<script>
(function($) {
// CMS.$ will be passed for $
$(document).ready(function () {
	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 }}djangocms_text_ckeditor',{% 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 %}',
						'link': '{% filter escapejs %}{% trans "Link" %}{% 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
	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>
