jellyfin-web/dashboard-ui/scripts/syncjob.js

445 lines
13 KiB
JavaScript
Raw Normal View History

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