(function (window, $) {
function submitJob(userId, syncOptions, form) {
if (!userId) {
throw new Error('userId cannot be null');
}
if (!syncOptions) {
throw new Error('syncOptions cannot be null');
}
if (!form) {
throw new Error('form cannot be null');
}
var target = $('#selectSyncTarget', form).val();
if (!target) {
Dashboard.alert(Globalize.translate('MessagePleaseSelectDeviceToSyncTo'));
return;
}
var options = {
userId: userId,
TargetId: target,
Quality: $('#selectQuality', form).val(),
Name: $('#txtSyncJobName', form).val(),
SyncNewContent: $('#chkSyncNewContent', form).checked(),
UnwatchedOnly: $('#chkUnwatchedOnly', form).checked(),
ItemLimit: $('#txtItemLimit').val() || null,
ParentId: syncOptions.ParentId,
Category: syncOptions.Category
};
if (syncOptions.items && syncOptions.items.length) {
options.ItemIds = (syncOptions.items || []).map(function (i) {
return i.Id || i;
}).join(',');
}
ApiClient.ajax({
type: "POST",
url: ApiClient.getUrl("Sync/Jobs"),
data: JSON.stringify(options),
contentType: "application/json"
}).done(function () {
$('.syncPanel').panel('close');
$(window.SyncManager).trigger('jobsubmit');
Dashboard.alert(Globalize.translate('MessageSyncJobCreated'));
});
}
function showSyncMenu(options) {
var userId = Dashboard.getCurrentUserId();
ApiClient.getJSON(ApiClient.getUrl('Sync/Options', {
UserId: userId,
ItemIds: (options.items || []).map(function (i) {
return i.Id || i;
}).join(','),
ParentId: options.ParentId,
Category: options.Category
})).done(function (result) {
var targets = result.Targets;
var html = '
';
html += '
';
html += '
' + Globalize.translate('SyncMedia') + '
';
html += '
';
html += '
';
html += '
';
$(document.body).append(html);
var elem = $('.syncPanel').panel({}).trigger('create').panel("open").on("panelclose", function () {
$(this).off("panelclose").remove();
});
$('form', elem).on('submit', function () {
submitJob(userId, options, this);
return false;
});
});
}
function showUnwatchedFilter(items) {
return items.filter(function (i) {
return i.MediaType == "Video" || i.IsFolder || i.Type == "Person" || i.Type == "Genre" || i.Type == "MusicGenre" || i.Type == "GameGenre" || i.Type == "Studio" || i.Type == "MusicArtist";
}).length > 0;
}
function showItemLimit(items) {
return items.length > 1 || items.filter(function (i) {
return i.IsFolder || i.Type == "Person" || i.Type == "Genre" || i.Type == "MusicGenre" || i.Type == "GameGenre" || i.Type == "Studio" || i.Type == "MusicArtist";
}).length > 0;
}
function showSyncNew(items) {
return items.filter(function (i) {
return i.IsFolder || i.Type == "Person" || i.Type == "Genre" || i.Type == "MusicGenre" || i.Type == "GameGenre" || i.Type == "Studio" || i.Type == "MusicArtist";
}).length > 0;
}
function isAvailable(item, user) {
return false;
return item.SupportsSync;
}
window.SyncManager = {
showMenu: showSyncMenu,
isAvailable: isAvailable
};
function showSyncButtonsPerUser(page) {
var apiClient = ConnectionManager.currentApiClient();
if (!apiClient) {
return;
}
Dashboard.getCurrentUser().done(function (user) {
if (user.Policy.EnableSync) {
$('.categorySyncButton', page).hide();
} else {
$('.categorySyncButton', page).hide();
}
});
}
function onCategorySyncButtonClick(page, button) {
var category = button.getAttribute('data-category');
var parentId = LibraryMenu.getTopParentId();
SyncManager.showMenu({
ParentId: parentId,
Category: category
});
}
$(document).on('pageinit', ".libraryPage", function () {
var page = this;
$('.categorySyncButton', page).on('click', function () {
onCategorySyncButtonClick(page, this);
});
}).on('pagebeforeshow', ".libraryPage", function () {
var page = this;
showSyncButtonsPerUser(page);
});
})(window, jQuery);