define(['connectionManager', 'serverNotifications', 'events', 'datetime', 'dom', 'imageLoader', 'loading', 'globalize', 'apphost', 'layoutManager', 'scrollHelper', 'dialogHelper', 'shell', 'listViewStyle', 'paper-icon-button-light', 'emby-button', 'formDialogStyle'], function (connectionManager, serverNotifications, events, datetime, dom, imageLoader, loading, globalize, appHost, layoutManager, scrollHelper, dialogHelper, shell) { 'use strict'; function renderJob(context, job, dialogOptions) { require(['syncDialog'], function (syncDialog) { syncDialog.renderForm({ elem: context.querySelector('.syncJobFormContent'), dialogOptions: dialogOptions, dialogOptionsFn: getTargetDialogOptionsFn(dialogOptions), showName: true, readOnlySyncTarget: true }).then(function () { fillJobValues(context, job, dialogOptions); }); }); } function getTargetDialogOptionsFn(dialogOptions) { return function (targetId) { return Promise.resolve(dialogOptions); }; } function getJobItemHtml(jobItem, apiClient, index) { var html = ''; html += '
'; var hasActions = ['Queued', 'Cancelled', 'Failed', 'ReadyToTransfer', 'Transferring', 'Converting', 'Synced'].indexOf(jobItem.Status) !== -1; var imgUrl; if (jobItem.PrimaryImageItemId) { imgUrl = apiClient.getImageUrl(jobItem.PrimaryImageItemId, { type: "Primary", width: 80, tag: jobItem.PrimaryImageTag, minScale: 1.5 }); } if (imgUrl) { html += ''; } else { html += ''; } html += '
'; html += '

'; html += jobItem.ItemName; html += '

'; if (jobItem.Status === 'Failed') { html += '
'; } else { html += '
'; } html += globalize.translate('sharedcomponents#SyncJobItemStatus' + jobItem.Status); if (jobItem.Status === 'Synced' && jobItem.IsMarkedForRemoval) { html += '
'; html += globalize.translate('sharedcomponents#RemovingFromDevice'); } html += '
'; html += '
'; html += '
'; html += '
'; html += '
'; var moreIcon = appHost.moreIcon === 'dots-horiz' ? '' : ''; if (hasActions) { html += ''; } else { html += ''; } html += '
'; return html; } function renderJobItems(context, items, apiClient) { var html = ''; html += '

' + globalize.translate('sharedcomponents#Items') + '

'; html += '
'; var index = 0; html += items.map(function (i) { return getJobItemHtml(i, apiClient, index++); }).join(''); html += '
'; var elem = context.querySelector('.jobItems'); elem.innerHTML = html; imageLoader.lazyChildren(elem); } function parentWithClass(elem, className) { while (!elem.classList || !elem.classList.contains(className)) { elem = elem.parentNode; if (!elem) { return null; } } return elem; } function showJobItemMenu(elem, jobId, apiClient) { var context = parentWithClass(elem, 'page'); var listItem = parentWithClass(elem, 'listItem'); var jobItemId = listItem.getAttribute('data-itemid'); var status = listItem.getAttribute('data-status'); var remove = listItem.getAttribute('data-remove').toLowerCase() === 'true'; var menuItems = []; if (status === 'Failed' || status === 'Cancelled') { menuItems.push({ name: globalize.translate('sharedcomponents#Retry'), id: 'retry' }); } else if (status === 'Queued' || status === 'Transferring' || status === 'Converting' || status === 'ReadyToTransfer') { menuItems.push({ name: globalize.translate('sharedcomponents#CancelDownload'), id: 'cancel' }); } else if (status === 'Synced' && remove) { menuItems.push({ name: globalize.translate('sharedcomponents#KeepOnDevice'), id: 'unmarkforremoval' }); } else if (status === 'Synced') { menuItems.push({ name: globalize.translate('sharedcomponents#RemoveFromDevice'), id: 'markforremoval' }); } require(['actionsheet'], function (actionsheet) { actionsheet.show({ items: menuItems, positionTo: elem, callback: function (id) { switch (id) { case 'cancel': cancelJobItem(context, jobId, jobItemId, apiClient); break; case 'retry': retryJobItem(context, jobId, jobItemId, apiClient); break; case 'markforremoval': markForRemoval(context, jobId, jobItemId, apiClient); break; case 'unmarkforremoval': unMarkForRemoval(context, jobId, jobItemId, apiClient); break; default: break; } } }); }); } function cancelJobItem(context, jobId, jobItemId, apiClient) { // Need a timeout because jquery mobile will not show a popup while another is in the act of closing loading.show(); apiClient.ajax({ type: "DELETE", url: apiClient.getUrl('Sync/JobItems/' + jobItemId) }).then(function () { loadJob(context, jobId, apiClient); }); } function markForRemoval(context, jobId, jobItemId, apiClient) { apiClient.ajax({ type: "POST", url: apiClient.getUrl('Sync/JobItems/' + jobItemId + '/MarkForRemoval') }).then(function () { loadJob(context, jobId, apiClient); }); } function unMarkForRemoval(context, jobId, jobItemId, apiClient) { apiClient.ajax({ type: "POST", url: apiClient.getUrl('Sync/JobItems/' + jobItemId + '/UnmarkForRemoval') }).then(function () { loadJob(context, jobId, apiClient); }); } function retryJobItem(context, jobId, jobItemId, apiClient) { apiClient.ajax({ type: "POST", url: apiClient.getUrl('Sync/JobItems/' + jobItemId + '/Enable') }).then(function () { loadJob(context, jobId, apiClient); }); } function fillJobValues(context, job, editOptions) { var txtSyncJobName = context.querySelector('#txtSyncJobName'); if (txtSyncJobName) { txtSyncJobName.value = job.Name; } var selectProfile = context.querySelector('#selectProfile'); if (selectProfile) { selectProfile.value = job.Profile || ''; } var selectQuality = context.querySelector('#selectQuality'); if (selectQuality) { selectQuality.value = job.Quality || ''; } var chkUnwatchedOnly = context.querySelector('#chkUnwatchedOnly'); if (chkUnwatchedOnly) { chkUnwatchedOnly.checked = job.UnwatchedOnly; } var chkSyncNewContent = context.querySelector('#chkSyncNewContent'); if (chkSyncNewContent) { chkSyncNewContent.checked = job.SyncNewContent; } var txtItemLimit = context.querySelector('#txtItemLimit'); if (txtItemLimit) { txtItemLimit.value = job.ItemLimit; } var txtBitrate = context.querySelector('#txtBitrate'); if (job.Bitrate) { txtBitrate.value = job.Bitrate / 1000000; } else { txtBitrate.value = ''; } var target = editOptions.Targets.filter(function (t) { return t.Id === job.TargetId; })[0]; var targetName = target ? target.Name : ''; var selectSyncTarget = context.querySelector('#selectSyncTarget'); if (selectSyncTarget) { selectSyncTarget.value = targetName; } } var _jobOptions; function loadJob(context, id, apiClient) { loading.show(); apiClient.getJSON(apiClient.getUrl('Sync/Jobs/' + id)).then(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, TargetId: job.TargetId })).then(function (options) { _jobOptions = options; renderJob(context, job, options); loading.hide(); }); }); apiClient.getJSON(apiClient.getUrl('Sync/JobItems', { JobId: id, AddMetadata: true })).then(function (result) { renderJobItems(context, result.Items, apiClient); loading.hide(); }); } function loadJobInfo(context, job, jobItems, apiClient) { //renderJob(page, job, _jobOptions); renderJobItems(context, jobItems, apiClient); loading.hide(); } function saveJob(context, id, apiClient) { loading.show(); apiClient.getJSON(apiClient.getUrl('Sync/Jobs/' + id)).then(function (job) { require(['syncDialog'], function (syncDialog) { syncDialog.setJobValues(job, context); apiClient.ajax({ url: apiClient.getUrl('Sync/Jobs/' + id), type: 'POST', data: JSON.stringify(job), contentType: "application/json" }).then(function () { loading.hide(); dialogHelper.close(context); }); }); }); } function onHelpLinkClick(e) { shell.openUrl(this.href); e.preventDefault(); return false; } function startListening(apiClient, jobId) { var startParams = "0,1500"; startParams += "," + jobId; if (apiClient.isWebSocketOpen()) { apiClient.sendWebSocketMessage("SyncJobStart", startParams); } } function stopListening(apiClient) { if (apiClient.isWebSocketOpen()) { apiClient.sendWebSocketMessage("SyncJobStop", ""); } } function bindEvents(context, jobId, apiClient) { context.querySelector('.jobItems').addEventListener('click', function (e) { var btnJobItemMenu = dom.parentWithClass(e.target, 'btnJobItemMenu'); if (btnJobItemMenu) { showJobItemMenu(btnJobItemMenu, jobId, apiClient); } }); } function showEditor(options) { var apiClient = connectionManager.getApiClient(options.serverId); var id = options.jobId; var dlgElementOptions = { removeOnClose: true, scrollY: false, autoFocus: false }; if (layoutManager.tv) { dlgElementOptions.size = 'fullscreen'; } else { dlgElementOptions.size = 'medium'; } var dlg = dialogHelper.createDialog(dlgElementOptions); dlg.classList.add('formDialog'); var html = ''; html += '
'; html += ''; html += '

'; html += globalize.translate('sharedcomponents#Sync'); html += '

'; html += ''; html += '
'; html += '
'; html += '
'; html += '
'; html += '
'; html += '
'; html += '
'; html += ''; html += '
'; html += '
'; html += '
'; html += '
'; dlg.innerHTML = html; dlg.querySelector('.lnkHelp').addEventListener('click', onHelpLinkClick); var submitted = false; dlg.querySelector('form').addEventListener('submit', function (e) { saveJob(dlg, id, apiClient); e.preventDefault(); return false; }); dlg.querySelector('.btnCancel').addEventListener('click', function () { dialogHelper.close(dlg); }); if (layoutManager.tv) { scrollHelper.centerFocus.on(dlg.querySelector('.formDialogContent'), false); } function onSyncJobMessage(e, apiClient, msg) { loadJobInfo(dlg, msg.Job, msg.JobItems, apiClient); } loadJob(dlg, id, apiClient); bindEvents(dlg, id, apiClient); var promise = dialogHelper.open(dlg); startListening(apiClient, id); events.on(serverNotifications, "SyncJob", onSyncJobMessage); return promise.then(function () { stopListening(apiClient); events.off(serverNotifications, "SyncJob", onSyncJobMessage); if (layoutManager.tv) { scrollHelper.centerFocus.off(dlg.querySelector('.formDialogContent'), false); } if (submitted) { return Promise.resolve(); } return Promise.reject(); }); } return { show: showEditor }; });