1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00
jellyfin-web/dashboard-ui/components/imageeditor/imageeditor.js

535 lines
17 KiB
JavaScript
Raw Normal View History

(function ($, document, window, FileReader, escape) {
var currentItem;
var browsableImagePageSize = 10;
var browsableImageStartIndex = 0;
var browsableImageType = 'Primary';
2014-02-09 16:11:11 -05:00
var selectedProvider;
function getBaseRemoteOptions() {
2014-11-14 21:31:03 -05:00
var options = {};
2014-11-14 21:31:03 -05:00
options.itemId = currentItem.Id;
return options;
}
function reloadBrowsableImages(page) {
Dashboard.showLoadingMsg();
var options = getBaseRemoteOptions();
options.type = browsableImageType;
options.startIndex = browsableImageStartIndex;
options.limit = browsableImagePageSize;
options.IncludeAllLanguages = $('#chkAllLanguages', page).checked();
2014-02-09 16:11:11 -05:00
var provider = selectedProvider || '';
if (provider) {
options.ProviderName = provider;
}
ApiClient.getAvailableRemoteImages(options).done(function (result) {
renderRemoteImages(page, currentItem, result, browsableImageType, options.startIndex, options.limit);
2015-09-03 13:01:51 -04:00
$('#selectBrowsableImageType', page).val(browsableImageType);
var providersHtml = result.Providers.map(function (p) {
return '<option value="' + p + '">' + p + '</option>';
});
2015-09-03 13:01:51 -04:00
$('#selectImageProvider', page).html('<option value="">' + Globalize.translate('LabelAll') + '</option>' + providersHtml).val(provider);
Dashboard.hideLoadingMsg();
});
}
function renderRemoteImages(page, item, imagesResult, imageType, startIndex, limit) {
$('.availableImagesPaging', page).html(getPagingHtml(startIndex, limit, imagesResult.TotalRecordCount)).trigger('create');
var html = '';
for (var i = 0, length = imagesResult.Images.length; i < length; i++) {
html += getRemoteImageHtml(imagesResult.Images[i], imageType);
}
$('.availableImagesList', page).html(html).trigger('create');
$('.btnNextPage', page).on('click', function () {
browsableImageStartIndex += browsableImagePageSize;
reloadBrowsableImages(page);
});
$('.btnPreviousPage', page).on('click', function () {
browsableImageStartIndex -= browsableImagePageSize;
reloadBrowsableImages(page);
});
$('.btnDownloadRemoteImage', page).on('click', function () {
downloadRemoteImage(page, this.getAttribute('data-imageurl'), this.getAttribute('data-imagetype'), this.getAttribute('data-imageprovider'));
});
}
function downloadRemoteImage(page, url, type, provider) {
var options = getBaseRemoteOptions();
options.Type = type;
options.ImageUrl = url;
options.ProviderName = provider;
Dashboard.showLoadingMsg();
ApiClient.downloadRemoteImage(options).done(function () {
$('.popupDownload', page).popup("close");
reload(page);
});
}
function getDisplayUrl(url) {
2014-02-09 01:08:10 -05:00
return ApiClient.getUrl("Images/Remote", { imageUrl: url });
}
function getRemoteImageHtml(image, imageType) {
var html = '';
html += '<div class="remoteImageContainer">';
var cssClass = "remoteImage";
if (imageType == "Backdrop" || imageType == "Art" || imageType == "Thumb" || imageType == "Logo") {
cssClass += " remoteBackdropImage";
}
else if (imageType == "Banner") {
cssClass += " remoteBannerImage";
}
else if (imageType == "Disc") {
cssClass += " remoteDiscImage";
}
else {
2013-11-27 14:04:19 -05:00
if (currentItem.Type == "Episode") {
cssClass += " remoteBackdropImage";
}
2013-11-21 15:48:26 -05:00
else if (currentItem.Type == "MusicAlbum" || currentItem.Type == "MusicArtist") {
cssClass += " remoteDiscImage";
}
else {
cssClass += " remotePosterImage";
}
}
var displayUrl = getDisplayUrl(image.ThumbnailUrl || image.Url);
html += '<a target="_blank" href="' + getDisplayUrl(image.Url) + '" class="' + cssClass + '" style="background-image:url(\'' + displayUrl + '\');">';
html += '</a>';
html += '<div class="remoteImageDetails">';
html += image.ProviderName;
html += '</div>';
if (image.Width || image.Height || image.Language) {
html += '<div class="remoteImageDetails">';
2015-07-23 19:40:54 -04:00
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="remoteImageDetails">';
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>';
}
2014-07-16 23:17:14 -04:00
html += '<div><button class="btnDownloadRemoteImage" data-imageprovider="' + image.ProviderName + '" data-imageurl="' + image.Url + '" data-imagetype="' + image.Type + '" type="button" data-icon="arrow-d" data-mini="true">' + Globalize.translate('ButtonDownload') + '</button></div>';
html += '</div>';
return html;
}
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) {
2014-01-22 15:46:01 -05:00
html += '<div data-role="controlgroup" data-type="horizontal" style="display:inline-block;">';
2015-09-06 15:09:36 -04:00
html += '<paper-icon-button icon="arrow-back" title="' + Globalize.translate('ButtonPreviousPage') + '" class="btnPreviousPage" ' + (startIndex ? '' : 'disabled') + '></paper-icon-button>';
html += '<paper-icon-button icon="arrow-forward" title="' + Globalize.translate('ButtonNextPage') + '" class="btnNextPage" ' + (startIndex + limit > totalRecordCount ? 'disabled' : '') + '></paper-icon-button>';
2014-01-22 15:46:01 -05:00
html += '</div>';
}
html += '</div>';
return html;
}
2013-11-27 14:04:19 -05:00
2015-09-17 12:04:04 -04:00
function reload(page, item) {
Dashboard.showLoadingMsg();
2014-02-09 16:11:11 -05:00
browsableImageStartIndex = 0;
browsableImageType = 'Primary';
selectedProvider = null;
2015-09-17 12:04:04 -04:00
if (item) {
reloadItem(page, item);
}
else {
ApiClient.getItem(Dashboard.getCurrentUserId(), currentItem.Id).done(function (item) {
reloadItem(page, item);
});
}
}
2015-09-17 12:04:04 -04:00
function reloadItem(page, item) {
2015-09-17 12:04:04 -04:00
currentItem = item;
2013-11-27 14:04:19 -05:00
2015-09-17 12:04:04 -04:00
ApiClient.getRemoteImageProviders(getBaseRemoteOptions()).done(function (providers) {
2013-11-27 14:04:19 -05:00
2015-09-17 12:04:04 -04:00
if (providers.length) {
$('.lnkBrowseAllImages', page).removeClass('hide');
} else {
$('.lnkBrowseAllImages', page).addClass('hide');
}
2013-11-27 14:04:19 -05:00
2015-09-17 12:04:04 -04:00
ApiClient.getItemImageInfos(currentItem.Id).done(function (imageInfos) {
2015-09-17 12:04:04 -04:00
renderStandardImages(page, item, imageInfos, providers);
renderBackdrops(page, item, imageInfos, providers);
renderScreenshots(page, item, imageInfos, providers);
Dashboard.hideLoadingMsg();
});
2013-05-05 00:49:49 -04:00
});
}
function renderImages(page, item, images, imageProviders, elem) {
2013-05-05 00:49:49 -04:00
var html = '';
for (var i = 0, length = images.length; i < length; i++) {
var image = images[i];
2013-11-27 14:04:19 -05:00
html += '<div class="editorTile imageEditorTile">';
2013-05-05 00:49:49 -04:00
2015-07-15 07:26:47 -04:00
if (image.ImageType !== "Backdrop" && image.ImageType !== "Screenshot") {
html += '<h3>' + image.ImageType + '</h3>';
}
2015-07-14 15:04:16 -04:00
var height = 150;
html += '<div style="height:' + height + 'px;vertical-align:top;background-repeat:no-repeat;background-position:center center;background-size:contain;background-image:url(\'' + LibraryBrowser.getImageUrl(currentItem, image.ImageType, image.ImageIndex, { height: height }) + '\');"></div>';
2013-05-05 00:49:49 -04:00
2015-07-15 07:26:47 -04:00
html += '<div class="editorTileInner">';
2013-05-05 00:49:49 -04:00
2015-08-02 19:47:31 -04:00
if (image.Width && image.Height) {
html += '<p>' + image.Width + ' X ' + image.Height + '</p>';
} else {
html += '<p>&nbsp;</p>';
}
2013-05-05 00:49:49 -04:00
2015-09-17 12:04:04 -04:00
html += '<div>';
2013-05-05 00:49:49 -04:00
if (image.ImageType == "Backdrop" || image.ImageType == "Screenshot") {
if (i > 0) {
2015-07-14 15:04:16 -04:00
html += '<paper-icon-button icon="chevron-left" onclick="EditItemImagesPage.moveImage(\'' + image.ImageType + '\', ' + image.ImageIndex + ', ' + (i - 1) + ');" title="' + Globalize.translate('ButtonMoveLeft') + '"></paper-icon-button>';
2013-05-05 00:49:49 -04:00
} else {
2015-07-14 15:04:16 -04:00
html += '<paper-icon-button icon="chevron-left" disabled title="' + Globalize.translate('ButtonMoveLeft') + '"></paper-icon-button>';
2013-05-05 00:49:49 -04:00
}
if (i < length - 1) {
2015-07-14 15:04:16 -04:00
html += '<paper-icon-button icon="chevron-right" onclick="EditItemImagesPage.moveImage(\'' + image.ImageType + '\', ' + image.ImageIndex + ', ' + (i + 1) + ');" title="' + Globalize.translate('ButtonMoveRight') + '"></paper-icon-button>';
2013-05-05 00:49:49 -04:00
} else {
2015-07-14 15:04:16 -04:00
html += '<paper-icon-button icon="chevron-right" disabled title="' + Globalize.translate('ButtonMoveRight') + '"></paper-icon-button>';
2013-05-05 00:49:49 -04:00
}
}
if (imageProviders.length) {
2015-07-14 15:04:16 -04:00
html += '<paper-icon-button icon="cloud" onclick="EditItemImagesPage.showDownloadMenu(\'' + image.ImageType + '\');" title="' + Globalize.translate('ButtonBrowseOnlineImages') + '"></paper-icon-button>';
}
2013-10-31 21:55:38 -04:00
2015-07-15 07:26:47 -04:00
html += '<paper-icon-button icon="delete" onclick="EditItemImagesPage.deleteImage(\'' + image.ImageType + '\', ' + (image.ImageIndex != null ? image.ImageIndex : "null") + ');" title="' + Globalize.translate('Delete') + '"></paper-icon-button>';
2015-09-17 12:04:04 -04:00
html += '</div>';
2013-05-05 00:49:49 -04:00
html += '</div>';
html += '</div>';
}
elem.html(html).trigger('create');
}
function renderStandardImages(page, item, imageInfos, imageProviders) {
2013-05-05 00:49:49 -04:00
var images = imageInfos.filter(function (i) {
return i.ImageType != "Screenshot" && i.ImageType != "Backdrop" && i.ImageType != "Chapter";
});
2013-05-05 00:49:49 -04:00
2015-09-17 12:04:04 -04:00
renderImages(page, item, images, imageProviders, $('#images', page));
2013-05-05 00:49:49 -04:00
}
function renderBackdrops(page, item, imageInfos, imageProviders) {
2013-05-05 00:49:49 -04:00
var images = imageInfos.filter(function (i) {
return i.ImageType == "Backdrop";
}).sort(function (a, b) {
return a.ImageIndex - b.ImageIndex;
});
if (images.length) {
$('#backdropsContainer', page).show();
renderImages(page, item, images, imageProviders, $('#backdrops', page));
2013-05-05 00:49:49 -04:00
} else {
$('#backdropsContainer', page).hide();
}
}
function renderScreenshots(page, item, imageInfos, imageProviders) {
2013-05-05 00:49:49 -04:00
var images = imageInfos.filter(function (i) {
return i.ImageType == "Screenshot";
}).sort(function (a, b) {
return a.ImageIndex - b.ImageIndex;
});
if (images.length) {
$('#screenshotsContainer', page).show();
renderImages(page, item, images, imageProviders, $('#screenshots', page));
2013-05-05 00:49:49 -04:00
} else {
$('#screenshotsContainer', page).hide();
}
}
2015-05-18 21:46:31 -04:00
function editItemImages() {
2015-05-18 21:46:31 -04:00
var self = this;
2013-05-05 00:49:49 -04:00
self.deleteImage = function (type, index) {
var page = $.mobile.activePage;
2014-07-16 23:17:14 -04:00
Dashboard.confirm(Globalize.translate('DeleteImageConfirmation'), Globalize.translate('HeaderDeleteImage'), function (result) {
2013-05-05 00:49:49 -04:00
if (result) {
2014-04-26 23:42:05 -04:00
ApiClient.deleteItemImage(currentItem.Id, type, index).done(function () {
2013-05-05 00:49:49 -04:00
2015-09-17 12:04:04 -04:00
reload(page);
2013-05-05 00:49:49 -04:00
});
}
});
};
self.moveImage = function (type, index, newIndex) {
var page = $.mobile.activePage;
2014-04-26 23:42:05 -04:00
ApiClient.updateItemImageIndex(currentItem.Id, type, index, newIndex).done(function () {
2013-05-05 00:49:49 -04:00
2015-09-17 12:04:04 -04:00
reload(page);
2013-05-05 00:49:49 -04:00
});
};
2013-10-31 21:55:38 -04:00
self.showDownloadMenu = function (type) {
browsableImageStartIndex = 0;
2013-10-31 21:55:38 -04:00
browsableImageType = type;
2015-07-14 15:04:16 -04:00
var page = $.mobile.activePage;
selectedProvider = null;
$('.popupDownload', page).popup('open');
reloadBrowsableImages(page);
2013-10-31 21:55:38 -04:00
};
}
window.EditItemImagesPage = new editItemImages();
2015-09-17 12:04:04 -04:00
function initEditor(page) {
$('#selectBrowsableImageType', page).on('change', function () {
browsableImageType = this.value;
browsableImageStartIndex = 0;
2014-02-09 16:11:11 -05:00
selectedProvider = null;
reloadBrowsableImages(page);
});
$('#selectImageProvider', page).on('change', function () {
browsableImageStartIndex = 0;
2014-02-09 16:11:11 -05:00
selectedProvider = this.value;
reloadBrowsableImages(page);
});
2014-02-11 22:46:27 -05:00
$('#chkAllLanguages', page).on('change', function () {
browsableImageStartIndex = 0;
reloadBrowsableImages(page);
});
2015-08-02 19:47:31 -04:00
$('.btnOpenUploadMenu', page).on('click', function () {
2015-07-14 15:04:16 -04:00
2015-09-17 12:04:04 -04:00
require(['components/imageuploader/imageuploader'], function () {
ImageUploader.show(currentItem.Id).done(function (hasChanges) {
if (hasChanges) {
reload(page);
}
});
});
2015-07-14 15:04:16 -04:00
});
$('.btnBrowseAllImages', page).on('click', function () {
selectedProvider = null;
browsableImageType = 'Primary';
$('.popupDownload', page).popup('open');
reloadBrowsableImages(page);
});
2015-09-17 12:04:04 -04:00
}
2015-05-18 21:46:31 -04:00
2015-09-17 12:04:04 -04:00
function showEditor(itemId) {
2013-05-05 00:49:49 -04:00
2015-09-17 12:04:04 -04:00
Dashboard.showLoadingMsg();
2015-09-17 12:04:04 -04:00
ApiClient.ajax({
2015-09-17 12:04:04 -04:00
type: 'GET',
url: 'components/imageeditor/imageeditor.template.html'
2015-09-17 12:04:04 -04:00
}).done(function (template) {
2015-09-17 12:04:04 -04:00
ApiClient.getItem(Dashboard.getCurrentUserId(), itemId).done(function (item) {
2015-09-17 12:04:04 -04:00
var dlg = document.createElement('paper-dialog');
2015-09-17 12:04:04 -04:00
dlg.setAttribute('with-backdrop', 'with-backdrop');
dlg.setAttribute('role', 'alertdialog');
dlg.entryAnimation = 'scale-up-animation';
dlg.exitAnimation = 'fade-out-animation';
dlg.classList.add('fullscreen-editor-paper-dialog');
dlg.classList.add('ui-body-b');
2015-09-17 12:04:04 -04:00
var html = '';
html += '<h2 class="dialogHeader">';
html += '<paper-fab icon="arrow-back" class="mini btnCloseDialog"></paper-fab>';
html += '<div style="display:inline-block;margin-left:.6em;vertical-align:middle;">' + item.Name + '</div>';
html += '</h2>';
2015-09-17 12:04:04 -04:00
html += '<div class="editorContent">';
html += Globalize.translateDocument(template);
html += '</div>';
2015-09-17 12:04:04 -04:00
dlg.innerHTML = html;
document.body.appendChild(dlg);
2015-09-17 12:04:04 -04:00
initEditor(dlg);
// Has to be assigned a z-index after the call to .open()
$(dlg).on('iron-overlay-closed', onDialogClosed);
document.body.classList.add('bodyWithPopupOpen');
PaperDialogHelper.openWithHash(dlg, 'imageeditor');
var editorContent = dlg.querySelector('.editorContent');
reload(editorContent, item);
$('.btnCloseDialog', dlg).on('click', closeDialog);
});
2015-07-14 16:29:51 -04:00
});
2015-09-17 12:04:04 -04:00
}
function closeDialog() {
history.back();
}
function onDialogClosed() {
document.body.classList.remove('bodyWithPopupOpen');
$(this).remove();
Dashboard.hideLoadingMsg();
}
window.ImageEditor = {
show: function (itemId) {
require(['components/paperdialoghelper', 'jqmpopup'], function () {
Dashboard.importCss('css/metadataeditor.css');
showEditor(itemId);
});
}
};
2013-11-27 14:04:19 -05:00
})(jQuery, document, window, window.FileReader, escape);