(function () { function cancelJob(page, id) { $('.jobMenu', page).on("popupafterclose.deleteuser", function () { $(this).off('popupafterclose.deleteuser'); var msg = Globalize.translate('CancelSyncJobConfirmation'); Dashboard.confirm(msg, Globalize.translate('HeaderCancelSyncJob'), function (result) { if (result) { Dashboard.showLoadingMsg(); ApiClient.ajax({ url: ApiClient.getUrl('Sync/Jobs/' + id), type: 'DELETE' }).done(function () { reloadData(page); }); } }); }).popup('close'); } function getSyncJobHtml(page, job, cardBoxCssClass, syncJobPage) { var html = ''; html += "
"; html += '
'; html += '"; html += '
'; var textLines = []; if (job.ParentName) { textLines.push(job.ParentName); } textLines.push(job.Name); if (job.ItemCount == 1) { textLines.push(Globalize.translate('ValueItemCount', job.ItemCount)); } else { textLines.push(Globalize.translate('ValueItemCountPlural', job.ItemCount)); } if (!job.ParentName) { textLines.push(' '); } html += '
'; html += ''; html += "
"; for (var i = 0, length = textLines.length; i < length; i++) { html += "
"; html += textLines[i]; html += "
"; } // cardFooter html += "
"; // cardBox html += "
"; // card html += "
"; return html; } function loadData(page, jobs) { var html = ''; var lastTargetName = ''; var cardBoxCssClass = 'cardBox visualCardBox'; var syncJobPage = 'syncjob.html'; if ($(page).hasClass('mySyncPage')) { syncJobPage = 'mysyncjob.html'; } for (var i = 0, length = jobs.length; i < length; i++) { var job = jobs[i]; var targetName = job.TargetName || 'Unknown'; if (targetName != lastTargetName) { if (lastTargetName) { html += '
'; html += '
'; html += '
'; } lastTargetName = targetName; html += '
'; html += '
' + targetName + '
'; html += '
'; } html += getSyncJobHtml(page, job, cardBoxCssClass, syncJobPage); } var elem = $('.syncActivity', page).html(html).trigger('create'); $(".lazy", elem).unveil(200); $('.btnJobMenu', elem).on('click', function () { showJobMenu(this); }); if (!jobs.length) { elem.html('
' + Globalize.translate('MessageNoSyncJobsFound') + '
'); } } function showJobMenu(elem) { var card = $(elem).parents('.card'); var page = $(elem).parents('.page'); var id = card.attr('data-id'); var status = card.attr('data-status'); $('.jobMenu', page).popup("close").remove(); var html = '
'; html += ''; html += '
'; page.append(html); var flyout = $('.jobMenu', page).popup({ positionTo: elem || "window" }).trigger('create').popup("open").on("popupafterclose", function () { $(this).off("popupafterclose").remove(); }); $('.btnCancelJob', flyout).on('click', function () { cancelJob(page, this.getAttribute('data-id')); }); } function reloadData(page) { Dashboard.showLoadingMsg(); var options = {}; Dashboard.getCurrentUser().done(function (user) { if ($(page).hasClass('mySyncPage')) { options.UserId = Dashboard.getCurrentUserId(); } ApiClient.getJSON(ApiClient.getUrl('Sync/Jobs', options)).done(function (response) { loadData(page, response.Items); Dashboard.hideLoadingMsg(); }); }); } function onWebSocketMessage(e, msg) { var page = $.mobile.activePage; if (msg.MessageType == "SyncJobs") { loadData(page, msg.Data); } } function startListening(page) { var startParams = "0,1500"; if ($(page).hasClass('mySyncPage')) { startParams += "," + Dashboard.getCurrentUserId(); } if (ApiClient.isWebSocketOpen()) { ApiClient.sendWebSocketMessage("SyncJobsStart", startParams); } } function stopListening() { if (ApiClient.isWebSocketOpen()) { ApiClient.sendWebSocketMessage("SyncJobsStop", ""); } } $(document).on('pageshow', ".syncActivityPage", function () { var page = this; Dashboard.getPluginSecurityInfo().done(function (pluginSecurityInfo) { if (pluginSecurityInfo.IsMBSupporter) { $('.syncPromotion', page).hide(); } else { $('.syncPromotion', page).show(); } }); reloadData(page); // on here $('.btnSync', page).taskButton({ mode: 'on', progressElem: $('.syncProgress', page), taskKey: 'SyncPrepare' }); startListening(page); $(ApiClient).on("websocketmessage.syncactivity", onWebSocketMessage); }).on('pagehide', ".syncActivityPage", function () { var page = this; // off here $('.btnSync', page).taskButton({ mode: 'off' }); stopListening(); $(ApiClient).off(".syncactivity"); }); })();