define([], function () { var currentDialogOptions; function submitJob(dlg, userId, syncOptions, form, paperDialogHelper) { 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, ParentId: syncOptions.ParentId, Category: syncOptions.Category }; setJobValues(options, form); 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", dataType: 'json' }).then(function () { paperDialogHelper.close(dlg); Dashboard.alert(Globalize.translate('MessageSyncJobCreated')); }); } 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(); } function renderForm(options) { return new Promise(function (resolve, reject) { require(['paper-checkbox', 'paper-input', 'jqmcollapsible'], function () { renderFormInternal(options); resolve(); }); }); } function renderFormInternal(options) { var elem = options.elem; var dialogOptions = options.dialogOptions; var targets = dialogOptions.Targets; var html = ''; if (options.showName || dialogOptions.Options.indexOf('Name') != -1) { html += '
'; html += ''; html += '
'; html += '
'; } html += '
'; if (options.readOnlySyncTarget) { html += ''; } else { html += ''; html += ''; if (!targets.length) { html += '
' + Globalize.translate('LabelSyncNoTargetsHelp') + '
'; html += '
' + Globalize.translate('ButtonLearnMore') + '
'; } } html += '
'; html += ''; html += ''; html += ''; if (dialogOptions.Options.indexOf('UnwatchedOnly') != -1) { html += '
'; html += '
'; html += '' + Globalize.translate('OptionSyncUnwatchedVideosOnly') + ''; html += '
' + Globalize.translate('OptionSyncUnwatchedVideosOnlyHelp') + '
'; html += '
'; } if (dialogOptions.Options.indexOf('SyncNewContent') != -1 || dialogOptions.Options.indexOf('ItemLimit') != -1) { html += '
'; html += '
'; html += '

' + Globalize.translate('HeaderAdvanced') + '

'; html += '
'; if (dialogOptions.Options.indexOf('SyncNewContent') != -1) { html += '
'; html += '
'; html += '' + Globalize.translate('OptionAutomaticallySyncNewContent') + ''; html += '
' + Globalize.translate('OptionAutomaticallySyncNewContentHelp') + '
'; html += '
'; } if (dialogOptions.Options.indexOf('ItemLimit') != -1) { html += '
'; html += ''; html += '
' + Globalize.translate('LabelItemLimitHelp') + '
'; html += '
'; } html += '
'; html += '
'; } //html += ''; //html += ''; $(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'); } function showSyncMenu(options) { requirejs(["registrationservices"], function () { RegistrationServices.validateFeature('sync').then(function () { showSyncMenuInternal(options); }); }); } function showSyncMenuInternal(options) { require(['paperdialoghelper', 'paper-fab'], function (paperDialogHelper) { var userId = Dashboard.getCurrentUserId(); var dialogOptionsQuery = { UserId: userId, ItemIds: (options.items || []).map(function (i) { return i.Id || i; }).join(','), ParentId: options.ParentId, Category: options.Category }; ApiClient.getJSON(ApiClient.getUrl('Sync/Options', dialogOptionsQuery)).then(function (dialogOptions) { currentDialogOptions = dialogOptions; var dlg = paperDialogHelper.createDialog({ size: 'small', removeOnClose: true, autoFocus: false }); dlg.classList.add('ui-body-a'); dlg.classList.add('background-theme-a'); dlg.classList.add('popupEditor'); var html = ''; html += '
'; html += ''; html += '
'; html += Globalize.translate('SyncMedia'); html += '
'; html += '' + Globalize.translate('ButtonHelp') + ''; html += '
'; html += '
'; html += '
'; html += '

'; html += ''; html += '

'; html += '
'; dlg.innerHTML = html; document.body.appendChild(dlg); paperDialogHelper.open(dlg); $('form', dlg).on('submit', function () { submitJob(dlg, userId, options, this, paperDialogHelper); return false; }); $('.btnCancel', dlg).on('click', function () { paperDialogHelper.close(dlg); }); renderForm({ elem: $('.formFields', dlg), dialogOptions: dialogOptions, dialogOptionsFn: getTargetDialogOptionsFn(dialogOptionsQuery) }); }); }); } function getTargetDialogOptionsFn(query) { return function (targetId) { query.TargetId = targetId; return ApiClient.getJSON(ApiClient.getUrl('Sync/Options', query)); }; } function setQualityFieldVisible(form, visible) { if (visible) { $('.fldQuality', form).show(); $('#selectQuality', form).attr('required', 'required'); } else { $('.fldQuality', form).hide(); $('#selectQuality', form).removeAttr('required'); } } 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 || ''); setQualityFieldVisible(form, options.QualityOptions.length > 0 && option.EnableQualityOptions && options.Options.indexOf('Quality') != -1); } else { $('.profileDescription', form).html(''); setQualityFieldVisible(form, options.QualityOptions.length > 0 && options.Options.indexOf('Quality') != -1); } } function onQualityChange(form, qualityId) { var options = currentDialogOptions || {}; var option = (options.QualityOptions || []).filter(function (o) { return o.Id == qualityId; })[0]; if (option) { $('.qualityDescription', form).html(option.Description || ''); } else { $('.qualityDescription', form).html(''); } if (qualityId == 'custom') { $('.fldBitrate', form).show(); $('#txtBitrate', form).attr('required', 'required'); } else { $('.fldBitrate', form).hide(); $('#txtBitrate', form).removeAttr('required').val(''); } } function renderTargetDialogOptions(form, options) { currentDialogOptions = options; if (options.ProfileOptions.length && options.Options.indexOf('Profile') != -1) { $('.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 ''; }).join('')).trigger('change'); $('#selectQuality', form).html(options.QualityOptions.map(function (o) { var selectedAttribute = o.IsDefault ? ' selected="selected"' : ''; return ''; }).join('')).trigger('change'); } function loadQualityOptions(form, targetId, dialogOptionsFn) { dialogOptionsFn(targetId).then(function (options) { renderTargetDialogOptions(form, options); }); } return { showMenu: showSyncMenu, renderForm: renderForm, setJobValues: setJobValues }; });