kb: add tag cloud in default view

This commit is contained in:
Frédéric Péters 2015-11-30 19:00:08 +01:00
parent 98869b9a57
commit ab31bf20df
4 changed files with 36 additions and 0 deletions

View File

@ -4,6 +4,14 @@
<input type="text" autocomplete="off" placeholder="{% trans 'Keywords...' %}" data-autocomplete-json="{% url 'kb-page-search-json' %}" name="q"/>
<ul class="result">
</ul>
{% if tags %}
<div class="tagcloud">
{% for tag in tags %}
<span style="font-size: {{tag.font_size}}">{{tag.name}}</span>
{% endfor %}
</div>
{% endif %}
</div>
<div class="page">
</div>

View File

@ -21,6 +21,7 @@ from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.core.exceptions import PermissionDenied
from django.core.urlresolvers import reverse_lazy
from django.db.models import Count
from django.http import HttpResponse, HttpResponseRedirect
from django.template import RequestContext
from django.views.decorators.csrf import csrf_exempt
@ -32,6 +33,7 @@ from haystack.generic_views import SearchView
from haystack.query import SearchQuerySet
from reversion.models import Version
from reversion.revisions import default_revision_manager
from taggit.models import Tag
from .models import Page
from .forms import PageForm
@ -164,6 +166,25 @@ class KbZone(TemplateView):
def get_context_data(self, **kwargs):
context = super(KbZone, self).get_context_data(**kwargs)
context['form'] = SearchForm()
context['tags'] = Tag.objects.all().annotate(
num_times=Count('taggit_taggeditem_items')).filter(num_times__gt=0)
num_times = context['tags'].values_list('num_times', flat=True)
delta = max(num_times) - min(num_times)
for tag in context['tags']:
if delta == 0:
tag.font_size = 'normal'
else:
factor = 1.0 * (tag.num_times - min(num_times)) / delta
if factor < 0.2:
tag.font_size = 'x-small'
elif factor < 0.4:
tag.font_size = 'small'
elif factor < 0.6:
tag.font_size = 'normal'
elif factor < 0.8:
tag.font_size = 'large'
else:
tag.font_size = 'x-large'
return context
zone = csrf_exempt(KbZone.as_view())

View File

@ -562,3 +562,7 @@ div.old-version.warning-notice form {
right: 1ex;
top: 1ex;
}
div.tagcloud {
padding: 1ex;
}

View File

@ -246,6 +246,9 @@ $(function() {
);
return false;
});
$('.kb').delegate('.tagcloud span', 'click', function() {
$('.kb input').val($(this).text()).trigger('keyup');
});
$('.kb').delegate('ul.result a', 'click', function() {
var page_slug = $(this).data('page-slug');
var fragment_url = $(this).parents('[data-page-fragment-url]').data('page-fragment-url') + page_slug + '/';