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

326 lines
11 KiB
JavaScript
Raw Normal View History

2016-06-21 21:39:59 -07:00
define(['dialogHelper', 'css!css/metadataeditor.css', 'emby-button', 'paper-icon-button-light'], function (dialogHelper) {
var currentItem;
2016-06-24 09:51:13 -07:00
var currentResolve;
var currentReject;
2015-10-15 22:36:16 -07:00
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);
});
}
}
2016-06-15 11:56:37 -07:00
function addListeners(elems, eventName, fn) {
for (var i = 0, length = elems.length; i < length; i++) {
elems[i].addEventListener(eventName, fn);
}
}
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
2016-06-15 11:56:37 -07:00
var btnBrowseAllImages = page.querySelectorAll('.btnBrowseAllImages');
for (var i = 0, length = btnBrowseAllImages.length; i < length; i++) {
if (providers.length) {
btnBrowseAllImages[i].classList.remove('hide');
} else {
btnBrowseAllImages[i].classList.add('hide');
}
2015-09-17 09:04:04 -07:00
}
2013-11-27 12:04:19 -07:00
2016-06-15 11:56:37 -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) {
2016-06-18 22:26:52 -07:00
html += '<button is="paper-icon-button-light" class="btnMoveImage autoSize" data-imagetype="' + image.ImageType + '" data-index="' + image.ImageIndex + '" data-newindex="' + (image.ImageIndex - 1) + '" title="' + Globalize.translate('ButtonMoveLeft') + '"><i class="md-icon">chevron_left</i></button>';
2013-05-04 21:49:49 -07:00
} else {
2016-06-18 22:26:52 -07:00
html += '<button is="paper-icon-button-light" class="autoSize" disabled title="' + Globalize.translate('ButtonMoveLeft') + '"><i class="md-icon">chevron_left</i></button>';
2013-05-04 21:49:49 -07:00
}
if (i < length - 1) {
2016-06-18 22:26:52 -07:00
html += '<button is="paper-icon-button-light" class="btnMoveImage autoSize" data-imagetype="' + image.ImageType + '" data-index="' + image.ImageIndex + '" data-newindex="' + (image.ImageIndex + 1) + '" title="' + Globalize.translate('ButtonMoveRight') + '"><i class="md-icon">chevron_right</i></button>';
2013-05-04 21:49:49 -07:00
} else {
2016-06-18 22:26:52 -07:00
html += '<button is="paper-icon-button-light" class="autoSize" disabled title="' + Globalize.translate('ButtonMoveRight') + '"><i class="md-icon">chevron_right</i></button>';
2013-05-04 21:49:49 -07:00
}
}
2015-09-17 18:51:22 -07:00
else {
if (imageProviders.length) {
2016-06-18 22:26:52 -07:00
html += '<button is="paper-icon-button-light" data-imagetype="' + image.ImageType + '" class="btnSearchImages autoSize" title="' + Globalize.translate('ButtonBrowseOnlineImages') + '"><i class="md-icon">search</i></button>';
2015-09-17 18:51:22 -07:00
}
}
2013-10-31 18:55:38 -07:00
2016-06-18 22:26:52 -07:00
html += '<button is="paper-icon-button-light" data-imagetype="' + image.ImageType + '" data-index="' + (image.ImageIndex != null ? image.ImageIndex : "null") + '" class="btnDeleteImage autoSize" title="' + Globalize.translate('Delete') + '"><i class="md-icon">delete</i></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
2016-06-15 11:56:37 -07:00
addListeners(elem.querySelectorAll('.btnSearchImages'), 'click', function () {
2015-09-17 10:24:23 -07:00
showImageDownloader(page, this.getAttribute('data-imagetype'));
});
2016-06-15 11:56:37 -07:00
addListeners(elem.querySelectorAll('.btnDeleteImage'), 'click', function () {
2015-09-17 10:24:23 -07:00
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
});
});
2016-06-15 11:56:37 -07:00
addListeners(elem.querySelectorAll('.btnMoveImage'), 'click', function () {
2015-09-17 10:24:23 -07:00
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) {
2016-06-15 09:45:45 -07:00
page.querySelector('#backdropsContainer', page).classList.remove('hide');
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 {
2016-06-15 09:45:45 -07:00
page.querySelector('#backdropsContainer', page).classList.add('hide');
2013-05-04 21:49:49 -07:00
}
}
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) {
2016-06-15 09:45:45 -07:00
page.querySelector('#screenshotsContainer', page).classList.remove('hide');
2016-05-21 12:01:54 -07:00
renderImages(page, item, images, imageProviders, page.querySelector('#screenshots'));
2013-05-04 21:49:49 -07:00
} else {
2016-06-15 09:45:45 -07:00
page.querySelector('#screenshotsContainer', page).classList.add('hide');
2013-05-04 21:49:49 -07:00
}
}
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
2016-06-24 21:22:11 -07:00
ImageDownloader.show(currentItem.Id, currentItem.Type, imageType).then(function () {
2013-05-04 21:49:49 -07:00
2016-06-24 21:22:11 -07:00
hasChanges = true;
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) {
2016-06-15 11:56:37 -07:00
addListeners(page.querySelectorAll('.btnOpenUploadMenu'), 'click', function () {
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
});
2016-06-15 11:56:37 -07:00
addListeners(page.querySelectorAll('.btnBrowseAllImages'), '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) {
2016-03-23 12:03:17 -07:00
var dlg = dialogHelper.createDialog({
2016-06-15 09:45:45 -07:00
size: 'fullscreen-border',
removeOnClose: true
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-06-18 22:26:52 -07:00
html += '<button type="button" is="emby-button" icon="arrow-back" class="fab mini btnCloseDialog autoSize" tabindex="-1"><i class="md-icon">arrow_back</i></button>';
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">';
2016-03-05 12:07:58 -07:00
html += Globalize.translateDocument(template);
2015-09-17 09:04:04 -07:00
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()
2016-06-15 09:45:45 -07:00
dlg.addEventListener('close', onDialogClosed);
2015-09-17 09:04:04 -07:00
2016-03-23 12:03:17 -07:00
dialogHelper.open(dlg);
2015-09-17 09:04:04 -07:00
var editorContent = dlg.querySelector('.editorContent');
reload(editorContent, item);
2016-06-15 09:45:45 -07:00
dlg.querySelector('.btnCloseDialog').addEventListener('click', function () {
2015-12-14 08:43:03 -07:00
2016-03-23 12:03:17 -07:00
dialogHelper.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() {
Dashboard.hideLoadingMsg();
2016-06-24 09:51:13 -07:00
if (hasChanges) {
currentResolve();
} else {
currentReject();
}
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-06-24 09:51:13 -07:00
return new Promise(function (resolve, reject) {
2015-10-15 22:36:16 -07:00
2016-06-24 09:51:13 -07:00
currentResolve = resolve;
currentReject = reject;
hasChanges = false;
2015-09-17 09:04:04 -07:00
2016-06-24 09:51:13 -07:00
showEditor(itemId, options);
});
2015-09-17 09:04:04 -07:00
}
};
2015-12-14 08:43:03 -07:00
});