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

408 lines
14 KiB
JavaScript
Raw Normal View History

2016-05-07 12:41:10 -07:00
define(['apphost', 'jQuery', 'paper-icon-button-light'], function (appHost, $) {
2015-03-14 21:17:35 -07:00
var currentDialogOptions;
2016-03-23 12:03:17 -07:00
function submitJob(dlg, userId, syncOptions, form, dialogHelper) {
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
2016-02-24 23:38:12 -07:00
require(['toast'], function (toast) {
toast(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
2015-12-14 08:43:03 -07:00
}).then(function () {
2014-07-22 09:36:34 -07:00
2016-03-23 12:03:17 -07:00
dialogHelper.close(dlg);
2016-02-24 23:38:12 -07:00
require(['toast'], function (toast) {
toast(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) {
2015-12-14 08:43:03 -07:00
return new Promise(function (resolve, reject) {
2016-06-11 08:56:15 -07:00
require(['emby-checkbox', 'emby-input', 'emby-collapsible'], function () {
2016-04-18 11:50:29 -07:00
appHost.appInfo().then(function (appInfo) {
renderFormInternal(options, appInfo, resolve);
});
2015-12-14 08:43:03 -07:00
});
});
}
2016-04-18 11:50:29 -07:00
function renderFormInternal(options, appInfo, resolve) {
2015-12-14 08:43:03 -07:00
2015-03-14 21:17:35 -07:00
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
2016-06-11 08:56:15 -07:00
html += '<div class="inputContainer">';
html += '<input is="emby-input" type="text" id="txtSyncJobName" class="txtSyncJobName" required="required" label="' + Globalize.translate('LabelSyncJobName') + '"/>';
2015-08-17 09:52:56 -07:00
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) {
2016-06-11 08:56:15 -07:00
html += '<div class="inputContainer">';
html += '<input is="emby-input" type="text" id="selectSyncTarget" readonly label="' + Globalize.translate('LabelSyncTo') + '"/>';
html += '</div>';
2015-03-14 21:39:29 -07:00
} else {
2016-02-08 14:15:31 -07:00
html += '<label for="selectSyncTarget" class="selectLabel">' + 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
2016-04-18 11:50:29 -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/>';
2016-02-08 14:15:31 -07:00
html += '<label for="selectProfile" class="selectLabel">' + Globalize.translate('LabelProfile') + '</label>';
2015-03-14 21:17:35 -07:00
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/>';
2016-02-08 14:15:31 -07:00
html += '<label for="selectQuality" class="selectLabel">' + Globalize.translate('LabelQuality') + '</label>';
2015-03-14 21:17:35 -07:00
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/>';
2016-06-11 08:56:15 -07:00
html += '<div class="inputContainer">';
html += '<input is="emby-input" type="number" step=".1" min=".1" id="txtBitrate" label="' + Globalize.translate('LabelBitrateMbps') + '"/>';
2015-03-14 21:17:35 -07:00
html += '</div>';
html += '</div>';
if (dialogOptions.Options.indexOf('UnwatchedOnly') != -1) {
html += '<br/>';
2016-06-11 08:56:15 -07:00
html += '<div class="checkboxContainer">';
html += '<label>';
html += '<input is="emby-checkbox" type="checkbox" id="chkUnwatchedOnly"/>';
html += '<span>' + Globalize.translate('OptionSyncUnwatchedVideosOnly') + '</span>';
html += '</label>';
html += '<div class="fieldDescription checkboxFieldDescription">' + 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) {
2016-03-01 20:27:33 -07:00
html += '<emby-collapsible title="' + Globalize.translate('HeaderAdvanced') + '">';
html += '<div style="padding:0 0 1em;">';
if (dialogOptions.Options.indexOf('SyncNewContent') != -1) {
html += '<br/>';
2016-06-11 08:56:15 -07:00
html += '<div class="checkboxContainer">';
html += '<label>';
html += '<input is="emby-checkbox" type="checkbox" id="chkSyncNewContent"/>';
html += '<span>' + Globalize.translate('OptionAutomaticallySyncNewContent') + '</span>';
html += '</label>';
html += '<div class="fieldDescription checkboxFieldDescription">' + Globalize.translate('OptionAutomaticallySyncNewContentHelp') + '</div>';
html += '</div>';
}
if (dialogOptions.Options.indexOf('ItemLimit') != -1) {
2016-06-11 08:56:15 -07:00
html += '<div class="inputContainer">';
html += '<input is="emby-input" type="number" step="1" min="1" id="txtItemLimit" label="' + Globalize.translate('LabelItemLimit') + '"/>';
html += '<div class="fieldDescription">' + Globalize.translate('LabelItemLimitHelp') + '</div>';
html += '</div>';
}
2015-03-14 21:17:35 -07:00
html += '</div>';
2016-06-11 08:56:15 -07:00
html += '</emby-collapsible>';
html += '<br/>';
2015-03-14 21:17:35 -07:00
}
//html += '</div>';
//html += '</div>';
2016-03-01 20:27:33 -07:00
$(elem).html(html);
2015-03-14 21:17:35 -07:00
$('#selectSyncTarget', elem).on('change', function () {
loadQualityOptions(elem, this.value, options.dialogOptionsFn).then(resolve);
2015-03-14 21:17:35 -07:00
}).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-12-14 08:43:03 -07:00
requirejs(["registrationservices"], function () {
RegistrationServices.validateFeature('sync').then(function () {
2015-07-29 19:08:35 -07:00
showSyncMenuInternal(options);
});
});
}
function showSyncMenuInternal(options) {
2016-06-21 21:39:47 -07:00
require(['dialogHelper'], function (dialogHelper) {
2014-07-22 09:36:34 -07:00
2015-12-14 08:43:03 -07:00
var userId = Dashboard.getCurrentUserId();
2014-12-18 21:20:07 -07:00
2015-12-14 08:43:03 -07:00
var dialogOptionsQuery = {
UserId: userId,
ItemIds: (options.items || []).map(function (i) {
return i.Id || i;
}).join(','),
2014-12-16 22:30:31 -07:00
2015-12-14 08:43:03 -07:00
ParentId: options.ParentId,
Category: options.Category
};
2014-07-22 09:36:34 -07:00
2015-12-14 08:43:03 -07:00
ApiClient.getJSON(ApiClient.getUrl('Sync/Options', dialogOptionsQuery)).then(function (dialogOptions) {
2014-07-22 09:36:34 -07:00
2015-12-14 08:43:03 -07:00
currentDialogOptions = dialogOptions;
2014-07-22 09:36:34 -07:00
2016-03-23 12:03:17 -07:00
var dlg = dialogHelper.createDialog({
2015-12-14 08:43:03 -07:00
size: 'small',
2016-01-30 13:59:09 -07:00
removeOnClose: true,
autoFocus: false
2015-12-14 08:43:03 -07:00
});
2015-02-04 23:13:20 -07:00
2016-01-30 13:59:09 -07:00
dlg.classList.add('ui-body-a');
dlg.classList.add('background-theme-a');
dlg.classList.add('popupEditor');
2015-12-14 08:43:03 -07:00
var html = '';
2016-05-13 11:28:05 -07:00
html += '<div class="dialogHeader" style="margin:0 0 2em;">';
2016-06-18 22:26:52 -07:00
html += '<button is="paper-icon-button-light" class="btnCancel autoSize" tabindex="-1"><i class="md-icon">arrow_back</i></button>';
2016-02-10 19:56:24 -07:00
html += '<div class="dialogHeaderTitle">';
html += Globalize.translate('SyncMedia');
html += '</div>';
2015-12-14 08:43:03 -07:00
2016-06-18 22:26:52 -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:auto;"><button is="emby-button" type="button" class="mini"><i class="md-icon">info</i><span>' + Globalize.translate('ButtonHelp') + '</span></button></a>';
2014-07-22 09:36:34 -07:00
2015-12-14 08:43:03 -07:00
html += '</div>';
2014-12-13 14:26:04 -07:00
2016-02-10 19:56:24 -07:00
html += '<form class="formSubmitSyncRequest" style="margin: auto;">';
2015-12-14 08:43:03 -07:00
html += '<div class="formFields"></div>';
2014-07-22 09:36:34 -07:00
2015-12-14 08:43:03 -07:00
html += '<p>';
2016-06-18 22:26:52 -07:00
html += '<button is="emby-button" type="submit" class="raised submit block"><i class="md-icon">sync</i><span>' + Globalize.translate('ButtonSync') + '</span></button>';
2015-12-14 08:43:03 -07:00
html += '</p>';
2014-07-22 09:36:34 -07:00
2015-12-14 08:43:03 -07:00
html += '</form>';
2014-07-22 09:36:34 -07:00
2015-12-14 08:43:03 -07:00
dlg.innerHTML = html;
document.body.appendChild(dlg);
2014-07-22 09:36:34 -07:00
2016-03-23 12:03:17 -07:00
dialogHelper.open(dlg);
2015-12-14 08:43:03 -07:00
$('form', dlg).on('submit', function () {
2015-03-14 21:17:35 -07:00
2016-03-23 12:03:17 -07:00
submitJob(dlg, userId, options, this, dialogHelper);
2015-12-14 08:43:03 -07:00
return false;
});
$('.btnCancel', dlg).on('click', function () {
2016-03-23 12:03:17 -07:00
dialogHelper.close(dlg);
2015-12-14 08:43:03 -07:00
});
renderForm({
elem: $('.formFields', dlg),
dialogOptions: dialogOptions,
dialogOptionsFn: getTargetDialogOptionsFn(dialogOptionsQuery)
});
2015-03-14 21:17:35 -07:00
});
2015-06-17 23:23:44 -07:00
2015-12-14 08:43:03 -07:00
});
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));
};
}
2016-02-17 21:57:19 -07:00
function setQualityFieldVisible(form, visible) {
if (visible) {
$('.fldQuality', form).show();
$('#selectQuality', form).attr('required', 'required');
} else {
$('.fldQuality', form).hide();
$('#selectQuality', form).removeAttr('required');
}
}
2015-03-14 21:17:35 -07:00
function onProfileChange(form, profileId) {
var options = currentDialogOptions || {};
var option = (options.ProfileOptions || []).filter(function (o) {
return o.Id == profileId;
})[0];
2016-04-15 21:35:09 -07:00
var qualityOptions = options.QualityOptions || [];
2015-03-14 21:17:35 -07:00
if (option) {
$('.profileDescription', form).html(option.Description || '');
2016-04-15 21:35:09 -07:00
setQualityFieldVisible(form, qualityOptions.length > 0 && option.EnableQualityOptions && options.Options.indexOf('Quality') != -1);
2015-03-14 21:17:35 -07:00
} else {
$('.profileDescription', form).html('');
2016-04-15 21:35:09 -07:00
setQualityFieldVisible(form, 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 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');
}
2016-02-17 21:57:19 -07:00
function loadQualityOptions(form, targetId, dialogOptionsFn) {
2015-10-05 19:50:20 -07:00
return dialogOptionsFn(targetId).then(function (options) {
2016-02-03 17:04:59 -07:00
return renderTargetDialogOptions(form, options);
2016-02-17 21:57:19 -07:00
});
}
2016-02-17 21:57:19 -07:00
return {
2014-07-19 21:46:29 -07:00
showMenu: showSyncMenu,
2015-03-15 12:10:27 -07:00
renderForm: renderForm,
setJobValues: setJobValues
};
2016-02-17 21:57:19 -07:00
});