Allow basic sorting by tag.

This commit is contained in:
Isaac Bythewood
2012-09-28 05:28:31 +00:00
parent 230a6e959f
commit ceda2d2cb0
4 changed files with 119 additions and 94 deletions

View File

@@ -27,6 +27,7 @@ class PinResource(ModelResource): # pylint: disable-msg=R0904
if 'tag' in filters: if 'tag' in filters:
orm_filters['tags__name__in'] = filters['tag'].split(',') orm_filters['tags__name__in'] = filters['tag'].split(',')
return orm_filters return orm_filters
def dehydrate_tags(self, bundle): def dehydrate_tags(self, bundle):

View File

@@ -59,6 +59,15 @@ body {
text-align: center; text-align: center;
} }
.tags {
padding: 13px 0;
}
.tag {
cursor: pointer;
}
#pins { #pins {
top: 70px; top: 70px;
position: absolute; position: absolute;

View File

@@ -1,23 +1,23 @@
/** /**
* Based on Wookmark's endless scroll. * Based on Wookmark's endless scroll.
*/ */
$(window).ready(function () { var apiURL = '/api/pin/?format=json&offset='
var apiURL = '/api/pin/?format=json&offset=' var page = 0;
var page = 0; var handler = null;
var handler = null; var globalTag = null;
var isLoading = false; var isLoading = false;
/** /**
* When scrolled all the way to the bottom, add more tiles. * When scrolled all the way to the bottom, add more tiles.
*/ */
function onScroll(event) { function onScroll(event) {
if(!isLoading) { if(!isLoading) {
var closeToBottom = ($(window).scrollTop() + $(window).height() > $(document).height() - 100); var closeToBottom = ($(window).scrollTop() + $(window).height() > $(document).height() - 100);
if(closeToBottom) loadData(); if(closeToBottom) loadData();
} }
}; };
function applyLayout() { function applyLayout() {
$('#pins').imagesLoaded(function() { $('#pins').imagesLoaded(function() {
// Clear our previous layout handler. // Clear our previous layout handler.
if(handler) handler.wookmarkClear(); if(handler) handler.wookmarkClear();
@@ -30,25 +30,39 @@ $(window).ready(function () {
itemWidth: 242 itemWidth: 242
}); });
}); });
}; };
/** /**
* Loads data from the API. * Loads data from the API.
*/ */
function loadData() { function loadData(tag) {
isLoading = true; isLoading = true;
$('#loader').show(); $('#loader').show();
if (tag !== undefined) {
globalTag = tag;
}
if (tag !== undefined && page != 0) {
$('#pins').html('');
page = 0;
if (tag != null) $('.tags').html('<span class="label tag" onclick="loadData(null)">' + tag + ' x</span>');
else $('.tags').html('');
}
var loadURL = apiURL+(page*20);
if (globalTag !== null) loadURL += "&tag=" + tag;
$.ajax({ $.ajax({
url: apiURL+(page*20), url: loadURL,
success: onLoadData success: onLoadData
}); });
}; };
/** /**
* Receives data from the API, creates HTML for images and updates the layout * Receives data from the API, creates HTML for images and updates the layout
*/ */
function onLoadData(data) { function onLoadData(data) {
data = data.objects; data = data.objects;
isLoading = false; isLoading = false;
$('#loader').hide(); $('#loader').hide();
@@ -72,7 +86,7 @@ $(window).ready(function () {
if (image.tags) { if (image.tags) {
html += '<p>'; html += '<p>';
for (tag in image.tags) { for (tag in image.tags) {
html += image.tags[tag] + ', '; html += '<span class="label tag" onclick="loadData(\'' + image.tags[tag] + '\')">' + image.tags[tag] + '</span> ';
} }
html += '</p>'; html += '</p>';
} }
@@ -82,18 +96,17 @@ $(window).ready(function () {
$('#pins').append(html); $('#pins').append(html);
applyLayout(); applyLayout();
}; };
$(document).ready(new function() { $(document).ready(new function() {
$(document).bind('scroll', onScroll); $(document).bind('scroll', onScroll);
loadData(); loadData();
}); });
/** /**
* On clicking an image show fancybox original. * On clicking an image show fancybox original.
*/ */
$('.fancybox').fancybox({ $('.fancybox').fancybox({
openEffect: 'none', openEffect: 'none',
closeEffect: 'none' closeEffect: 'none'
});
}); });

View File

@@ -18,7 +18,9 @@
<body> <body>
<div class="navbar navbar-fixed-top"> <div class="navbar navbar-fixed-top">
<div class="navbar-inner"> <div class="navbar-inner">
<a href="{% url core:home %}" class="brand">{{ site_name }}</a> <a href="{% url core:home %}" class="brand pull-left">{{ site_name }}</a>
<div class="tags pull-left"></div>
<ul class="nav pull-right"> <ul class="nav pull-right">
{% if user.is_authenticated %} {% if user.is_authenticated %}