jellyfin-web/dashboard-ui/bower_components/emby-webcomponents/imageeditor/imageeditor.js

500 lines
18 KiB
JavaScript
Raw Normal View History

2016-08-30 11:13:41 -07:00
define(['dialogHelper', 'connectionManager', 'loading', 'dom', 'layoutManager', 'focusManager', 'globalize', 'scrollHelper', 'imageLoader', 'require', 'cardStyle', 'formDialogStyle', 'emby-button', 'paper-icon-button-light'], function (dialogHelper, connectionManager, loading, dom, layoutManager, focusManager, globalize, scrollHelper, imageLoader, require) {
var currentItem;
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;
}
2016-08-30 11:13:41 -07:00
function reload(page, item, focusContext) {
2016-08-29 21:33:24 -07:00
loading.show();
2016-08-29 23:06:24 -07:00
var apiClient;
2015-09-17 09:04:04 -07:00
if (item) {
2016-08-29 23:06:24 -07:00
apiClient = connectionManager.getApiClient(item.ServerId);
2016-08-30 11:13:41 -07:00
reloadItem(page, item, apiClient, focusContext);
2015-09-17 09:04:04 -07:00
}
else {
2016-08-29 23:06:24 -07:00
apiClient = connectionManager.getApiClient(currentItem.ServerId);
apiClient.getItem(apiClient.getCurrentUserId(), currentItem.Id).then(function (item) {
2016-08-30 11:13:41 -07:00
reloadItem(page, item, apiClient, focusContext);
2015-09-17 09:04:04 -07:00
});
}
}
2016-08-30 10:39:15 -07:00
function addListeners(container, className, eventName, fn) {
2016-06-15 11:56:37 -07:00
2016-08-30 10:39:15 -07:00
container.addEventListener(eventName, function (e) {
var elem = dom.parentWithClass(e.target, className);
if (elem) {
fn.call(elem, e);
}
});
2016-06-15 11:56:37 -07:00
}
2016-08-30 11:13:41 -07:00
function reloadItem(page, item, apiClient, focusContext) {
2015-09-17 09:04:04 -07:00
currentItem = item;
2013-11-27 12:04:19 -07:00
2016-08-29 23:06:24 -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
2016-08-29 23:06:24 -07:00
apiClient.getItemImageInfos(currentItem.Id).then(function (imageInfos) {
2016-08-29 23:06:24 -07:00
renderStandardImages(page, apiClient, item, imageInfos, providers);
renderBackdrops(page, apiClient, item, imageInfos, providers);
renderScreenshots(page, apiClient, item, imageInfos, providers);
2016-08-29 21:33:24 -07:00
loading.hide();
2016-08-30 11:13:41 -07:00
if (layoutManager.tv) {
focusManager.autoFocus((focusContext || page));
}
});
2013-05-04 21:49:49 -07:00
});
}
2016-08-29 23:06:24 -07:00
function getImageUrl(item, apiClient, type, index, options) {
options = options || {};
options.type = type;
options.index = index;
if (type == 'Backdrop') {
options.tag = item.BackdropImageTags[index];
} else if (type == 'Screenshot') {
options.tag = item.ScreenshotImageTags[index];
} else if (type == 'Primary') {
options.tag = item.PrimaryImageTag || item.ImageTags[type];
} else {
options.tag = item.ImageTags[type];
}
// For search hints
2016-08-29 23:06:24 -07:00
return apiClient.getScaledImageUrl(item.Id || item.ItemId, options);
}
2016-08-30 11:13:41 -07:00
function getCardHtml(image, index, numImages, apiClient, imageProviders, imageSize, tagName, enableFooterButtons) {
2013-05-04 21:49:49 -07:00
var html = '';
2016-08-30 11:13:41 -07:00
var cssClass = "card scalableCard imageEditorCard";
2016-08-30 10:39:15 -07:00
var cardBoxCssClass = 'cardBox visualCardBox';
cssClass += " backdropCard backdropCard-scalable";
2013-05-04 21:49:49 -07:00
2016-08-29 23:06:24 -07:00
if (tagName == 'button') {
2016-08-30 10:39:15 -07:00
cssClass += ' card-focusscale btnImageCard';
cardBoxCssClass += ' cardBox-focustransform cardBox-focustransform-transition';
html += '<button type="button" class="' + cssClass + '"';
2016-08-29 23:06:24 -07:00
} else {
2016-08-30 10:39:15 -07:00
html += '<div class="' + cssClass + '"';
2016-08-29 23:06:24 -07:00
}
2013-05-04 21:49:49 -07:00
2016-08-30 11:13:41 -07:00
html += ' data-id="' + currentItem.Id + '" data-serverid="' + apiClient.serverId() + '" data-index="' + index + '" data-numimages="' + numImages + '" data-imagetype="' + image.ImageType + '" data-providers="' + imageProviders.length + '"';
2016-08-30 10:39:15 -07:00
html += '>';
html += '<div class="' + cardBoxCssClass + '">';
2016-08-29 23:06:24 -07:00
html += '<div class="cardScalable visualCardBox-cardScalable" style="background-color:transparent;">';
html += '<div class="cardPadder-backdrop"></div>';
2015-07-15 04:26:47 -07:00
2016-08-29 23:06:24 -07:00
html += '<div class="cardContent">';
2015-07-14 12:04:16 -07:00
2016-08-29 23:06:24 -07:00
var imageUrl = getImageUrl(currentItem, apiClient, image.ImageType, image.ImageIndex, { maxWidth: imageSize });
2013-05-04 21:49:49 -07:00
2016-08-29 23:06:24 -07:00
html += '<div class="cardImageContainer" style="background-image:url(\'' + imageUrl + '\');background-position:center bottom;"></div>';
2015-09-17 18:51:22 -07:00
2016-08-29 23:06:24 -07:00
html += '</div>';
html += '</div>';
2013-05-04 21:49:49 -07:00
2016-08-29 23:06:24 -07:00
html += '<div class="cardFooter visualCardBox-cardFooter">';
html += '<h3 class="cardText cardTextCentered" style="margin:0;">' + image.ImageType + '</h3>';
html += '<div class="cardText cardText-secondary cardTextCentered">';
if (image.Width && image.Height) {
html += image.Width + ' X ' + image.Height;
} else {
html += '&nbsp;';
}
html += '</div>';
2013-05-04 21:49:49 -07:00
2016-08-29 23:06:24 -07:00
if (enableFooterButtons) {
html += '<div class="cardText cardTextCentered">';
2013-05-04 21:49:49 -07:00
if (image.ImageType == "Backdrop" || image.ImageType == "Screenshot") {
2016-08-29 23:06:24 -07:00
if (index > 0) {
2016-09-22 23:57:24 -07:00
html += '<button type="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('sharedcomponents#MoveLeft') + '"><i class="md-icon">chevron_left</i></button>';
2013-05-04 21:49:49 -07:00
} else {
2016-09-22 23:57:24 -07:00
html += '<button type="button" is="paper-icon-button-light" class="autoSize" disabled title="' + globalize.translate('sharedcomponents#MoveLeft') + '"><i class="md-icon">chevron_left</i></button>';
2013-05-04 21:49:49 -07:00
}
2016-08-30 11:13:41 -07:00
if (index < numImages - 1) {
2016-09-22 23:57:24 -07:00
html += '<button type="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('sharedcomponents#MoveRight') + '"><i class="md-icon">chevron_right</i></button>';
2013-05-04 21:49:49 -07:00
} else {
2016-09-22 23:57:24 -07:00
html += '<button type="button" is="paper-icon-button-light" class="autoSize" disabled title="' + globalize.translate('sharedcomponents#MoveRight') + '"><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-09-22 23:57:24 -07:00
html += '<button type="button" is="paper-icon-button-light" data-imagetype="' + image.ImageType + '" class="btnSearchImages autoSize" title="' + globalize.translate('sharedcomponents#Search') + '"><i class="md-icon">search</i></button>';
2015-09-17 18:51:22 -07:00
}
}
2013-10-31 18:55:38 -07:00
2016-09-22 23:57:24 -07:00
html += '<button type="button" is="paper-icon-button-light" data-imagetype="' + image.ImageType + '" data-index="' + (image.ImageIndex != null ? image.ImageIndex : "null") + '" class="btnDeleteImage autoSize" title="' + globalize.translate('sharedcomponents#Delete') + '"><i class="md-icon">delete</i></button>';
2015-09-17 09:04:04 -07:00
html += '</div>';
2016-08-29 23:06:24 -07:00
}
2013-05-04 21:49:49 -07:00
2016-08-29 23:06:24 -07:00
html += '</div>';
html += '</div>';
html += '</div>';
html += '</' + tagName + '>';
return html;
}
2016-08-30 10:39:15 -07:00
function deleteImage(context, itemId, type, index, apiClient, enableConfirmation) {
var afterConfirm = function () {
apiClient.deleteItemImage(itemId, type, index).then(function () {
hasChanges = true;
reload(context);
});
};
if (!enableConfirmation) {
afterConfirm();
return;
}
require(['confirm'], function (confirm) {
2016-09-17 22:52:10 -07:00
confirm({
text: globalize.translate('sharedcomponents#ConfirmDeleteImage'),
confirmText: globalize.translate('sharedcomponents#Delete'),
primary: 'cancel'
}).then(afterConfirm);
2016-08-30 10:39:15 -07:00
});
}
2016-08-30 11:13:41 -07:00
function moveImage(context, apiClient, itemId, type, index, newIndex, focusContext) {
apiClient.updateItemImageIndex(itemId, type, index, newIndex).then(function () {
hasChanges = true;
reload(context, null, focusContext);
2016-09-22 23:57:24 -07:00
}, function() {
require(['alert'], function (alert) {
alert(globalize.translate('sharedcomponents#DefaultErrorMessage'));
});
2016-08-30 11:13:41 -07:00
});
}
2016-08-29 23:06:24 -07:00
function renderImages(page, item, apiClient, images, imageProviders, elem) {
var html = '';
var imageSize = 300;
var windowSize = dom.getWindowSize();
if (windowSize.innerWidth >= 1280) {
imageSize = Math.round(windowSize.innerWidth / 4);
}
var tagName = layoutManager.tv ? 'button' : 'div';
var enableFooterButtons = !layoutManager.tv;
for (var i = 0, length = images.length; i < length; i++) {
var image = images[i];
2016-08-30 11:13:41 -07:00
html += getCardHtml(image, i, length, apiClient, imageProviders, imageSize, tagName, enableFooterButtons);
2013-05-04 21:49:49 -07:00
}
2015-09-17 10:24:23 -07:00
elem.innerHTML = html;
2016-08-29 23:09:42 -07:00
imageLoader.lazyChildren(elem);
2013-05-04 21:49:49 -07:00
}
2016-08-29 23:06:24 -07:00
function renderStandardImages(page, apiClient, 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
2016-08-29 23:06:24 -07:00
renderImages(page, item, apiClient, images, imageProviders, page.querySelector('#images'));
2013-05-04 21:49:49 -07:00
}
2016-08-29 23:06:24 -07:00
function renderBackdrops(page, apiClient, 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');
2016-08-29 23:06:24 -07:00
renderImages(page, item, apiClient, 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
}
}
2016-08-29 23:06:24 -07:00
function renderScreenshots(page, apiClient, 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-08-29 23:06:24 -07:00
renderImages(page, item, apiClient, 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
});
2016-09-12 11:10:09 -07:00
}, function () {
require(['alert'], function (alert) {
alert('This feature is coming soon to Emby Theater.');
});
2015-09-17 10:24:23 -07:00
});
}
2016-08-30 10:39:15 -07:00
function showActionSheet(context, imageCard) {
var itemId = imageCard.getAttribute('data-id');
var serverId = imageCard.getAttribute('data-serverid');
var apiClient = connectionManager.getApiClient(serverId);
var type = imageCard.getAttribute('data-imagetype');
var index = parseInt(imageCard.getAttribute('data-index'));
var providerCount = parseInt(imageCard.getAttribute('data-providers'));
2016-08-30 11:13:41 -07:00
var numImages = parseInt(imageCard.getAttribute('data-numimages'));
2016-08-30 10:39:15 -07:00
require(['actionsheet'], function (actionSheet) {
var commands = [];
commands.push({
name: globalize.translate('sharedcomponents#Delete'),
id: 'delete'
});
2016-08-30 11:13:41 -07:00
if (type == 'Backdrop' || type == 'Screenshot') {
if (index > 0) {
commands.push({
name: globalize.translate('sharedcomponents#MoveLeft'),
id: 'moveleft'
});
}
if (index < numImages - 1) {
commands.push({
name: globalize.translate('sharedcomponents#MoveRight'),
id: 'moveright'
});
}
}
2016-08-30 10:39:15 -07:00
if (providerCount) {
commands.push({
name: globalize.translate('sharedcomponents#Search'),
id: 'search'
});
}
actionSheet.show({
items: commands,
positionTo: imageCard
}).then(function (id) {
switch (id) {
case 'delete':
deleteImage(context, itemId, type, index, apiClient, false);
break;
case 'search':
showImageDownloader(context, type);
break;
2016-08-30 11:13:41 -07:00
case 'moveleft':
moveImage(context, apiClient, itemId, type, index, index - 1, dom.parentWithClass(imageCard, 'itemsContainer'));
break;
case 'moveright':
moveImage(context, apiClient, itemId, type, index, index + 1, dom.parentWithClass(imageCard, 'itemsContainer'));
break;
2016-08-30 10:39:15 -07:00
default:
break;
}
});
});
}
2016-09-22 23:57:24 -07:00
function initEditor(context, options) {
2016-09-22 23:57:24 -07:00
addListeners(context, '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;
2016-09-22 23:57:24 -07:00
reload(context);
2015-09-17 09:04:04 -07:00
}
});
2016-09-12 11:10:09 -07:00
}, function () {
require(['alert'], function (alert) {
alert('This feature is coming soon to Emby Theater.');
});
2015-09-17 09:04:04 -07:00
});
2015-07-14 12:04:16 -07:00
});
2016-09-22 23:57:24 -07:00
addListeners(context, 'btnSearchImages', 'click', function () {
showImageDownloader(context, this.getAttribute('data-imagetype'));
});
addListeners(context, 'btnBrowseAllImages', 'click', function () {
showImageDownloader(context, this.getAttribute('data-imagetype') || 'Primary');
});
addListeners(context, 'btnImageCard', 'click', function () {
showActionSheet(context, this);
});
addListeners(context, 'btnDeleteImage', 'click', function () {
var type = this.getAttribute('data-imagetype');
var index = this.getAttribute('data-index');
index = index == "null" ? null : parseInt(index);
var apiClient = connectionManager.getApiClient(currentItem.ServerId);
deleteImage(context, currentItem.Id, type, index, apiClient, true);
2015-07-14 12:04:16 -07:00
});
2016-08-30 10:39:15 -07:00
2016-09-22 23:57:24 -07:00
addListeners(context, 'btnMoveImage', 'click', function () {
var type = this.getAttribute('data-imagetype');
var index = this.getAttribute('data-index');
var newIndex = this.getAttribute('data-newindex');
var apiClient = connectionManager.getApiClient(currentItem.ServerId);
moveImage(context, apiClient, currentItem.Id, type, index, newIndex, dom.parentWithClass(this, 'itemsContainer'));
2016-08-30 10:39:15 -07:00
});
2015-09-17 09:04:04 -07:00
}
2015-05-18 18:46:31 -07:00
2016-08-29 23:06:24 -07:00
function showEditor(options, resolve, reject) {
2015-10-15 22:36:16 -07:00
2016-08-29 23:06:24 -07:00
var itemId = options.itemId;
var serverId = options.serverId;
2013-05-04 21:49:49 -07:00
2016-08-29 21:33:24 -07:00
loading.show();
2016-08-29 23:06:24 -07:00
require(['text!./imageeditor.template.html'], function (template) {
var apiClient = connectionManager.getApiClient(serverId);
apiClient.getItem(apiClient.getCurrentUserId(), itemId).then(function (item) {
2016-08-29 23:06:24 -07:00
var dialogOptions = {
2016-06-15 09:45:45 -07:00
removeOnClose: true
2016-08-29 23:06:24 -07:00
};
2016-08-29 23:06:24 -07:00
if (layoutManager.tv) {
dialogOptions.size = 'fullscreen';
} else {
dialogOptions.size = 'fullscreen-border';
}
2016-01-30 13:59:09 -07:00
2016-08-29 23:06:24 -07:00
var dlg = dialogHelper.createDialog(dialogOptions);
2016-01-30 13:59:09 -07:00
2016-08-29 23:06:24 -07:00
dlg.classList.add('formDialog');
2016-08-29 23:06:24 -07:00
dlg.innerHTML = globalize.translateDocument(template, 'sharedcomponents');
2016-08-29 23:06:24 -07:00
if (layoutManager.tv) {
scrollHelper.centerFocus.on(dlg, false);
}
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.addEventListener('close', function () {
2016-08-29 23:06:24 -07:00
if (layoutManager.tv) {
scrollHelper.centerFocus.off(dlg, false);
}
2016-08-29 21:33:24 -07:00
loading.hide();
if (hasChanges) {
resolve();
} else {
reject();
}
});
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
2016-08-29 23:06:24 -07:00
reload(dlg, item);
2015-09-17 09:04:04 -07:00
2016-08-29 23:06:24 -07:00
dlg.querySelector('.btnCancel').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
});
2016-08-29 23:06:24 -07:00
});
2015-09-17 09:04:04 -07:00
}
2015-12-14 08:43:03 -07:00
return {
2016-08-29 23:06:24 -07:00
show: function (options) {
2015-10-15 22:36:16 -07:00
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
hasChanges = false;
2015-09-17 09:04:04 -07:00
2016-08-29 23:06:24 -07:00
showEditor(options, resolve, reject);
2016-06-24 09:51:13 -07:00
});
2015-09-17 09:04:04 -07:00
}
};
2015-12-14 08:43:03 -07:00
});