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

234 lines
5.8 KiB
JavaScript
Raw Normal View History

2014-05-10 22:11:53 -07:00
(function ($, document) {
function getElement() {
2015-06-20 17:49:42 -07:00
//var elem = $('.backdropContainer');
2014-05-10 22:11:53 -07:00
2015-06-20 17:49:42 -07:00
//if (!elem.length) {
2014-05-10 22:11:53 -07:00
2015-06-20 17:49:42 -07:00
// elem = $('<div class="backdropContainer"></div>').prependTo(document.body);
//}
2014-05-10 22:11:53 -07:00
2015-06-28 07:45:21 -07:00
var elem = document.documentElement;
elem.classList.add('backdropContainer');
return elem;
}
function clearBackdrop() {
var elem = document.documentElement;
elem.classList.remove('backdropContainer');
elem.style.backgroundImage = '';
2014-05-10 22:11:53 -07:00
}
function getRandom(min, max) {
return Math.floor(Math.random() * (max - min) + min);
}
2014-10-29 15:01:02 -07:00
function getBackdropItemIds(apiClient, userId, types, parentId) {
2014-05-10 22:11:53 -07:00
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-07-15 12:16:16 -07:00
var data = sessionStore.getItem(key);
2014-05-10 22:11:53 -07:00
if (data) {
2015-06-26 20:27:38 -07:00
Logger.log('Found backdrop id list in cache. Key: ' + key)
2014-05-10 22:11:53 -07:00
data = JSON.parse(data);
deferred.resolveWith(null, [data]);
} else {
var options = {
2014-05-18 14:23:03 -07:00
SortBy: "IsFavoriteOrLiked,Random",
2014-10-15 20:26:39 -07:00
Limit: 20,
2014-05-10 22:11:53 -07:00
Recursive: true,
IncludeItemTypes: types,
ImageTypes: "Backdrop",
//Ids: "8114409aa00a2722456c08e298f90bed",
ParentId: parentId
};
2014-10-29 15:01:02 -07:00
apiClient.getItems(Dashboard.getCurrentUserId(), options).done(function (result) {
2014-05-10 22:11:53 -07:00
var images = result.Items.map(function (i) {
return {
id: i.Id,
tag: i.BackdropImageTags[0]
};
});
2014-07-15 12:16:16 -07:00
sessionStore.setItem(key, JSON.stringify(images));
2014-05-10 22:11:53 -07:00
deferred.resolveWith(null, [images]);
});
}
return deferred.promise();
}
2015-05-09 21:29:04 -07:00
function setBackdropImage(elem, url) {
2015-06-28 07:45:21 -07:00
ImageLoader.lazyImage(elem, url);
2015-05-09 21:29:04 -07:00
}
2015-04-12 09:46:29 -07:00
function showBackdrop(type, parentId) {
2014-05-10 22:11:53 -07:00
2015-05-18 15:23:03 -07:00
var apiClient = window.ApiClient;
2014-10-29 15:01:02 -07:00
if (!apiClient) {
return;
}
2015-04-12 09:46:29 -07:00
getBackdropItemIds(apiClient, Dashboard.getCurrentUserId(), type, parentId).done(function (images) {
2014-05-10 22:11:53 -07:00
2015-04-12 09:46:29 -07:00
if (images.length) {
2014-05-10 22:11:53 -07:00
2015-04-12 09:46:29 -07:00
var index = getRandom(0, images.length - 1);
var item = images[index];
2014-05-10 22:11:53 -07:00
2015-04-12 09:46:29 -07:00
var screenWidth = $(window).width();
2015-04-12 09:46:29 -07:00
var imgUrl = apiClient.getScaledImageUrl(item.id, {
type: "Backdrop",
tag: item.tag,
maxWidth: screenWidth,
2015-06-07 14:21:30 -07:00
quality: 50
2015-04-12 09:46:29 -07:00
});
2014-05-10 22:11:53 -07:00
2015-05-09 21:29:04 -07:00
setBackdropImage(getElement(), imgUrl);
2014-05-10 22:11:53 -07:00
2015-04-12 09:46:29 -07:00
} else {
2014-05-10 22:11:53 -07:00
2015-04-12 09:46:29 -07:00
clearBackdrop();
}
});
2014-05-10 22:11:53 -07:00
}
2015-05-12 21:55:19 -07:00
function setDefault(page) {
2015-05-27 22:51:48 -07:00
2015-06-29 11:45:42 -07:00
getElement().style.backgroundImage = "url(css/images/splash.jpg)";
2015-05-12 21:55:19 -07:00
2015-06-29 11:45:42 -07:00
page.classList.add('backdropPage');
page.classList.add('staticBackdropPage');
2014-05-10 22:11:53 -07:00
}
2015-05-09 21:29:04 -07:00
function isEnabledByDefault() {
if (AppInfo.hasLowImageBandwidth) {
return false;
}
2015-05-24 11:33:28 -07:00
if ($.browser.mobile) {
return false;
2015-05-09 21:29:04 -07:00
}
2015-07-19 20:43:13 -07:00
return !AppInfo.isTouchPreferred;
2015-05-09 21:29:04 -07:00
}
2014-05-21 11:38:12 -07:00
function enabled() {
2014-05-10 22:11:53 -07:00
2014-05-21 11:38:12 -07:00
var userId = Dashboard.getCurrentUserId();
2014-06-01 12:41:35 -07:00
2015-05-20 10:29:26 -07:00
var val = appStorage.getItem('enableBackdrops-' + userId);
2014-05-21 11:38:12 -07:00
2014-07-11 19:31:08 -07:00
// For bandwidth
2015-05-09 21:29:04 -07:00
return val == '1' || (val != '0' && isEnabledByDefault());
2014-05-21 11:38:12 -07:00
}
2014-08-16 22:38:13 -07:00
function setBackdrops(page, items) {
2015-07-06 07:20:23 -07:00
var images = items.map(function (i) {
2014-08-16 22:38:13 -07:00
2015-07-06 07:20:23 -07:00
if (i.BackdropImageTags.length > 0) {
return {
id: i.Id,
tag: i.BackdropImageTags[0]
};
}
2014-08-16 22:38:13 -07:00
2015-07-06 07:20:23 -07:00
if (i.ParentBackdropItemId && i.ParentBackdropImageTags && i.ParentBackdropImageTags.length) {
return {
id: i.ParentBackdropItemId,
tag: i.ParentBackdropImageTags[0]
};
}
return null;
}).filter(function (i) {
return i != null;
2014-08-16 22:38:13 -07:00
});
if (images.length) {
2015-06-28 07:45:21 -07:00
page.classList.add('backdropPage');
2014-08-16 22:38:13 -07:00
var index = getRandom(0, images.length - 1);
var item = images[index];
var screenWidth = $(window).width();
var imgUrl = ApiClient.getScaledImageUrl(item.id, {
type: "Backdrop",
tag: item.tag,
maxWidth: screenWidth,
2015-06-07 14:21:30 -07:00
quality: 50
2014-08-16 22:38:13 -07:00
});
2015-05-09 21:29:04 -07:00
setBackdropImage(getElement(), imgUrl);
} else {
2015-06-28 07:45:21 -07:00
page.classList.remove('backdropPage');
2015-05-09 21:29:04 -07:00
}
}
2015-05-27 22:51:48 -07:00
2015-05-09 21:29:04 -07:00
function setBackdropUrl(page, url) {
if (url) {
2015-06-28 07:45:21 -07:00
page.classList.add('backdropPage');
2015-05-09 21:29:04 -07:00
setBackdropImage(getElement(), url);
2014-08-16 22:38:13 -07:00
} else {
2015-06-28 07:45:21 -07:00
page.classList.remove('backdropPage');
2015-05-09 21:29:04 -07:00
clearBackdrop();
2014-08-16 22:38:13 -07:00
}
}
2015-06-28 07:45:21 -07:00
Events.on(document, 'pagebeforeshowready', ".page", function () {
2014-05-21 11:38:12 -07:00
var page = this;
2015-06-28 07:45:21 -07:00
if (!page.classList.contains('staticBackdropPage')) {
2015-04-12 09:46:29 -07:00
2015-06-28 07:45:21 -07:00
if (page.classList.contains('backdropPage')) {
2014-06-01 12:41:35 -07:00
2015-01-17 22:45:10 -07:00
if (enabled()) {
var type = page.getAttribute('data-backdroptype');
2014-05-10 22:11:53 -07:00
2015-06-28 07:45:21 -07:00
var parentId = page.classList.contains('globalBackdropPage') ? '' : LibraryMenu.getTopParentId();
2015-04-12 09:46:29 -07:00
2015-06-16 10:37:49 -07:00
showBackdrop(type, parentId);
2015-01-17 22:45:10 -07:00
} else {
2015-06-28 07:45:21 -07:00
page.classList.remove('backdropPage');
2015-01-17 22:45:10 -07:00
clearBackdrop();
}
2014-06-01 12:41:35 -07:00
} else {
clearBackdrop();
}
2014-05-10 22:11:53 -07:00
}
});
2014-08-16 22:38:13 -07:00
window.Backdrops = {
2015-05-09 21:29:04 -07:00
setBackdrops: setBackdrops,
2015-05-12 21:55:19 -07:00
setBackdropUrl: setBackdropUrl,
setDefault: setDefault
2014-08-16 22:38:13 -07:00
};
2014-05-10 22:11:53 -07:00
})(jQuery, document);