Files
Pinry/pinry/static/js/pinry.js

178 lines
6.2 KiB
JavaScript
Raw Normal View History

2013-03-03 22:32:22 +00:00
/**
* Pinry
* Descrip: Core of pinry, loads and tiles pins.
* Authors: Pinry Contributors
* Updated: Apr 5th, 2013
2013-03-03 22:32:22 +00:00
* Require: jQuery, Pinry JavaScript Helpers
*/
$(window).load(function() {
/**
* tileLayout will simply tile/retile the block/pin container when run. This
2019-02-21 18:29:56 +08:00
* was put into a function in order to adjust frequently on screen size
* changes.
*/
2013-03-12 06:12:09 +00:00
window.tileLayout = function() {
var blockContainer = $('#pins'),
blocks = blockContainer.children('.pin'),
blockMargin = 15,
blockWidth = 240,
rowSize = Math.floor(blockContainer.width()/(blockWidth+blockMargin)),
colHeights = [],
rowMargins = [],
marginLeft = 0;
2012-09-28 05:28:31 +00:00
// Fill our colHeights array with 0 for each row we have
for (var i=0; i < rowSize; i++) colHeights[i] = 0;
// Fill out our rowMargins which will be static after this
for (var i=0; i < rowSize; i++) {
// Our first item has a special margin to keep things centered
if (i == 0) rowMargins[0] = (blockContainer.width()-rowSize*(blockWidth+blockMargin))/2;
else rowMargins[i] = rowMargins[i-1]+(blockWidth+blockMargin);
}
// Loop through every block
for (var b=0; b < blocks.length; b++) {
// Get the jQuery object of the current block
block = blocks.eq(b);
// Position our new pin in the shortest column
var sCol = 0;
for (var i=0; i < rowSize; i++) {
if (colHeights[sCol] > colHeights[i]) sCol = i;
}
block.css({
'margin-left': rowMargins[sCol],
2013-03-03 22:32:22 +00:00
'margin-top': colHeights[sCol],
});
block.fadeIn(300);
colHeights[sCol] += block.height()+(blockMargin);
}
2013-03-12 06:12:09 +00:00
// Edit pin if pencil icon clicked
$('.glyphicon-pencil').each(function() {
2013-03-12 06:12:09 +00:00
var thisPin = $(this);
$(this).off('click');
$(this).click(function() {
$(this).off('click');
pinForm($(this).data('id'));
});
});
2013-03-03 22:32:22 +00:00
// Delete pin if trash icon clicked
$('.glyphicon-trash').each(function() {
2013-03-03 22:32:22 +00:00
var thisPin = $(this);
$(this).off('click');
2013-03-03 22:32:22 +00:00
$(this).click(function() {
$(this).off('click');
var promise = deletePinData($(this).data('id'));
promise.success(function() {
thisPin.closest('.pin').remove();
tileLayout();
2013-03-03 14:30:06 +00:00
});
promise.error(function() {
2016-02-04 19:29:48 +00:00
message('Problem deleting image.', 'alert alert-danger');
});
2013-03-03 14:30:06 +00:00
});
2013-03-03 22:32:22 +00:00
});
2013-03-03 14:30:06 +00:00
2013-03-09 21:39:01 +01:00
// Show edit-buttons only on mouse over
$('.pin').each(function(){
var thisPin = $(this);
thisPin.find('.editable').hide();
thisPin.off('hover');
2013-03-09 21:39:01 +01:00
thisPin.hover(function() {
thisPin.find('.editable').stop(true, true).fadeIn(300);
}, function() {
thisPin.find('.editable').stop(true, false).fadeOut(300);
2013-03-09 21:39:01 +01:00
});
});
$('.spinner').css('display', 'none');
blockContainer.css('height', colHeights.sort().slice(-1)[0]);
2012-09-28 05:28:31 +00:00
}
/**
* On scroll load more pins from the server
*/
window.scrollHandler = function() {
var windowPosition = $(window).scrollTop() + $(window).height();
var bottom = $(document).height() - 100;
if(windowPosition > bottom) loadPins();
}
2012-09-28 05:28:31 +00:00
/**
* Load our pins using the pins template into our UI, be sure to define a
* offset outside the function to keep a running tally of your location.
*/
2019-02-21 18:29:56 +08:00
function isPinEditable(pinObject) {
return pinObject.submitter.username === currentUser.username
}
function loadPins() {
// Disable scroll
$(window).off('scroll');
// Show our loading symbol
$('.spinner').css('display', 'block');
// Fetch our pins from the api using our current offset
2019-02-21 18:29:56 +08:00
var apiUrl = API_BASE + 'pins/?format=json&ordering=-id&limit=50&offset='+String(offset);
2013-02-26 05:56:42 +00:00
if (tagFilter) apiUrl = apiUrl + '&tag=' + tagFilter;
2013-03-03 22:48:30 +00:00
if (userFilter) apiUrl = apiUrl + '&submitter__username=' + userFilter;
2019-02-21 18:29:56 +08:00
$.get(apiUrl, function(pins_page) {
2013-02-21 07:43:52 +00:00
// Set which items are editable by the current user
2019-02-21 18:29:56 +08:00
var pins = pins_page.results;
for (var i=0; i < pins.length; i++) {
pins[i].editable = isPinEditable(pins[i]);
pins[i].tags.sort(function (a, b) {
return a.toLowerCase().localeCompare(b.toLowerCase());
});
}
2013-02-21 07:43:52 +00:00
// Use the fetched pins as our context for our pins template
var template = Handlebars.compile($('#pins-template').html());
2019-02-21 18:29:56 +08:00
var html = template({pins: pins});
// Append the newly compiled data to our container
$('#pins').append(html);
// We need to then wait for images to load in and then tile
tileLayout();
lightbox();
$('#pins').ajaxStop(function() {
$('img').load(function() {
$(this).fadeIn(300);
});
});
2019-02-21 18:29:56 +08:00
if (pins.length < apiLimitPerPage) {
$('.spinner').css('display', 'none');
if ($('#pins').length !== 0) {
2013-03-03 22:32:22 +00:00
var theEnd = document.createElement('div');
theEnd.id = 'the-end';
$(theEnd).html('&mdash; End &mdash;');
$(theEnd).css('padding', 50);
2013-03-03 22:32:22 +00:00
$('body').append(theEnd);
}
} else {
$(window).scroll(scrollHandler);
}
});
// Up our offset, it's currently defined as 50 in our settings
offset += apiLimitPerPage;
2012-09-28 05:28:31 +00:00
}
2012-04-26 03:44:16 +00:00
// Set offset for loadPins and do our initial load
var offset = 0;
loadPins();
// If our window gets resized keep the tiles looking clean and in our window
$(window).resize(function() {
tileLayout();
lightbox();
})
2012-04-26 03:44:16 +00:00
});