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);
|
|
|
|
|
}
|
|
|
|
|
|
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) {
|
|
|
|
|
|
|
|
|
|
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-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) {
|
|
|
|
|
|
|
|
|
|
elem.lazyImage(url);
|
|
|
|
|
}
|
|
|
|
|
|
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();
|
2014-05-23 16:58:28 -07:00
|
|
|
|
|
2015-04-12 09:46:29 -07:00
|
|
|
|
var imgUrl = apiClient.getScaledImageUrl(item.id, {
|
|
|
|
|
type: "Backdrop",
|
|
|
|
|
tag: item.tag,
|
|
|
|
|
maxWidth: screenWidth,
|
|
|
|
|
quality: 80
|
|
|
|
|
});
|
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) {
|
|
|
|
|
|
|
|
|
|
var backdropContainer = $('.backdropContainer');
|
|
|
|
|
|
|
|
|
|
if (backdropContainer.length) {
|
|
|
|
|
backdropContainer.css('backgroundImage', 'url(css/images/splash.jpg)');
|
|
|
|
|
} else {
|
|
|
|
|
$(document.body).prepend('<div class="backdropContainer" style="background-image:url(css/images/splash.jpg);top:0;"></div>');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$(page).addClass('backdropPage staticBackdropPage');
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-10 22:11:53 -07:00
|
|
|
|
function clearBackdrop() {
|
|
|
|
|
|
|
|
|
|
$('.backdropContainer').css('backgroundImage', '');
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-09 21:29:04 -07:00
|
|
|
|
function isEnabledByDefault() {
|
|
|
|
|
|
|
|
|
|
if (AppInfo.hasLowImageBandwidth) {
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-11 19:32:53 -07:00
|
|
|
|
// It flickers too much in IE
|
|
|
|
|
if ($.browser.msie) {
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-09 21:29:04 -07:00
|
|
|
|
if (!$.browser.mobile) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var screenWidth = $(window).width();
|
|
|
|
|
|
|
|
|
|
return screenWidth >= 600;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
2014-07-21 18:29:06 -07:00
|
|
|
|
var val = store.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) {
|
|
|
|
|
|
|
|
|
|
var images = items.filter(function (i) {
|
|
|
|
|
|
|
|
|
|
return i.BackdropImageTags.length > 0;
|
|
|
|
|
|
|
|
|
|
}).map(function (i) {
|
|
|
|
|
return {
|
|
|
|
|
id: i.Id,
|
|
|
|
|
tag: i.BackdropImageTags[0]
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (images.length) {
|
|
|
|
|
$(page).addClass('backdropPage');
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
quality: 80
|
|
|
|
|
});
|
|
|
|
|
|
2015-05-09 21:29:04 -07:00
|
|
|
|
setBackdropImage(getElement(), imgUrl);
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
$(page).removeClass('backdropPage');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function setBackdropUrl(page, url) {
|
|
|
|
|
|
|
|
|
|
if (url) {
|
|
|
|
|
$(page).addClass('backdropPage');
|
|
|
|
|
|
|
|
|
|
setBackdropImage(getElement(), url);
|
2014-08-16 22:38:13 -07:00
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
$(page).removeClass('backdropPage');
|
2015-05-09 21:29:04 -07:00
|
|
|
|
clearBackdrop();
|
2014-08-16 22:38:13 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-19 12:15:40 -07:00
|
|
|
|
$(document).on('pagebeforeshowready', ".page", function () {
|
2014-05-21 11:38:12 -07:00
|
|
|
|
|
|
|
|
|
var page = this;
|
|
|
|
|
|
2015-04-12 09:46:29 -07:00
|
|
|
|
var $page = $(page);
|
2014-06-01 12:41:35 -07:00
|
|
|
|
|
2015-04-12 09:46:29 -07:00
|
|
|
|
if (!$page.hasClass('staticBackdropPage')) {
|
|
|
|
|
|
|
|
|
|
if ($page.hasClass('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-04-12 09:46:29 -07:00
|
|
|
|
var parentId = $page.hasClass('globalBackdropPage') ? '' : LibraryMenu.getTopParentId();
|
|
|
|
|
|
|
|
|
|
showBackdrop(type, parentId);
|
2015-01-17 22:45:10 -07:00
|
|
|
|
|
|
|
|
|
} else {
|
2015-04-12 09:46:29 -07:00
|
|
|
|
$page.removeClass('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);
|