(function () { function cancelJob(page, id) { 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); }); } }); } 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).lazyChildren(); Events.trigger(elem[0], 'create'); $('.btnJobMenu', elem).on('click', function () { showJobMenu(page, this); }); if (!jobs.length) { elem.html('
' + Globalize.translate('MessageNoSyncJobsFound') + '
'); } } function showJobMenu(page, elem) { var card = $(elem).parents('.card'); var jobId = card.attr('data-id'); var status = card.attr('data-status'); var menuItems = []; if (status == 'Cancelled') { menuItems.push({ name: Globalize.translate('ButtonDelete'), id: 'delete', ironIcon: 'delete' }); } else { menuItems.push({ name: Globalize.translate('ButtonCancelSyncJob'), id: 'cancel', ironIcon: 'delete' }); } require(['actionsheet'], function () { ActionSheetElement.show({ items: menuItems, positionTo: elem, callback: function (id) { switch (id) { case 'delete': cancelJob(page, jobId); break; case 'cancel': cancelJob(page, jobId); break; default: break; } } }); }); } 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('pageshowready', ".syncActivityPage", function () { var page = this; Dashboard.getPluginSecurityInfo().done(function (pluginSecurityInfo) { if (pluginSecurityInfo.IsMBSupporter || !AppInfo.enableSupporterMembership) { $('.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", onWebSocketMessage); }).on('pagebeforehide', ".syncActivityPage", function () { var page = this; // off here $('.btnSync', page).taskButton({ mode: 'off' }); stopListening(); $(ApiClient).off("websocketmessage", onWebSocketMessage); }); })();