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

437 lines
12 KiB
JavaScript
Raw Normal View History

2016-06-11 08:56:15 -07:00
define(['dialogHelper', 'loading', 'jQuery', 'emby-input', 'emby-checkbox', 'paper-icon-button-light'], function (dialogHelper, loading, $) {
2015-10-04 09:15:08 -07:00
var currentItem;
2016-02-25 23:36:03 -07:00
var currentItemType;
2015-10-04 09:15:08 -07:00
var currentDeferred;
var hasChanges = false;
var currentSearchResult;
function searchForIdentificationResults(page) {
var lookupInfo = {
ProviderIds: {}
};
$('.identifyField', page).each(function () {
var value = this.value;
if (value) {
if (this.type == 'number') {
value = parseInt(value);
}
lookupInfo[this.getAttribute('data-lookup')] = value;
}
});
var hasId = false;
$('.txtLookupId', page).each(function () {
var value = this.value;
if (value) {
hasId = true;
}
lookupInfo.ProviderIds[this.getAttribute('data-providerkey')] = value;
});
if (!hasId && !lookupInfo.Name) {
2016-02-24 23:38:12 -07:00
require(['toast'], function (toast) {
toast(Globalize.translate('MessagePleaseEnterNameOrId'));
});
2015-10-04 09:15:08 -07:00
return;
}
2016-02-25 23:36:03 -07:00
if (currentItem && currentItem.GameSystem) {
2015-10-04 09:15:08 -07:00
lookupInfo.GameSystem = currentItem.GameSystem;
}
lookupInfo = {
SearchInfo: lookupInfo,
IncludeDisabledProviders: true
};
2016-05-03 19:29:49 -07:00
loading.show();
2015-10-04 09:15:08 -07:00
ApiClient.ajax({
type: "POST",
2016-02-25 23:36:03 -07:00
url: ApiClient.getUrl("Items/RemoteSearch/" + currentItemType),
2015-10-04 09:15:08 -07:00
data: JSON.stringify(lookupInfo),
2015-12-14 08:43:03 -07:00
contentType: "application/json",
dataType: 'json'
2015-10-04 09:15:08 -07:00
2015-12-14 08:43:03 -07:00
}).then(function (results) {
2015-10-04 09:15:08 -07:00
2016-05-03 19:29:49 -07:00
loading.hide();
2015-10-04 09:15:08 -07:00
showIdentificationSearchResults(page, results);
});
}
function showIdentificationSearchResults(page, results) {
$('.popupIdentifyForm', page).hide();
$('.identificationSearchResults', page).show();
$('.identifyOptionsForm', page).hide();
$('.btnIdentifyBack', page).show();
var html = '';
for (var i = 0, length = results.length; i < length; i++) {
var result = results[i];
html += getSearchResultHtml(result, i);
}
2016-03-06 11:09:20 -07:00
var elem = $('.identificationSearchResultList', page).html(html);
2015-10-04 09:15:08 -07:00
$('.searchImage', elem).on('click', function () {
var index = parseInt(this.getAttribute('data-index'));
var currentResult = results[index];
2016-02-25 23:36:03 -07:00
if (currentItem != null) {
showIdentifyOptions(page, currentResult);
} else {
finishFindNewDialog(page, currentResult);
}
2015-10-04 09:15:08 -07:00
});
}
2016-02-25 23:36:03 -07:00
function finishFindNewDialog(dlg, identifyResult) {
currentSearchResult = identifyResult;
hasChanges = true;
2016-05-03 19:29:49 -07:00
loading.hide();
2016-02-25 23:36:03 -07:00
2016-03-23 12:03:17 -07:00
dialogHelper.close(dlg);
2016-02-25 23:36:03 -07:00
}
2015-10-04 09:15:08 -07:00
function showIdentifyOptions(page, identifyResult) {
$('.popupIdentifyForm', page).hide();
$('.identificationSearchResults', page).hide();
$('.identifyOptionsForm', page).show();
$('.btnIdentifyBack', page).show();
$('#chkIdentifyReplaceImages', page).checked(true);
currentSearchResult = identifyResult;
var lines = [];
lines.push(identifyResult.Name);
if (identifyResult.ProductionYear) {
lines.push(identifyResult.ProductionYear);
}
if (identifyResult.GameSystem) {
lines.push(identifyResult.GameSystem);
}
var resultHtml = lines.join('<br/>');
if (identifyResult.ImageUrl) {
var displayUrl = getSearchImageDisplayUrl(identifyResult.ImageUrl, identifyResult.SearchProviderName);
resultHtml = '<img src="' + displayUrl + '" style="max-height:160px;" /><br/>' + resultHtml;
}
$('.selectedSearchResult', page).html(resultHtml);
}
function getSearchResultHtml(result, index) {
var html = '';
var cssClass = "card";
2016-02-25 23:36:03 -07:00
if (currentItemType == "Episode") {
2015-10-04 09:15:08 -07:00
cssClass += " backdropCard";
}
2016-02-25 23:36:03 -07:00
else if (currentItemType == "MusicAlbum" || currentItemType == "MusicArtist") {
2015-10-04 09:15:08 -07:00
cssClass += " squareCard";
}
else {
cssClass += " portraitCard";
}
html += '<div class="' + cssClass + '">';
2015-10-04 11:20:56 -07:00
html += '<div class="cardBox">';
2015-10-04 09:15:08 -07:00
html += '<div class="cardScalable">';
html += '<div class="cardPadder"></div>';
html += '<a class="cardContent searchImage" href="#" data-index="' + index + '">';
if (result.ImageUrl) {
var displayUrl = getSearchImageDisplayUrl(result.ImageUrl, result.SearchProviderName);
html += '<div class="cardImage" style="background-image:url(\'' + displayUrl + '\');"></div>';
} else {
html += '<div class="cardImage iconCardImage"><iron-icon icon="search"></iron-icon></div>';
}
html += '</a>';
html += '</div>';
html += '<div class="cardFooter outerCardFooter">';
html += '<div class="cardText cardTextCentered">' + result.Name + '</div>';
html += '<div class="cardText cardTextCentered">';
html += result.ProductionYear || '&nbsp;';
html += '</div>';
if (result.GameSystem) {
html += '<div class="cardText cardTextCentered">';
html += result.GameSystem;
html += '</div>';
}
html += '</div>';
2015-10-04 11:20:56 -07:00
html += '</div>';
2015-10-04 09:15:08 -07:00
html += '</div>';
return html;
}
function getSearchImageDisplayUrl(url, provider) {
return ApiClient.getUrl("Items/RemoteSearch/Image", { imageUrl: url, ProviderName: provider });
}
function submitIdentficationResult(page) {
2016-05-03 19:29:49 -07:00
loading.show();
2015-10-04 09:15:08 -07:00
var options = {
ReplaceAllImages: $('#chkIdentifyReplaceImages', page).checked()
};
ApiClient.ajax({
type: "POST",
url: ApiClient.getUrl("Items/RemoteSearch/Apply/" + currentItem.Id, options),
data: JSON.stringify(currentSearchResult),
contentType: "application/json"
2015-12-14 08:43:03 -07:00
}).then(function () {
2015-10-04 09:15:08 -07:00
hasChanges = true;
2016-05-03 19:29:49 -07:00
loading.hide();
2015-10-04 09:15:08 -07:00
2016-03-23 12:03:17 -07:00
dialogHelper.close(page);
2015-10-04 09:15:08 -07:00
2015-12-14 08:43:03 -07:00
}, function () {
2015-10-04 09:15:08 -07:00
2016-05-03 19:29:49 -07:00
loading.hide();
2015-10-04 09:15:08 -07:00
2016-03-23 12:03:17 -07:00
dialogHelper.close(page);
2015-10-04 09:15:08 -07:00
});
}
function showIdentificationForm(page, item) {
2015-12-14 08:43:03 -07:00
ApiClient.getJSON(ApiClient.getUrl("Items/" + item.Id + "/ExternalIdInfos")).then(function (idList) {
2015-10-04 09:15:08 -07:00
var html = '';
var providerIds = item.ProviderIds || {};
for (var i = 0, length = idList.length; i < length; i++) {
var idInfo = idList[i];
var id = "txtLookup" + idInfo.Key;
2016-06-02 09:17:35 -07:00
html += '<div class="inputContainer">';
2015-10-04 09:15:08 -07:00
var idLabel = Globalize.translate('LabelDynamicExternalId').replace('{0}', idInfo.Name);
var value = providerIds[idInfo.Key] || '';
2016-06-02 09:17:35 -07:00
html += '<input is="emby-input" class="txtLookupId" data-providerkey="' + idInfo.Key + '" id="' + id + '" label="' + idLabel + '"/>';
2015-10-04 09:15:08 -07:00
html += '</div>';
}
2016-05-03 19:29:49 -07:00
$('#txtLookupName', page).val('');
2015-10-04 09:15:08 -07:00
if (item.Type == "Person" || item.Type == "BoxSet") {
$('.fldLookupYear', page).hide();
$('#txtLookupYear', page).val('');
} else {
$('.fldLookupYear', page).show();
2016-05-03 19:29:49 -07:00
$('#txtLookupYear', page).val('');
2015-10-04 09:15:08 -07:00
}
2016-03-06 11:09:20 -07:00
$('.identifyProviderIds', page).html(html);
2015-10-04 09:15:08 -07:00
2015-12-15 13:10:41 -07:00
page.querySelector('.dialogHeaderTitle').innerHTML = Globalize.translate('HeaderIdentify');
2015-10-04 09:15:08 -07:00
});
}
function showEditor(itemId) {
2016-05-03 19:29:49 -07:00
loading.show();
2015-10-04 09:15:08 -07:00
2015-12-14 08:43:03 -07:00
var xhr = new XMLHttpRequest();
xhr.open('GET', 'components/itemidentifier/itemidentifier.template.html', true);
2015-10-04 09:15:08 -07:00
2015-12-14 08:43:03 -07:00
xhr.onload = function (e) {
2015-10-04 09:15:08 -07:00
2015-12-14 08:43:03 -07:00
var template = this.response;
2016-05-03 19:29:49 -07:00
ApiClient.getItem(ApiClient.getCurrentUserId(), itemId).then(function (item) {
2015-10-04 09:15:08 -07:00
currentItem = item;
2016-02-25 23:36:03 -07:00
currentItemType = currentItem.Type;
2015-10-04 09:15:08 -07:00
2016-03-23 12:03:17 -07:00
var dlg = dialogHelper.createDialog({
2015-12-15 13:58:52 -07:00
size: 'medium'
});
2015-10-04 09:15:08 -07:00
2016-01-30 13:59:09 -07:00
dlg.classList.add('ui-body-b');
dlg.classList.add('background-theme-b');
2015-10-04 09:15:08 -07:00
var html = '';
2016-03-05 12:07:58 -07:00
html += Globalize.translateDocument(template);
2015-10-04 09:15:08 -07:00
dlg.innerHTML = html;
document.body.appendChild(dlg);
// Has to be assigned a z-index after the call to .open()
2016-03-22 10:46:57 -07:00
$(dlg).on('close', onDialogClosed);
2015-10-04 09:15:08 -07:00
2016-03-23 12:03:17 -07:00
dialogHelper.open(dlg);
2015-10-04 09:15:08 -07:00
2016-02-25 23:36:03 -07:00
dlg.querySelector('.popupIdentifyForm').addEventListener('submit', function (e) {
e.preventDefault();
searchForIdentificationResults(dlg);
return false;
});
dlg.querySelector('.identifyOptionsForm').addEventListener('submit', function (e) {
e.preventDefault();
submitIdentficationResult(dlg);
return false;
});
2015-10-04 09:15:08 -07:00
2015-12-15 13:10:41 -07:00
$('.btnCancel', dlg).on('click', function () {
2015-10-04 09:15:08 -07:00
2016-03-23 12:03:17 -07:00
dialogHelper.close(dlg);
2015-10-04 09:15:08 -07:00
});
dlg.classList.add('identifyDialog');
showIdentificationForm(dlg, item);
2016-05-03 19:29:49 -07:00
loading.hide();
2015-10-04 09:15:08 -07:00
});
2015-12-14 08:43:03 -07:00
}
xhr.send();
2015-10-04 09:15:08 -07:00
}
function onDialogClosed() {
$(this).remove();
2016-05-03 19:29:49 -07:00
loading.hide();
2015-10-04 09:15:08 -07:00
currentDeferred.resolveWith(null, [hasChanges]);
}
2016-02-25 23:36:03 -07:00
function showEditorFindNew(itemName, itemYear, itemType, resolveFunc) {
currentItem = null;
currentItemType = itemType;
var xhr = new XMLHttpRequest();
xhr.open('GET', 'components/itemidentifier/itemidentifier.template.html', true);
xhr.onload = function (e) {
var template = this.response;
2016-03-23 12:03:17 -07:00
var dlg = dialogHelper.createDialog({
2016-02-25 23:36:03 -07:00
size: 'medium'
});
dlg.classList.add('ui-body-a');
dlg.classList.add('background-theme-a');
var html = '';
2016-03-05 12:07:58 -07:00
html += Globalize.translateDocument(template);
2016-02-25 23:36:03 -07:00
dlg.innerHTML = html;
document.body.appendChild(dlg);
2016-03-23 12:03:17 -07:00
dialogHelper.open(dlg);
2016-02-25 23:36:03 -07:00
dlg.querySelector('.btnCancel').addEventListener('click', function (e) {
2016-03-23 12:03:17 -07:00
dialogHelper.close(dlg);
2016-02-25 23:36:03 -07:00
});
dlg.querySelector('.popupIdentifyForm').addEventListener('submit', function (e) {
e.preventDefault();
searchForIdentificationResults(dlg);
return false;
});
2016-03-22 10:46:57 -07:00
dlg.addEventListener('close', function () {
2016-02-25 23:36:03 -07:00
2016-05-03 19:29:49 -07:00
loading.hide();
2016-02-25 23:36:03 -07:00
var foundItem = hasChanges ? currentSearchResult : null;
resolveFunc(foundItem);
});
dlg.classList.add('identifyDialog');
showIdentificationFormFindNew(dlg, itemName, itemYear, itemType);
}
xhr.send();
}
function showIdentificationFormFindNew(dlg, itemName, itemYear, itemType) {
dlg.querySelector('#txtLookupName').value = itemName;
if (itemType == "Person" || itemType == "BoxSet") {
dlg.querySelector('.fldLookupYear').classList.add('hide');
dlg.querySelector('#txtLookupYear').value = '';
} else {
dlg.querySelector('.fldLookupYear').classList.remove('hide');
dlg.querySelector('#txtLookupYear').value = itemYear;
}
dlg.querySelector('.dialogHeaderTitle').innerHTML = Globalize.translate('HeaderSearch');
}
2016-02-12 23:13:54 -07:00
return {
2015-10-04 09:15:08 -07:00
show: function (itemId) {
2016-02-17 19:55:15 -07:00
var deferred = jQuery.Deferred();
2015-10-04 09:15:08 -07:00
currentDeferred = deferred;
hasChanges = false;
2015-12-14 08:43:03 -07:00
showEditor(itemId);
2015-10-04 09:15:08 -07:00
return deferred.promise();
2016-02-25 23:36:03 -07:00
},
showFindNew: function (itemName, itemYear, itemType) {
return new Promise(function (resolve, reject) {
hasChanges = false;
showEditorFindNew(itemName, itemYear, itemType, resolve);
});
2015-10-04 09:15:08 -07:00
}
};
2015-12-14 08:43:03 -07:00
});