jellyfin-web/dashboard-ui/dashboard/autoorganizelog.js

423 lines
12 KiB
JavaScript
Raw Normal View History

define(['serverNotifications', 'events', 'scripts/taskbutton', 'datetime', 'paper-icon-button-light'], function (serverNotifications, events, taskButton, datetime) {
2016-10-22 22:11:46 -07:00
'use strict';
2014-01-20 23:10:58 -07:00
var query = {
StartIndex: 0,
2016-02-08 19:15:26 -07:00
Limit: 50
2014-01-20 23:10:58 -07:00
};
var currentResult;
var page;
function parentWithClass(elem, className) {
while (!elem.classList || !elem.classList.contains(className)) {
elem = elem.parentNode;
if (!elem) {
return null;
}
}
return elem;
}
2014-01-20 23:10:58 -07:00
function showStatusMessage(id) {
var item = currentResult.Items.filter(function (i) {
2016-04-22 09:27:17 -07:00
return i.Id == id;
2014-01-20 23:10:58 -07:00
})[0];
Dashboard.alert({
title: getStatusText(item, false),
message: item.StatusMessage
});
}
function deleteOriginalFile(page, id) {
var item = currentResult.Items.filter(function (i) {
2016-04-22 09:27:17 -07:00
return i.Id == id;
2014-01-20 23:10:58 -07:00
})[0];
var message = Globalize.translate('MessageFileWillBeDeleted') + '<br/><br/>' + item.OriginalPath + '<br/><br/>' + Globalize.translate('MessageSureYouWishToProceed');
2014-01-20 23:10:58 -07:00
2016-02-22 12:12:06 -07:00
require(['confirm'], function (confirm) {
2014-01-20 23:10:58 -07:00
2016-02-22 12:12:06 -07:00
confirm(message, Globalize.translate('HeaderDeleteFile')).then(function () {
2014-01-20 23:10:58 -07:00
Dashboard.showLoadingMsg();
2014-01-22 10:05:06 -07:00
2015-12-14 08:43:03 -07:00
ApiClient.deleteOriginalFileFromOrganizationResult(id).then(function () {
2014-01-20 23:10:58 -07:00
Dashboard.hideLoadingMsg();
reloadItems(page, true);
2014-01-20 23:10:58 -07:00
2016-04-23 13:23:37 -07:00
}, Dashboard.processErrorResponse);
2016-02-22 12:12:06 -07:00
});
2014-01-20 23:10:58 -07:00
});
}
2016-03-10 13:06:47 -07:00
function organizeFileWithCorrections(page, item) {
2014-01-22 10:05:06 -07:00
2016-03-10 13:06:47 -07:00
showCorrectionPopup(page, item);
}
2016-03-10 13:06:47 -07:00
function showCorrectionPopup(page, item) {
2014-01-22 10:05:06 -07:00
2016-02-12 21:34:47 -07:00
require(['components/fileorganizer/fileorganizer'], function (fileorganizer) {
2014-01-22 10:05:06 -07:00
2016-02-12 21:34:47 -07:00
fileorganizer.show(item).then(function () {
reloadItems(page, false);
});
});
2014-01-22 10:05:06 -07:00
}
2014-01-20 23:10:58 -07:00
function organizeFile(page, id) {
var item = currentResult.Items.filter(function (i) {
2016-04-22 09:27:17 -07:00
return i.Id == id;
2014-01-20 23:10:58 -07:00
})[0];
2014-01-22 10:05:06 -07:00
if (!item.TargetPath) {
if (item.Type == "Episode") {
2016-03-10 13:06:47 -07:00
organizeFileWithCorrections(page, item);
2014-01-22 10:05:06 -07:00
}
return;
}
var message = Globalize.translate('MessageFollowingFileWillBeMovedFrom') + '<br/><br/>' + item.OriginalPath + '<br/><br/>' + Globalize.translate('MessageDestinationTo') + '<br/><br/>' + item.TargetPath;
if (item.DuplicatePaths.length) {
message += '<br/><br/>' + Globalize.translate('MessageDuplicatesWillBeDeleted');
message += '<br/><br/>' + item.DuplicatePaths.join('<br/>');
}
message += '<br/><br/>' + Globalize.translate('MessageSureYouWishToProceed');
2014-01-20 23:10:58 -07:00
2016-02-22 12:12:06 -07:00
require(['confirm'], function (confirm) {
2014-01-20 23:10:58 -07:00
2016-02-22 12:12:06 -07:00
confirm(message, Globalize.translate('HeaderOrganizeFile')).then(function () {
2014-01-20 23:10:58 -07:00
Dashboard.showLoadingMsg();
2015-12-14 08:43:03 -07:00
ApiClient.performOrganization(id).then(function () {
2014-01-20 23:10:58 -07:00
Dashboard.hideLoadingMsg();
reloadItems(page, true);
2014-01-20 23:10:58 -07:00
2016-04-23 13:23:37 -07:00
}, Dashboard.processErrorResponse);
2016-02-22 12:12:06 -07:00
});
2014-01-20 23:10:58 -07:00
});
2014-01-22 10:05:06 -07:00
}
function reloadItems(page, showSpinner) {
2014-01-20 23:10:58 -07:00
if (showSpinner) {
Dashboard.showLoadingMsg();
}
2014-01-20 23:10:58 -07:00
2015-12-14 08:43:03 -07:00
ApiClient.getFileOrganizationResults(query).then(function (result) {
2014-01-20 23:10:58 -07:00
currentResult = result;
renderResults(page, result);
Dashboard.hideLoadingMsg();
2016-04-23 13:23:37 -07:00
}, Dashboard.processErrorResponse);
2014-01-20 23:10:58 -07:00
}
function getStatusText(item, enhance) {
var status = item.Status;
var color = null;
2014-01-21 09:24:12 -07:00
if (status == 'SkippedExisting') {
status = Globalize.translate('StatusSkipped');
2014-01-20 23:10:58 -07:00
}
else if (status == 'Failure') {
color = '#cc0000';
status = Globalize.translate('StatusFailed');
2014-01-20 23:10:58 -07:00
}
if (status == 'Success') {
color = 'green';
status = Globalize.translate('StatusSuccess');
2014-01-20 23:10:58 -07:00
}
2015-08-07 07:21:29 -07:00
if (enhance) {
2014-01-20 23:10:58 -07:00
if (item.StatusMessage) {
return '<a style="color:' + color + ';" data-resultid="' + item.Id + '" href="#" class="btnShowStatusMessage">' + status + '</a>';
} else {
return '<span data-resultid="' + item.Id + '" style="color:' + color + ';">' + status + '</span>';
}
}
return status;
}
function renderResults(page, result) {
var rows = result.Items.map(function (item) {
var html = '';
html += '<tr id="row' + item.Id + '">';
2015-09-06 12:09:36 -07:00
html += renderItemRow(item);
2015-09-06 12:09:36 -07:00
2014-01-20 23:10:58 -07:00
html += '</tr>';
return html;
}).join('');
var resultBody = page.querySelector('.resultBody');
resultBody.innerHTML = rows;
2014-01-20 23:10:58 -07:00
resultBody.addEventListener('click', handleItemClick);
2014-01-20 23:10:58 -07:00
2015-06-18 11:29:44 -07:00
var pagingHtml = LibraryBrowser.getQueryPagingHtml({
startIndex: query.StartIndex,
limit: query.Limit,
totalRecordCount: result.TotalRecordCount,
showLimit: false,
updatePageSizeSetting: false
});
var topPaging = page.querySelector('.listTopPaging');
topPaging.innerHTML = pagingHtml;
2014-01-20 23:10:58 -07:00
var bottomPaging = page.querySelector('.listBottomPaging');
bottomPaging.innerHTML = pagingHtml;
2016-04-22 09:27:17 -07:00
var btnNextTop = topPaging.querySelector(".btnNextPage");
var btnNextBottom = bottomPaging.querySelector(".btnNextPage");
var btnPrevTop = topPaging.querySelector(".btnPreviousPage");
var btnPrevBottom = bottomPaging.querySelector(".btnPreviousPage");
2016-04-22 09:27:17 -07:00
if (btnNextTop) {
btnNextTop.addEventListener('click', function () {
query.StartIndex += query.Limit;
reloadItems(page, true);
});
}
2016-04-22 09:27:17 -07:00
if (btnNextBottom) {
btnNextBottom.addEventListener('click', function () {
query.StartIndex += query.Limit;
reloadItems(page, true);
});
}
2014-01-20 23:10:58 -07:00
if (btnPrevTop) {
btnPrevTop.addEventListener('click', function () {
query.StartIndex -= query.Limit;
reloadItems(page, true);
});
}
2016-04-22 09:27:17 -07:00
if (btnPrevBottom) {
btnPrevBottom.addEventListener('click', function () {
query.StartIndex -= query.Limit;
reloadItems(page, true);
});
}
2014-01-22 10:05:06 -07:00
var btnClearLog = page.querySelector('.btnClearLog');
2014-01-22 10:05:06 -07:00
if (result.TotalRecordCount) {
btnClearLog.classList.remove('hide');
2014-01-22 10:05:06 -07:00
} else {
btnClearLog.classList.add('hide');
2014-01-22 10:05:06 -07:00
}
2014-01-20 23:10:58 -07:00
}
function renderItemRow(item) {
var html = '';
html += '<td>';
var hide = item.IsInProgress ? '' : ' hide';
html += '<img src="css/images/throbber.gif" alt="" class="syncSpinner' + hide + '" style="vertical-align: middle;" />';
html += '</td>';
html += '<td data-title="Date">';
var date = datetime.parseISO8601Date(item.Date, true);
html += date.toLocaleDateString();
html += '</td>';
html += '<td data-title="Source" class="fileCell">';
var status = item.Status;
if (item.IsInProgress) {
html += '<span style="color:darkorange;">';
html += item.OriginalFileName;
html += '</span>';
}
else if (status == 'SkippedExisting') {
html += '<a data-resultid="' + item.Id + '" style="color:blue;" href="#" class="btnShowStatusMessage">';
html += item.OriginalFileName;
html += '</a>';
}
else if (status == 'Failure') {
html += '<a data-resultid="' + item.Id + '" style="color:red;" href="#" class="btnShowStatusMessage">';
html += item.OriginalFileName;
html += '</a>';
} else {
html += '<span style="color:green;">';
html += item.OriginalFileName;
html += '</span>';
}
html += '</td>';
html += '<td data-title="Destination" class="fileCell">';
html += item.TargetPath || '';
html += '</td>';
2016-09-22 23:57:24 -07:00
html += '<td class="organizerButtonCell" style="whitespace:no-wrap;">';
if (item.Status != 'Success') {
html += '<button type="button" is="paper-icon-button-light" data-resultid="' + item.Id + '" class="btnProcessResult organizerButton autoSize" title="' + Globalize.translate('ButtonOrganizeFile') + '"><i class="md-icon">folder</i></button>';
html += '<button type="button" is="paper-icon-button-light" data-resultid="' + item.Id + '" class="btnDeleteResult organizerButton autoSize" title="' + Globalize.translate('ButtonDeleteFile') + '"><i class="md-icon">delete</i></button>';
}
html += '</td>';
return html;
}
function handleItemClick(e) {
var id;
var buttonStatus = parentWithClass(e.target, 'btnShowStatusMessage');
if (buttonStatus) {
id = buttonStatus.getAttribute('data-resultid');
showStatusMessage(id);
}
var buttonOrganize = parentWithClass(e.target, 'btnProcessResult');
if (buttonOrganize) {
id = buttonOrganize.getAttribute('data-resultid');
organizeFile(e.view, id);
}
var buttonDelete = parentWithClass(e.target, 'btnDeleteResult');
if (buttonDelete) {
2015-02-14 12:36:40 -07:00
id = buttonDelete.getAttribute('data-resultid');
deleteOriginalFile(e.view, id);
}
}
function onServerEvent(e, apiClient, data) {
2015-02-14 12:36:40 -07:00
if (e.type == 'ScheduledTaskEnded') {
if (data && data.Key == 'AutoOrganize') {
reloadItems(page, false);
}
} else if (e.type == 'AutoOrganize_ItemUpdated' && data) {
updateItemStatus(page, data);
} else {
reloadItems(page, false);
}
}
function updateItemStatus(page, item) {
var rowId = '#row' + item.Id;
var row = page.querySelector(rowId);
if (row) {
2015-02-14 12:36:40 -07:00
row.innerHTML = renderItemRow(item);
2015-02-14 12:36:40 -07:00
}
}
2016-04-12 22:28:45 -07:00
function getTabs() {
return [
{
href: 'autoorganizelog.html',
name: Globalize.translate('TabActivityLog')
},
{
href: 'autoorganizetv.html',
name: Globalize.translate('TabTV')
},
{
href: 'autoorganizesmart.html',
name: Globalize.translate('TabSmartMatches')
}];
}
2014-01-22 10:05:06 -07:00
return function (view, params) {
page = view;
2014-01-22 10:05:06 -07:00
var clearButton = view.querySelector('.btnClearLog');
clearButton.addEventListener('click', function () {
2014-01-22 10:05:06 -07:00
2015-12-14 08:43:03 -07:00
ApiClient.clearOrganizationLog().then(function () {
query.StartIndex = 0;
reloadItems(view, true);
2016-04-23 13:23:37 -07:00
}, Dashboard.processErrorResponse);
2014-01-22 10:05:06 -07:00
});
view.addEventListener('viewshow', function (e) {
2014-01-20 23:10:58 -07:00
LibraryMenu.setTabs('autoorganize', 0, getTabs);
2016-04-12 22:28:45 -07:00
reloadItems(view, true);
2014-01-20 23:10:58 -07:00
events.on(serverNotifications, 'AutoOrganize_LogReset', onServerEvent);
events.on(serverNotifications, 'AutoOrganize_ItemUpdated', onServerEvent);
events.on(serverNotifications, 'AutoOrganize_ItemRemoved', onServerEvent);
events.on(serverNotifications, 'AutoOrganize_ItemAdded', onServerEvent);
events.on(serverNotifications, 'ScheduledTaskEnded', onServerEvent);
2014-01-20 23:10:58 -07:00
// on here
taskButton({
mode: 'on',
progressElem: view.querySelector('.organizeProgress'),
panel: view.querySelector('.organizeTaskPanel'),
taskKey: 'AutoOrganize',
button: view.querySelector('.btnOrganize')
});
2015-01-20 13:19:54 -07:00
});
view.addEventListener('viewhide', function (e) {
2014-01-20 23:10:58 -07:00
currentResult = null;
2015-01-20 14:32:48 -07:00
events.off(serverNotifications, 'AutoOrganize_LogReset', onServerEvent);
events.off(serverNotifications, 'AutoOrganize_ItemUpdated', onServerEvent);
events.off(serverNotifications, 'AutoOrganize_ItemRemoved', onServerEvent);
events.off(serverNotifications, 'AutoOrganize_ItemAdded', onServerEvent);
events.off(serverNotifications, 'ScheduledTaskEnded', onServerEvent);
2015-01-20 13:19:54 -07:00
// off here
taskButton({
mode: 'off',
button: view.querySelector('.btnOrganize')
});
2015-01-20 13:19:54 -07:00
});
};
2016-04-22 09:27:17 -07:00
});