jellyfin-web/dashboard-ui/components/imagedownloader/imagedownloader.js

371 lines
12 KiB
JavaScript
Raw Normal View History

2016-06-24 21:22:11 -07:00
define(['dialogHelper', 'emby-checkbox', 'emby-button', 'paper-icon-button-light'], function (dialogHelper) {
2015-09-17 10:24:23 -07:00
var currentItemId;
2015-09-17 10:32:39 -07:00
var currentItemType;
2016-06-24 21:22:11 -07:00
var currentResolve;
var currentReject;
2015-09-17 10:24:23 -07:00
var hasChanges = false;
2015-09-17 14:26:06 -07:00
// These images can be large and we're seeing memory problems in safari
2016-02-06 13:32:14 -07:00
var browsableImagePageSize = browserInfo.safari ? 6 : (browserInfo.mobile ? 10 : 40);
2015-09-17 14:26:06 -07:00
2015-09-17 10:24:23 -07:00
var browsableImageStartIndex = 0;
var browsableImageType = 'Primary';
var selectedProvider;
function getBaseRemoteOptions() {
var options = {};
options.itemId = currentItemId;
return options;
}
function reloadBrowsableImages(page) {
Dashboard.showLoadingMsg();
var options = getBaseRemoteOptions();
options.type = browsableImageType;
options.startIndex = browsableImageStartIndex;
options.limit = browsableImagePageSize;
2016-06-24 21:22:11 -07:00
options.IncludeAllLanguages = page.querySelector('#chkAllLanguages').checked;
2015-09-17 10:24:23 -07:00
var provider = selectedProvider || '';
if (provider) {
options.ProviderName = provider;
}
2015-12-14 08:43:03 -07:00
ApiClient.getAvailableRemoteImages(options).then(function (result) {
2015-09-17 10:24:23 -07:00
renderRemoteImages(page, result, browsableImageType, options.startIndex, options.limit);
2016-06-24 21:22:11 -07:00
page.querySelector('#selectBrowsableImageType').value = browsableImageType;
2015-09-17 10:24:23 -07:00
var providersHtml = result.Providers.map(function (p) {
return '<option value="' + p + '">' + p + '</option>';
});
2016-06-24 21:22:11 -07:00
var selectImageProvider = page.querySelector('#selectImageProvider');
selectImageProvider.innerHTML = '<option value="">' + Globalize.translate('LabelAll') + '</option>' + providersHtml;
selectImageProvider.value = provider;
2015-09-17 10:24:23 -07:00
Dashboard.hideLoadingMsg();
});
}
function renderRemoteImages(page, imagesResult, imageType, startIndex, limit) {
2016-06-24 21:22:11 -07:00
page.querySelector('.availableImagesPaging').innerHTML = getPagingHtml(startIndex, limit, imagesResult.TotalRecordCount);
2015-09-17 10:24:23 -07:00
var html = '';
for (var i = 0, length = imagesResult.Images.length; i < length; i++) {
html += getRemoteImageHtml(imagesResult.Images[i], imageType);
}
2015-12-14 08:43:03 -07:00
var availableImagesList = page.querySelector('.availableImagesList');
availableImagesList.innerHTML = html;
ImageLoader.lazyChildren(availableImagesList);
2015-09-17 10:24:23 -07:00
2016-06-24 21:22:11 -07:00
var btnNextPage = page.querySelector('.btnNextPage');
var btnPreviousPage = page.querySelector('.btnPreviousPage');
2015-09-17 10:24:23 -07:00
2016-06-24 21:22:11 -07:00
if (btnNextPage) {
btnNextPage.addEventListener('click', function () {
browsableImageStartIndex += browsableImagePageSize;
reloadBrowsableImages(page);
});
}
2015-09-17 10:24:23 -07:00
2016-06-24 21:22:11 -07:00
if (btnPreviousPage) {
btnPreviousPage.addEventListener('click', function () {
browsableImageStartIndex -= browsableImagePageSize;
reloadBrowsableImages(page);
});
}
2015-09-17 10:24:23 -07:00
}
function getPagingHtml(startIndex, limit, totalRecordCount) {
var html = '';
var recordsEnd = Math.min(startIndex + limit, totalRecordCount);
// 20 is the minimum page size
var showControls = totalRecordCount > limit;
html += '<div class="listPaging">';
html += '<span style="margin-right: 10px;">';
var startAtDisplay = totalRecordCount ? startIndex + 1 : 0;
html += startAtDisplay + '-' + recordsEnd + ' of ' + totalRecordCount;
html += '</span>';
if (showControls) {
html += '<div data-role="controlgroup" data-type="horizontal" style="display:inline-block;">';
2016-07-07 20:23:40 -07:00
html += '<button is="paper-icon-button-light" title="' + Globalize.translate('ButtonPreviousPage') + '" class="btnPreviousPage autoSize" ' + (startIndex ? '' : 'disabled') + '><i class="md-icon">&#xE5C4;</i></button>';
2016-06-18 13:31:22 -07:00
html += '<button is="paper-icon-button-light" title="' + Globalize.translate('ButtonNextPage') + '" class="btnNextPage autoSize" ' + (startIndex + limit >= totalRecordCount ? 'disabled' : '') + '><i class="md-icon">arrow_forward</i></button>';
2015-09-17 10:24:23 -07:00
html += '</div>';
}
html += '</div>';
return html;
}
2016-06-24 21:22:11 -07:00
function parentWithClass(elem, className) {
while (!elem.classList || !elem.classList.contains(className)) {
elem = elem.parentNode;
if (!elem) {
return null;
}
}
return elem;
}
2015-09-17 10:24:23 -07:00
function downloadRemoteImage(page, url, type, provider) {
var options = getBaseRemoteOptions();
options.Type = type;
options.ImageUrl = url;
options.ProviderName = provider;
Dashboard.showLoadingMsg();
2015-12-14 08:43:03 -07:00
ApiClient.downloadRemoteImage(options).then(function () {
2015-09-17 10:24:23 -07:00
hasChanges = true;
2016-06-24 21:22:11 -07:00
var dlg = parentWithClass(page, 'dialog');
2016-03-23 12:03:17 -07:00
dialogHelper.close(dlg);
2015-09-17 10:24:23 -07:00
});
}
function getDisplayUrl(url) {
return ApiClient.getUrl("Images/Remote", { imageUrl: url });
}
function getRemoteImageHtml(image, imageType) {
var html = '';
html += '<div class="remoteImageContainer">';
2015-09-24 22:15:29 -07:00
var cssClass = "remoteImage lazy";
2015-09-17 10:24:23 -07:00
if (imageType == "Backdrop" || imageType == "Art" || imageType == "Thumb" || imageType == "Logo") {
cssClass += " remoteBackdropImage";
}
else if (imageType == "Banner") {
cssClass += " remoteBannerImage";
}
else if (imageType == "Disc") {
cssClass += " remoteDiscImage";
}
else {
2015-09-17 10:32:39 -07:00
if (currentItemType == "Episode") {
2015-09-17 10:24:23 -07:00
cssClass += " remoteBackdropImage";
}
2015-09-17 10:32:39 -07:00
else if (currentItemType == "MusicAlbum" || currentItemType == "MusicArtist") {
2015-09-17 10:24:23 -07:00
cssClass += " remoteDiscImage";
}
else {
cssClass += " remotePosterImage";
}
}
var displayUrl = getDisplayUrl(image.ThumbnailUrl || image.Url);
2015-09-24 22:15:29 -07:00
html += '<a target="_blank" href="' + getDisplayUrl(image.Url) + '" class="' + cssClass + '" data-src="' + displayUrl + '">';
2015-09-17 10:24:23 -07:00
html += '</a>';
html += '<div class="remoteImageDetails">';
html += '<div class="remoteImageDetailText">';
html += image.ProviderName;
html += '</div>';
if (image.Width || image.Height || image.Language) {
html += '<div class="remoteImageDetailText">';
if (image.Width && image.Height) {
html += image.Width + ' x ' + image.Height;
if (image.Language) {
html += ' • ' + image.Language;
}
} else {
if (image.Language) {
html += image.Language;
}
}
html += '</div>';
}
if (image.CommunityRating != null) {
html += '<div class="remoteImageDetailText">';
if (image.RatingType == "Likes") {
html += image.CommunityRating + (image.CommunityRating == 1 ? " like" : " likes");
} else {
if (image.CommunityRating) {
html += image.CommunityRating.toFixed(1);
if (image.VoteCount) {
html += ' • ' + image.VoteCount + (image.VoteCount == 1 ? " vote" : " votes");
}
} else {
html += "Unrated";
}
}
html += '</div>';
}
2016-06-18 13:31:22 -07:00
html += '<button is="paper-icon-button-light" class="btnDownloadRemoteImage autoSize" raised data-imageprovider="' + image.ProviderName + '" data-imageurl="' + image.Url + '" data-imagetype="' + image.Type + '" title="' + Globalize.translate('ButtonDownload') + '"><i class="md-icon">cloud_download</i></button>';
2015-09-17 10:24:23 -07:00
html += '</div>';
html += '</div>';
return html;
}
function initEditor(page) {
2016-06-24 21:22:11 -07:00
page.querySelector('#selectBrowsableImageType').addEventListener('change', function () {
2015-09-17 10:24:23 -07:00
browsableImageType = this.value;
browsableImageStartIndex = 0;
selectedProvider = null;
reloadBrowsableImages(page);
});
2016-06-24 21:22:11 -07:00
page.querySelector('#selectImageProvider').addEventListener('change', function () {
2015-09-17 10:24:23 -07:00
browsableImageStartIndex = 0;
selectedProvider = this.value;
reloadBrowsableImages(page);
});
2016-06-24 21:22:11 -07:00
page.querySelector('#chkAllLanguages').addEventListener('change', function () {
2015-09-17 10:24:23 -07:00
browsableImageStartIndex = 0;
reloadBrowsableImages(page);
});
2016-06-24 21:22:11 -07:00
page.addEventListener('click', function(e) {
var btnDownloadRemoteImage = parentWithClass(e.target, 'btnDownloadRemoteImage');
if (btnDownloadRemoteImage) {
downloadRemoteImage(page, btnDownloadRemoteImage.getAttribute('data-imageurl'), btnDownloadRemoteImage.getAttribute('data-imagetype'), btnDownloadRemoteImage.getAttribute('data-imageprovider'));
}
});
2015-09-17 10:24:23 -07:00
}
2015-09-17 10:32:39 -07:00
function showEditor(itemId, itemType) {
2015-09-17 10:24:23 -07:00
Dashboard.showLoadingMsg();
2015-12-14 08:43:03 -07:00
var xhr = new XMLHttpRequest();
xhr.open('GET', 'components/imagedownloader/imagedownloader.template.html', true);
2015-09-17 10:24:23 -07:00
2015-12-14 08:43:03 -07:00
xhr.onload = function (e) {
2015-09-17 10:24:23 -07:00
2015-12-14 08:43:03 -07:00
var template = this.response;
2015-09-17 10:24:23 -07:00
currentItemId = itemId;
2015-09-17 10:32:39 -07:00
currentItemType = itemType;
2015-09-17 10:24:23 -07:00
2016-03-23 12:03:17 -07:00
var dlg = dialogHelper.createDialog({
2016-01-30 21:04:00 -07:00
size: 'fullscreen-border',
2016-06-24 21:22:11 -07:00
lockScroll: true,
removeOnClose: true
2016-01-30 13:59:09 -07:00
});
var theme = 'b';
dlg.classList.add('ui-body-' + theme);
dlg.classList.add('background-theme-' + theme);
dlg.classList.add('popupEditor');
2015-09-17 10:24:23 -07:00
var html = '';
html += '<h2 class="dialogHeader">';
2016-07-07 20:23:40 -07:00
html += '<button type="button" is="emby-button" icon="arrow-back" class="fab mini btnCloseDialog autoSize" tabindex="-1"><i class="md-icon">&#xE5C4;</i></button>';
2015-09-17 10:24:23 -07:00
html += '<div style="display:inline-block;margin-left:.6em;vertical-align:middle;">' + Globalize.translate('HeaderSearch') + '</div>';
html += '</h2>';
html += '<div class="editorContent">';
2016-03-05 12:07:58 -07:00
html += Globalize.translateDocument(template);
2015-09-17 10:24:23 -07:00
html += '</div>';
dlg.innerHTML = html;
document.body.appendChild(dlg);
// Has to be assigned a z-index after the call to .open()
2016-06-24 21:22:11 -07:00
dlg.addEventListener('close', onDialogClosed);
2015-09-17 10:24:23 -07:00
2016-03-23 12:03:17 -07:00
dialogHelper.open(dlg);
2015-09-17 10:24:23 -07:00
var editorContent = dlg.querySelector('.editorContent');
initEditor(editorContent);
2016-06-24 21:22:11 -07:00
dlg.querySelector('.btnCloseDialog').addEventListener('click', function () {
2015-10-01 09:28:24 -07:00
2016-03-23 12:03:17 -07:00
dialogHelper.close(dlg);
2015-10-01 09:28:24 -07:00
});
2015-09-17 10:24:23 -07:00
reloadBrowsableImages(editorContent);
2015-12-14 08:43:03 -07:00
}
xhr.send();
2015-09-17 10:24:23 -07:00
}
function onDialogClosed() {
Dashboard.hideLoadingMsg();
2016-06-24 21:22:11 -07:00
if (hasChanges) {
currentResolve();
} else {
currentReject();
}
2015-09-17 10:24:23 -07:00
}
2015-12-14 08:43:03 -07:00
return {
2015-09-17 10:32:39 -07:00
show: function (itemId, itemType, imageType) {
2015-09-17 10:24:23 -07:00
2016-06-24 21:22:11 -07:00
return new Promise(function (resolve, reject) {
2015-09-17 10:24:23 -07:00
2016-06-24 21:22:11 -07:00
currentResolve = resolve;
currentReject = reject;
hasChanges = false;
browsableImageStartIndex = 0;
browsableImageType = imageType || 'Primary';
selectedProvider = null;
2015-09-17 10:24:23 -07:00
2016-06-24 21:22:11 -07:00
showEditor(itemId, itemType);
});
2015-09-17 10:24:23 -07:00
}
};
2015-12-14 08:43:03 -07:00
});