define(['serverNotifications', 'events', 'loading', 'connectionManager', 'imageLoader', 'dom', 'globalize', 'registrationServices', 'layoutManager', 'listViewStyle'], function (serverNotifications, events, loading, connectionManager, imageLoader, dom, globalize, registrationServices, layoutManager) {
'use strict';
function onSyncJobsUpdated(e, apiClient, data) {
var listInstance = this;
renderList(listInstance, data, apiClient);
}
function refreshList(listInstance, jobs) {
for (var i = 0, length = jobs.length; i < length; i++) {
var job = jobs[i];
refreshJob(listInstance, job);
}
}
function cancelJob(listInstance, id) {
require(['confirm'], function (confirm) {
var msg = listInstance.options.isLocalSync ?
globalize.translate('sharedcomponents#ConfirmRemoveDownload') :
globalize.translate('sharedcomponents#CancelSyncJobConfirmation');
confirm({
text: msg,
primary: 'cancel'
}).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;
}
listItem.querySelector('.jobStatus').innerHTML = getProgressText(job);
}
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) + '%';
}
return html;
}
function getSyncJobHtml(listInstance, job, apiClient) {
var html = '';
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 + '">';
var imgUrl;
if (job.PrimaryImageItemId) {
imgUrl = apiClient.getImageUrl(job.PrimaryImageItemId, {
type: "Primary",
width: 80,
tag: job.PrimaryImageTag,
minScale: 1.5
});
}
if (imgUrl) {
html += '
';
html += '
';
}
else {
html += 'file_download';
}
var textLines = [];
if (job.ParentName) {
textLines.push(job.ParentName);
}
textLines.push(job.Name);
if (job.ItemCount === 1) {
textLines.push(globalize.translate('sharedcomponents#ValueOneItem'));
} else {
textLines.push(globalize.translate('sharedcomponents#ItemCount', job.ItemCount));
}
html += '
';
for (var i = 0, length = textLines.length; i < length; i++) {
if (i === 0) {
html += '
';
html += textLines[i];
html += '
';
} else {
html += '
';
html += textLines[i];
html += '
';
}
}
html += '
';
html += getProgressText(job);
html += '
';
html += '
';
if (!layoutManager.tv) {
html += '';
}
html += '' + tagName + '>';
return html;
}
function renderList(listInstance, jobs, apiClient) {
if ((new Date().getTime() - listInstance.lastDataLoad) < 60000) {
refreshList(listInstance, jobs);
return;
}
listInstance.lastDataLoad = new Date().getTime();
var html = '';
var lastTargetName = '';
var isLocalSync = listInstance.options.isLocalSync;
var showTargetName = !isLocalSync;
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 += '';
html += ' ';
hasOpenSection = false;
}
lastTargetName = targetName;
html += '
';
html += '
' + targetName + '
';
html += '
';
html += '
';
hasOpenSection = true;
}
}
html += getSyncJobHtml(listInstance, job, apiClient);
}
if (hasOpenSection) {
html += '
';
}
var elem = listInstance.options.element.querySelector('.syncJobListContent');
if (!html) {
if (isLocalSync) {
html = '