{% load i18n sekizai_tags %}
{% load url from future %}

{% regroup installed_plugins by module as module_list %}
<div id="cms_placeholder-bar-{{ placeholder.pk }}" class="cms_placeholder-bar">
	{% language request.toolbar.language %}
	<div class="cms_submenu">
		<div class="cms_submenu-quicksearch"><label><input type="text" placeholder="{% trans 'Quicksearch' %}" /></label></div>
		<ul class="cms_submenu-dropdown">
			<li><span>{% trans "Settings" %}</span></li>
			<li><a data-rel="copy" href="#">{% trans "Copy all" %}</a></li>
			<li><a data-rel="modal" href="{% url "admin:cms_page_clear_placeholder" placeholder.pk %}" data-name="{{ placeholder_label }}">{% trans "Empty all" %}</a></li>
			<li><a data-rel="modal" href='{% url "admin:stacks_stack_create_stack" placeholder_id=placeholder.pk %}?_popup'>{% trans "Create stack" %}</a></li>
			<li><a data-rel="modal" href='{% url "admin:stacks_stack_insert_stack" placeholder_id=placeholder.pk %}?language_code={{ language }}'>{% trans "Insert stack" %}</a></li>
			{% for module in module_list %}
			<li><span>{% if module.grouper %}{{ module.grouper|capfirst }}{% else %}{% trans "Available plugins" %}{% endif %}</span></li>
			{% for p in module.list %}
			<li><a data-rel="add" href="{{ p.value }}">{{ p.name }}</a></li>
			{% endfor %}
			{% endfor %}
		</ul>
	</div>
	{% endlanguage %}

	<div class="cms_placeholder-title">{{ placeholder_label }}</div>
</div>

{% addtoblock "js" %}
<script type="text/javascript">
(function($) {
// CMS.$ will be passed for $
$(document).ready(function () {
	new CMS.PlaceholderItem('#cms_placeholder-bar-{{ placeholder.pk }}', {
		'type': 'bar',
		'placeholder_id': '{{ placeholder.pk }}',
		'plugin_language': '{{ language }}', // todo not required should be within backend
		'plugin_restriction': [{% for module in module_list %}{% for p in module.list %}"{{ p.value }}"{% if not forloop.last or not forloop.parentloop.last %}, {% endif %}{% endfor %}{% endfor %}],
		'urls': {
			'add_plugin': '{{ placeholder.get_add_url }}',
			'edit_plugin': '{{ placeholder.get_changelist_url }}',
			'copy_plugin': '{% url "admin:cms_page_copy_plugins" %}',
			'stack_plugin': '{% url "admin:stacks_stack_insert_stack" placeholder_id=placeholder.pk %}?language_code={{ language }}'
		}
	});
});
})(CMS.$);
</script>
{% endaddtoblock %}