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

466 lines
13 KiB
JavaScript
Raw Normal View History

2016-08-01 09:26:42 -07:00
define(['jQuery', 'paper-icon-button-light', 'cardStyle'], function ($) {
2014-07-26 10:30:15 -07:00
2014-12-10 23:20:28 -07:00
function cancelJob(page, id) {
2015-06-22 08:43:19 -07:00
var msg = Globalize.translate('CancelSyncJobConfirmation');
2014-12-10 23:20:28 -07:00
2016-02-22 12:31:28 -07:00
require(['confirm'], function (confirm) {
confirm(msg, Globalize.translate('HeaderCancelSyncJob')).then(function () {
2014-12-10 23:20:28 -07:00
2015-06-22 08:43:19 -07:00
Dashboard.showLoadingMsg();
2014-12-10 23:20:28 -07:00
2015-06-22 08:43:19 -07:00
ApiClient.ajax({
2014-12-10 23:20:28 -07:00
2015-06-22 08:43:19 -07:00
url: ApiClient.getUrl('Sync/Jobs/' + id),
type: 'DELETE'
2014-12-10 23:20:28 -07:00
2015-12-14 08:43:03 -07:00
}).then(function () {
2014-12-10 23:20:28 -07:00
2015-06-22 08:43:19 -07:00
reloadData(page);
});
2016-02-22 12:31:28 -07:00
});
2015-06-22 08:43:19 -07:00
});
2014-12-10 23:20:28 -07:00
}
2015-09-21 08:43:10 -07:00
function getSyncStatusBanner(job) {
var opacity = '.85';
var background = 'rgba(204,51,51,' + opacity + ')';
var text = Globalize.translate('SyncJobStatus' + job.Status);
if (job.Status == 'Completed') {
background = 'rgba(82, 181, 75, ' + opacity + ')';
}
else if (job.Status == 'CompletedWithError') {
}
else if (job.Status == 'Queued') {
background = 'rgba(51, 136, 204, ' + opacity + ')';
}
else if (job.Status == 'ReadyToTransfer') {
background = 'rgba(51, 136, 204, ' + opacity + ')';
}
else if (job.Status == 'Transferring') {
background = 'rgba(72, 0, 255, ' + opacity + ')';
}
else if (job.Status == 'Converting') {
background = 'rgba(255, 106, 0, ' + opacity + ')';
}
var html = '';
2015-10-14 10:33:30 -07:00
html += '<div class="syncStatusBanner" data-status="' + job.Status + '" style="background-color:' + background + ';position:absolute;top:0;right:0;padding:.5em .5em; text-align:left;color: #fff; font-weight: 500; text-transform:uppercase; border-bottom-left-radius: 3px;">';
2015-09-21 08:43:10 -07:00
html += text;
html += '</div>';
return html;
}
2014-12-30 23:24:49 -07:00
function getSyncJobHtml(page, job, cardBoxCssClass, syncJobPage) {
2014-07-27 15:01:29 -07:00
var html = '';
2016-08-01 09:26:42 -07:00
html += "<div class='card squareCard scalableCard' data-id='" + job.Id + "' data-status='" + job.Status + "'>";
2015-10-14 10:33:30 -07:00
html += '<div class="' + cardBoxCssClass + '">';
html += '<div class="cardScalable">';
2016-08-11 13:28:49 -07:00
html += '<div class="cardPadder cardPadder-square"></div>';
2015-10-14 10:33:30 -07:00
syncJobPage += '?id=' + job.Id;
html += '<a class="cardContent" href="' + syncJobPage + '">';
var imgUrl;
var style = '';
2014-07-27 15:01:29 -07:00
if (job.PrimaryImageItemId) {
2015-10-14 10:33:30 -07:00
imgUrl = ApiClient.getScaledImageUrl(job.PrimaryImageItemId, {
2014-07-27 15:01:29 -07:00
type: "Primary",
2015-10-14 10:33:30 -07:00
width: 400,
tag: job.PrimaryImageTag
2014-07-27 15:01:29 -07:00
});
2015-10-14 10:33:30 -07:00
style = "background-position:center center;";
2014-12-10 23:20:28 -07:00
} else {
2015-10-14 10:33:30 -07:00
style = "background-color:#38c;background-position:center center;";
imgUrl = "css/images/items/detail/video.png";
2014-07-27 15:01:29 -07:00
}
2015-10-14 10:33:30 -07:00
html += '<div class="cardImage coveredCardImage lazy" data-src="' + imgUrl + '" style="' + style + '">';
var progress = job.Progress || 0;
var footerClass = 'cardFooter fullCardFooter lightCardFooter';
if (progress == 0 || progress >= 100) {
footerClass += ' hide';
}
html += '<div class="' + footerClass + '">';
html += "<div class='cardText cardProgress'>";
html += '<progress class="itemProgressBar" min="0" max="100" value="' + progress + '"></progress>';
html += "</div>";
html += "</div>";
html += "</div>";
html += getSyncStatusBanner(job);
// cardContent
html += "</a>";
// cardScalable
html += "</div>";
html += '<div class="cardFooter outerCardFooter">';
2014-07-27 15:01:29 -07:00
var textLines = [];
if (job.ParentName) {
textLines.push(job.ParentName);
}
textLines.push(job.Name);
if (job.ItemCount == 1) {
2015-02-04 12:41:10 -07:00
textLines.push(Globalize.translate('ValueItemCount', job.ItemCount));
2014-07-27 15:01:29 -07:00
} else {
2015-02-04 12:41:10 -07:00
textLines.push(Globalize.translate('ValueItemCountPlural', job.ItemCount));
2014-07-27 15:01:29 -07:00
}
if (!job.ParentName) {
textLines.push('&nbsp;');
}
2015-10-14 10:33:30 -07:00
html += '<div class="cardText" style="text-align:right; float:right;padding:0;">';
2016-06-18 22:26:52 -07:00
html += '<button type="button" is="paper-icon-button-light" class="btnJobMenu autoSize"><i class="md-icon">' + AppInfo.moreIcon.replace('-', '_') + '</i></button>';
2015-10-14 10:33:30 -07:00
html += "</div>";
2015-09-24 10:08:10 -07:00
2015-10-14 10:33:30 -07:00
for (var i = 0, length = textLines.length; i < length; i++) {
html += "<div class='cardText' style='margin-right:30px;'>";
2014-07-27 15:01:29 -07:00
html += textLines[i];
html += "</div>";
}
2015-10-14 10:33:30 -07:00
// cardFooter
html += "</div>";
2015-09-24 10:08:10 -07:00
2015-10-14 10:33:30 -07:00
// cardBox
html += "</div>";
2015-09-24 10:08:10 -07:00
2015-10-14 10:33:30 -07:00
// card
html += "</div>";
2014-07-27 15:01:29 -07:00
return html;
}
2015-09-21 08:43:10 -07:00
var lastDataLoad = 0;
2015-02-04 13:16:23 -07:00
function loadData(page, jobs) {
2014-07-26 10:30:15 -07:00
2015-09-21 08:43:10 -07:00
if ((new Date().getTime() - lastDataLoad) < 60000) {
refreshData(page, jobs);
return;
}
lastDataLoad = new Date().getTime();
2014-07-26 10:30:15 -07:00
var html = '';
var lastTargetName = '';
2014-12-30 23:24:49 -07:00
var cardBoxCssClass = 'cardBox visualCardBox';
var syncJobPage = 'syncjob.html';
2015-09-19 19:06:56 -07:00
var showTargetName = true;
2014-12-30 23:24:49 -07:00
if ($(page).hasClass('mySyncPage')) {
syncJobPage = 'mysyncjob.html';
2015-09-19 19:06:56 -07:00
showTargetName = !hasLocalSync();
2014-12-30 23:24:49 -07:00
}
2016-08-01 11:16:07 -07:00
var hasOpenSection = false;
2014-07-26 10:30:15 -07:00
for (var i = 0, length = jobs.length; i < length; i++) {
var job = jobs[i];
2015-09-19 19:06:56 -07:00
if (showTargetName) {
var targetName = job.TargetName || 'Unknown';
2014-07-26 10:30:15 -07:00
2015-09-19 19:06:56 -07:00
if (targetName != lastTargetName) {
2014-12-30 23:24:49 -07:00
2015-09-19 19:06:56 -07:00
if (lastTargetName) {
2016-08-01 11:16:07 -07:00
html += '</div>';
2015-10-14 10:33:30 -07:00
html += '<br/>';
html += '<br/>';
html += '<br/>';
2016-08-01 11:16:07 -07:00
hasOpenSection = false;
2015-09-19 19:06:56 -07:00
}
2014-07-26 10:30:15 -07:00
2015-09-19 19:06:56 -07:00
lastTargetName = targetName;
2014-12-30 23:24:49 -07:00
2015-10-14 10:33:30 -07:00
html += '<div class="detailSectionHeader">';
html += '<div>' + targetName + '</div>';
html += '</div>';
2016-08-01 11:16:07 -07:00
html += '<div class="itemsContainer vertical-wrap">';
hasOpenSection = true;
2015-09-19 19:06:56 -07:00
}
2014-07-26 10:30:15 -07:00
}
2014-07-27 15:01:29 -07:00
2014-12-30 23:24:49 -07:00
html += getSyncJobHtml(page, job, cardBoxCssClass, syncJobPage);
2014-07-26 10:30:15 -07:00
}
2016-08-01 11:16:07 -07:00
if (hasOpenSection) {
html += '</div>';
}
2015-06-29 18:56:25 -07:00
var elem = $('.syncActivity', page).html(html).lazyChildren();
2015-01-22 23:15:15 -07:00
2014-12-10 23:20:28 -07:00
$('.btnJobMenu', elem).on('click', function () {
2015-07-02 05:55:07 -07:00
showJobMenu(page, this);
2014-12-10 23:20:28 -07:00
});
2014-12-30 23:24:49 -07:00
if (!jobs.length) {
elem.html('<div style="padding:1em .25em;">' + Globalize.translate('MessageNoSyncJobsFound') + '</div>');
}
2014-07-26 10:30:15 -07:00
}
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;
};
2015-09-21 08:43:10 -07:00
function refreshData(page, jobs) {
for (var i = 0, length = jobs.length; i < length; i++) {
var job = jobs[i];
refreshJob(page, job);
}
}
function refreshJob(page, job) {
2015-10-14 10:33:30 -07:00
var card = page.querySelector('.card[data-id=\'' + job.Id + '\']');
2015-09-21 08:43:10 -07:00
if (!card) {
return;
}
2015-10-14 10:33:30 -07:00
var banner = card.querySelector('.syncStatusBanner');
2015-09-21 08:43:10 -07:00
if (banner.getAttribute('data-status') == job.Status) {
var elem = document.createElement('div');
elem.innerHTML = getSyncStatusBanner(job);
2015-10-14 10:33:30 -07:00
elem = elem.querySelector('.syncStatusBanner');
2015-09-21 08:43:10 -07:00
elem.parentNode.removeChild(elem);
banner.parentNode.replaceChild(elem, banner);
}
var progress = job.Progress || 0;
2015-10-14 10:33:30 -07:00
var cardFooter = card.querySelector('.cardFooter');
2015-09-21 08:43:10 -07:00
2015-10-14 10:33:30 -07:00
if (progress == 0 || progress >= 100) {
cardFooter.classList.add('hide');
}
else {
cardFooter.classList.remove('hide');
cardFooter.querySelector('.itemProgressBar').value = progress;
}
2015-09-21 08:43:10 -07:00
}
2015-07-02 05:55:07 -07:00
function showJobMenu(page, elem) {
2014-07-26 10:30:15 -07:00
2015-10-14 10:33:30 -07:00
var card = $(elem).parents('.card');
2015-06-22 08:43:19 -07:00
var jobId = card.attr('data-id');
2015-02-04 12:41:10 -07:00
var status = card.attr('data-status');
2014-07-26 10:30:15 -07:00
2015-06-22 08:43:19 -07:00
var menuItems = [];
2014-12-10 23:20:28 -07:00
2015-02-04 12:41:10 -07:00
if (status == 'Cancelled') {
2015-06-22 08:43:19 -07:00
menuItems.push({
name: Globalize.translate('ButtonDelete'),
2016-06-18 22:26:52 -07:00
id: 'delete'
2015-06-22 08:43:19 -07:00
});
2015-02-04 12:41:10 -07:00
} else {
2015-06-22 08:43:19 -07:00
menuItems.push({
name: Globalize.translate('ButtonCancelSyncJob'),
2016-06-18 22:26:52 -07:00
id: 'cancel'
2015-06-22 08:43:19 -07:00
});
2015-02-04 12:41:10 -07:00
}
2014-12-10 23:20:28 -07:00
2016-01-30 21:04:00 -07:00
require(['actionsheet'], function (actionsheet) {
2014-12-10 23:20:28 -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) {
2014-12-10 23:20:28 -07:00
2015-06-22 08:43:19 -07:00
switch (id) {
2014-12-10 23:20:28 -07:00
2015-06-22 08:43:19 -07:00
case 'delete':
2015-07-02 05:55:07 -07:00
cancelJob(page, jobId);
2015-06-22 08:43:19 -07:00
break;
case 'cancel':
2015-07-02 05:55:07 -07:00
cancelJob(page, jobId);
2015-06-22 08:43:19 -07:00
break;
default:
break;
}
}
});
2014-12-10 23:20:28 -07:00
});
}
2015-09-19 19:06:56 -07:00
function hasLocalSync() {
return Dashboard.capabilities().SupportsSync;
}
2014-12-10 23:20:28 -07:00
function reloadData(page) {
2015-09-25 19:31:13 -07:00
lastDataLoad = 0;
2014-12-10 23:20:28 -07:00
Dashboard.showLoadingMsg();
2014-07-26 10:30:15 -07:00
2014-12-30 23:24:49 -07:00
var options = {};
2015-12-14 08:43:03 -07:00
Dashboard.getCurrentUser().then(function (user) {
2014-12-30 23:24:49 -07:00
if ($(page).hasClass('mySyncPage')) {
options.UserId = Dashboard.getCurrentUserId();
2015-09-19 19:06:56 -07:00
if (hasLocalSync()) {
options.TargetId = ApiClient.deviceId();
}
2014-12-30 23:24:49 -07:00
}
2014-07-26 10:30:15 -07:00
2015-12-14 08:43:03 -07:00
ApiClient.getJSON(ApiClient.getUrl('Sync/Jobs', options)).then(function (response) {
2014-07-26 10:30:15 -07:00
2015-02-04 13:16:23 -07:00
loadData(page, response.Items);
2014-12-30 23:24:49 -07:00
Dashboard.hideLoadingMsg();
});
2014-07-26 10:30:15 -07:00
});
2014-12-10 23:20:28 -07:00
}
2015-02-04 12:13:00 -07:00
function onWebSocketMessage(e, msg) {
2015-02-04 13:16:23 -07:00
2015-09-21 18:05:33 -07:00
var page = $($.mobile.activePage)[0];
2015-02-04 12:13:00 -07:00
2015-02-04 13:16:23 -07:00
if (msg.MessageType == "SyncJobs") {
2015-09-19 19:06:56 -07:00
var data = msg.Data;
if (hasLocalSync()) {
var targetId = ApiClient.deviceId();
data = data.filter(function (j) {
2015-09-20 09:16:06 -07:00
return j.TargetId == targetId;
2015-09-19 19:06:56 -07:00
});
}
loadData(page, data);
2015-02-04 12:13:00 -07:00
}
2015-02-04 13:16:23 -07:00
}
function startListening(page) {
var startParams = "0,1500";
if ($(page).hasClass('mySyncPage')) {
startParams += "," + Dashboard.getCurrentUserId();
}
if (ApiClient.isWebSocketOpen()) {
ApiClient.sendWebSocketMessage("SyncJobsStart", startParams);
}
}
function stopListening() {
if (ApiClient.isWebSocketOpen()) {
2015-02-04 20:01:37 -07:00
ApiClient.sendWebSocketMessage("SyncJobsStop", "");
2015-02-04 12:13:00 -07:00
}
2015-02-04 13:16:23 -07:00
2015-02-04 12:13:00 -07:00
}
2016-04-12 23:02:07 -07:00
function getTabs() {
return [
{
href: 'syncactivity.html',
name: Globalize.translate('TabSyncJobs')
},
{
href: 'devicesupload.html',
name: Globalize.translate('TabCameraUpload')
},
2016-07-04 15:53:40 -07:00
{
href: 'appservices.html?context=sync',
name: Globalize.translate('TabServices')
},
2016-04-12 23:02:07 -07:00
{
href: 'syncsettings.html',
name: Globalize.translate('TabSettings')
}];
}
2016-08-16 22:29:05 -07:00
$(document).on('pageinit', "#syncActivityPage", function () {
2015-10-02 11:30:27 -07:00
var page = this;
$('.btnSyncSupporter', page).on('click', function () {
2016-08-16 13:54:13 -07:00
requirejs(["registrationservices"], function (registrationServices) {
registrationServices.validateFeature('sync');
2015-10-02 11:30:27 -07:00
});
});
$('.supporterPromotion .mainText', page).html(Globalize.translate('HeaderSyncRequiresSupporterMembership'));
2016-08-16 22:29:05 -07:00
}).on('pageshow', "#syncActivityPage", function () {
2014-12-10 23:20:28 -07:00
if (this.id == 'syncActivityPage') {
LibraryMenu.setTabs('syncadmin', 0, getTabs);
}
2014-12-10 23:20:28 -07:00
var page = this;
2015-12-14 08:43:03 -07:00
Dashboard.getPluginSecurityInfo().then(function (pluginSecurityInfo) {
2015-01-24 15:33:26 -07:00
2015-07-29 19:08:35 -07:00
if (pluginSecurityInfo.IsMBSupporter) {
$('.supporterPromotionContainer', page).hide();
2015-01-24 15:33:26 -07:00
} else {
2015-07-29 10:16:00 -07:00
$('.supporterPromotionContainer', page).show();
2015-01-24 15:33:26 -07:00
}
});
2014-12-10 23:20:28 -07:00
reloadData(page);
2014-07-26 10:30:15 -07:00
2015-01-20 13:19:54 -07:00
// on here
$('.btnSync', page).taskButton({
mode: 'on',
2015-09-12 09:39:24 -07:00
progressElem: page.querySelector('.syncProgress'),
2015-01-20 13:19:54 -07:00
taskKey: 'SyncPrepare'
});
2015-02-04 13:16:23 -07:00
startListening(page);
2015-12-23 10:46:01 -07:00
Events.on(ApiClient, "websocketmessage", onWebSocketMessage);
2015-01-20 13:19:54 -07:00
2016-08-16 22:29:05 -07:00
}).on('pagebeforehide', "#syncActivityPage", function () {
2015-01-20 13:19:54 -07:00
var page = this;
// off here
$('.btnSync', page).taskButton({
mode: 'off'
});
2015-02-04 13:16:23 -07:00
stopListening();
2015-12-23 10:46:01 -07:00
Events.off(ApiClient, "websocketmessage", onWebSocketMessage);
2014-07-26 10:30:15 -07:00
});
});