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

131 lines
3.3 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,
quality: 80
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();
2014-06-01 12:41:35 -07:00
2014-05-21 11:38:12 -07:00
var val = LocalSettings.val('enableBackdrops', userId);
2014-07-11 19:31:08 -07:00
// For bandwidth
return val == '1' || (val != '0' && !$.browser.mobile);
2014-05-21 11:38:12 -07:00
}
2014-06-01 12:41:35 -07:00
$(document).on('pagebeforeshow', ".page", function () {
2014-05-21 11:38:12 -07:00
var page = this;
2014-06-01 12:41:35 -07:00
if ($(page).hasClass('backdropPage')) {
if (enabled()) {
var type = page.getAttribute('data-backdroptype');
showBackdrop(type);
2014-05-10 22:11:53 -07:00
2014-06-01 12:41:35 -07:00
} else {
$(page).removeClass('backdropPage');
clearBackdrop();
}
2014-05-21 11:38:12 -07:00
} else {
clearBackdrop();
2014-05-10 22:11:53 -07:00
}
});
})(jQuery, document);