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

610 lines
19 KiB
JavaScript
Raw Normal View History

(function ($, document, window, FileReader, escape) {
var currentItem;
var currentFile;
var browsableImagePageSize = 10;
var browsableImageStartIndex = 0;
var browsableImageType = 'Primary';
2014-02-09 14:11:11 -07:00
var selectedProvider;
2013-08-02 13:36:44 -07:00
function updateTabs(page, item) {
2013-06-27 06:31:49 -07:00
2013-08-02 13:36:44 -07:00
var query = MetadataEditor.getEditQueryString(item);
2013-06-27 06:31:49 -07:00
2013-08-02 13:36:44 -07:00
$('#btnEditMetadata', page).attr('href', 'edititemmetadata.html?' + query);
2014-05-16 21:24:10 -07:00
$('#btnEditSubtitles', page).attr('href', 'edititemsubtitles.html?' + query);
$('#btnEditCollectionTitles', page).attr('href', 'editcollectionitems.html?' + query);
2013-06-27 06:31:49 -07:00
}
function getBaseRemoteOptions() {
2014-11-14 19:31:03 -07:00
var options = {};
2014-11-14 19:31:03 -07: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 14:11:11 -07:00
var provider = selectedProvider || '';
if (provider) {
options.ProviderName = provider;
}
ApiClient.getAvailableRemoteImages(options).done(function (result) {
renderRemoteImages(page, currentItem, result, browsableImageType, options.startIndex, options.limit);
$('#selectBrowsableImageType', page).val(browsableImageType).selectmenu('refresh');
var providersHtml = result.Providers.map(function (p) {
return '<option value="' + p + '">' + p + '</option>';
});
2014-07-16 20:17:14 -07:00
$('#selectImageProvider', page).html('<option value="">' + Globalize.translate('LabelAll') + '</option>' + providersHtml).val(provider).selectmenu('refresh');
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-08 23:08:10 -07: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 12:04:19 -07:00
if (currentItem.Type == "Episode") {
cssClass += " remoteBackdropImage";
}
2013-11-21 13:48:26 -07: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">';
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 20:17:14 -07: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 13:46:01 -07:00
html += '<div data-role="controlgroup" data-type="horizontal" style="display:inline-block;">';
2014-07-16 20:17:14 -07:00
html += '<button data-icon="arrow-l" data-iconpos="notext" data-inline="true" data-mini="true" class="btnPreviousPage" ' + (startIndex ? '' : 'disabled') + '>' + Globalize.translate('ButtonPreviousPage') + '</button>';
2014-07-16 20:17:14 -07:00
html += '<button data-icon="arrow-r" data-iconpos="notext" data-inline="true" data-mini="true" class="btnNextPage" ' + (startIndex + limit > totalRecordCount ? 'disabled' : '') + '>' + Globalize.translate('ButtonNextPage') + '</button>';
2014-01-22 13:46:01 -07:00
html += '</div>';
}
html += '</div>';
return html;
}
2013-11-27 12:04:19 -07:00
2013-06-27 06:31:49 -07:00
function reload(page) {
Dashboard.showLoadingMsg();
2014-02-09 14:11:11 -07:00
browsableImageStartIndex = 0;
browsableImageType = 'Primary';
selectedProvider = null;
2013-08-02 13:36:44 -07:00
MetadataEditor.getItemPromise().done(function (item) {
currentItem = item;
2013-11-27 12:04:19 -07:00
LibraryBrowser.renderName(item, $('.itemName', page), true);
updateTabs(page, item);
if (item.Type == "BoxSet") {
$('#btnEditCollectionTitles', page).show();
} else {
$('#btnEditCollectionTitles', page).hide();
}
2014-07-16 20:17:14 -07:00
if (item.MediaType == "Video" && item.LocationType == "FileSystem" && item.Type !== 'TvChannel') {
2014-05-16 21:24:10 -07:00
$('#btnEditSubtitles', page).show();
} else {
$('#btnEditSubtitles', page).hide();
}
2013-11-27 12:04:19 -07:00
ApiClient.getRemoteImageProviders(getBaseRemoteOptions()).done(function (providers) {
if (providers.length) {
$('.lnkBrowseAllImages', page).removeClass('hide');
} else {
$('.lnkBrowseAllImages', page).addClass('hide');
}
2013-11-27 12:04:19 -07:00
2014-04-26 20:42:05 -07:00
ApiClient.getItemImageInfos(currentItem.Id).done(function (imageInfos) {
renderStandardImages(page, item, imageInfos, providers);
renderBackdrops(page, item, imageInfos, providers);
renderScreenshots(page, item, imageInfos, providers);
Dashboard.hideLoadingMsg();
});
});
2013-05-04 21:49:49 -07:00
});
}
function renderImages(page, item, images, imageProviders, elem) {
2013-05-04 21:49:49 -07:00
var html = '';
for (var i = 0, length = images.length; i < length; i++) {
var image = images[i];
2013-11-27 12:04:19 -07:00
html += '<div class="editorTile imageEditorTile">';
2013-05-04 21:49:49 -07:00
2015-02-16 23:12:48 -07:00
html += '<div style="height:144px;vertical-align:top;background-repeat:no-repeat;background-size:contain;background-image:url(\'' + LibraryBrowser.getImageUrl(currentItem, image.ImageType, image.ImageIndex, { height: 144 }) + '\');"></div>';
2013-05-04 21:49:49 -07:00
2013-11-27 12:04:19 -07:00
html += '<div>';
2013-12-01 13:17:40 -07:00
2013-11-29 12:31:57 -07:00
if (image.ImageType !== "Backdrop" && image.ImageType !== "Screenshot") {
html += '<p>' + image.ImageType + '</p>';
}
2013-05-04 21:49:49 -07:00
2013-11-27 12:04:19 -07:00
html += '<p>' + image.Width + ' X ' + image.Height + '</p>';
2013-05-04 21:49:49 -07:00
2013-11-27 12:04:19 -07:00
html += '<p>';
2013-05-04 21:49:49 -07:00
if (image.ImageType == "Backdrop" || image.ImageType == "Screenshot") {
if (i > 0) {
2014-07-16 20:17:14 -07:00
html += '<button type="button" data-icon="arrow-l" data-mini="true" data-inline="true" data-iconpos="notext" onclick="EditItemImagesPage.moveImage(\'' + image.ImageType + '\', ' + image.ImageIndex + ', ' + (i - 1) + ');" style="margin-bottom:0;">' + Globalize.translate('ButtonMoveLeft') + '</button>';
2013-05-04 21:49:49 -07:00
} else {
2014-07-16 20:17:14 -07:00
html += '<button type="button" data-icon="arrow-l" data-mini="true" data-inline="true" data-iconpos="notext" style="margin-bottom:0;" disabled>' + Globalize.translate('ButtonMoveLeft') + '</button>';
2013-05-04 21:49:49 -07:00
}
if (i < length - 1) {
2014-07-16 20:17:14 -07:00
html += '<button type="button" data-icon="arrow-r" data-mini="true" data-inline="true" data-iconpos="notext" onclick="EditItemImagesPage.moveImage(\'' + image.ImageType + '\', ' + image.ImageIndex + ', ' + (i + 1) + ');" style="margin-bottom:0;">' + Globalize.translate('ButtonMoveRight') + '</button>';
2013-05-04 21:49:49 -07:00
} else {
2014-07-16 20:17:14 -07:00
html += '<button type="button" data-icon="arrow-r" data-mini="true" data-inline="true" data-iconpos="notext" style="margin-bottom:0;" disabled>' + Globalize.translate('ButtonMoveRight') + '</button>';
2013-05-04 21:49:49 -07:00
}
}
2014-07-16 20:17:14 -07:00
html += '<button type="button" data-icon="delete" data-mini="true" data-inline="true" data-iconpos="notext" onclick="EditItemImagesPage.deleteImage(\'' + image.ImageType + '\', ' + (image.ImageIndex != null ? image.ImageIndex : "null") + ');" style="margin-bottom:0;">' + Globalize.translate('Delete') + '</button>';
2013-12-01 13:17:40 -07:00
if (imageProviders.length) {
2014-07-16 20:17:14 -07:00
html += '<button type="button" data-icon="cloud" data-mini="true" data-inline="true" data-iconpos="notext" onclick="EditItemImagesPage.showDownloadMenu(\'' + image.ImageType + '\');" style="margin-bottom:0;">' + Globalize.translate('ButtonBrowseOnlineImages') + '</button>';
}
2013-10-31 18:55:38 -07:00
2013-05-04 21:49:49 -07:00
html += '</p>';
html += '</div>';
html += '</div>';
}
elem.html(html).trigger('create');
}
function renderStandardImages(page, item, imageInfos, imageProviders) {
2013-05-04 21:49:49 -07:00
var images = imageInfos.filter(function (i) {
return i.ImageType != "Screenshot" && i.ImageType != "Backdrop" && i.ImageType != "Chapter";
});
2013-05-04 21:49:49 -07:00
if (images.length) {
$('#imagesContainer', page).show();
renderImages(page, item, images, imageProviders, $('#images', page));
2013-05-04 21:49:49 -07:00
} else {
$('#imagesContainer', page).hide();
}
}
function renderBackdrops(page, item, imageInfos, imageProviders) {
2013-05-04 21:49:49 -07: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-04 21:49:49 -07:00
} else {
$('#backdropsContainer', page).hide();
}
}
function renderScreenshots(page, item, imageInfos, imageProviders) {
2013-05-04 21:49:49 -07: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-04 21:49:49 -07:00
} else {
$('#screenshotsContainer', page).hide();
}
}
function onFileReaderError(evt) {
Dashboard.hideLoadingMsg();
switch (evt.target.error.code) {
case evt.target.error.NOT_FOUND_ERR:
2014-07-16 20:17:14 -07:00
Dashboard.showError(Globalize.translate('MessageFileNotFound'));
break;
case evt.target.error.ABORT_ERR:
break; // noop
default:
2014-07-16 20:17:14 -07:00
Dashboard.showError(Globalize.translate('MessageFileReadError'));
break;
};
}
function setFiles(page, files) {
var file = files[0];
if (!file || !file.type.match('image.*')) {
$('#imageOutput', page).html('');
$('#fldUpload', page).hide();
currentFile = null;
return;
}
currentFile = file;
var reader = new FileReader();
reader.onerror = onFileReaderError;
reader.onloadstart = function () {
$('#fldUpload', page).hide();
};
reader.onabort = function () {
Dashboard.hideLoadingMsg();
2014-07-16 20:17:14 -07:00
console.log('File read cancelled');
};
// Closure to capture the file information.
reader.onload = (function (theFile) {
return function (e) {
// Render thumbnail.
2013-11-27 12:04:19 -07:00
var html = ['<img style="max-width:300px;max-height:100px;" src="', e.target.result, '" title="', escape(theFile.name), '"/>'].join('');
$('#imageOutput', page).html(html);
$('#fldUpload', page).show();
};
})(file);
// Read in the image file as a data URL.
reader.readAsDataURL(file);
}
function processImageChangeResult(page) {
reload(page);
}
2015-05-18 18:46:31 -07:00
function onSubmit() {
var file = currentFile;
2015-05-18 18:46:31 -07:00
if (!file) {
return false;
}
2015-05-18 18:46:31 -07:00
if (file.type != "image/png" && file.type != "image/jpeg" && file.type != "image/jpeg") {
return false;
}
2015-05-18 18:46:31 -07:00
Dashboard.showLoadingMsg();
2015-05-18 18:46:31 -07:00
var page = $.mobile.activePage;
2015-05-18 18:46:31 -07:00
var imageType = $('#selectImageType', page).val();
2015-05-18 18:46:31 -07:00
ApiClient.uploadItemImage(currentItem.Id, imageType, file).done(function () {
2015-05-18 18:46:31 -07:00
$('#uploadImage', page).val('').trigger('change');
$('#popupUpload', page).popup("close");
processImageChangeResult(page);
2015-05-18 18:46:31 -07:00
});
2015-05-18 18:46:31 -07:00
return false;
}
2015-05-18 18:46:31 -07:00
function editItemImages() {
2015-05-18 18:46:31 -07:00
var self = this;
2013-05-04 21:49:49 -07:00
self.deleteImage = function (type, index) {
var page = $.mobile.activePage;
2014-07-16 20:17:14 -07:00
Dashboard.confirm(Globalize.translate('DeleteImageConfirmation'), Globalize.translate('HeaderDeleteImage'), function (result) {
2013-05-04 21:49:49 -07:00
if (result) {
2014-04-26 20:42:05 -07:00
ApiClient.deleteItemImage(currentItem.Id, type, index).done(function () {
2013-05-04 21:49:49 -07:00
processImageChangeResult(page);
});
}
});
};
self.moveImage = function (type, index, newIndex) {
var page = $.mobile.activePage;
2014-04-26 20:42:05 -07:00
ApiClient.updateItemImageIndex(currentItem.Id, type, index, newIndex).done(function () {
2013-05-04 21:49:49 -07:00
processImageChangeResult(page);
});
};
2013-10-31 18:55:38 -07:00
self.showDownloadMenu = function (type) {
browsableImageStartIndex = 0;
2013-10-31 18:55:38 -07:00
browsableImageType = type;
2013-10-31 18:55:38 -07:00
$('.lnkBrowseImages').trigger('click');
};
}
window.EditItemImagesPage = new editItemImages();
2015-05-18 18:46:31 -07:00
$(document).on('pageinitdepends', "#editItemImagesPage", function () {
var page = this;
2013-08-02 13:36:44 -07:00
$('.libraryTree', page).on('itemclicked', function (event, data) {
if (data.id != currentItem.Id) {
2013-08-02 13:36:44 -07:00
MetadataEditor.currentItemId = data.id;
MetadataEditor.currentItemType = data.itemType;
//Dashboard.navigate('edititemmetadata.html?id=' + data.id);
2013-12-24 11:37:29 -07:00
//$.mobile.urlHistory.ignoreNextHashChange = true;
2013-08-02 13:36:44 -07:00
window.location.hash = 'editItemImagesPage?id=' + data.id;
reload(page);
}
});
2013-10-31 18:55:38 -07:00
$('.lnkBrowseImages', page).on('click', function () {
2014-02-09 14:11:11 -07:00
selectedProvider = null;
$('.popupDownload', page).popup('open');
reloadBrowsableImages(page);
});
$('#selectBrowsableImageType', page).on('change', function () {
browsableImageType = this.value;
browsableImageStartIndex = 0;
2014-02-09 14:11:11 -07:00
selectedProvider = null;
reloadBrowsableImages(page);
});
$('#selectImageProvider', page).on('change', function () {
browsableImageStartIndex = 0;
2014-02-09 14:11:11 -07:00
selectedProvider = this.value;
reloadBrowsableImages(page);
});
2014-02-11 20:46:27 -07:00
$('#chkAllLanguages', page).on('change', function () {
browsableImageStartIndex = 0;
reloadBrowsableImages(page);
});
2015-05-18 18:46:31 -07:00
$('.uploadItemImageForm').off('submit', onSubmit).on('submit', onSubmit);
2015-05-19 12:15:40 -07:00
}).on('pageshowready', "#editItemImagesPage", function () {
var page = this;
reload(page);
2013-05-04 21:49:49 -07:00
$('#uploadImage', page).on("change", function () {
setFiles(page, this.files);
});
$("#imageDropZone", page).on('dragover', function (e) {
e.preventDefault();
e.originalEvent.dataTransfer.dropEffect = 'Copy';
return false;
}).on('drop', function (e) {
e.preventDefault();
setFiles(page, e.originalEvent.dataTransfer.files);
return false;
});
2015-06-20 17:49:42 -07:00
}).on('pagebeforehide', "#editItemImagesPage", function () {
var page = this;
currentItem = null;
2013-05-04 21:49:49 -07:00
$('#uploadImage', page).off("change");
$("#imageDropZone", page).off('dragover').off('drop');
});
2013-11-27 12:04:19 -07:00
})(jQuery, document, window, window.FileReader, escape);