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

310 lines
10 KiB
JavaScript
Raw Normal View History

2016-01-30 12:31:22 -07:00
define(['paperdialoghelper', 'css!css/metadataeditor.css', 'paper-fab'], function (paperDialogHelper) {
var currentItem;
2015-10-15 22:36:16 -07:00
var currentDeferred;
var hasChanges = false;
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;
}
2015-09-17 09:04:04 -07:00
function reload(page, item) {
Dashboard.showLoadingMsg();
2015-09-17 09:04:04 -07:00
if (item) {
reloadItem(page, item);
}
else {
2015-12-14 08:43:03 -07:00
ApiClient.getItem(Dashboard.getCurrentUserId(), currentItem.Id).then(function (item) {
2015-09-17 09:04:04 -07:00
reloadItem(page, item);
});
}
}
2015-09-17 09:04:04 -07:00
function reloadItem(page, item) {
2015-09-17 09:04:04 -07:00
currentItem = item;
2013-11-27 12:04:19 -07:00
2015-12-14 08:43:03 -07:00
ApiClient.getRemoteImageProviders(getBaseRemoteOptions()).then(function (providers) {
2013-11-27 12:04:19 -07:00
2015-09-17 09:04:04 -07:00
if (providers.length) {
2015-10-15 22:36:16 -07:00
$('.btnBrowseAllImages', page).removeClass('hide');
2015-09-17 09:04:04 -07:00
} else {
2015-10-15 22:36:16 -07:00
$('.btnBrowseAllImages', page).addClass('hide');
2015-09-17 09:04:04 -07:00
}
2013-11-27 12:04:19 -07:00
2015-12-14 08:43:03 -07:00
ApiClient.getItemImageInfos(currentItem.Id).then(function (imageInfos) {
2015-09-17 09:04:04 -07:00
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">';
2015-09-17 18:51:22 -07:00
html += '<div class="editorTileInner">';
2015-07-15 04:26:47 -07:00
2015-07-14 12:04:16 -07:00
var height = 150;
2015-09-24 22:15:29 -07:00
html += '<div style="height:' + height + 'px;vertical-align:top;background-repeat:no-repeat;background-position:center bottom;background-size:contain;" class="lazy" data-src="' + LibraryBrowser.getImageUrl(currentItem, image.ImageType, image.ImageIndex, { height: height }) + '"></div>';
2013-05-04 21:49:49 -07:00
2015-09-17 18:51:22 -07:00
html += '<div class="editorTileFooter">';
if (image.ImageType !== "Backdrop" && image.ImageType !== "Screenshot") {
html += '<h3>' + image.ImageType + '</h3>';
}
2013-05-04 21:49:49 -07:00
2015-08-02 16:47:31 -07:00
if (image.Width && image.Height) {
html += '<p>' + image.Width + ' X ' + image.Height + '</p>';
} else {
html += '<p>&nbsp;</p>';
}
2013-05-04 21:49:49 -07:00
2015-09-17 09:04:04 -07:00
html += '<div>';
2013-05-04 21:49:49 -07:00
if (image.ImageType == "Backdrop" || image.ImageType == "Screenshot") {
if (i > 0) {
2015-09-17 10:24:23 -07:00
html += '<paper-icon-button class="btnMoveImage" icon="chevron-left" data-imagetype="' + image.ImageType + '" data-index="' + image.ImageIndex + '" data-newindex="' + (image.ImageIndex - 1) + '" title="' + Globalize.translate('ButtonMoveLeft') + '"></paper-icon-button>';
2013-05-04 21:49:49 -07:00
} else {
2015-07-14 12:04:16 -07:00
html += '<paper-icon-button icon="chevron-left" disabled title="' + Globalize.translate('ButtonMoveLeft') + '"></paper-icon-button>';
2013-05-04 21:49:49 -07:00
}
if (i < length - 1) {
2015-09-17 10:24:23 -07:00
html += '<paper-icon-button class="btnMoveImage" icon="chevron-right" data-imagetype="' + image.ImageType + '" data-index="' + image.ImageIndex + '" data-newindex="' + (image.ImageIndex + 1) + '" title="' + Globalize.translate('ButtonMoveRight') + '"></paper-icon-button>';
2013-05-04 21:49:49 -07:00
} else {
2015-07-14 12:04:16 -07:00
html += '<paper-icon-button icon="chevron-right" disabled title="' + Globalize.translate('ButtonMoveRight') + '"></paper-icon-button>';
2013-05-04 21:49:49 -07:00
}
}
2015-09-17 18:51:22 -07:00
else {
if (imageProviders.length) {
html += '<paper-icon-button icon="search" data-imagetype="' + image.ImageType + '" class="btnSearchImages" title="' + Globalize.translate('ButtonBrowseOnlineImages') + '"></paper-icon-button>';
}
}
2013-10-31 18:55:38 -07:00
2015-09-17 10:24:23 -07:00
html += '<paper-icon-button icon="delete" data-imagetype="' + image.ImageType + '" data-index="' + (image.ImageIndex != null ? image.ImageIndex : "null") + '" class="btnDeleteImage" title="' + Globalize.translate('Delete') + '"></paper-icon-button>';
2015-07-15 04:26:47 -07:00
2015-09-17 09:04:04 -07:00
html += '</div>';
2013-05-04 21:49:49 -07:00
html += '</div>';
2015-09-17 18:51:22 -07:00
html += '</div>';
2013-05-04 21:49:49 -07:00
html += '</div>';
}
2015-09-17 10:24:23 -07:00
elem.innerHTML = html;
2015-09-24 22:15:29 -07:00
ImageLoader.lazyChildren(elem);
2015-09-17 10:24:23 -07:00
$('.btnSearchImages', elem).on('click', function () {
showImageDownloader(page, this.getAttribute('data-imagetype'));
});
$('.btnDeleteImage', elem).on('click', function () {
var type = this.getAttribute('data-imagetype');
var index = this.getAttribute('data-index');
index = index == "null" ? null : parseInt(index);
2016-02-22 12:12:06 -07:00
require(['confirm'], function (confirm) {
confirm(Globalize.translate('DeleteImageConfirmation'), Globalize.translate('HeaderDeleteImage')).then(function () {
2015-12-14 08:43:03 -07:00
ApiClient.deleteItemImage(currentItem.Id, type, index).then(function () {
2015-09-17 10:24:23 -07:00
2015-10-15 22:36:16 -07:00
hasChanges = true;
2015-09-17 10:24:23 -07:00
reload(page);
});
2016-02-22 12:12:06 -07:00
});
2015-09-17 10:24:23 -07:00
});
});
$('.btnMoveImage', elem).on('click', function () {
var type = this.getAttribute('data-imagetype');
var index = parseInt(this.getAttribute('data-index'));
var newIndex = parseInt(this.getAttribute('data-newindex'));
2015-12-14 08:43:03 -07:00
ApiClient.updateItemImageIndex(currentItem.Id, type, index, newIndex).then(function () {
2015-09-17 10:24:23 -07:00
2015-10-15 22:36:16 -07:00
hasChanges = true;
2015-09-17 10:24:23 -07:00
reload(page);
});
});
2013-05-04 21:49:49 -07:00
}
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
2015-09-17 10:24:23 -07:00
renderImages(page, item, images, imageProviders, page.querySelector('#images'));
2013-05-04 21:49:49 -07:00
}
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();
2015-09-17 10:24:23 -07:00
renderImages(page, item, images, imageProviders, page.querySelector('#backdrops'));
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();
}
}
2015-09-17 10:24:23 -07:00
function showImageDownloader(page, imageType) {
2015-12-14 08:43:03 -07:00
require(['components/imagedownloader/imagedownloader'], function (ImageDownloader) {
2013-05-04 21:49:49 -07:00
2015-12-14 08:43:03 -07:00
ImageDownloader.show(currentItem.Id, currentItem.Type, imageType).then(function (hasChanged) {
2013-05-04 21:49:49 -07:00
2015-10-15 22:36:16 -07:00
if (hasChanged) {
hasChanges = true;
2015-09-17 10:24:23 -07:00
reload(page);
2013-05-04 21:49:49 -07:00
}
});
2015-09-17 10:24:23 -07:00
});
}
2015-10-15 22:36:16 -07:00
function initEditor(page, options) {
2015-08-02 16:47:31 -07:00
$('.btnOpenUploadMenu', page).on('click', function () {
2015-07-14 12:04:16 -07:00
2016-02-06 13:32:14 -07:00
var imageType = this.getAttribute('data-imagetype');
2015-12-14 08:43:03 -07:00
require(['components/imageuploader/imageuploader'], function (imageUploader) {
imageUploader.show(currentItem.Id, {
2015-09-17 09:04:04 -07:00
2016-02-06 13:32:14 -07:00
theme: options.theme,
imageType: imageType
2015-10-15 22:36:16 -07:00
2015-12-14 08:43:03 -07:00
}).then(function (hasChanged) {
2015-09-17 09:04:04 -07:00
2015-10-15 22:36:16 -07:00
if (hasChanged) {
hasChanges = true;
2015-09-17 09:04:04 -07:00
reload(page);
}
});
});
2015-07-14 12:04:16 -07:00
});
$('.btnBrowseAllImages', page).on('click', function () {
2015-09-17 18:51:22 -07:00
showImageDownloader(page, this.getAttribute('data-imagetype') || 'Primary');
2015-07-14 12:04:16 -07:00
});
2015-09-17 09:04:04 -07:00
}
2015-05-18 18:46:31 -07:00
2015-10-15 22:36:16 -07:00
function showEditor(itemId, options) {
options = options || {};
2013-05-04 21:49:49 -07:00
2015-09-17 09:04:04 -07:00
Dashboard.showLoadingMsg();
2015-12-14 08:43:03 -07:00
var xhr = new XMLHttpRequest();
xhr.open('GET', 'components/imageeditor/imageeditor.template.html', true);
2015-12-14 08:43:03 -07:00
xhr.onload = function (e) {
2015-12-14 08:43:03 -07:00
var template = this.response;
ApiClient.getItem(Dashboard.getCurrentUserId(), itemId).then(function (item) {
2015-12-14 08:43:03 -07:00
var dlg = paperDialogHelper.createDialog({
2016-01-30 13:59:09 -07:00
size: 'fullscreen-border'
2015-10-15 22:36:16 -07:00
});
2016-01-30 13:59:09 -07:00
var theme = options.theme || 'b';
dlg.classList.add('ui-body-' + theme);
dlg.classList.add('background-theme-' + theme);
dlg.classList.add('popupEditor');
2015-09-17 09:04:04 -07:00
var html = '';
html += '<h2 class="dialogHeader">';
2016-01-30 13:59:09 -07:00
html += '<paper-fab icon="arrow-back" mini class="btnCloseDialog" tabindex="-1"></paper-fab>';
2015-09-17 09:04:04 -07:00
html += '<div style="display:inline-block;margin-left:.6em;vertical-align:middle;">' + item.Name + '</div>';
html += '</h2>';
2015-09-17 09:04:04 -07:00
html += '<div class="editorContent">';
html += Globalize.translateDocument(template);
html += '</div>';
2015-09-17 09:04:04 -07:00
dlg.innerHTML = html;
document.body.appendChild(dlg);
2015-10-15 22:36:16 -07:00
initEditor(dlg, options);
2015-09-17 09:04:04 -07:00
// Has to be assigned a z-index after the call to .open()
$(dlg).on('iron-overlay-closed', onDialogClosed);
2015-12-14 08:43:03 -07:00
paperDialogHelper.open(dlg);
2015-09-17 09:04:04 -07:00
var editorContent = dlg.querySelector('.editorContent');
reload(editorContent, item);
2015-12-14 08:43:03 -07:00
$('.btnCloseDialog', dlg).on('click', function () {
paperDialogHelper.close(dlg);
2015-09-29 09:29:06 -07:00
});
2015-09-17 09:04:04 -07:00
});
2015-12-14 08:43:03 -07:00
}
xhr.send();
2015-09-17 09:04:04 -07:00
}
function onDialogClosed() {
$(this).remove();
Dashboard.hideLoadingMsg();
2015-10-15 22:36:16 -07:00
currentDeferred.resolveWith(null, [hasChanges]);
2015-09-17 09:04:04 -07:00
}
2015-12-14 08:43:03 -07:00
return {
2015-10-15 22:36:16 -07:00
show: function (itemId, options) {
2016-02-17 19:55:15 -07:00
var deferred = jQuery.Deferred();
2015-10-15 22:36:16 -07:00
currentDeferred = deferred;
hasChanges = false;
2015-09-17 09:04:04 -07:00
2015-12-14 08:43:03 -07:00
showEditor(itemId, options);
2015-10-15 22:36:16 -07:00
return deferred.promise();
2015-09-17 09:04:04 -07:00
}
};
2015-12-14 08:43:03 -07:00
});