jellyfin-web/dashboard-ui/bower_components/emby-webcomponents/sync/syncjoblist.js

420 lines
12 KiB
JavaScript
Raw Normal View History

2016-12-29 12:16:33 -07:00
define(['serverNotifications', 'events', 'loading', 'connectionManager', 'imageLoader', 'dom', 'globalize', 'registrationServices', 'layoutManager', 'listViewStyle'], function (serverNotifications, events, loading, connectionManager, imageLoader, dom, globalize, registrationServices, layoutManager) {
2016-10-22 22:11:46 -07:00
'use strict';
2016-08-16 13:54:13 -07:00
function onSyncJobsUpdated(e, apiClient, data) {
2016-08-16 22:29:05 -07:00
var listInstance = this;
2017-01-10 22:18:59 -07:00
renderList(listInstance, data, apiClient);
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 ?
2016-12-26 10:37:05 -07:00
globalize.translate('sharedcomponents#ConfirmRemoveDownload') :
globalize.translate('sharedcomponents#CancelSyncJobConfirmation');
2016-08-16 22:29:05 -07:00
2016-12-26 10:37:05 -07:00
confirm({
text: msg,
primary: 'cancel'
}).then(function () {
2016-08-16 22:29:05 -07:00
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;
}
2017-01-10 22:18:59 -07:00
listItem.querySelector('.jobStatus').innerHTML = getProgressText(job);
}
2016-08-16 22:29:05 -07:00
2017-01-10 22:18:59 -07:00
function getProgressText(job) {
var status = job.Status;
if (status === 'Completed') {
status = 'Synced';
}
var html = globalize.translate('sharedcomponents#SyncJobItemStatus' + status);
if (job.Status === 'Transferring' || job.Status === 'Converting' || job.Status === 'Completed') {
html += ' ';
html += (job.Progress || 0) + '%';
2016-08-16 22:29:05 -07:00
}
2017-01-10 22:18:59 -07:00
return html;
2016-08-16 22:29:05 -07:00
}
2017-01-10 22:18:59 -07:00
function getSyncJobHtml(listInstance, job, apiClient) {
2016-08-16 22:29:05 -07:00
var html = '';
2016-12-29 12:16:33 -07:00
var tagName = layoutManager.tv ? 'button' : 'div';
var typeAttribute = tagName === 'button' ? ' type="button"' : '';
var listItemClass = 'listItem';
if (layoutManager.tv) {
listItemClass += ' listItem-button listItem-focusscale';
}
html += '<' + tagName + typeAttribute + ' class="' + listItemClass + '" data-id="' + job.Id + '" data-status="' + job.Status + '">';
2016-08-16 22:29:05 -07:00
2017-01-10 22:18:59 -07:00
var imgUrl;
2016-08-16 22:29:05 -07:00
2017-01-10 22:18:59 -07:00
if (job.PrimaryImageItemId) {
imgUrl = apiClient.getImageUrl(job.PrimaryImageItemId, {
type: "Primary",
width: 80,
tag: job.PrimaryImageTag,
minScale: 1.5
});
}
if (imgUrl) {
html += '<div class="listItemImage lazy" data-src="' + imgUrl + '" item-icon>';
html += '</div>';
}
else {
html += '<i class="md-icon listItemIcon">file_download</i>';
2016-08-16 22:29:05 -07:00
}
var textLines = [];
if (job.ParentName) {
textLines.push(job.ParentName);
}
textLines.push(job.Name);
2016-12-26 10:37:05 -07:00
if (job.ItemCount === 1) {
textLines.push(globalize.translate('sharedcomponents#ValueOneItem'));
2016-08-16 22:29:05 -07:00
} else {
2016-12-26 10:37:05 -07:00
textLines.push(globalize.translate('sharedcomponents#ItemCount', job.ItemCount));
2016-08-16 22:29:05 -07:00
}
2017-01-10 22:18:59 -07:00
html += '<div class="listItemBody three-line">';
2016-08-16 22:29:05 -07:00
for (var i = 0, length = textLines.length; i < length; i++) {
2016-12-27 01:07:18 -07:00
if (i === 0) {
2016-08-16 22:29:05 -07:00
html += '<h3 class="listItemBodyText">';
html += textLines[i];
html += '</h3>';
} else {
html += '<div class="listItemBodyText secondary">';
html += textLines[i];
html += '</div>';
}
}
2017-01-10 22:18:59 -07:00
html += '<div class="secondary listItemBodyText jobStatus" style="color:green;">';
html += getProgressText(job);
html += '</div>';
2016-08-16 22:29:05 -07:00
html += '</div>';
2016-12-29 12:16:33 -07:00
if (!layoutManager.tv) {
html += '<button type="button" is="paper-icon-button-light" class="btnJobMenu listItemButton"><i class="md-icon">more_vert</i></button>';
}
2016-08-16 22:29:05 -07:00
2016-12-29 12:16:33 -07:00
html += '</' + tagName + '>';
2016-08-16 22:29:05 -07:00
return html;
}
2017-01-10 22:18:59 -07:00
function renderList(listInstance, jobs, apiClient) {
2016-08-16 22:29:05 -07:00
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';
2016-12-27 01:07:18 -07:00
if (targetName !== lastTargetName) {
2016-08-16 22:29:05 -07:00
if (lastTargetName) {
html += '</div>';
html += '<br/>';
hasOpenSection = false;
}
lastTargetName = targetName;
html += '<div class="detailSectionHeader">';
2016-12-26 10:37:05 -07:00
html += '<h1>' + targetName + '</h1>';
2016-08-16 22:29:05 -07:00
html += '</div>';
2016-12-26 10:37:05 -07:00
html += '<div class="itemsContainer vertical-list paperList">';
2016-08-16 22:29:05 -07:00
hasOpenSection = true;
}
}
2017-01-10 22:18:59 -07:00
html += getSyncJobHtml(listInstance, job, apiClient);
2016-08-16 22:29:05 -07:00
}
if (hasOpenSection) {
html += '</div>';
}
2016-12-26 10:37:05 -07:00
var elem = listInstance.options.element.querySelector('.syncJobListContent');
2016-08-17 22:56:10 -07:00
if (!html) {
if (isLocalSync) {
2016-12-26 10:37:05 -07:00
html = '<div style="padding:1em .25em;">' + globalize.translate('sharedcomponents#MessageNoDownloadsFound') + '</div>';
2016-08-17 22:56:10 -07:00
} else {
2016-12-26 10:37:05 -07:00
html = '<div style="padding:1em .25em;">' + globalize.translate('sharedcomponents#MessageNoSyncJobsFound') + '</div>';
2016-08-17 22:56:10 -07:00
}
}
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
}
2016-12-27 14:53:48 -07:00
return apiClient.getJSON(apiClient.getUrl('Sync/Jobs', options)).then(function (response) {
2016-08-16 13:54:13 -07:00
2017-01-10 22:18:59 -07:00
renderList(listInstance, response.Items, apiClient);
2016-08-16 13:54:13 -07:00
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 = [];
2016-12-27 01:07:18 -07:00
if (status === 'Cancelled') {
2016-08-16 22:29:05 -07:00
menuItems.push({
2016-12-26 10:37:05 -07:00
name: globalize.translate('sharedcomponents#Delete'),
2016-08-16 22:29:05 -07:00
id: 'delete'
});
} else {
2016-08-19 23:27:48 -07:00
var txt = listInstance.options.isLocalSync ?
2016-12-26 10:37:05 -07:00
globalize.translate('sharedcomponents#RemoveDownload') :
globalize.translate('sharedcomponents#ButtonCancelSyncJob');
2016-08-19 23:27:48 -07:00
2016-08-16 22:29:05 -07:00
menuItems.push({
2016-12-26 10:37:05 -07:00
name: 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
2016-12-26 10:37:05 -07:00
require(['syncJobEditor'], function (syncJobEditor) {
syncJobEditor.show({
serverId: listInstance.options.serverId,
jobId: jobId
}).then(function () {
fetchData(listInstance);
});
});
2016-08-16 22:29:05 -07:00
}
}
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-12-26 10:37:05 -07:00
options.element.innerHTML = '<div class="syncJobListContent"></div>';
2016-08-16 13:54:13 -07:00
fetchData(this);
2016-08-16 22:29:05 -07:00
startListening(this);
2016-12-26 10:37:05 -07:00
initSupporterInfo(options.element, getApiClient(this));
}
function showSupporterInfo(context) {
var html = '<button is="emby-button" class="raised button-accent block btnSyncSupporter" style="margin:1em 0;">';
html += '<div>';
html += globalize.translate('sharedcomponents#HeaderSyncRequiresSub');
html += '</div>';
html += '<div style="margin-top:.5em;">';
html += globalize.translate('sharedcomponents#LearnMore');
html += '</div>';
html += '</button';
context.insertAdjacentHTML('afterbegin', html);
context.querySelector('.btnSyncSupporter').addEventListener('click', function () {
registrationServices.validateFeature('sync');
});
}
function initSupporterInfo(context, apiClient) {
apiClient.getPluginSecurityInfo().then(function (regInfo) {
if (!regInfo.IsMBSupporter) {
showSupporterInfo(context, apiClient);
}
}, function () {
showSupporterInfo(context, apiClient);
});
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;
});