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

431 lines
12 KiB
JavaScript
Raw Normal View History

2016-04-26 19:59:43 -07:00
define(['jQuery', 'paper-progress', 'paper-fab', 'paper-item-body', 'paper-icon-item', 'paper-icon-button', 'paper-button'], function ($) {
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>';
html += Globalize.translate('ValueDateCreated', parseISO8601Date(job.DateCreated, { toLocal: true }).toLocaleString());
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/>';
2015-06-22 08:43:19 -07:00
html += '<button type="submit" data-role="none" class="clearButton">';
html += '<paper-button raised class="submit block"><iron-icon icon="check"></iron-icon><span>' + Globalize.translate('ButtonSave') + '</span></paper-button>';
html += '</button>';
2014-12-30 23:24:49 -07:00
2015-12-14 08:43:03 -07:00
$('.syncJobForm', page).html(html);
2016-02-17 21:57:19 -07:00
require(['syncDialog'], function (syncDialog) {
syncDialog.renderForm({
elem: $('.formFields', page),
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
2015-03-14 21:17:35 -07:00
var deferred = $.Deferred();
2014-12-30 23:24:49 -07:00
2015-03-14 21:17:35 -07:00
deferred.resolveWith(null, [dialogOptions]);
return deferred.promise();
};
2014-12-30 23:24:49 -07:00
}
function getJobItemHtml(jobItem, index) {
var html = '';
2015-08-17 09:52:56 -07:00
html += '<paper-icon-item 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) {
2015-10-26 11:55:46 -07:00
html += '<paper-fab mini class="blue" style="background-image:url(\'' + imgUrl + '\');background-repeat:no-repeat;background-position:center center;background-size: cover;" item-icon></paper-fab>';
2014-12-30 23:24:49 -07:00
}
2015-08-17 09:52:56 -07:00
else {
2015-10-26 11:55:46 -07:00
html += '<paper-fab mini class="blue" icon="sync" item-icon></paper-fab>';
2015-08-17 09:52:56 -07:00
}
html += '<paper-item-body 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') {
2015-08-17 09:52:56 -07:00
html += '<div secondary style="color:red;">';
2015-01-20 21:56:00 -07:00
} else {
2015-08-17 09:52:56 -07:00
html += '<div 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
2015-09-21 08:43:10 -07:00
html += '<div secondary style="padding-top:5px;">';
html += '<paper-progress class="mini" style="width:100%;" value="' + (jobItem.Progress || 0) + '"></paper-progress>';
html += '</div>';
2015-08-17 09:52:56 -07:00
html += '</paper-item-body>';
2014-12-30 23:24:49 -07:00
if (hasActions) {
2015-08-17 09:52:56 -07:00
html += '<paper-icon-button icon="' + AppInfo.moreIcon + '" class="btnJobItemMenu"></paper-icon-button>';
2014-12-30 23:24:49 -07:00
} else {
2015-08-17 09:52:56 -07:00
html += '<paper-icon-button icon="' + AppInfo.moreIcon + '" class="btnJobItemMenu" disabled></paper-icon-button>';
2014-12-30 23:24:49 -07:00
}
2015-08-17 09:52:56 -07:00
html += '</paper-icon-item>';
2014-12-30 23:24:49 -07:00
return html;
}
2015-12-14 08:43:03 -07:00
$.fn.lazyChildren = function () {
for (var i = 0, length = this.length; i < length; i++) {
ImageLoader.lazyChildren(this[i]);
}
return this;
};
2014-12-30 23:24:49 -07:00
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
2015-12-14 08:43:03 -07:00
var elem = $('.jobItems', page).html(html).lazyChildren();
2015-01-22 23:15:15 -07:00
2014-12-30 23:24:49 -07:00
$('.btnJobItemMenu', elem).on('click', function () {
showJobItemMenu(this);
});
}
function showJobItemMenu(elem) {
var page = $(elem).parents('.page');
2015-08-17 09:52:56 -07:00
var listItem = $(elem).parents('paper-icon-item');
2015-06-22 08:43:19 -07:00
var jobItemId = listItem.attr('data-itemid');
2014-12-30 23:24:49 -07:00
var status = listItem.attr('data-status');
2015-02-03 11:14:53 -07:00
var remove = listItem.attr('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'),
id: 'retry',
ironIcon: 'check'
});
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'),
id: 'retry',
ironIcon: 'check'
});
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'),
id: 'cancel',
ironIcon: 'delete'
});
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'),
id: 'unmarkforremoval',
ironIcon: 'check'
});
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'),
id: 'markforremoval',
ironIcon: 'delete'
});
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;
}
2015-09-03 10:01:51 -07:00
$('#selectProfile', page).val(job.Profile || '').trigger('change');
$('#selectQuality', page).val(job.Quality || '').trigger('change');
2015-09-14 12:47:03 -07:00
$('#chkUnwatchedOnly', page).checked(job.UnwatchedOnly);
$('#chkSyncNewContent', page).checked(job.SyncNewContent);
2014-12-30 23:24:49 -07:00
$('#txtItemLimit', page).val(job.ItemLimit);
2015-03-15 12:10:27 -07:00
if (job.Bitrate) {
$('#txtBitrate', page).val(job.Bitrate / 1000000);
} else {
$('#txtBitrate', page).val('');
}
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 : '';
2015-03-14 21:39:29 -07:00
$('#selectSyncTarget', page).val(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
});
});
}
2015-02-04 20:01:37 -07:00
function onWebSocketMessage(e, msg) {
var page = $.mobile.activePage;
if (msg.MessageType == "SyncJob") {
loadJobInfo(page, msg.Data.Job, msg.Data.JobItems);
}
}
function startListening(page) {
var startParams = "0,1500";
startParams += "," + getParameterByName('id');
if (ApiClient.isWebSocketOpen()) {
ApiClient.sendWebSocketMessage("SyncJobStart", startParams);
}
}
function stopListening() {
if (ApiClient.isWebSocketOpen()) {
ApiClient.sendWebSocketMessage("SyncJobStop", "");
}
}
2015-06-08 22:56:46 -07:00
function onSubmit() {
var form = this;
var page = $(form).parents('.page');
saveJob(page);
return false;
}
2015-09-01 07:01:59 -07:00
$(document).on('pageinit', ".syncJobPage", function () {
2015-06-08 22:56:46 -07:00
$('.syncJobForm').off('submit', onSubmit).on('submit', onSubmit);
2015-09-24 10:08:10 -07:00
}).on('pageshow', ".syncJobPage", function () {
2014-12-30 23:24:49 -07:00
var page = this;
loadJob(page);
2015-02-04 20:01:37 -07:00
startListening(page);
2015-12-23 10:46:01 -07:00
Events.on(ApiClient, "websocketmessage", onWebSocketMessage);
2014-12-30 23:24:49 -07:00
2015-06-20 17:49:42 -07:00
}).on('pagebeforehide', ".syncJobPage", function () {
2014-12-30 23:24:49 -07:00
2015-02-04 20:01:37 -07:00
var page = this;
2014-12-30 23:24:49 -07:00
2015-02-04 20:01:37 -07:00
stopListening();
2015-12-23 10:46:01 -07:00
Events.off(ApiClient, "websocketmessage", onWebSocketMessage);
2014-12-30 23:24:49 -07:00
});
});