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

244 lines
8.2 KiB
JavaScript
Raw Normal View History

2016-02-12 21:34:47 -07:00
define(['paperdialoghelper', 'paper-checkbox', 'paper-input', 'paper-button'], function (paperDialogHelper) {
2016-02-25 23:36:03 -07:00
var extractedName;
var extractedYear;
var currentNewItem;
var existingSeriesHtml;
2016-02-12 21:34:47 -07:00
function onApiFailure(e) {
2016-02-12 21:34:47 -07:00
Dashboard.hideLoadingMsg();
2016-02-12 21:34:47 -07:00
Dashboard.alert({
title: Globalize.translate('AutoOrganizeError'),
message: Globalize.translate('ErrorOrganizingFileWithErrorCode', e.getResponseHeader("X-Application-Error-Code"))
});
}
2016-02-12 21:34:47 -07:00
function initEpisodeForm(context, item) {
2016-02-20 23:25:25 -07:00
if (!item.ExtractedName || item.ExtractedName.length < 4) {
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-02-28 10:41:48 -07:00
//context.querySelector('.extractedName').value = 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);
}
}
}
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;
paperDialogHelper.close(dlg);
}, onApiFailure);
}
2016-02-25 23:36:03 -07:00
function showNewSeriesDialog(dlg) {
require(['components/itemidentifier/itemidentifier'], function (itemidentifier) {
itemidentifier.showFindNew(extractedName, extractedYear, 'Series').then(function (newItem) {
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');
}
else {
dlg.querySelector('.fldSelectSeriesFolder').classList.add('hide');
}
}
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;
var dlg = paperDialogHelper.createDialog({
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-02-12 21:34:47 -07:00
dlg.querySelector('.dialogHeaderTitle').innerHTML = Globalize.translate('FileOrganizeManually');
2016-02-12 21:34:47 -07:00
paperDialogHelper.open(dlg);
2016-02-12 21:34:47 -07:00
dlg.addEventListener('iron-overlay-closed', 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-02-12 21:34:47 -07:00
paperDialogHelper.close(dlg);
});
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
});