jellyfin-web/dashboard-ui/scripts/backdrops.js

123 lines
3.1 KiB
JavaScript
Raw Normal View History

2014-05-10 22:11:53 -07:00
(function ($, document) {
function getElement() {
var elem = $('.backdropContainer');
if (!elem.length) {
elem = $('<div class="backdropContainer"></div>').prependTo(document.body);
}
return elem;
}
function getRandom(min, max) {
return Math.floor(Math.random() * (max - min) + min);
}
function getBackdropItemIds(userId, types, parentId) {
2014-05-18 14:23:03 -07:00
var key = 'backdrops2_' + userId + (types || '') + (parentId || '');
2014-05-10 22:11:53 -07:00
var deferred = $.Deferred();
2014-05-11 15:38:10 -07:00
var data = sessionStorage.getItem(key);
2014-05-10 22:11:53 -07:00
if (data) {
console.log('Found backdrop id list in cache. Key: ' + key)
data = JSON.parse(data);
deferred.resolveWith(null, [data]);
} else {
var options = {
2014-05-18 14:23:03 -07:00
SortBy: "IsFavoriteOrLiked,Random",
2014-05-10 22:11:53 -07:00
Limit: 50,
Recursive: true,
IncludeItemTypes: types,
ImageTypes: "Backdrop",
//Ids: "8114409aa00a2722456c08e298f90bed",
ParentId: parentId
};
ApiClient.getItems(Dashboard.getCurrentUserId(), options).done(function (result) {
var images = result.Items.map(function (i) {
return {
id: i.Id,
tag: i.BackdropImageTags[0]
};
});
2014-05-11 15:38:10 -07:00
sessionStorage.setItem(key, JSON.stringify(images));
2014-05-10 22:11:53 -07:00
deferred.resolveWith(null, [images]);
});
}
return deferred.promise();
}
function showBackdrop(type) {
getBackdropItemIds(Dashboard.getCurrentUserId(), type, LibraryMenu.getTopParentId()).done(function (images) {
if (images.length) {
var index = getRandom(0, images.length - 1);
var item = images[index];
var screenWidth = $(window).width();
var imgUrl = ApiClient.getScaledImageUrl(item.id, {
2014-05-10 22:11:53 -07:00
type: "Backdrop",
tag: item.tag,
maxWidth: screenWidth
2014-05-10 22:11:53 -07:00
});
getElement().css('backgroundImage', 'url(\'' + imgUrl + '\')');
} else {
clearBackdrop();
}
});
}
function clearBackdrop() {
$('.backdropContainer').css('backgroundImage', '');
}
2014-05-21 11:38:12 -07:00
function enabled() {
2014-05-10 22:11:53 -07:00
// Gets real messy and jumps around the page when scrolling
// Can be reviewed later.
if ($.browser.msie) {
2014-05-21 11:38:12 -07:00
return false;
}
var userId = Dashboard.getCurrentUserId();
var val = LocalSettings.val('enableBackdrops', userId);
return val == '1';
2014-05-21 11:38:12 -07:00
}
$(document).on('pagebeforeshow', ".backdropPage", function () {
var page = this;
if (enabled()) {
2014-05-10 22:11:53 -07:00
var type = page.getAttribute('data-backdroptype');
showBackdrop(type);
2014-05-21 11:38:12 -07:00
} else {
$(page).removeClass('backdropPage');
clearBackdrop();
2014-05-10 22:11:53 -07:00
}
});
})(jQuery, document);