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

406 lines
12 KiB
JavaScript
Raw Normal View History

2014-12-30 23:24:49 -07:00
(function () {
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/>';
html += '<button type="submit" data-icon="check">' + Globalize.translate('ButtonSave') + '</button>';
2014-12-30 23:24:49 -07:00
2015-03-14 21:17:35 -07:00
$('.syncJobForm', page).html(html).trigger('create');
SyncManager.renderForm({
elem: $('.formFields', page),
dialogOptions: dialogOptions,
2015-03-14 21:24:43 -07:00
dialogOptionsFn: getTargetDialogOptionsFn(dialogOptions),
2015-03-14 21:39:29 -07:00
showName: true,
readOnlySyncTarget: true
2015-03-14 21:17:35 -07:00
});
fillJobValues(page, job, dialogOptions);
}
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 = '';
var cssClass = 'ui-li-has-thumb listItem';
2015-01-20 23:26:16 -07:00
html += '<li class="' + cssClass + '"' + ' 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
html += '<a href="#">';
var imgUrl;
if (jobItem.PrimaryImageItemId) {
imgUrl = ApiClient.getImageUrl(jobItem.PrimaryImageItemId, {
type: "Primary",
width: 80,
tag: jobItem.PrimaryImageTag,
minScale: 1.5
});
}
if (imgUrl) {
if (index < 10) {
html += '<div class="listviewImage ui-li-thumb" style="background-image:url(\'' + imgUrl + '\');"></div>';
} else {
html += '<div class="listviewImage ui-li-thumb lazy" data-src="' + imgUrl + '"></div>';
}
}
html += '<h3>';
html += jobItem.ItemName;
html += '</h3>';
2015-01-20 21:56:00 -07:00
if (jobItem.Status == 'Failed') {
html += '<p style="color:red;">';
} else {
html += '<p>';
}
html += Globalize.translate('SyncJobItemStatus' + jobItem.Status);
if (jobItem.Status == 'Synced' && jobItem.IsMarkedForRemoval) {
html += '<br/>';
html += Globalize.translate('SyncJobItemStatusSyncedMarkForRemoval');
}
2014-12-30 23:24:49 -07:00
html += '</p>';
html += '</a>';
if (hasActions) {
html += '<a href="#" data-icon="ellipsis-v" class="listviewMenuButton btnJobItemMenu">';
} else {
html += '<a href="#" data-icon="ellipsis-v" class="listviewMenuButton btnJobItemMenu" style="visibility:hidden;">';
}
html += '</a>';
html += '</li>';
return html;
}
function renderJobItems(page, items) {
var html = '';
html += '<ul data-role="listview" class="itemsListview">';
html += '<li data-role="list-divider">';
html += Globalize.translate('HeaderItems');
html += '</li>';
var index = 0;
html += items.map(function (i) {
return getJobItemHtml(i, index++);
}).join('');
html += '</ul>';
var elem = $('.jobItems', page).html(html).trigger('create');
2015-01-22 23:15:15 -07:00
$(".lazy", elem).unveil(200);
2014-12-30 23:24:49 -07:00
$('.btnJobItemMenu', elem).on('click', function () {
showJobItemMenu(this);
});
}
function showJobItemMenu(elem) {
var page = $(elem).parents('.page');
var listItem = $(elem).parents('li');
var id = listItem.attr('data-itemid');
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
$('.jobMenu', page).popup("close").remove();
var html = '<div data-role="popup" class="jobMenu tapHoldMenu" data-theme="a">';
html += '<ul data-role="listview" style="min-width: 180px;">';
html += '<li data-role="list-divider">' + Globalize.translate('HeaderMenu') + '</li>';
2015-01-20 20:54:45 -07:00
if (status == 'Failed') {
html += '<li data-icon="check"><a href="#" class="btnRetryJobItem" data-id="' + id + '">' + Globalize.translate('ButtonQueueForRetry') + '</a></li>';
}
else if (status == 'Cancelled') {
html += '<li data-icon="check"><a href="#" class="btnRetryJobItem" data-id="' + id + '">' + Globalize.translate('ButtonReenable') + '</a></li>';
}
2015-02-04 20:01:37 -07:00
else if (status == 'Queued' || status == 'Transferring' || status == 'Converting' || status == 'ReadyToTransfer') {
2015-01-20 20:54:45 -07:00
html += '<li data-icon="delete"><a href="#" class="btnCancelJobItem" data-id="' + id + '">' + Globalize.translate('ButtonCancelItem') + '</a></li>';
2014-12-30 23:24:49 -07:00
}
2015-01-20 21:56:00 -07:00
else if (status == 'Synced' && remove) {
html += '<li data-icon="check"><a href="#" class="btnUnmarkForRemoval" data-id="' + id + '">' + Globalize.translate('ButtonUnmarkForRemoval') + '</a></li>';
}
else if (status == 'Synced') {
html += '<li data-icon="check"><a href="#" class="btnMarkForRemoval" data-id="' + id + '">' + Globalize.translate('ButtonMarkForRemoval') + '</a></li>';
}
2014-12-30 23:24:49 -07:00
html += '</ul>';
html += '</div>';
page.append(html);
var flyout = $('.jobMenu', page).popup({ positionTo: elem || "window" }).trigger('create').popup("open").on("popupafterclose", function () {
$(this).off("popupafterclose").remove();
});
2015-01-20 20:54:45 -07:00
$('.btnCancelJobItem', flyout).on('click', function () {
cancelJobItem(page, this.getAttribute('data-id'));
});
$('.btnRetryJobItem', flyout).on('click', function () {
retryJobItem(page, this.getAttribute('data-id'));
});
2015-01-20 21:56:00 -07:00
$('.btnUnmarkForRemoval', flyout).on('click', function () {
unMarkForRemoval(page, this.getAttribute('data-id'));
});
$('.btnMarkForRemoval', flyout).on('click', function () {
markForRemoval(page, this.getAttribute('data-id'));
});
2015-01-20 20:54:45 -07:00
}
function cancelJobItem(page, jobItemId) {
$('.jobMenu', page).popup('close');
// 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)
}).done(function () {
loadJob(page);
});
}
2015-01-20 21:56:00 -07:00
function markForRemoval(page, jobItemId) {
$('.jobMenu', page).popup('close');
ApiClient.ajax({
type: "POST",
url: ApiClient.getUrl('Sync/JobItems/' + jobItemId + '/MarkForRemoval')
}).done(function () {
loadJob(page);
});
}
function unMarkForRemoval(page, jobItemId) {
$('.jobMenu', page).popup('close');
ApiClient.ajax({
type: "POST",
url: ApiClient.getUrl('Sync/JobItems/' + jobItemId + '/UnmarkForRemoval')
}).done(function () {
loadJob(page);
});
}
2015-01-20 20:54:45 -07:00
function retryJobItem(page, jobItemId) {
$('.jobMenu', page).popup('close');
ApiClient.ajax({
type: "POST",
url: ApiClient.getUrl('Sync/JobItems/' + jobItemId + '/Enable')
}).done(function () {
loadJob(page);
2014-12-30 23:24:49 -07:00
});
}
function fillJobValues(page, job, editOptions) {
2015-03-14 21:24:43 -07:00
$('#txtSyncJobName', page).val(job.Name);
2015-03-14 21:17:35 -07:00
$('#selectProfile', page).val(job.Profile || '').trigger('change').selectmenu('refresh');
$('#selectQuality', page).val(job.Quality || '').trigger('change').selectmenu('refresh');
2014-12-30 23:24:49 -07:00
$('#chkUnwatchedOnly', page).checked(job.UnwatchedOnly).checkboxradio('refresh');
$('#chkSyncNewContent', page).checked(job.SyncNewContent).checkboxradio('refresh');
$('#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');
ApiClient.getJSON(ApiClient.getUrl('Sync/Jobs/' + id)).done(function (job) {
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
})).done(function (options) {
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
})).done(function (result) {
renderJobItems(page, result.Items);
Dashboard.hideLoadingMsg();
});
}
2015-02-04 20:01:37 -07:00
function loadJobInfo(page, job, jobItems) {
renderJob(page, job, _jobOptions);
renderJobItems(page, jobItems);
Dashboard.hideLoadingMsg();
}
2014-12-30 23:24:49 -07:00
function saveJob(page) {
Dashboard.showLoadingMsg();
var id = getParameterByName('id');
ApiClient.getJSON(ApiClient.getUrl('Sync/Jobs/' + id)).done(function (job) {
2015-03-15 12:10:27 -07:00
SyncManager.setJobValues(job, page);
2014-12-30 23:24:49 -07:00
ApiClient.ajax({
url: ApiClient.getUrl('Sync/Jobs/' + id),
type: 'POST',
data: JSON.stringify(job),
contentType: "application/json"
}).done(function () {
Dashboard.hideLoadingMsg();
Dashboard.alert(Globalize.translate('SettingsSaved'));
});
});
}
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", "");
}
}
2014-12-30 23:24:49 -07:00
$(document).on('pageshow', ".syncJobPage", function () {
var page = this;
loadJob(page);
2015-02-04 20:01:37 -07:00
startListening(page);
$(ApiClient).on("websocketmessage.syncJobPage", onWebSocketMessage);
2014-12-30 23:24:49 -07:00
2015-02-04 20:01:37 -07:00
}).on('pagehide', ".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();
$(ApiClient).off(".syncJobPage");
2014-12-30 23:24:49 -07:00
});
window.SyncJobPage = {
onSubmit: function () {
var form = this;
var page = $(form).parents('.page');
saveJob(page);
return false;
}
};
})();