var DashboardPage = { onPageShow: function () { Dashboard.showLoadingMsg(); DashboardPage.pollForInfo(); DashboardPage.startInterval(); $(ApiClient).on("websocketmessage", DashboardPage.onWebSocketMessage).on("websocketopen", DashboardPage.onWebSocketConnectionChange).on("websocketerror", DashboardPage.onWebSocketConnectionChange).on("websocketclose", DashboardPage.onWebSocketConnectionChange); DashboardPage.lastAppUpdateCheck = null; DashboardPage.lastPluginUpdateCheck = null; }, onPageHide: function () { $(ApiClient).off("websocketmessage", DashboardPage.onWebSocketMessage).off("websocketopen", DashboardPage.onWebSocketConnectionChange).off("websocketerror", DashboardPage.onWebSocketConnectionChange).off("websocketclose", DashboardPage.onWebSocketConnectionChange); DashboardPage.stopInterval(); }, startInterval: function () { if (ApiClient.isWebSocketOpen()) { ApiClient.sendWebSocketMessage("DashboardInfoStart", "0,1500"); } }, stopInterval: function () { if (ApiClient.isWebSocketOpen()) { ApiClient.sendWebSocketMessage("DashboardInfoStop"); } }, onWebSocketMessage: function (e, msg) { if (msg.MessageType == "DashboardInfo") { DashboardPage.renderInfo(msg.Data); } }, onWebSocketConnectionChange: function () { DashboardPage.stopInterval(); DashboardPage.startInterval(); }, pollForInfo: function () { $.getJSON("dashboardInfo").done(DashboardPage.renderInfo); }, renderInfo: function (dashboardInfo) { DashboardPage.lastDashboardInfo = dashboardInfo; DashboardPage.renderRunningTasks(dashboardInfo); DashboardPage.renderSystemInfo(dashboardInfo); DashboardPage.renderActiveConnections(dashboardInfo); Dashboard.hideLoadingMsg(); }, renderActiveConnections: function (dashboardInfo) { var page = $.mobile.activePage; var html = ''; if (!dashboardInfo.ActiveConnections.length) { html += '

There are no users currently connected.

'; $('#divConnections', page).html(html).trigger('create'); return; } html += ''; for (var i = 0, length = dashboardInfo.ActiveConnections.length; i < length; i++) { var connection = dashboardInfo.ActiveConnections[i]; var user = dashboardInfo.Users.filter(function (u) { return u.Id == connection.UserId; })[0]; html += ''; html += ''; html += ''; html += ''; html += ''; html += ''; html += ''; } html += '
'; html += DashboardPage.getClientType(connection); html += ''; html += user.Name; html += ''; html += connection.DeviceName; html += ''; html += DashboardPage.getNowPlayingImage(connection.NowPlayingItem); html += ''; html += DashboardPage.getNowPlayingText(connection, connection.NowPlayingItem); html += '
'; $('#divConnections', page).html(html); }, getClientType: function (connection) { var clientLowered = connection.Client.toLowerCase(); if (clientLowered == "dashboard") { return "Dashboard"; } if (clientLowered == "media browser classic") { return "Media Browser Classic"; } if (clientLowered == "media browser theater") { return "Media Browser Theater"; } if (clientLowered == "android") { return "Android"; } if (clientLowered == "ios") { return "iOS"; } if (clientLowered == "windows rt") { return "Windows RT"; } if (clientLowered == "windows phone") { return "Windows Phone"; } if (clientLowered == "dlna") { return "Dlna"; } return connection.Client; }, getNowPlayingImage: function (item) { if (item) { if (item.BackdropImageTag) { var url = ApiClient.getImageUrl(item.Id, { type: "Backdrop", height: 100, tag: item.BackdropImageTag }); return "" + item.Name + ""; } else if (item.PrimaryImageTag) { var url = ApiClient.getImageUrl(item.Id, { type: "Primary", height: 100, tag: item.PrimaryImageTag }); return "" + item.Name + ""; } } return ""; }, getNowPlayingText: function (connection, item) { var html = ""; if (item) { html += "
" + item.Name + "
"; html += "
"; if (item.RunTimeTicks) { html += DashboardPage.getDisplayText(connection.NowPlayingPositionTicks || 0) + " / "; html += DashboardPage.getDisplayText(item.RunTimeTicks); } html += "
"; } return html; }, getDisplayText: function (ticks) { var ticksPerHour = 36000000000; var parts = []; var hours = ticks / ticksPerHour; hours = parseInt(hours); if (hours) { parts.push(hours); } ticks -= (hours * ticksPerHour); var ticksPerMinute = 600000000; var minutes = ticks / ticksPerMinute; minutes = parseInt(minutes); ticks -= (minutes * ticksPerMinute); if (minutes < 10) { minutes = '0' + minutes; } parts.push(minutes); var ticksPerSecond = 10000000; var seconds = ticks / ticksPerSecond; seconds = parseInt(seconds); if (seconds < 10) { seconds = '0' + seconds; } parts.push(seconds); return parts.join(':'); }, renderRunningTasks: function (dashboardInfo) { var page = $.mobile.activePage; var html = ''; if (!dashboardInfo.RunningTasks.length) { html += '

No tasks are currently running.

'; } for (var i = 0, length = dashboardInfo.RunningTasks.length; i < length; i++) { var task = dashboardInfo.RunningTasks[i]; html += '

'; html += task.Name; if (task.State == "Running") { var progress = Math.round(task.CurrentProgressPercentage || 0); html += ' - ' + progress + '%'; html += ''; } else if (task.State == "Cancelling") { html += ' - Stopping'; } html += '

'; } $('#divRunningTasks', page).html(html).trigger('create'); }, renderSystemInfo: function (dashboardInfo) { Dashboard.updateSystemInfo(dashboardInfo.SystemInfo); var page = $.mobile.activePage; $('#appVersionNumber', page).html(dashboardInfo.SystemInfo.Version); if (dashboardInfo.RunningTasks.filter(function (task) { return task.Id == dashboardInfo.ApplicationUpdateTaskId; }).length) { $('#btnUpdateApplication', page).button('disable'); } else { $('#btnUpdateApplication', page).button('enable'); } DashboardPage.renderApplicationUpdateInfo(dashboardInfo); DashboardPage.renderPluginUpdateInfo(dashboardInfo); DashboardPage.renderPendingInstallations(dashboardInfo.SystemInfo); }, renderApplicationUpdateInfo: function (dashboardInfo) { var page = $.mobile.activePage; if (dashboardInfo.SystemInfo.IsNetworkDeployed && !dashboardInfo.SystemInfo.HasPendingRestart) { // Only check once every 10 mins if (DashboardPage.lastAppUpdateCheck && (new Date().getTime() - DashboardPage.lastAppUpdateCheck) < 600000) { return; } DashboardPage.lastAppUpdateCheck = new Date().getTime(); ApiClient.getAvailableApplicationUpdate().done(function (packageInfo) { var version = packageInfo[0]; if (!version) { $('#pUpToDate', page).show(); $('#pUpdateNow', page).hide(); } else { $('#pUpToDate', page).hide(); $('#pUpdateNow', page).show(); $('#newVersionNumber', page).html("Version " + version.versionStr + " is now available for download."); } }).fail(function () { Dashboard.showFooterNotification({ html: 'There was an error connecting to the remote Media Browser repository.', id: "MB3ConnectionError" }); }); } else { if (dashboardInfo.SystemInfo.HasPendingRestart) { $('#pUpToDate', page).hide(); } else { $('#pUpToDate', page).show(); } $('#pUpdateNow', page).hide(); } }, renderPendingInstallations: function (systemInfo) { var page = $.mobile.activePage; if (systemInfo.CompletedInstallations.length) { $('#collapsiblePendingInstallations', page).show(); } else { $('#collapsiblePendingInstallations', page).hide(); return; } var html = ''; for (var i = 0, length = systemInfo.CompletedInstallations.length; i < length; i++) { var update = systemInfo.CompletedInstallations[i]; html += '
' + update.Name + ' (' + update.Version + ')
'; } $('#pendingInstallations', page).html(html); }, renderPluginUpdateInfo: function (dashboardInfo) { // Only check once every 10 mins if (DashboardPage.lastPluginUpdateCheck && (new Date().getTime() - DashboardPage.lastPluginUpdateCheck) < 600000) { return; } DashboardPage.lastPluginUpdateCheck = new Date().getTime(); var page = $.mobile.activePage; ApiClient.getAvailablePluginUpdates().done(function (updates) { var elem = $('#pPluginUpdates', page); if (updates.length) { elem.show(); } else { elem.hide(); return; } var html = ''; for (var i = 0, length = updates.length; i < length; i++) { var update = updates[i]; html += '

A new version of ' + update.name + ' is available!

'; html += ''; } elem.html(html).trigger('create'); }).fail(function () { Dashboard.showFooterNotification({ html: 'There was an error connecting to the remote Media Browser repository.', id: "MB3ConnectionError" }); }); }, installPluginUpdate: function (button) { $(button).button('disable'); var name = button.getAttribute('data-name'); var version = button.getAttribute('data-version'); var classification = button.getAttribute('data-classification'); Dashboard.showLoadingMsg(); ApiClient.installPlugin(name, classification, version).done(function () { Dashboard.hideLoadingMsg(); }); }, updateApplication: function () { var page = $.mobile.activePage; $('#btnUpdateApplication', page).button('disable'); Dashboard.showLoadingMsg(); ApiClient.startScheduledTask(DashboardPage.lastDashboardInfo.ApplicationUpdateTaskId).done(function () { DashboardPage.pollForInfo(); Dashboard.hideLoadingMsg(); }); }, stopTask: function (id) { ApiClient.stopScheduledTask(id).done(function () { DashboardPage.pollForInfo(); }); } }; $(document).on('pageshow', "#dashboardPage", DashboardPage.onPageShow).on('pagehide', "#dashboardPage", DashboardPage.onPageHide);