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; Dashboard.getPluginSecurityInfo().done(function (pluginSecurityInfo) { if (pluginSecurityInfo.IsMBSupporter) { $('#contribute').hide(); } else { $('#contribute').show(); } }); }, 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 = ''; var table = $('.tblConnections', page); $('.trSession', table).addClass('deadSession'); for (var i = 0, length = dashboardInfo.ActiveConnections.length; i < length; i++) { var connection = dashboardInfo.ActiveConnections[i]; var rowId = 'trSession' + connection.Id; var elem = $('#' + rowId, page); if (elem.length) { DashboardPage.updateSession(elem, connection); continue; } html += ''; html += ''; html += DashboardPage.getClientType(connection); html += ''; html += ''; html += '
' + connection.Client + '
'; html += '
' + connection.ApplicationVersion + '
'; html += '
' + connection.DeviceName + '
'; html += ''; html += ''; html += connection.UserName || ''; html += ''; var nowPlayingItem = connection.NowPlayingItem; html += ''; html += DashboardPage.getNowPlayingImage(nowPlayingItem); html += ''; html += ''; html += DashboardPage.getNowPlayingText(connection, nowPlayingItem); html += ''; html += ''; } table.append(html).trigger('create'); $('.deadSession', table).remove(); }, updateSession: function (row, session) { row.removeClass('deadSession'); $('.username', row).html(session.UserName || ''); var nowPlayingItem = session.NowPlayingItem; $('.nowPlayingText', row).html(DashboardPage.getNowPlayingText(session, nowPlayingItem)).trigger('create'); var imageRow = $('.nowPlayingImage', row); var image = $('img', imageRow)[0]; var nowPlayingItemId = nowPlayingItem ? nowPlayingItem.Id : null; var nowPlayingItemImageTag = nowPlayingItem ? nowPlayingItem.PrimaryImageTag : null; if (!image || image.getAttribute('data-itemid') != nowPlayingItemId || image.getAttribute('data-tag') != nowPlayingItemImageTag) { imageRow.html(DashboardPage.getNowPlayingImage(nowPlayingItem)); } }, getClientType: function (connection) { var clientLowered = connection.Client.toLowerCase(); if (clientLowered == "dashboard") { var device = connection.DeviceName.toLowerCase(); var imgUrl; if (device.indexOf('chrome') != -1) { imgUrl = 'css/images/clients/chrome.png'; } else if (device.indexOf('firefox') != -1) { imgUrl = 'css/images/clients/firefox.png'; } else if (device.indexOf('internet explorer') != -1) { imgUrl = 'css/images/clients/ie.png'; } else if (device.indexOf('safari') != -1) { imgUrl = 'css/images/clients/safari.png'; } else { imgUrl = 'css/images/clients/html5.png'; } return "Dashboard"; } if (clientLowered == "mb-classic") { return "Media Browser Classic"; } if (clientLowered == "media browser theater") { return "Media Browser Theater"; } if (clientLowered == "android") { return "Android"; } if (clientLowered == "roku") { return "Roku"; } if (clientLowered == "ios") { return "iOS"; } if (clientLowered == "windows rt") { return "Windows RT"; } if (clientLowered == "windows phone") { return "Windows Phone"; } if (clientLowered == "dlna") { return "Dlna"; } if (clientLowered == "mbkinect") { return "MB Kinect"; } return connection.Client; }, getNowPlayingImage: function (item) { if (item && item.PrimaryImageTag) { var url = ApiClient.getImageUrl(item.Id, { type: "Primary", height: 100, tag: item.PrimaryImageTag }); url += "&xxx=" + new Date().getTime(); return "" + item.Name + ""; } return ""; }, getNowPlayingText: function (connection, item) { var html = ""; if (item) { html += "
" + item.Name + "
"; html += "
"; if (item.RunTimeTicks) { html += Dashboard.getDisplayTime(connection.NowPlayingPositionTicks || 0) + " / "; html += Dashboard.getDisplayTime(item.RunTimeTicks); } html += "
"; } return html; }, renderRunningTasks: function (dashboardInfo) { var page = $.mobile.activePage; var html = ''; if (!dashboardInfo.RunningTasks.length) { html += '

No tasks are currently running.

'; $('#runningTasksCollapsible', page).hide(); } else { $('#runningTasksCollapsible', page).show(); } 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 = (task.CurrentProgressPercentage || 0).toFixed(1); html += ' - ' + progress + '%'; html += ''; } else if (task.State == "Cancelling") { html += ' - Stopping'; } html += '

'; } $('#divRunningTasks', page).html(html).trigger('create'); }, bookmarkPageIfSupported: function (url) { if (window.sidebar && window.sidebar.addPanel) { // Mozilla Firefox Bookmark window.sidebar.addPanel("Media Browser", url, ''); } else if (window.external && window.external.AddFavorite) { // IE Favorite window.external.AddFavorite(url, "Media Browser"); } else { // webkit - safari/chrome return false; } return true; }, renderSystemInfo: function (dashboardInfo) { Dashboard.updateSystemInfo(dashboardInfo.SystemInfo); var page = $.mobile.activePage; $('#appVersionNumber', page).html(dashboardInfo.SystemInfo.Version); var port = dashboardInfo.SystemInfo.HttpServerPortNumber; if (port == dashboardInfo.SystemInfo.WebSocketPortNumber) { $('#ports', page).html('Running on port ' + port + ''); } else { $('#ports', page).html('Running on ports ' + port + ' and ' + dashboardInfo.SystemInfo.WebSocketPortNumber + ''); } $('#logPath', page).html(dashboardInfo.SystemInfo.LogPath); $('#imagesByNamePath', page).html(dashboardInfo.SystemInfo.ItemsByNamePath); var host = ApiClient.serverHostName(); var url = "http://" + host + ":" + port + "/mediabrowser"; $('#bookmarkUrl', page).html(url).attr("href", url); if (dashboardInfo.RunningTasks.filter(function (task) { return task.Id == dashboardInfo.ApplicationUpdateTaskId; }).length) { $('#btnUpdateApplication', page).button('disable'); } else { $('#btnUpdateApplication', page).button('enable'); } if (dashboardInfo.SystemInfo.CanSelfRestart) { $('.btnRestartContainer', page).removeClass('hide'); } else { $('.btnRestartContainer', page).addClass('hide'); } DashboardPage.renderApplicationUpdateInfo(dashboardInfo); DashboardPage.renderPluginUpdateInfo(dashboardInfo); DashboardPage.renderPendingInstallations(dashboardInfo.SystemInfo); }, renderApplicationUpdateInfo: function (dashboardInfo) { var page = $.mobile.activePage; $('#updateFail', page).hide(); if (!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(); if (dashboardInfo.SystemInfo.CanSelfUpdate) { $('#btnUpdateApplicationContainer', page).show(); $('#btnManualUpdateContainer', page).hide(); } else { $('#btnUpdateApplicationContainer', page).hide(); $('#btnManualUpdateContainer', page).show(); } $('#newVersionNumber', page).html("Version " + version.versionStr + " is now available for download."); } }).fail(function () { $('#updateFail', page).show(); }); } 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 () { $('#updateFail', page).show(); }); }, installPluginUpdate: function (button) { $(button).button('disable'); var name = button.getAttribute('data-name'); var guid = button.getAttribute('data-guid'); var version = button.getAttribute('data-version'); var classification = button.getAttribute('data-classification'); Dashboard.showLoadingMsg(); ApiClient.installPlugin(name, guid, 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(); }); }, restart: function () { Dashboard.confirm("Are you sure you wish to restart Media Browser Server?", "Restart", function (result) { if (result) { $('#btnRestartServer').button('disable'); $('#btnShutdown').button('disable'); Dashboard.restartServer(); } }); }, shutdown: function () { Dashboard.confirm("Are you sure you wish to shutdown Media Browser Server?", "Shutdown", function (result) { if (result) { $('#btnRestartServer').button('disable'); $('#btnShutdown').button('disable'); ApiClient.shutdownServer(); } }); } }; $(document).on('pageshow', "#dashboardPage", DashboardPage.onPageShow).on('pagehide', "#dashboardPage", DashboardPage.onPageHide);