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

322 lines
9.3 KiB
JavaScript
Raw Normal View History

2014-07-26 10:30:15 -07:00
(function () {
2014-12-10 23:20:28 -07:00
function cancelJob(page, id) {
$('.jobMenu', page).on("popupafterclose.deleteuser", function () {
$(this).off('popupafterclose.deleteuser');
var msg = Globalize.translate('CancelSyncJobConfirmation');
Dashboard.confirm(msg, Globalize.translate('HeaderCancelSyncJob'), function (result) {
if (result) {
Dashboard.showLoadingMsg();
ApiClient.ajax({
url: ApiClient.getUrl('Sync/Jobs/' + id),
type: 'DELETE'
}).done(function () {
reloadData(page);
});
}
});
}).popup('close');
}
2014-07-26 10:30:15 -07:00
function getSyncTargetName(targets, id) {
var target = targets.filter(function (t) {
return t.Id == id;
})[0];
return target ? target.Name : 'Unknown Device';
}
2014-12-30 23:24:49 -07:00
function getSyncJobHtml(page, job, cardBoxCssClass, syncJobPage) {
2014-07-27 15:01:29 -07:00
var html = '';
2014-12-15 22:01:57 -07:00
html += "<div class='card squareCard' data-id='" + job.Id + "'>";
2014-07-27 15:01:29 -07:00
2014-12-30 23:24:49 -07:00
html += '<div class="' + cardBoxCssClass + '">';
2014-07-27 15:01:29 -07:00
html += '<div class="cardScalable">';
html += '<div class="cardPadder"></div>';
2014-12-30 23:24:49 -07:00
syncJobPage += '?id=' + job.Id;
html += '<a class="cardContent" href="' + syncJobPage + '">';
2014-07-27 15:01:29 -07:00
var imgUrl;
2014-12-10 23:20:28 -07:00
var style = '';
2014-07-27 15:01:29 -07:00
if (job.PrimaryImageItemId) {
imgUrl = ApiClient.getScaledImageUrl(job.PrimaryImageItemId, {
type: "Primary",
width: 400,
tag: job.PrimaryImageTag
});
2014-12-12 20:56:30 -07:00
style = "background-position:center center;";
2014-12-10 23:20:28 -07:00
} else {
style = "background-color:#38c;background-position:center center;";
imgUrl = "css/images/items/detail/video.png";
2014-07-27 15:01:29 -07:00
}
2014-12-10 23:20:28 -07:00
html += '<div class="cardImage coveredCardImage lazy" data-src="' + imgUrl + '" style="' + style + '">';
2014-12-27 15:52:41 -07:00
if (job.Progress && job.Progress < 100) {
2014-12-10 23:20:28 -07:00
html += '<div class="cardFooter">';
html += "<div class='cardText cardProgress'>";
html += '<progress class="itemProgressBar" min="0" max="100" value="' + job.Progress + '"></progress>';
html += "</div>";
2014-07-27 15:01:29 -07:00
html += "</div>";
}
2014-12-10 23:20:28 -07:00
html += "</div>";
var opacity = '.85';
var background = 'rgba(204,51,51,' + opacity + ')';
var text = Globalize.translate('SyncJobStatus' + job.Status);
2014-07-27 15:01:29 -07:00
if (job.Status == 'Completed') {
background = 'rgba(82, 181, 75, ' + opacity + ')';
2014-07-27 15:01:29 -07:00
}
2014-12-12 20:56:30 -07:00
else if (job.Status == 'CompletedWithError') {
}
else if (job.Status == 'Queued') {
background = 'rgba(51, 136, 204, ' + opacity + ')';
2014-07-27 15:01:29 -07:00
}
2015-02-04 12:13:00 -07:00
else if (job.Status == 'Transferring') {
background = 'rgba(72, 0, 255, ' + opacity + ')';
}
else if (job.Status == 'Converting') {
background = 'rgba(72, 0, 255, ' + opacity + ')';
}
2015-02-03 14:06:56 -07:00
html += '<div class="syncStatusBanner" style="background-color:' + background + ';position:absolute;top:0;right:0;padding:.5em .5em; text-align:left;color: #fff; font-weight: 500; text-transform:uppercase;">';
html += text;
html += '</div>';
2014-07-27 15:01:29 -07:00
// cardContent
2014-12-30 23:24:49 -07:00
html += "</a>";
2014-07-27 15:01:29 -07:00
// cardScalable
html += "</div>";
html += '<div class="cardFooter">';
var textLines = [];
if (job.ParentName) {
textLines.push(job.ParentName);
}
textLines.push(job.Name);
if (job.ItemCount == 1) {
textLines.push(job.ItemCount + ' item');
} else {
textLines.push(job.ItemCount + ' items');
}
if (!job.ParentName) {
textLines.push('&nbsp;');
}
2014-12-10 23:20:28 -07:00
html += '<div class="cardText" style="text-align:right; position:absolute; bottom:5px; right: 5px;">';
html += '<button class="btnJobMenu" type="button" data-inline="true" data-iconpos="notext" data-icon="ellipsis-v" style="margin: 0 0 0;"></button>';
html += "</div>";
2014-07-27 15:01:29 -07:00
for (var i = 0, length = textLines.length; i < length; i++) {
2014-12-10 23:20:28 -07:00
html += "<div class='cardText' style='margin-right:30px;'>";
2014-07-27 15:01:29 -07:00
html += textLines[i];
html += "</div>";
}
// cardFooter
html += "</div>";
// cardBox
html += "</div>";
// card
html += "</div>";
return html;
}
2014-07-26 10:30:15 -07:00
function loadData(page, jobs, targets) {
var html = '';
var lastTargetName = '';
2014-12-30 23:24:49 -07:00
var cardBoxCssClass = 'cardBox visualCardBox';
var syncJobPage = 'syncjob.html';
2015-01-28 20:58:39 -07:00
var showHelpButtons = false;
2014-12-30 23:24:49 -07:00
if ($(page).hasClass('mySyncPage')) {
syncJobPage = 'mysyncjob.html';
2015-01-28 20:58:39 -07:00
showHelpButtons = true;
2014-12-30 23:24:49 -07:00
}
2014-07-26 10:30:15 -07:00
for (var i = 0, length = jobs.length; i < length; i++) {
var job = jobs[i];
var targetName = getSyncTargetName(targets, job.TargetId);
if (targetName != lastTargetName) {
2014-12-30 23:24:49 -07:00
if (lastTargetName) {
html += '<br/>';
html += '<br/>';
html += '<br/>';
}
2014-07-26 10:30:15 -07:00
lastTargetName = targetName;
2014-12-30 23:24:49 -07:00
2015-01-28 20:58:39 -07:00
html += '<div class="detailSectionHeader" style="padding: .85em 0 .85em 1em;">';
html += '<div style="display:inline-block;vertical-align:middle;">' + targetName + '</div>';
if (showHelpButtons) {
html += '<a class="accentButton accentButton-g" style="display:inline-block;vertical-align:middle;margin-top:0;margin-left: 20px;" href="https://github.com/MediaBrowser/Wiki/wiki/Sync" target="_blank">';
html += '<i class="fa fa-info-circle"></i>';
html += Globalize.translate('ButtonHelp');
html += '</a>';
}
html += '</div>';
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
}
2014-12-10 23:20:28 -07:00
var elem = $('.syncActivity', page).html(html).trigger('create');
2015-01-22 23:15:15 -07:00
$(".lazy", elem).unveil(200);
2014-12-10 23:20:28 -07:00
$('.btnJobMenu', elem).on('click', function () {
showJobMenu(this);
});
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
}
2014-12-10 23:20:28 -07:00
function showJobMenu(elem) {
2014-07-26 10:30:15 -07:00
2014-12-10 23:20:28 -07:00
var card = $(elem).parents('.card');
var page = $(elem).parents('.page');
var id = card.attr('data-id');
2014-07-26 10:30:15 -07:00
2014-12-10 23:20:28 -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>';
html += '<li data-icon="delete"><a href="#" class="btnCancelJob" data-id="' + id + '">' + Globalize.translate('ButtonCancel') + '</a></li>';
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();
});
$('.btnCancelJob', flyout).on('click', function () {
cancelJob(page, this.getAttribute('data-id'));
});
}
function reloadData(page) {
Dashboard.showLoadingMsg();
2014-07-26 10:30:15 -07:00
2014-12-30 23:24:49 -07:00
var options = {};
Dashboard.getCurrentUser().done(function (user) {
if ($(page).hasClass('mySyncPage')) {
options.UserId = Dashboard.getCurrentUserId();
}
2014-07-26 10:30:15 -07:00
2014-12-30 23:24:49 -07:00
var promise1 = ApiClient.getJSON(ApiClient.getUrl('Sync/Jobs', options));
2014-07-26 10:30:15 -07:00
2014-12-30 23:24:49 -07:00
var promise2 = ApiClient.getJSON(ApiClient.getUrl('Sync/Targets', options));
2014-07-26 10:30:15 -07:00
2014-12-30 23:24:49 -07:00
$.when(promise1, promise2).done(function (response1, response2) {
2014-07-26 10:30:15 -07:00
2014-12-30 23:24:49 -07:00
loadData(page, response1[0].Items, response2[0]);
2014-07-26 10:30:15 -07:00
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) {
var page = $.mobile.activePage;
if (msg.MessageType == "SyncJobCreated") {
reloadData(page);
}
else if (msg.MessageType == "SyncJobCancelled") {
reloadData(page);
}
}
2014-12-30 23:24:49 -07:00
$(document).on('pageshow', ".syncActivityPage", function () {
2014-12-10 23:20:28 -07:00
var page = this;
2015-01-24 15:33:26 -07:00
Dashboard.getPluginSecurityInfo().done(function (pluginSecurityInfo) {
if (pluginSecurityInfo.IsMBSupporter) {
$('.syncPromotion', page).hide();
} else {
$('.syncPromotion', page).show();
}
});
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',
progressElem: $('.syncProgress', page),
taskKey: 'SyncPrepare'
});
2015-02-04 12:13:00 -07:00
$(ApiClient).on("websocketmessage.syncactivity", onWebSocketMessage);
2015-01-20 13:19:54 -07:00
}).on('pagehide', ".syncActivityPage", function () {
var page = this;
// off here
$('.btnSync', page).taskButton({
mode: 'off'
});
2015-02-04 12:13:00 -07:00
$(ApiClient).off(".syncactivity");
2014-07-26 10:30:15 -07:00
});
})();