(function () {
function renderJob(page, job, editOptions) {
var html = '';
html += '
';
html += '' + Globalize.translate('LabelName') + ' ';
html += ' ';
html += '
';
html += '';
html += '' + Globalize.translate('LabelSyncTo') + ' ';
html += ' ';
html += '
';
html += '';
html += '' + Globalize.translate('LabelQuality') + ' ';
html += '';
html += '' + Globalize.translate('OptionHigh') + ' ';
html += '' + Globalize.translate('OptionMedium') + ' ';
html += '' + Globalize.translate('OptionLow') + ' ';
html += ' ';
html += '
';
if (editOptions.Options.indexOf('UnwatchedOnly') != -1) {
html += ' ';
html += '';
html += '
' + Globalize.translate('OptionSyncUnwatchedVideosOnly') + ' ';
html += '
';
html += '
' + Globalize.translate('OptionSyncUnwatchedVideosOnlyHelp') + '
';
html += '
';
}
if (editOptions.Options.indexOf('SyncNewContent') != -1) {
html += ' ';
html += '';
html += '
' + Globalize.translate('OptionAutomaticallySyncNewContent') + ' ';
html += '
';
html += '
' + Globalize.translate('OptionAutomaticallySyncNewContentHelp') + '
';
html += '
';
}
if (editOptions.Options.indexOf('ItemLimit') != -1) {
html += ' ';
html += '';
html += '
' + Globalize.translate('LabelItemLimit') + ' ';
html += '
';
html += '
' + Globalize.translate('LabelItemLimitHelp') + '
';
html += '
';
}
html += ' ';
html += ' ';
html += '' + Globalize.translate('ButtonSave') + ' ';
$('.syncJobForm', page).html(html).trigger('create');
fillJobValues(page, job, editOptions);
}
function getJobItemHtml(jobItem, index) {
var html = '';
var cssClass = 'ui-li-has-thumb listItem';
html += '';
var hasActions = ['Queued', 'Cancelled', 'Failed', 'Transferring', 'Converting', 'Synced'].indexOf(jobItem.Status) != -1;
html += '';
var imgUrl;
if (jobItem.PrimaryImageItemId) {
imgUrl = ApiClient.getImageUrl(jobItem.PrimaryImageItemId, {
type: "Primary",
width: 80,
tag: jobItem.PrimaryImageTag,
minScale: 1.5
});
}
if (imgUrl) {
if (index < 10) {
html += '
';
} else {
html += '
';
}
}
html += '';
html += jobItem.ItemName;
html += ' ';
if (jobItem.Status == 'Failed') {
html += '';
} else {
html += '
';
}
html += Globalize.translate('SyncJobItemStatus' + jobItem.Status);
if (jobItem.Status == 'Synced' && jobItem.IsMarkedForRemoval) {
html += ' ';
html += Globalize.translate('SyncJobItemStatusSyncedMarkForRemoval');
}
html += '
';
html += ' ';
if (hasActions) {
html += '';
html += ' ';
return html;
}
function renderJobItems(page, items) {
var html = '';
html += '';
html += '';
html += Globalize.translate('HeaderItems');
html += ' ';
var index = 0;
html += items.map(function (i) {
return getJobItemHtml(i, index++);
}).join('');
html += ' ';
var elem = $('.jobItems', page).html(html).trigger('create');
$('.btnJobItemMenu', elem).on('click', function () {
showJobItemMenu(this);
});
}
function showJobItemMenu(elem) {
var page = $(elem).parents('.page');
var listItem = $(elem).parents('li');
var id = listItem.attr('data-itemid');
var status = listItem.attr('data-status');
var remove = listItem.attr('data-remove');
$('.jobMenu', page).popup("close").remove();
var html = '';
page.append(html);
var flyout = $('.jobMenu', page).popup({ positionTo: elem || "window" }).trigger('create').popup("open").on("popupafterclose", function () {
$(this).off("popupafterclose").remove();
});
$('.btnCancelJobItem', flyout).on('click', function () {
cancelJobItem(page, this.getAttribute('data-id'));
});
$('.btnRetryJobItem', flyout).on('click', function () {
retryJobItem(page, this.getAttribute('data-id'));
});
$('.btnUnmarkForRemoval', flyout).on('click', function () {
unMarkForRemoval(page, this.getAttribute('data-id'));
});
$('.btnMarkForRemoval', flyout).on('click', function () {
markForRemoval(page, this.getAttribute('data-id'));
});
}
function cancelJobItem(page, jobItemId) {
$('.jobMenu', page).popup('close');
// Need a timeout because jquery mobile will not show a popup while another is in the act of closing
Dashboard.showLoadingMsg();
ApiClient.ajax({
type: "DELETE",
url: ApiClient.getUrl('Sync/JobItems/' + jobItemId)
}).done(function () {
loadJob(page);
});
}
function markForRemoval(page, jobItemId) {
$('.jobMenu', page).popup('close');
ApiClient.ajax({
type: "POST",
url: ApiClient.getUrl('Sync/JobItems/' + jobItemId + '/MarkForRemoval')
}).done(function () {
loadJob(page);
});
}
function unMarkForRemoval(page, jobItemId) {
$('.jobMenu', page).popup('close');
ApiClient.ajax({
type: "POST",
url: ApiClient.getUrl('Sync/JobItems/' + jobItemId + '/UnmarkForRemoval')
}).done(function () {
loadJob(page);
});
}
function retryJobItem(page, jobItemId) {
$('.jobMenu', page).popup('close');
ApiClient.ajax({
type: "POST",
url: ApiClient.getUrl('Sync/JobItems/' + jobItemId + '/Enable')
}).done(function () {
loadJob(page);
});
}
function fillJobValues(page, job, editOptions) {
$('#txtJobName', page).val(job.Name);
$('#selectQuality', page).val(job.Quality).selectmenu('refresh');
$('#chkUnwatchedOnly', page).checked(job.UnwatchedOnly).checkboxradio('refresh');
$('#chkSyncNewContent', page).checked(job.SyncNewContent).checkboxradio('refresh');
$('#txtItemLimit', page).val(job.ItemLimit);
var target = editOptions.Targets.filter(function (t) {
return t.Id == job.TargetId;
})[0];
var targetName = target ? target.Name : '';
$('#txtTargetName', page).val(targetName);
}
function loadJob(page) {
Dashboard.showLoadingMsg();
var id = getParameterByName('id');
ApiClient.getJSON(ApiClient.getUrl('Sync/Jobs/' + id)).done(function (job) {
ApiClient.getJSON(ApiClient.getUrl('Sync/Options', {
UserId: job.UserId,
ItemIds: (job.RequestedItemIds && job.RequestedItemIds.length ? job.RequestedItemIds.join('') : null),
ParentId: job.ParentId,
Category: job.Category
})).done(function (options) {
renderJob(page, job, options);
Dashboard.hideLoadingMsg();
});
});
ApiClient.getJSON(ApiClient.getUrl('Sync/JobItems', {
JobId: id,
AddMetadata: true
})).done(function (result) {
renderJobItems(page, result.Items);
Dashboard.hideLoadingMsg();
});
}
function saveJob(page) {
Dashboard.showLoadingMsg();
var id = getParameterByName('id');
ApiClient.getJSON(ApiClient.getUrl('Sync/Jobs/' + id)).done(function (job) {
job.Name = $('#txtJobName', page).val();
job.Quality = $('#selectQuality', page).val();
job.ItemLimit = $('#txtItemLimit', page).val();
job.SyncNewContent = $('#chkSyncNewContent', page).checked();
job.UnwatchedOnly = $('#chkUnwatchedOnly', page).checked();
ApiClient.ajax({
url: ApiClient.getUrl('Sync/Jobs/' + id),
type: 'POST',
data: JSON.stringify(job),
contentType: "application/json"
}).done(function () {
Dashboard.hideLoadingMsg();
Dashboard.alert(Globalize.translate('SettingsSaved'));
});
});
}
$(document).on('pageshow', ".syncJobPage", function () {
var page = this;
loadJob(page);
}).on('pageinit', ".syncJobPage", function () {
var page = this;
});
window.SyncJobPage = {
onSubmit: function () {
var form = this;
var page = $(form).parents('.page');
saveJob(page);
return false;
}
};
})();