(function ($, document, window) { var query = { StartIndex: 0, Limit: 20 }; var currentResult; function showStatusMessage(id) { var item = currentResult.Items.filter(function (i) { return i.Id == id; })[0]; Dashboard.alert({ title: getStatusText(item, false), message: item.StatusMessage }); } function deleteOriginalFile(page, id) { var item = currentResult.Items.filter(function (i) { return i.Id == id; })[0]; var message = Globalize.translate('MessageFileWillBeDeleted') + '

' + item.OriginalPath + '

' + Globalize.translate('MessageSureYouWishToProceed') + '

'; Dashboard.confirm(message, Globalize.translate('HeaderDeleteFile'), function (confirmResult) { if (confirmResult) { Dashboard.showLoadingMsg(); ApiClient.deleteOriginalFileFromOrganizationResult(id).done(function () { Dashboard.hideLoadingMsg(); reloadItems(page); }); } }); } function organizeEpsiodeWithCorrections(page, item) { Dashboard.showLoadingMsg(); ApiClient.getItems(null, { recursive: true, includeItemTypes: 'Series', sortBy: 'SortName' }).done(function (result) { Dashboard.hideLoadingMsg(); showEpisodeCorrectionPopup(page, item, result.Items); }); } function showEpisodeCorrectionPopup(page, item, allSeries) { var popup = $('.episodeCorrectionPopup', page).popup("open"); $('.inputFile', popup).html(item.OriginalFileName); $('#txtSeason', popup).val(item.ExtractedSeasonNumber); $('#txtEpisode', popup).val(item.ExtractedEpisodeNumber); $('#txtEndingEpisode', popup).val(item.ExtractedEndingEpisodeNumber); $('#hfResultId', popup).val(item.Id); var seriesHtml = allSeries.map(function (s) { return ''; }).join(''); seriesHtml = '' + seriesHtml; $('#selectSeries', popup).html(seriesHtml).selectmenu('refresh'); } function organizeFile(page, id) { var item = currentResult.Items.filter(function (i) { return i.Id == id; })[0]; if (!item.TargetPath) { if (item.Type == "Episode") { organizeEpsiodeWithCorrections(page, item); } return; } var message = Globalize.translate('MessageFollowingFileWillBeMovedFrom') + '

' + item.OriginalPath + '

' + Globalize.translate('MessageDestinationTo') + '

' + item.TargetPath + '

'; if (item.DuplicatePaths.length) { message += '

' + Globalize.translate('MessageDuplicatesWillBeDeleted') + '

'; message += '

' + item.DuplicatePaths.join('
') + '

'; } message += '

' + Globalize.translate('MessageSureYouWishToProceed') + '

'; Dashboard.confirm(message, Globalize.translate('HeaderOrganizeFile'), function (confirmResult) { if (confirmResult) { Dashboard.showLoadingMsg(); ApiClient.performOrganization(id).done(function () { Dashboard.hideLoadingMsg(); reloadItems(page); }); } }); } function submitEpisodeForm(form) { Dashboard.showLoadingMsg(); var page = $(form).parents('.page'); var resultId = $('#hfResultId', form).val(); var options = { SeriesId: $('#selectSeries', form).val(), SeasonNumber: $('#txtSeason', form).val(), EpisodeNumber: $('#txtEpisode', form).val(), EndingEpisodeNumber: $('#txtEndingEpisode', form).val() }; ApiClient.performEpisodeOrganization(resultId, options).done(function () { Dashboard.hideLoadingMsg(); $('.episodeCorrectionPopup', page).popup("close"); reloadItems(page); }); } function reloadItems(page) { Dashboard.showLoadingMsg(); ApiClient.getFileOrganizationResults(query).done(function (result) { currentResult = result; renderResults(page, result); Dashboard.hideLoadingMsg(); }); } function getStatusText(item, enhance) { var status = item.Status; var color = null; if (status == 'SkippedExisting') { status = Globalize.translate('StatusSkipped'); } else if (status == 'Failure') { color = '#cc0000'; status = Globalize.translate('StatusFailed'); } if (status == 'Success') { color = 'green'; status = Globalize.translate('StatusSuccess'); } if (enhance) { if (item.StatusMessage) { return '' + status + ''; } else { return '' + status + ''; } } return status; } function renderResults(page, result) { var rows = result.Items.map(function (item) { var html = ''; html += ''; html += ''; if (item.Status != 'Success') { html += ''; html += ''; } html += ''; html += ''; var date = parseISO8601Date(item.Date, { toLocal: true }); html += date.toLocaleDateString(); html += ''; html += ''; var status = item.Status; if (status == 'SkippedExisting') { html += '
'; html += item.OriginalFileName; html += '
'; } else if (status == 'Failure') { html += ''; html += item.OriginalFileName; html += ''; } else { html += '
'; html += item.OriginalFileName; html += '
'; } html += ''; html += ''; html += item.TargetPath || ''; html += ''; html += ''; return html; }).join(''); var elem = $('.resultBody', page).html(rows).parents('.tblOrganizationResults').table("refresh").trigger('create'); $('.btnShowStatusMessage', elem).on('click', function () { var id = this.getAttribute('data-resultid'); showStatusMessage(id); }); $('.btnProcessResult', elem).on('click', function () { var id = this.getAttribute('data-resultid'); organizeFile(page, id); }); $('.btnDeleteResult', elem).on('click', function () { var id = this.getAttribute('data-resultid'); deleteOriginalFile(page, id); }); var pagingHtml = LibraryBrowser.getQueryPagingHtml({ startIndex: query.StartIndex, limit: query.Limit, totalRecordCount: result.TotalRecordCount, showLimit: false, updatePageSizeSetting: false }); $(page)[0].querySelector('.listTopPaging').innerHTML = pagingHtml; if (result.TotalRecordCount > query.Limit && result.TotalRecordCount > 50) { $('.listBottomPaging', page).html(pagingHtml).trigger('create'); } else { $('.listBottomPaging', page).empty(); } $('.btnNextPage', page).on('click', function () { query.StartIndex += query.Limit; reloadItems(page); }); $('.btnPreviousPage', page).on('click', function () { query.StartIndex -= query.Limit; reloadItems(page); }); if (result.TotalRecordCount) { $('.btnClearLog', page).show(); } else { $('.btnClearLog', page).hide(); } } function onWebSocketMessage(e, msg) { var page = $.mobile.activePage; if (msg.MessageType == "ScheduledTaskEnded") { var result = msg.Data; if (result.Key == 'AutoOrganize') { reloadItems(page); } } } function onEpisodeCorrectionFormSubmit() { submitEpisodeForm(this); return false; } $(document).on('pageinitdepends', "#libraryFileOrganizerLogPage", function () { var page = this; $('.btnClearLog', page).on('click', function () { ApiClient.clearOrganizationLog().done(function () { reloadItems(page); }); }); $('.episodeCorrectionForm').off('submit', onEpisodeCorrectionFormSubmit).on('submit', onEpisodeCorrectionFormSubmit); }).on('pageshowready', "#libraryFileOrganizerLogPage", function () { var page = this; reloadItems(page); // on here $('.btnOrganize', page).taskButton({ mode: 'on', progressElem: $('.organizeProgress', page), panel: $('.organizeTaskPanel', page), taskKey: 'AutoOrganize' }); $(ApiClient).on("websocketmessage.autoorganizelog", onWebSocketMessage); }).on('pagebeforehide', "#libraryFileOrganizerLogPage", function () { var page = this; currentResult = null; // off here $('.btnOrganize', page).taskButton({ mode: 'off' }); $(ApiClient).off("websocketmessage.autoorganizelog", onWebSocketMessage); }); })(jQuery, document, window);