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

262 lines
8.8 KiB
JavaScript
Raw Normal View History

2016-08-22 22:08:07 -07:00
define(['dialogHelper', 'paper-checkbox', 'emby-input', 'emby-button', 'paper-icon-button-light'], function (dialogHelper) {
2016-02-25 23:36:03 -07:00
var extractedName;
var extractedYear;
var currentNewItem;
var existingSeriesHtml;
2016-04-22 09:27:17 -07:00
var seriesLocationsCount = 0;
2016-02-25 23:36:03 -07:00
2016-02-12 21:34:47 -07:00
function onApiFailure(e) {
2016-02-12 21:34:47 -07:00
Dashboard.hideLoadingMsg();
2016-04-22 09:27:17 -07:00
require(['alert'], function (alert) {
alert({
title: Globalize.translate('AutoOrganizeError'),
text: Globalize.translate('ErrorOrganizingFileWithErrorCode', e.headers.get("X-Application-Error-Code"))
});
2016-02-12 21:34:47 -07:00
});
}
2016-02-12 21:34:47 -07:00
function initEpisodeForm(context, item) {
2016-05-14 09:06:01 -07:00
if (!item.ExtractedName || item.ExtractedName.length < 3) {
2016-02-20 23:25:25 -07:00
context.querySelector('.fldRemember').classList.add('hide');
}
else {
context.querySelector('.fldRemember').classList.remove('hide');
}
2016-02-26 12:23:00 -07:00
context.querySelector('.inputFile').innerHTML = item.OriginalFileName;
2016-02-26 12:23:00 -07:00
context.querySelector('#txtSeason').value = item.ExtractedSeasonNumber;
context.querySelector('#txtEpisode').value = item.ExtractedEpisodeNumber;
context.querySelector('#txtEndingEpisode').value = item.ExtractedEndingEpisodeNumber;
2016-04-22 09:27:17 -07:00
//context.querySelector('.extractedName').innerHTML = item.ExtractedName;
2016-02-25 23:36:03 -07:00
extractedName = item.ExtractedName;
extractedYear = item.ExtractedYear;
2016-02-26 12:23:00 -07:00
context.querySelector('#chkRememberCorrection').checked = false;
2016-02-26 12:23:00 -07:00
context.querySelector('#hfResultId').value = item.Id;
2016-02-12 21:34:47 -07:00
ApiClient.getItems(null, {
recursive: true,
includeItemTypes: 'Series',
sortBy: 'SortName'
2016-02-12 21:34:47 -07:00
}).then(function (result) {
2016-02-25 23:36:03 -07:00
existingSeriesHtml = result.Items.map(function (s) {
2016-02-12 21:34:47 -07:00
return '<option value="' + s.Id + '">' + s.Name + '</option>';
2016-02-12 21:34:47 -07:00
}).join('');
2016-02-25 23:36:03 -07:00
existingSeriesHtml = '<option value=""></option>' + existingSeriesHtml;
2016-02-26 12:23:00 -07:00
context.querySelector('#selectSeries').innerHTML = existingSeriesHtml;
ApiClient.getVirtualFolders().then(function (result) {
//var movieLocations = [];
var seriesLocations = [];
for (var n = 0; n < result.length; n++) {
var virtualFolder = result[n];
for (var i = 0, length = virtualFolder.Locations.length; i < length; i++) {
var location = {
value: virtualFolder.Locations[i],
display: virtualFolder.Name + ': ' + virtualFolder.Locations[i]
};
//if (virtualFolder.CollectionType == 'movies') {
// movieLocations.push(location);
//}
if (virtualFolder.CollectionType == 'tvshows') {
seriesLocations.push(location);
}
}
}
2016-04-22 09:27:17 -07:00
seriesLocationsCount = seriesLocations.length;
var seriesFolderHtml = seriesLocations.map(function (s) {
return '<option value="' + s.value + '">' + s.display + '</option>';
}).join('');
if (seriesLocations.length > 1) {
// If the user has multiple folders, add an empty item to enforce a manual selection
seriesFolderHtml = '<option value=""></option>' + seriesFolderHtml;
}
context.querySelector('#selectSeriesFolder').innerHTML = seriesFolderHtml;
}, onApiFailure);
}, onApiFailure);
}
2016-02-12 21:34:47 -07:00
function submitEpisodeForm(dlg) {
2016-02-12 21:34:47 -07:00
Dashboard.showLoadingMsg();
2016-02-26 12:23:00 -07:00
var resultId = dlg.querySelector('#hfResultId').value;
var seriesId = dlg.querySelector('#selectSeries').value;
var targetFolder;
var newProviderIds;
var newSeriesName;
var newSeriesYear;
if (seriesId == "##NEW##" && currentNewItem != null) {
seriesId = null;
newProviderIds = JSON.stringify(currentNewItem.ProviderIds);
newSeriesName = currentNewItem.Name;
newSeriesYear = currentNewItem.ProductionYear;
targetFolder = dlg.querySelector('#selectSeriesFolder').value;
}
2016-02-12 21:34:47 -07:00
var options = {
SeriesId: seriesId,
2016-02-26 12:23:00 -07:00
SeasonNumber: dlg.querySelector('#txtSeason').value,
EpisodeNumber: dlg.querySelector('#txtEpisode').value,
EndingEpisodeNumber: dlg.querySelector('#txtEndingEpisode').value,
RememberCorrection: dlg.querySelector('#chkRememberCorrection').checked,
NewSeriesProviderIds: newProviderIds,
NewSeriesName: newSeriesName,
NewSeriesYear: newSeriesYear,
TargetFolder: targetFolder
};
2016-02-12 21:34:47 -07:00
ApiClient.performEpisodeOrganization(resultId, options).then(function () {
Dashboard.hideLoadingMsg();
2016-02-12 21:34:47 -07:00
dlg.submitted = true;
2016-03-23 12:03:17 -07:00
dialogHelper.close(dlg);
}, onApiFailure);
}
2016-02-25 23:36:03 -07:00
function showNewSeriesDialog(dlg) {
2016-04-22 09:27:17 -07:00
if (seriesLocationsCount == 0) {
require(['alert'], function (alert) {
alert({
title: Globalize.translate('AutoOrganizeError'),
text: Globalize.translate('NoTvFoldersConfigured')
});
});
return;
}
2016-07-30 21:11:23 -07:00
require(['itemIdentifier'], function (itemIdentifier) {
2016-02-25 23:36:03 -07:00
2016-07-30 21:11:23 -07:00
itemIdentifier.showFindNew(extractedName, extractedYear, 'Series', ApiClient.serverId()).then(function (newItem) {
2016-02-25 23:36:03 -07:00
2016-02-26 12:23:00 -07:00
if (newItem != null) {
currentNewItem = newItem;
var seriesHtml = existingSeriesHtml;
2016-02-25 23:36:03 -07:00
seriesHtml = seriesHtml + '<option selected value="##NEW##">' + currentNewItem.Name + '</option>';
2016-02-26 12:23:00 -07:00
dlg.querySelector('#selectSeries').innerHTML = seriesHtml;
selectedSeriesChanged(dlg);
2016-02-25 23:36:03 -07:00
}
});
});
}
function selectedSeriesChanged(dlg) {
var seriesId = dlg.querySelector('#selectSeries').value;
if (seriesId == "##NEW##") {
dlg.querySelector('.fldSelectSeriesFolder').classList.remove('hide');
2016-03-17 10:13:54 -07:00
dlg.querySelector('#selectSeriesFolder').setAttribute('required', 'required');
}
else {
dlg.querySelector('.fldSelectSeriesFolder').classList.add('hide');
2016-03-17 10:13:54 -07:00
dlg.querySelector('#selectSeriesFolder').removeAttribute('required');
}
}
2016-02-12 21:34:47 -07:00
return {
show: function (item) {
return new Promise(function (resolve, reject) {
2016-02-25 23:36:03 -07:00
extractedName = null;
extractedYear = null;
currentNewItem = null;
existingSeriesHtml = null;
2016-02-12 21:34:47 -07:00
var xhr = new XMLHttpRequest();
xhr.open('GET', 'components/fileorganizer/fileorganizer.template.html', true);
2016-02-12 21:34:47 -07:00
xhr.onload = function (e) {
2016-02-12 21:34:47 -07:00
var template = this.response;
2016-03-23 12:03:17 -07:00
var dlg = dialogHelper.createDialog({
2016-02-12 21:34:47 -07:00
removeOnClose: true,
size: 'small'
});
2016-02-12 21:34:47 -07:00
dlg.classList.add('ui-body-a');
dlg.classList.add('background-theme-a');
2016-02-12 21:34:47 -07:00
dlg.classList.add('formDialog');
2016-02-12 21:34:47 -07:00
var html = '';
2016-03-05 12:07:58 -07:00
html += Globalize.translateDocument(template);
2016-02-12 21:34:47 -07:00
dlg.innerHTML = html;
document.body.appendChild(dlg);
2016-08-06 23:15:03 -07:00
dlg.querySelector('.formDialogHeaderTitle').innerHTML = Globalize.translate('FileOrganizeManually');
2016-03-23 12:03:17 -07:00
dialogHelper.open(dlg);
2016-03-22 10:46:57 -07:00
dlg.addEventListener('close', function () {
2016-02-12 21:34:47 -07:00
if (dlg.submitted) {
resolve();
} else {
reject();
}
});
2016-02-12 21:34:47 -07:00
dlg.querySelector('.btnCancel').addEventListener('click', function (e) {
2016-03-23 12:03:17 -07:00
dialogHelper.close(dlg);
2016-02-12 21:34:47 -07:00
});
2016-02-12 21:34:47 -07:00
dlg.querySelector('form').addEventListener('submit', function (e) {
2016-02-12 21:34:47 -07:00
submitEpisodeForm(dlg);
2016-02-12 21:34:47 -07:00
e.preventDefault();
return false;
});
2016-02-25 23:36:03 -07:00
dlg.querySelector('#btnNewSeries').addEventListener('click', function (e) {
showNewSeriesDialog(dlg);
});
dlg.querySelector('#selectSeries').addEventListener('change', function (e) {
selectedSeriesChanged(dlg);
});
2016-02-12 21:34:47 -07:00
initEpisodeForm(dlg, item);
}
2016-02-12 21:34:47 -07:00
xhr.send();
});
}
};
2016-02-12 21:34:47 -07:00
});