list.html 4.94 KB
{% load static i18n pagination permissions_tags class_filter %}
{% load django_bootstrap_breadcrumbs %}

{% subject_permissions request.user topic.subject as has_subject_permissions %}

<div id="resource_{{ topic.slug }}_topic" class="list-group resource_list">
	{% for resource in topic.resource_topic.all %}
		{% resource_permissions request.user resource as has_resource_permissions %}

		{% if has_resource_permissions %}
	  		<div class="list-group-item {% if not resource.visible %}disabled{% endif %}">
	  			<input type="hidden" class="id_inp_resource" name="id" value="{{ resource.id }}" />
		    	<input type="hidden" class="order_inp_resource" name="order" value="{{ resource.order }}" />
		    	<input type="hidden" class="url_order_resource" value="{% url 'topics:update_resource_order' %}" />

	  			<h4 class="pull-left list-group-item-heading">
	  				<a data-href="{{ resource.access_link }}" class="resource_link" {% if resource.show_window %}target="_blank"{% endif %}>

						{{ resource.name }}
	  				</a>
				</h4>

				{% if has_subject_permissions %}
					<div class="pull-right category-card-items">
		                <a><i class="fa fa-arrows" aria-hidden="true"></i></a>
		                <span class="btn-group pull-right">
			                <button class="btn btn-sm btn_menu" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
			                	<i class="fa fa-ellipsis-v" aria-hidden="true"></i>
			                </button>
			                <ul class="dropdown-menu pull-right" role="menu" aria-labelledby="moreResources">
			                    <li><a href="{% url resource.update_link topic.slug resource.slug %}" class="edit"><i class="fa fa-pencil fa-fw" aria-hidden="true"></i>{% trans 'Edit' %}</a></li>
                                {% if resource|class_name == 'filelink' %}
			                    	<li><a href="{% url 'file_links:get_chart' resource.slug %}" class="edit"><i class="fa fa-line-chart fa-fw" aria-hidden="true"></i>{% trans 'Reports' %}</a></li>
			                    {% endif %}
			                    {% if resource|class_name == 'webpage' %}
			                    	<li><a href="{% url 'webpages:get_chart' resource.slug %}" class="edit"><i class="fa fa-line-chart fa-fw" aria-hidden="true"></i>{% trans 'Reports' %}</a></li>
			                    {% endif %}
                                {% if resource|class_name == 'pdffile' %}
			                    	<li><a href="{% url 'pdf_files:get_chart' resource.slug %}" class="edit"><i class="fa fa-line-chart fa-fw" aria-hidden="true"></i>{% trans 'Reports' %}</a></li>
			                    {% endif %}
			                    {% if resource|class_name == 'goals' %}
			                    	<li><a href="{% url 'goals:reports' resource.slug %}" class="edit"><i class="fa fa-file-pdf-o fa-fw" aria-hidden="true"></i>{% trans 'Reports' %}</a></li>
			                    {% endif %}
			                    <li><a href="javascript:delete_resource('{% url resource.delete_link resource.slug %}')" class="delete"><i class="fa fa-trash fa-fw" aria-hidden="true"></i>{% trans 'Remove' %}</a></li>
			                </ul>
			            </span>
		        	</div>
		        {% endif %}
		        <br clear="all" />
				{% autoescape off %}
					{{ resource.brief_description }}
				{% endautoescape %}
	  		</div>
	  	{% endif %}
  	{% endfor %}
</div>
<script type="text/javascript">
	function delete_resource(url) {
	    $('.modal').remove();

	    $.get(url, function (modal) {
	        $("#resource_{{ topic.slug }}_topic").parent().after(modal);

	        $('.modal').modal('show');
	    });
	}

	$(".list-group-item").unbind().on('click', function (e) {
		var arrow = $(this).find('i.fa-arrows').is(e.target),
			menu = $(this).find('i.fa-ellipsis-v').is(e.target),
			btn = $(this).find('button.btn_menu').is(e.target),
			del = $(this).find('a.delete').is(e.target),
			upd = $(this).find('a.edit').is(e.target);

		if (!arrow && !menu && !del && !upd && !btn) {
			var link = $(this).find('.resource_link').data('href'),
				target = $(this).find('.resource_link').attr('target');

			if (typeof(target) != 'undefined') {
				window.open(link, target);
			} else {
				window.location = link;
			}
		}
	});

	$("#resource_{{ topic.slug }}_topic").sortable({
	    delay: 100,
	    distance: 5,
	    handle: 'i.fa-arrows',
	    update: function( event, ui ) {
	  		var cont = 1;
	  		var data = [];

	  		$("#resource_{{ topic.slug }}_topic").find('.order_inp_resource').each(function () {
	  			$(this).val(cont++);

	  			data.push({
	  				'resource_id': $(this).parent().find('.id_inp_resource').val(),
	  				'resource_order': $(this).val()
	  			});
	  		});

	  		data = JSON.stringify(data);

	  		sendUpdateResource(data);
	    },
	});

	function sendUpdateResource(data) {
		$.ajax({
			url: $('.url_order_resource').val(),
			dataType: 'json',
			data: {'data': data},
			success: function(response) {
				console.log(response);
			},
			error: function(response) {
				console.log(response);
			}
		});
	}
</script>