jellyfin-web/dashboard-ui/components/syncjoblist/syncjoblist.js

349 lines
10 KiB
JavaScript
Raw Normal View History

2016-08-16 22:29:05 -07:00
define(['serverNotifications', 'events', 'loading', 'connectionManager', 'imageLoader', 'dom', 'globalize', 'listViewStyle'], function (serverNotifications, events, loading, connectionManager, imageLoader, dom, globalize) {
2016-08-16 13:54:13 -07:00
function onSyncJobsUpdated(e, apiClient, data) {
2016-08-16 22:29:05 -07:00
var listInstance = this;
renderList(listInstance, data);
2016-08-16 13:54:13 -07:00
}
2016-08-16 22:29:05 -07:00
function refreshList(listInstance, jobs) {
for (var i = 0, length = jobs.length; i < length; i++) {
2016-08-16 13:54:13 -07:00
2016-08-16 22:29:05 -07:00
var job = jobs[i];
refreshJob(listInstance, job);
}
}
function cancelJob(listInstance, id) {
require(['confirm'], function (confirm) {
var msg = listInstance.options.isLocalSync ?
globalize.translate('ConfirmRemoveDownload') :
globalize.translate('CancelSyncJobConfirmation');
confirm(msg).then(function () {
loading.show();
var apiClient = getApiClient(listInstance);
apiClient.ajax({
url: apiClient.getUrl('Sync/Jobs/' + id),
type: 'DELETE'
}).then(function () {
fetchData(listInstance);
});
});
});
}
function refreshJob(listInstance, job) {
var listItem = listInstance.options.element.querySelector('.listItem[data-id=\'' + job.Id + '\']');
if (!listItem) {
return;
}
var progress = job.Progress || 0;
var statusIcon = listItem.querySelector('.statusIcon');
if (progress === 0) {
statusIcon.innerHTML = 'file_download';
statusIcon.classList.add('md-icon');
statusIcon.classList.remove('status-text-icon');
statusIcon.classList.add('zeroProgressStatus');
} else if (progress >= 100) {
2016-08-17 22:56:10 -07:00
statusIcon.innerHTML = 'check';
2016-08-16 22:29:05 -07:00
statusIcon.classList.add('md-icon');
statusIcon.classList.remove('status-text-icon');
statusIcon.classList.remove('zeroProgressStatus');
} else {
statusIcon.classList.remove('md-icon');
statusIcon.classList.remove('zeroProgressStatus');
statusIcon.classList.add('status-text-icon');
statusIcon.innerHTML = (Math.round(progress)) + '%';
}
}
function getSyncJobHtml(listInstance, job) {
var html = '';
html += '<div class="listItem" data-id="' + job.Id + '" data-status="' + job.Status + '">';
var progress = job.Progress || 0;
if (progress === 0) {
html += '<i class="md-icon listItemIcon statusIcon zeroProgressStatus">file_download</i>';
} else if (progress >= 100) {
2016-08-17 22:56:10 -07:00
html += '<i class="md-icon listItemIcon statusIcon">check</i>';
2016-08-16 22:29:05 -07:00
} else {
html += '<i class="listItemIcon statusIcon status-text-icon">' + (Math.round(progress)) + '%</i>';
}
var textLines = [];
if (job.ParentName) {
textLines.push(job.ParentName);
}
textLines.push(job.Name);
if (job.ItemCount == 1) {
textLines.push(globalize.translate('ValueItemCount', job.ItemCount));
} else {
textLines.push(globalize.translate('ValueItemCountPlural', job.ItemCount));
}
if (textLines >= 3) {
html += '<div class="listItemBody three-line">';
} else {
html += '<div class="listItemBody two-line">';
}
for (var i = 0, length = textLines.length; i < length; i++) {
if (i == 0) {
html += '<h3 class="listItemBodyText">';
html += textLines[i];
html += '</h3>';
} else {
html += '<div class="listItemBodyText secondary">';
html += textLines[i];
html += '</div>';
}
}
html += '</div>';
html += '<button type="button" is="paper-icon-button-light" class="btnJobMenu listItemButton"><i class="md-icon">more_vert</i></button>';
html += '</div>';
return html;
}
function renderList(listInstance, jobs) {
if ((new Date().getTime() - listInstance.lastDataLoad) < 60000) {
refreshList(listInstance, jobs);
return;
}
listInstance.lastDataLoad = new Date().getTime();
var html = '';
var lastTargetName = '';
2016-08-17 22:56:10 -07:00
var isLocalSync = listInstance.options.isLocalSync;
var showTargetName = !isLocalSync;
2016-08-16 22:29:05 -07:00
var hasOpenSection = false;
for (var i = 0, length = jobs.length; i < length; i++) {
var job = jobs[i];
if (showTargetName) {
var targetName = job.TargetName || 'Unknown';
if (targetName != lastTargetName) {
if (lastTargetName) {
html += '</div>';
html += '<br/>';
html += '<br/>';
html += '<br/>';
hasOpenSection = false;
}
lastTargetName = targetName;
html += '<div class="detailSectionHeader">';
html += '<div>' + targetName + '</div>';
html += '</div>';
html += '<div class="itemsContainer vertical-list">';
hasOpenSection = true;
}
}
html += getSyncJobHtml(listInstance, job);
}
if (hasOpenSection) {
html += '</div>';
}
var elem = listInstance.options.element;
2016-08-17 22:56:10 -07:00
if (!html) {
if (isLocalSync) {
html = '<div style="padding:1em .25em;">' + globalize.translate('MessageDownloadsFound') + '</div>';
} else {
html = '<div style="padding:1em .25em;">' + globalize.translate('MessageNoSyncJobsFound') + '</div>';
}
}
2016-08-16 22:29:05 -07:00
elem.innerHTML = html;
imageLoader.lazyChildren(elem);
2016-08-16 13:54:13 -07:00
}
function fetchData(listInstance) {
listInstance.lastDataLoad = 0;
loading.show();
var options = {};
2016-08-16 22:29:05 -07:00
var apiClient = getApiClient(listInstance);
2016-08-16 13:54:13 -07:00
if (listInstance.options.userId) {
options.UserId = listInstance.options.userId;
}
if (listInstance.options.isLocalSync) {
options.TargetId = apiClient.deviceId();
2016-08-17 12:28:43 -07:00
} else {
options.ExcludeTargetIds = apiClient.deviceId();
2016-08-16 13:54:13 -07:00
}
return apiClient.getJSON(ApiClient.getUrl('Sync/Jobs', options)).then(function (response) {
renderList(listInstance, response.Items);
loading.hide();
});
}
2016-08-16 22:29:05 -07:00
function startListening(listInstance) {
var startParams = "0,1500";
var apiClient = getApiClient(listInstance);
if (listInstance.options.userId) {
startParams += "," + listInstance.options.userId;
}
if (listInstance.options.isLocalSync) {
startParams += "," + apiClient.deviceId();
}
if (apiClient.isWebSocketOpen()) {
apiClient.sendWebSocketMessage("SyncJobsStart", startParams);
}
}
function stopListening(listInstance) {
var apiClient = getApiClient(listInstance);
if (apiClient.isWebSocketOpen()) {
apiClient.sendWebSocketMessage("SyncJobsStop", "");
}
}
function getApiClient(listInstance) {
return connectionManager.getApiClient(listInstance.options.serverId);
}
function showJobMenu(listInstance, elem) {
var item = dom.parentWithClass(elem, 'listItem');
var jobId = item.getAttribute('data-id');
var status = item.getAttribute('data-status');
var menuItems = [];
if (status == 'Cancelled') {
menuItems.push({
name: globalize.translate('ButtonDelete'),
id: 'delete'
});
} else {
2016-08-19 23:27:48 -07:00
var txt = listInstance.options.isLocalSync ?
globalize.translate('RemoveDownload') :
globalize.translate('ButtonCancelSyncJob');
2016-08-16 22:29:05 -07:00
menuItems.push({
2016-08-19 23:27:48 -07:00
name: globalize.translate(txt),
2016-08-16 22:29:05 -07:00
id: 'cancel'
});
}
require(['actionsheet'], function (actionsheet) {
actionsheet.show({
items: menuItems,
positionTo: elem,
callback: function (id) {
switch (id) {
case 'delete':
cancelJob(listInstance, jobId);
break;
case 'cancel':
cancelJob(listInstance, jobId);
break;
default:
break;
}
}
});
});
}
function onElementClick(e) {
var listInstance = this;
var btnJobMenu = dom.parentWithClass(e.target, 'btnJobMenu');
if (btnJobMenu) {
showJobMenu(this, btnJobMenu);
return;
}
var listItem = dom.parentWithClass(e.target, 'listItem');
if (listItem) {
var jobId = listItem.getAttribute('data-id');
// edit job
events.trigger(listInstance, 'jobedit', [jobId, listInstance.options.serverId]);
}
}
2016-08-16 13:54:13 -07:00
function syncJobList(options) {
this.options = options;
var onSyncJobsUpdatedHandler = onSyncJobsUpdated.bind(this);
2016-08-17 12:28:43 -07:00
this.onSyncJobsUpdatedHandler = onSyncJobsUpdatedHandler;
2016-08-16 13:54:13 -07:00
events.on(serverNotifications, 'SyncJobs', onSyncJobsUpdatedHandler);
2016-08-16 22:29:05 -07:00
var onClickHandler = onElementClick.bind(this);
options.element.addEventListener('click', onClickHandler);
this.onClickHandler = onClickHandler;
2016-08-16 13:54:13 -07:00
fetchData(this);
2016-08-16 22:29:05 -07:00
startListening(this);
2016-08-16 13:54:13 -07:00
}
syncJobList.prototype.destroy = function () {
2016-08-16 22:29:05 -07:00
stopListening(this);
2016-08-16 13:54:13 -07:00
var onSyncJobsUpdatedHandler = this.onSyncJobsUpdatedHandler;
this.onSyncJobsUpdatedHandler = null;
events.off(serverNotifications, 'SyncJobs', onSyncJobsUpdatedHandler);
2016-08-16 22:29:05 -07:00
var onClickHandler = this.onClickHandler;
this.onClickHandler = null;
2016-08-17 12:28:43 -07:00
this.options.element.removeEventListener('click', onClickHandler);
this.options = null;
2016-08-16 13:54:13 -07:00
};
return syncJobList;
});