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

247 lines
6.2 KiB
JavaScript
Raw Normal View History

2014-05-10 22:11:53 -07:00
(function ($, document) {
2015-09-04 09:20:54 -07:00
var pageBackgroundCreated;
2014-05-10 22:11:53 -07:00
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');
2015-10-12 12:09:56 -07:00
elem.classList.add('noFade');
2015-09-04 09:20:54 -07:00
if (!pageBackgroundCreated) {
pageBackgroundCreated = true;
var div = document.createElement('div');
div.classList.add('pageBackground');
document.body.insertBefore(div, document.body.firstChild);
}
2015-06-28 07:45:21 -07:00
return elem;
}
function clearBackdrop() {
var elem = document.documentElement;
elem.classList.remove('backdropContainer');
2015-09-14 05:50:37 -07:00
elem.removeAttribute('data-url');
2015-06-28 07:45:21 -07:00
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-12-23 10:46:01 -07:00
console.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
};
2015-12-14 08:43:03 -07:00
apiClient.getItems(Dashboard.getCurrentUserId(), options).then(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-09-14 05:50:37 -07:00
if (url == elem.getAttribute('data-url')) {
return;
}
elem.setAttribute('data-url', 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-12-14 08:43:03 -07:00
getBackdropItemIds(apiClient, Dashboard.getCurrentUserId(), type, parentId).then(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-09-14 05:50:37 -07:00
var elem = getElement();
elem.style.backgroundImage = "url(css/images/splash.jpg)";
elem.setAttribute('data-url', 'css/images/splash.jpg');
2015-08-28 17:50:39 -07:00
page = $(page)[0];
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-08-16 08:53:30 -07:00
return false;
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-09-07 21:22:38 -07:00
pageClassOn('pagebeforeshow', "page", function () {
2014-05-21 11:38:12 -07:00
var page = this;
2015-09-08 10:07:35 -07:00
if (page.classList.contains('backdropPage')) {
2015-04-12 09:46:29 -07:00
2015-09-08 10:07:35 -07:00
if (enabled()) {
var type = page.getAttribute('data-backdroptype');
2014-06-01 12:41:35 -07:00
2015-09-08 10:07:35 -07:00
var parentId = page.classList.contains('globalBackdropPage') ? '' : LibraryMenu.getTopParentId();
2014-05-10 22:11:53 -07:00
2015-09-08 10:07:35 -07:00
showBackdrop(type, parentId);
2015-04-12 09:46:29 -07:00
2014-06-01 12:41:35 -07:00
} else {
2015-09-08 10:07:35 -07:00
page.classList.remove('backdropPage');
2014-06-01 12:41:35 -07:00
clearBackdrop();
}
2015-09-08 10:07: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,
2015-10-25 22:29:32 -07:00
setDefault: setDefault,
clear: clearBackdrop
2014-08-16 22:38:13 -07:00
};
2014-05-10 22:11:53 -07:00
})(jQuery, document);