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

425 lines
14 KiB
JavaScript
Raw Normal View History

(function (window, $) {
2015-03-14 21:17:35 -07:00
var currentDialogOptions;
2015-08-15 13:33:53 -07:00
function submitJob(panel, userId, syncOptions, form) {
2014-12-12 20:56:30 -07:00
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');
}
2014-07-22 09:36:34 -07:00
2014-12-30 09:36:49 -07:00
var target = $('#selectSyncTarget', form).val();
2014-07-26 10:30:15 -07:00
if (!target) {
2014-07-22 09:36:34 -07:00
2014-12-12 20:56:30 -07:00
Dashboard.alert(Globalize.translate('MessagePleaseSelectDeviceToSyncTo'));
2014-07-22 09:36:34 -07:00
return;
}
var options = {
userId: userId,
2014-07-26 10:30:15 -07:00
TargetId: target,
2014-07-22 09:36:34 -07:00
2014-12-18 21:20:07 -07:00
ParentId: syncOptions.ParentId,
Category: syncOptions.Category
2014-07-22 09:36:34 -07:00
};
2015-03-15 12:10:27 -07:00
setJobValues(options, form);
2014-12-30 09:36:49 -07:00
if (syncOptions.items && syncOptions.items.length) {
options.ItemIds = (syncOptions.items || []).map(function (i) {
return i.Id || i;
}).join(',');
}
2014-07-22 09:36:34 -07:00
ApiClient.ajax({
type: "POST",
url: ApiClient.getUrl("Sync/Jobs"),
data: JSON.stringify(options),
2015-08-15 13:33:53 -07:00
contentType: "application/json",
dataType: 'json'
2014-07-22 09:36:34 -07:00
}).done(function () {
2015-08-15 13:33:53 -07:00
panel.panel('close');
2014-12-10 23:20:28 -07:00
$(window.SyncManager).trigger('jobsubmit');
2014-12-12 20:56:30 -07:00
Dashboard.alert(Globalize.translate('MessageSyncJobCreated'));
2014-07-22 09:36:34 -07:00
});
}
2015-03-15 12:10:27 -07:00
function setJobValues(job, form) {
var bitrate = $('#txtBitrate', form).val() || null;
if (bitrate) {
bitrate = parseFloat(bitrate) * 1000000;
}
job.Name = $('#txtSyncJobName', form).val();
job.Quality = $('#selectQuality', form).val() || null;
job.Profile = $('#selectProfile', form).val() || null;
job.Bitrate = bitrate;
job.ItemLimit = $('#txtItemLimit', form).val() || null;
job.SyncNewContent = $('#chkSyncNewContent', form).checked();
job.UnwatchedOnly = $('#chkUnwatchedOnly', form).checked();
}
2015-03-14 21:17:35 -07:00
function renderForm(options) {
var elem = options.elem;
var dialogOptions = options.dialogOptions;
var targets = dialogOptions.Targets;
var html = '';
2015-03-14 21:24:43 -07:00
if (options.showName || dialogOptions.Options.indexOf('Name') != -1) {
2015-03-14 21:17:35 -07:00
2015-08-17 09:52:56 -07:00
html += '<div>';
html += '<paper-input type="text" id="txtSyncJobName" class="txtSyncJobName" required="required" label="' + Globalize.translate('LabelSyncJobName') + '"></paper-input>';
html += '</div>';
html += '<br/>';
2015-03-14 21:17:35 -07:00
}
html += '<div>';
2015-03-14 21:39:29 -07:00
if (options.readOnlySyncTarget) {
2015-08-17 09:52:56 -07:00
html += '<paper-input type="text" id="selectSyncTarget" readonly label="' + Globalize.translate('LabelSyncTo') + '"></paper-input>';
2015-03-14 21:39:29 -07:00
} else {
2015-08-17 09:52:56 -07:00
html += '<label for="selectSyncTarget">' + Globalize.translate('LabelSyncTo') + '</label>';
2015-03-14 21:39:29 -07:00
html += '<select id="selectSyncTarget" required="required" data-mini="true">';
2015-03-14 21:17:35 -07:00
2015-03-14 21:39:29 -07:00
html += targets.map(function (t) {
2015-03-14 21:17:35 -07:00
var isSelected = t.Id == AppInfo.deviceId;
var selectedHtml = isSelected ? ' selected="selected"' : '';
return '<option' + selectedHtml + ' value="' + t.Id + '">' + t.Name + '</option>';
2015-03-14 21:17:35 -07:00
2015-03-14 21:39:29 -07:00
}).join('');
html += '</select>';
if (!targets.length) {
html += '<div class="fieldDescription">' + Globalize.translate('LabelSyncNoTargetsHelp') + '</div>';
html += '<div class="fieldDescription"><a href="https://github.com/MediaBrowser/Wiki/wiki/Sync" target="_blank">' + Globalize.translate('ButtonLearnMore') + '</a></div>';
}
2015-03-14 21:17:35 -07:00
}
html += '</div>';
html += '<div class="fldProfile" style="display:none;">';
html += '<br/>';
html += '<label for="selectProfile">' + Globalize.translate('LabelProfile') + '</label>';
html += '<select id="selectProfile" data-mini="true">';
html += '</select>';
html += '<div class="fieldDescription profileDescription"></div>';
html += '</div>';
html += '<div class="fldQuality" style="display:none;">';
html += '<br/>';
html += '<label for="selectQuality">' + Globalize.translate('LabelQuality') + '</label>';
html += '<select id="selectQuality" data-mini="true" required="required">';
html += '</select>';
html += '<div class="fieldDescription qualityDescription"></div>';
html += '</div>';
html += '<div class="fldBitrate" style="display:none;">';
html += '<br/>';
html += '<div>';
2015-08-17 09:52:56 -07:00
html += '<paper-input type="number" step=".1" min=".1" id="txtBitrate" label="' + Globalize.translate('LabelBitrateMbps') + '"></paper-input>';
2015-03-14 21:17:35 -07:00
html += '</div>';
html += '</div>';
if (dialogOptions.Options.indexOf('UnwatchedOnly') != -1) {
html += '<br/>';
html += '<div>';
2015-08-17 09:52:56 -07:00
html += '<paper-checkbox id="chkUnwatchedOnly">' + Globalize.translate('OptionSyncUnwatchedVideosOnly') + '</paper-checkbox>';
html += '<div class="fieldDescription paperCheckboxFieldDescription">' + Globalize.translate('OptionSyncUnwatchedVideosOnlyHelp') + '</div>';
2015-03-14 21:17:35 -07:00
html += '</div>';
}
if (dialogOptions.Options.indexOf('SyncNewContent') != -1 ||
dialogOptions.Options.indexOf('ItemLimit') != -1) {
2015-03-14 21:17:35 -07:00
html += '<br/>';
html += '<div data-role="collapsible" data-mini="true">';
html += '<h2>' + Globalize.translate('HeaderAdvanced') + '</h2>';
html += '<div style="padding:0 0 1em;">';
if (dialogOptions.Options.indexOf('SyncNewContent') != -1) {
html += '<br/>';
html += '<div>';
2015-08-17 09:52:56 -07:00
html += '<paper-checkbox id="chkSyncNewContent" checked>' + Globalize.translate('OptionAutomaticallySyncNewContent') + '</paper-checkbox>';
html += '<div class="fieldDescription paperCheckboxFieldDescription">' + Globalize.translate('OptionAutomaticallySyncNewContentHelp') + '</div>';
html += '</div>';
}
if (dialogOptions.Options.indexOf('ItemLimit') != -1) {
html += '<div>';
2015-08-17 09:52:56 -07:00
html += '<paper-input type="number" step="1" min="1" id="txtItemLimit" label="' + Globalize.translate('LabelItemLimit') + '"></paper-input>';
html += '<div class="fieldDescription">' + Globalize.translate('LabelItemLimitHelp') + '</div>';
html += '</div>';
}
html += '</div>';
2015-03-14 21:17:35 -07:00
html += '</div>';
}
//html += '</div>';
//html += '</div>';
$(elem).html(html).trigger('create');
$('#selectSyncTarget', elem).on('change', function () {
loadQualityOptions(elem, this.value, options.dialogOptionsFn);
}).trigger('change');
$('#selectProfile', elem).on('change', function () {
onProfileChange(elem, this.value);
}).trigger('change');
$('#selectQuality', elem).on('change', function () {
onQualityChange(elem, this.value);
}).trigger('change');
}
2014-12-12 20:56:30 -07:00
function showSyncMenu(options) {
2015-09-05 11:05:29 -07:00
requirejs(["scripts/registrationservices", "jqmcollapsible", "jqmpanel"], function () {
2015-07-29 19:08:35 -07:00
RegistrationServices.validateFeature('sync').done(function () {
showSyncMenuInternal(options);
});
});
}
function showSyncMenuInternal(options) {
2014-07-22 09:36:34 -07:00
var userId = Dashboard.getCurrentUserId();
2015-03-11 22:24:02 -07:00
var dialogOptionsQuery = {
2014-12-16 22:30:31 -07:00
UserId: userId,
2015-03-14 21:17:35 -07:00
ItemIds: (options.items || []).map(function (i) {
2014-12-16 22:30:31 -07:00
return i.Id || i;
2014-12-18 21:20:07 -07:00
}).join(','),
ParentId: options.ParentId,
Category: options.Category
2015-03-11 22:24:02 -07:00
};
2014-12-16 22:30:31 -07:00
2015-03-14 21:17:35 -07:00
ApiClient.getJSON(ApiClient.getUrl('Sync/Options', dialogOptionsQuery)).done(function (dialogOptions) {
2014-07-22 09:36:34 -07:00
2015-03-14 21:17:35 -07:00
currentDialogOptions = dialogOptions;
2014-07-22 09:36:34 -07:00
var html = '<div data-role="panel" data-position="right" data-display="overlay" class="syncPanel" data-position-fixed="true" data-theme="a">';
html += '<div>';
2015-02-04 23:13:20 -07:00
2015-03-14 21:17:35 -07:00
html += '<form class="formSubmitSyncRequest">';
2015-02-04 23:13:20 -07:00
html += '<div style="margin:1em 0 1.5em;">';
html += '<h1 style="margin: 0;display:inline-block;vertical-align:middle;">' + Globalize.translate('SyncMedia') + '</h1>';
2015-06-15 21:52:01 -07:00
2015-08-17 09:52:56 -07:00
html += '<a href="https://github.com/MediaBrowser/Wiki/wiki/Sync" target="_blank" class="clearLink" style="margin-top:0;display:inline-block;vertical-align:middle;margin-left:1em;"><paper-button raised class="secondary mini"><iron-icon icon="info"></iron-icon><span>' + Globalize.translate('ButtonHelp') + '</span></paper-button></a>';
2015-02-04 23:13:20 -07:00
html += '</div>';
2014-07-22 09:36:34 -07:00
2015-03-14 21:17:35 -07:00
html += '<div class="formFields"></div>';
2014-12-13 14:26:04 -07:00
2014-07-22 09:36:34 -07:00
html += '<p>';
2015-09-19 19:06:56 -07:00
html += '<button type="submit" data-role="none" class="clearButton"><paper-button raised class="submit block"><iron-icon icon="sync"></iron-icon><span>' + Globalize.translate('ButtonSync') + '</span></paper-button></button>';
2014-07-22 09:36:34 -07:00
html += '</p>';
html += '</form>';
html += '</div>';
html += '</div>';
$(document.body).append(html);
var elem = $('.syncPanel').panel({}).trigger('create').panel("open").on("panelclose", function () {
$(this).off("panelclose").remove();
});
$('form', elem).on('submit', function () {
2015-08-15 13:33:53 -07:00
submitJob(elem, userId, options, this);
2014-07-22 09:36:34 -07:00
return false;
});
2015-03-14 21:17:35 -07:00
renderForm({
elem: $('.formFields', elem),
dialogOptions: dialogOptions,
dialogOptionsFn: getTargetDialogOptionsFn(dialogOptionsQuery)
});
2014-07-22 09:36:34 -07:00
});
2015-06-17 23:23:44 -07:00
require(['jqmicons']);
2014-07-19 21:46:29 -07:00
}
2015-03-14 21:17:35 -07:00
function getTargetDialogOptionsFn(query) {
return function (targetId) {
query.TargetId = targetId;
return ApiClient.getJSON(ApiClient.getUrl('Sync/Options', query));
};
}
function onProfileChange(form, profileId) {
var options = currentDialogOptions || {};
var option = (options.ProfileOptions || []).filter(function (o) {
return o.Id == profileId;
})[0];
if (option) {
$('.profileDescription', form).html(option.Description || '');
2015-03-14 21:24:43 -07:00
setQualityFieldVisible(form, options.QualityOptions.length > 0 && option.EnableQualityOptions && options.Options.indexOf('Quality') != -1);
2015-03-14 21:17:35 -07:00
} else {
$('.profileDescription', form).html('');
2015-03-14 21:24:43 -07:00
setQualityFieldVisible(form, options.QualityOptions.length > 0 && options.Options.indexOf('Quality') != -1);
2015-03-14 21:17:35 -07:00
}
}
function onQualityChange(form, qualityId) {
2015-03-14 21:17:35 -07:00
var options = currentDialogOptions || {};
var option = (options.QualityOptions || []).filter(function (o) {
return o.Id == qualityId;
})[0];
2015-03-14 21:17:35 -07:00
if (option) {
$('.qualityDescription', form).html(option.Description || '');
} else {
$('.qualityDescription', form).html('');
}
2015-03-14 21:17:35 -07:00
if (qualityId == 'custom') {
$('.fldBitrate', form).show();
$('#txtBitrate', form).attr('required', 'required');
} else {
$('.fldBitrate', form).hide();
2015-03-15 12:10:27 -07:00
$('#txtBitrate', form).removeAttr('required').val('');
2015-03-14 21:17:35 -07:00
}
}
2015-03-14 21:17:35 -07:00
function loadQualityOptions(form, targetId, dialogOptionsFn) {
2015-03-14 21:17:35 -07:00
dialogOptionsFn(targetId).done(function (options) {
2015-03-14 21:17:35 -07:00
renderTargetDialogOptions(form, options);
});
2015-03-14 21:17:35 -07:00
}
function setQualityFieldVisible(form, visible) {
2015-03-14 21:17:35 -07:00
if (visible) {
$('.fldQuality', form).show();
$('#selectQuality', form).attr('required', 'required');
} else {
$('.fldQuality', form).hide();
$('#selectQuality', form).removeAttr('required');
}
}
function renderTargetDialogOptions(form, options) {
currentDialogOptions = options;
2015-03-16 20:48:05 -07:00
if (options.ProfileOptions.length && options.Options.indexOf('Profile') != -1) {
2015-03-14 21:17:35 -07:00
$('.fldProfile', form).show();
$('#selectProfile', form).attr('required', 'required');
} else {
$('.fldProfile', form).hide();
$('#selectProfile', form).removeAttr('required');
}
setQualityFieldVisible(options.QualityOptions.length > 0);
$('#selectProfile', form).html(options.ProfileOptions.map(function (o) {
var selectedAttribute = o.IsDefault ? ' selected="selected"' : '';
return '<option value="' + o.Id + '"' + selectedAttribute + '>' + o.Name + '</option>';
2015-09-03 10:01:51 -07:00
}).join('')).trigger('change');
2015-03-14 21:17:35 -07:00
$('#selectQuality', form).html(options.QualityOptions.map(function (o) {
var selectedAttribute = o.IsDefault ? ' selected="selected"' : '';
return '<option value="' + o.Id + '"' + selectedAttribute + '>' + o.Name + '</option>';
2015-09-03 10:01:51 -07:00
}).join('')).trigger('change');
}
2014-07-19 21:46:29 -07:00
function isAvailable(item, user) {
2014-07-26 10:30:15 -07:00
2015-10-05 19:50:20 -07:00
if (AppInfo.isNativeApp && !Dashboard.capabilities().SupportsSync) {
return false;
}
return item.SupportsSync;
}
window.SyncManager = {
2014-07-19 21:46:29 -07:00
showMenu: showSyncMenu,
2015-03-14 21:17:35 -07:00
isAvailable: isAvailable,
2015-03-15 12:10:27 -07:00
renderForm: renderForm,
setJobValues: setJobValues
};
2014-12-18 21:20:07 -07:00
function showSyncButtonsPerUser(page) {
2015-05-18 15:23:03 -07:00
var apiClient = window.ApiClient;
2014-12-29 13:18:48 -07:00
2015-05-22 08:59:17 -07:00
if (!apiClient || !apiClient.getCurrentUserId()) {
2014-12-29 13:18:48 -07:00
return;
}
2014-12-18 21:20:07 -07:00
Dashboard.getCurrentUser().done(function (user) {
2015-06-15 21:52:01 -07:00
$('.categorySyncButton', page).visible(user.Policy.EnableSync);
2014-12-18 21:20:07 -07:00
});
}
function onCategorySyncButtonClick(page, button) {
var category = button.getAttribute('data-category');
var parentId = LibraryMenu.getTopParentId();
SyncManager.showMenu({
ParentId: parentId,
Category: category
});
}
2015-09-01 07:01:59 -07:00
$(document).on('pageinit', ".libraryPage", function () {
2014-12-18 21:20:07 -07:00
var page = this;
$('.categorySyncButton', page).on('click', function () {
onCategorySyncButtonClick(page, this);
});
2015-09-24 10:08:10 -07:00
}).on('pageshow', ".libraryPage", function () {
2014-12-18 21:20:07 -07:00
var page = this;
2015-05-05 08:24:47 -07:00
if (!Dashboard.isServerlessPage()) {
showSyncButtonsPerUser(page);
}
2014-12-18 21:20:07 -07:00
});
})(window, jQuery);