1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00
jellyfin-web/dashboard-ui/scripts/syncjob.js

451 lines
14 KiB
JavaScript
Raw Normal View History

2016-12-24 02:41:25 -05:00
define(['connectionManager', 'serverNotifications', 'events', 'datetime', 'dom', 'imageLoader', 'loading', 'globalize', 'apphost', 'listViewStyle', 'paper-icon-button-light', 'emby-button'], function (connectionManager, serverNotifications, events, datetime, dom, imageLoader, loading, globalize, appHost) {
2016-10-23 01:11:46 -04:00
'use strict';
2014-12-31 01:24:49 -05:00
2015-03-15 00:17:35 -04:00
function renderJob(page, job, dialogOptions) {
2014-12-31 01:24:49 -05:00
var html = '';
2015-02-03 14:37:50 -05:00
html += '<div>';
2016-12-24 02:41:25 -05:00
html += globalize.translate('ValueDateCreated', datetime.parseISO8601Date(job.DateCreated, true).toLocaleString());
2015-02-03 14:37:50 -05:00
html += '</div>';
html += '<br/>';
2015-03-15 00:17:35 -04:00
html += '<div class="formFields"></div>';
2015-01-23 01:15:15 -05:00
2015-02-03 14:37:50 -05:00
html += '<br/>';
2015-03-15 00:17:35 -04:00
html += '<br/>';
2016-12-24 02:41:25 -05:00
html += '<button is="emby-button" type="submit" class="raised button-submit block"><span>' + globalize.translate('ButtonSave') + '</span></button>';
2014-12-31 01:24:49 -05:00
2016-06-20 21:59:39 -04:00
page.querySelector('.syncJobForm').innerHTML = html;
2016-02-17 23:57:19 -05:00
require(['syncDialog'], function (syncDialog) {
syncDialog.renderForm({
2016-08-16 14:54:08 -04:00
elem: page.querySelector('.formFields'),
2016-02-17 23:57:19 -05:00
dialogOptions: dialogOptions,
dialogOptionsFn: getTargetDialogOptionsFn(dialogOptions),
showName: true,
readOnlySyncTarget: true
}).then(function () {
fillJobValues(page, job, dialogOptions);
});
2015-03-15 00:17:35 -04:00
});
}
2014-12-31 01:24:49 -05:00
2015-03-15 00:17:35 -04:00
function getTargetDialogOptionsFn(dialogOptions) {
2014-12-31 01:24:49 -05:00
2015-03-15 00:17:35 -04:00
return function (targetId) {
2014-12-31 01:24:49 -05:00
2016-06-11 13:10:06 -04:00
return Promise.resolve(dialogOptions);
2015-03-15 00:17:35 -04:00
};
2014-12-31 01:24:49 -05:00
}
2016-12-24 02:41:25 -05:00
function getJobItemHtml(jobItem, apiClient, index) {
2014-12-31 01:24:49 -05:00
var html = '';
2016-07-02 14:46:05 -04:00
html += '<div class="listItem" data-itemid="' + jobItem.Id + '" data-status="' + jobItem.Status + '" data-remove="' + jobItem.IsMarkedForRemoval + '">';
2014-12-31 01:24:49 -05:00
2015-02-04 22:01:37 -05:00
var hasActions = ['Queued', 'Cancelled', 'Failed', 'ReadyToTransfer', 'Transferring', 'Converting', 'Synced'].indexOf(jobItem.Status) != -1;
2014-12-31 01:24:49 -05:00
var imgUrl;
if (jobItem.PrimaryImageItemId) {
2016-12-24 02:41:25 -05:00
imgUrl = apiClient.getImageUrl(jobItem.PrimaryImageItemId, {
2014-12-31 01:24:49 -05:00
type: "Primary",
width: 80,
tag: jobItem.PrimaryImageTag,
minScale: 1.5
});
}
if (imgUrl) {
2016-07-02 14:46:05 -04:00
html += '<button type="button" is="emby-button" class="blue mini fab autoSize" icon="sync" style="background-image:url(\'' + imgUrl + '\');background-repeat:no-repeat;background-position:center center;background-size: cover;"><i style="visibility:hidden;" class="md-icon">sync</i></button>';
2014-12-31 01:24:49 -05:00
}
2015-08-17 12:52:56 -04:00
else {
2016-07-02 14:46:05 -04:00
html += '<button type="button" is="emby-button" class="blue mini fab autoSize" icon="sync"><i class="md-icon">sync</i></button>';
2015-08-17 12:52:56 -04:00
}
2016-07-02 14:46:05 -04:00
html += '<div class="listItemBody three-line">';
2014-12-31 01:24:49 -05:00
2015-08-17 12:52:56 -04:00
html += '<div>';
2014-12-31 01:24:49 -05:00
html += jobItem.ItemName;
2015-08-17 12:52:56 -04:00
html += '</div>';
2014-12-31 01:24:49 -05:00
2015-01-20 23:56:00 -05:00
if (jobItem.Status == 'Failed') {
2016-07-02 14:46:05 -04:00
html += '<div class="secondary" style="color:red;">';
2015-01-20 23:56:00 -05:00
} else {
2016-07-02 14:46:05 -04:00
html += '<div class="secondary">';
2015-01-20 23:56:00 -05:00
}
2016-12-24 02:41:25 -05:00
html += globalize.translate('SyncJobItemStatus' + jobItem.Status);
2015-01-20 23:56:00 -05:00
if (jobItem.Status == 'Synced' && jobItem.IsMarkedForRemoval) {
html += '<br/>';
2016-12-24 02:41:25 -05:00
html += globalize.translate('SyncJobItemStatusSyncedMarkForRemoval');
2015-01-20 23:56:00 -05:00
}
2015-08-17 12:52:56 -04:00
html += '</div>';
2014-12-31 01:24:49 -05:00
2016-07-02 14:46:05 -04:00
html += '<div class="secondary" style="padding-top:5px;">';
2016-07-02 22:47:39 -04:00
html += '<div style="background:#e0e0e0;height:4px;"><div style="background:#52B54B;width:' + (jobItem.Progress || 0) + '%;height:100%;"></div></div>';
2015-09-21 11:43:10 -04:00
html += '</div>';
2016-07-02 14:46:05 -04:00
html += '</div>';
2014-12-31 01:24:49 -05:00
2016-12-24 02:41:25 -05:00
var moreIcon = appHost.moreIcon === 'dots-horiz' ? '&#xE5D3;' : '&#xE5D4;';
2014-12-31 01:24:49 -05:00
if (hasActions) {
2016-12-24 02:41:25 -05:00
html += '<button type="button" is="paper-icon-button-light" class="btnJobItemMenu autoSize"><i class="md-icon">' + moreIcon + '</i></button>';
2014-12-31 01:24:49 -05:00
} else {
2016-12-24 02:41:25 -05:00
html += '<button type="button" is="paper-icon-button-light" class="btnJobItemMenu autoSize" disabled><i class="md-icon">' + moreIcon + '</i></button>';
2014-12-31 01:24:49 -05:00
}
2016-07-02 14:46:05 -04:00
html += '</div>';
2014-12-31 01:24:49 -05:00
return html;
}
2016-12-24 02:41:25 -05:00
function renderJobItems(page, items, apiClient) {
2014-12-31 01:24:49 -05:00
var html = '';
2016-12-24 02:41:25 -05:00
html += '<h1>' + globalize.translate('HeaderItems') + '</h1>';
2014-12-31 01:24:49 -05:00
2015-08-17 12:52:56 -04:00
html += '<div class="paperList">';
2014-12-31 01:24:49 -05:00
var index = 0;
html += items.map(function (i) {
2016-12-24 02:41:25 -05:00
return getJobItemHtml(i, apiClient, index++);
2014-12-31 01:24:49 -05:00
}).join('');
2015-08-17 12:52:56 -04:00
html += '</div>';
2014-12-31 01:24:49 -05:00
2016-06-20 21:59:39 -04:00
var elem = page.querySelector('.jobItems');
elem.innerHTML = html;
2016-11-25 01:58:38 -05:00
imageLoader.lazyChildren(elem);
2014-12-31 01:24:49 -05:00
}
2016-07-02 14:46:05 -04:00
function parentWithClass(elem, className) {
while (!elem.classList || !elem.classList.contains(className)) {
elem = elem.parentNode;
if (!elem) {
return null;
}
}
return elem;
}
2016-12-24 02:41:25 -05:00
function showJobItemMenu(elem, jobId, apiClient) {
2014-12-31 01:24:49 -05:00
2016-07-02 14:46:05 -04:00
var page = parentWithClass(elem, 'page');
var listItem = parentWithClass(elem, 'listItem');
var jobItemId = listItem.getAttribute('data-itemid');
var status = listItem.getAttribute('data-status');
var remove = listItem.getAttribute('data-remove').toLowerCase() == 'true';
2014-12-31 01:24:49 -05:00
2015-06-22 11:43:19 -04:00
var menuItems = [];
2014-12-31 01:24:49 -05:00
2015-01-20 22:54:45 -05:00
if (status == 'Failed') {
2015-06-22 11:43:19 -04:00
menuItems.push({
2016-12-24 02:41:25 -05:00
name: globalize.translate('ButtonQueueForRetry'),
2016-06-19 01:26:52 -04:00
id: 'retry'
2015-06-22 11:43:19 -04:00
});
2015-01-20 22:54:45 -05:00
}
else if (status == 'Cancelled') {
2015-06-22 11:43:19 -04:00
menuItems.push({
2016-12-24 02:41:25 -05:00
name: globalize.translate('ButtonReenable'),
2016-06-19 01:26:52 -04:00
id: 'retry'
2015-06-22 11:43:19 -04:00
});
2015-01-20 22:54:45 -05:00
}
2015-02-04 22:01:37 -05:00
else if (status == 'Queued' || status == 'Transferring' || status == 'Converting' || status == 'ReadyToTransfer') {
2015-06-22 11:43:19 -04:00
menuItems.push({
2016-12-24 02:41:25 -05:00
name: globalize.translate('ButtonCancelItem'),
2016-06-19 01:26:52 -04:00
id: 'cancel'
2015-06-22 11:43:19 -04:00
});
2014-12-31 01:24:49 -05:00
}
2015-01-20 23:56:00 -05:00
else if (status == 'Synced' && remove) {
2015-06-22 11:43:19 -04:00
menuItems.push({
2016-12-24 02:41:25 -05:00
name: globalize.translate('ButtonUnmarkForRemoval'),
2016-06-19 01:26:52 -04:00
id: 'unmarkforremoval'
2015-06-22 11:43:19 -04:00
});
2015-01-20 23:56:00 -05:00
}
else if (status == 'Synced') {
2015-06-22 11:43:19 -04:00
menuItems.push({
2016-12-24 02:41:25 -05:00
name: globalize.translate('ButtonMarkForRemoval'),
2016-06-19 01:26:52 -04:00
id: 'markforremoval'
2015-06-22 11:43:19 -04:00
});
2015-01-20 23:56:00 -05:00
}
2014-12-31 01:24:49 -05:00
2016-01-30 23:04:00 -05:00
require(['actionsheet'], function (actionsheet) {
2015-06-22 11:43:19 -04:00
2016-01-30 23:04:00 -05:00
actionsheet.show({
2015-06-22 11:43:19 -04:00
items: menuItems,
positionTo: elem,
callback: function (id) {
switch (id) {
case 'cancel':
2016-12-24 02:41:25 -05:00
cancelJobItem(page, jobId, jobItemId, apiClient);
2015-06-22 11:43:19 -04:00
break;
case 'retry':
2016-12-24 02:41:25 -05:00
retryJobItem(page, jobId, jobItemId, apiClient);
2015-06-22 11:43:19 -04:00
break;
case 'markforremoval':
2016-12-24 02:41:25 -05:00
markForRemoval(page, jobId, jobItemId, apiClient);
2015-06-22 11:43:19 -04:00
break;
case 'unmarkforremoval':
2016-12-24 02:41:25 -05:00
unMarkForRemoval(page, jobId, jobItemId, apiClient);
2015-06-22 11:43:19 -04:00
break;
default:
break;
}
}
});
2015-01-20 23:56:00 -05:00
});
2015-01-20 22:54:45 -05:00
}
2016-12-24 02:41:25 -05:00
function cancelJobItem(page, jobId, jobItemId, apiClient) {
2015-01-20 22:54:45 -05:00
// Need a timeout because jquery mobile will not show a popup while another is in the act of closing
2016-12-24 02:41:25 -05:00
loading.show();
2015-01-20 22:54:45 -05:00
2016-12-24 02:41:25 -05:00
apiClient.ajax({
2015-01-20 22:54:45 -05:00
type: "DELETE",
2016-12-24 02:41:25 -05:00
url: apiClient.getUrl('Sync/JobItems/' + jobItemId)
2015-01-20 22:54:45 -05:00
2015-12-14 10:43:03 -05:00
}).then(function () {
2015-01-20 22:54:45 -05:00
2016-12-24 02:41:25 -05:00
loadJob(page, jobId, apiClient);
2015-01-20 22:54:45 -05:00
});
}
2016-12-24 02:41:25 -05:00
function markForRemoval(page, jobId, jobItemId, apiClient) {
2015-01-20 23:56:00 -05:00
2016-12-24 02:41:25 -05:00
apiClient.ajax({
2015-01-20 23:56:00 -05:00
type: "POST",
2016-12-24 02:41:25 -05:00
url: apiClient.getUrl('Sync/JobItems/' + jobItemId + '/MarkForRemoval')
2015-01-20 23:56:00 -05:00
2015-12-14 10:43:03 -05:00
}).then(function () {
2015-01-20 23:56:00 -05:00
2016-12-24 02:41:25 -05:00
loadJob(page, jobId, apiClient);
2015-01-20 23:56:00 -05:00
});
}
2016-12-24 02:41:25 -05:00
function unMarkForRemoval(page, jobId, jobItemId, apiClient) {
2015-01-20 23:56:00 -05:00
2016-12-24 02:41:25 -05:00
apiClient.ajax({
2015-01-20 23:56:00 -05:00
type: "POST",
2016-12-24 02:41:25 -05:00
url: apiClient.getUrl('Sync/JobItems/' + jobItemId + '/UnmarkForRemoval')
2015-01-20 23:56:00 -05:00
2015-12-14 10:43:03 -05:00
}).then(function () {
2015-01-20 23:56:00 -05:00
2016-12-24 02:41:25 -05:00
loadJob(page, jobId, apiClient);
2015-01-20 23:56:00 -05:00
});
}
2016-12-24 02:41:25 -05:00
function retryJobItem(page, jobId, jobItemId, apiClient) {
2015-01-20 22:54:45 -05:00
2016-12-24 02:41:25 -05:00
apiClient.ajax({
2015-01-20 22:54:45 -05:00
type: "POST",
2016-12-24 02:41:25 -05:00
url: apiClient.getUrl('Sync/JobItems/' + jobItemId + '/Enable')
2015-01-20 22:54:45 -05:00
2015-12-14 10:43:03 -05:00
}).then(function () {
2015-01-20 22:54:45 -05:00
2016-12-24 02:41:25 -05:00
loadJob(page, jobId, apiClient);
2014-12-31 01:24:49 -05:00
});
}
function fillJobValues(page, job, editOptions) {
2015-12-14 10:43:03 -05:00
var txtSyncJobName = page.querySelector('#txtSyncJobName');
if (txtSyncJobName) {
txtSyncJobName.value = job.Name;
}
2016-08-18 01:56:10 -04:00
var selectProfile = page.querySelector('#selectProfile');
if (selectProfile) {
selectProfile.value = job.Profile || '';
}
var selectQuality = page.querySelector('#selectQuality');
if (selectQuality) {
selectQuality.value = job.Quality || '';
}
var chkUnwatchedOnly = page.querySelector('#chkUnwatchedOnly');
if (chkUnwatchedOnly) {
chkUnwatchedOnly.checked = job.UnwatchedOnly;
}
2014-12-31 01:24:49 -05:00
2016-08-18 01:56:10 -04:00
var chkSyncNewContent = page.querySelector('#chkSyncNewContent');
if (chkSyncNewContent) {
chkSyncNewContent.checked = job.SyncNewContent;
}
var txtItemLimit = page.querySelector('#txtItemLimit');
if (txtItemLimit) {
txtItemLimit.value = job.ItemLimit;
}
var txtBitrate = page.querySelector('#txtBitrate');
2015-03-15 15:10:27 -04:00
if (job.Bitrate) {
2016-08-18 01:56:10 -04:00
txtBitrate.value = job.Bitrate / 1000000;
2015-03-15 15:10:27 -04:00
} else {
2016-08-18 01:56:10 -04:00
txtBitrate.value = '';
2015-03-15 15:10:27 -04:00
}
2014-12-31 01:24:49 -05:00
var target = editOptions.Targets.filter(function (t) {
return t.Id == job.TargetId;
})[0];
var targetName = target ? target.Name : '';
2016-08-18 01:56:10 -04:00
var selectSyncTarget = page.querySelector('#selectSyncTarget');
if (selectSyncTarget) {
selectSyncTarget.value = targetName;
}
2014-12-31 01:24:49 -05:00
}
2015-02-04 22:01:37 -05:00
var _jobOptions;
2016-12-24 02:41:25 -05:00
function loadJob(page, id, apiClient) {
2014-12-31 01:24:49 -05:00
2016-12-24 02:41:25 -05:00
loading.show();
2014-12-31 01:24:49 -05:00
2016-12-24 02:41:25 -05:00
apiClient.getJSON(apiClient.getUrl('Sync/Jobs/' + id)).then(function (job) {
2014-12-31 01:24:49 -05:00
2016-12-24 02:41:25 -05:00
apiClient.getJSON(apiClient.getUrl('Sync/Options', {
2014-12-31 01:24:49 -05:00
UserId: job.UserId,
ItemIds: (job.RequestedItemIds && job.RequestedItemIds.length ? job.RequestedItemIds.join('') : null),
ParentId: job.ParentId,
Category: job.Category,
TargetId: job.TargetId
2014-12-31 01:24:49 -05:00
2015-12-14 10:43:03 -05:00
})).then(function (options) {
2014-12-31 01:24:49 -05:00
2015-02-04 22:01:37 -05:00
_jobOptions = options;
2014-12-31 01:24:49 -05:00
renderJob(page, job, options);
2016-12-24 02:41:25 -05:00
loading.hide();
2014-12-31 01:24:49 -05:00
});
});
2016-12-24 02:41:25 -05:00
apiClient.getJSON(apiClient.getUrl('Sync/JobItems', {
2014-12-31 01:24:49 -05:00
JobId: id,
AddMetadata: true
2015-12-14 10:43:03 -05:00
})).then(function (result) {
2014-12-31 01:24:49 -05:00
2016-12-24 02:41:25 -05:00
renderJobItems(page, result.Items, apiClient);
loading.hide();
2014-12-31 01:24:49 -05:00
});
}
2015-02-04 22:01:37 -05:00
function loadJobInfo(page, job, jobItems) {
//renderJob(page, job, _jobOptions);
2015-02-04 22:01:37 -05:00
renderJobItems(page, jobItems);
2016-12-24 02:41:25 -05:00
loading.hide();
2015-02-04 22:01:37 -05:00
}
2016-12-24 02:41:25 -05:00
function saveJob(page, id, apiClient) {
2014-12-31 01:24:49 -05:00
2016-12-24 02:41:25 -05:00
loading.show();
2014-12-31 01:24:49 -05:00
2016-12-24 02:41:25 -05:00
apiClient.getJSON(apiClient.getUrl('Sync/Jobs/' + id)).then(function (job) {
2014-12-31 01:24:49 -05:00
2016-02-17 23:57:19 -05:00
require(['syncDialog'], function (syncDialog) {
syncDialog.setJobValues(job, page);
2014-12-31 01:24:49 -05:00
2016-12-24 02:41:25 -05:00
apiClient.ajax({
2014-12-31 01:24:49 -05:00
2016-12-24 02:41:25 -05:00
url: apiClient.getUrl('Sync/Jobs/' + id),
2016-02-17 23:57:19 -05:00
type: 'POST',
data: JSON.stringify(job),
contentType: "application/json"
2014-12-31 01:24:49 -05:00
2016-02-17 23:57:19 -05:00
}).then(function () {
2014-12-31 01:24:49 -05:00
2016-12-24 02:41:25 -05:00
loading.hide();
2016-02-25 01:38:12 -05:00
require(['toast'], function (toast) {
2016-12-24 02:41:25 -05:00
toast(globalize.translate('SettingsSaved'));
2016-02-25 01:38:12 -05:00
});
2016-02-17 23:57:19 -05:00
});
2014-12-31 01:24:49 -05:00
});
});
}
2016-06-11 13:10:06 -04:00
return function (view, params) {
2015-02-04 22:01:37 -05:00
2016-12-24 02:41:25 -05:00
function getApiClient() {
return connectionManager.getApiClient(params.serverId);
}
2016-06-11 13:10:06 -04:00
view.querySelector('.syncJobForm').addEventListener('submit', function (e) {
2015-02-04 22:01:37 -05:00
2016-12-24 02:41:25 -05:00
saveJob(view, params.id, getApiClient());
2016-06-11 13:10:06 -04:00
e.preventDefault();
return false;
});
2015-02-04 22:01:37 -05:00
2016-12-24 02:41:25 -05:00
function onSyncJobMessage(e, msg) {
loadJobInfo(view, msg.Data.Job, msg.Data.JobItems);
2015-02-04 22:01:37 -05:00
}
2016-06-11 13:10:06 -04:00
function startListening(page) {
2015-02-04 22:01:37 -05:00
2016-06-11 13:10:06 -04:00
var startParams = "0,1500";
2015-02-04 22:01:37 -05:00
2016-12-24 02:41:25 -05:00
startParams += "," + params.id;
var apiClient = getApiClient();
2015-06-09 01:56:46 -04:00
2016-12-24 02:41:25 -05:00
if (apiClient.isWebSocketOpen()) {
apiClient.sendWebSocketMessage("SyncJobStart", startParams);
2016-06-11 13:10:06 -04:00
}
}
2015-06-09 01:56:46 -04:00
2016-06-11 13:10:06 -04:00
function stopListening() {
2015-06-09 01:56:46 -04:00
2016-12-24 02:41:25 -05:00
var apiClient = getApiClient();
if (apiClient.isWebSocketOpen()) {
apiClient.sendWebSocketMessage("SyncJobStop", "");
2016-06-11 13:10:06 -04:00
}
2014-12-31 01:24:49 -05:00
2016-06-11 13:10:06 -04:00
}
2014-12-31 01:24:49 -05:00
2016-08-18 01:56:10 -04:00
view.querySelector('.jobItems').addEventListener('click', function (e) {
var btnJobItemMenu = dom.parentWithClass(e.target, 'btnJobItemMenu');
if (btnJobItemMenu) {
2016-12-24 02:41:25 -05:00
showJobItemMenu(btnJobItemMenu, params.id);
2016-08-18 01:56:10 -04:00
}
});
2016-06-11 13:10:06 -04:00
view.addEventListener('viewshow', function () {
var page = this;
2016-12-24 02:41:25 -05:00
loadJob(page, params.id, getApiClient());
2014-12-31 01:24:49 -05:00
2016-06-11 13:10:06 -04:00
startListening(page);
2016-12-24 02:41:25 -05:00
events.on(serverNotifications, "SyncJob", onSyncJobMessage);
2016-06-11 13:10:06 -04:00
});
2014-12-31 01:24:49 -05:00
2016-06-11 13:10:06 -04:00
view.addEventListener('viewbeforehide', function () {
2014-12-31 01:24:49 -05:00
2016-06-11 13:10:06 -04:00
stopListening();
2016-12-24 02:41:25 -05:00
events.off(serverNotifications, "SyncJob", onSyncJobMessage);
2016-06-11 13:10:06 -04:00
});
};
2014-12-31 01:24:49 -05:00
});