From 9fc4c861116d8c9a2e328ecc2a9f44fb82f5c669 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Wed, 20 May 2015 12:28:55 -0400 Subject: [PATCH] hls updates --- dashboard-ui/css/chromecast.css | 2 +- dashboard-ui/css/librarybrowser.css | 2 +- dashboard-ui/css/librarymenu.css | 4 +- dashboard-ui/css/mediaplayer.css | 2 +- dashboard-ui/scripts/connectlogin.js | 13 +- dashboard-ui/scripts/dashboardpage.js | 12 +- dashboard-ui/scripts/librarylist.js | 2 +- dashboard-ui/scripts/librarymenu.js | 87 ++++---- dashboard-ui/scripts/loginpage.js | 6 +- dashboard-ui/scripts/selectserver.js | 4 +- dashboard-ui/scripts/site.js | 203 ++++++++---------- dashboard-ui/scripts/supporterkeypage.js | 11 +- dashboard-ui/scripts/sync.js | 4 +- dashboard-ui/supporterkey.html | 9 +- .../thirdparty/apiclient/apiclient.js | 64 ++++-- .../thirdparty/apiclient/connectionmanager.js | 48 +++-- dashboard-ui/thirdparty/cordova/chromecast.js | 11 +- dashboard-ui/thirdparty/cordova/connectsdk.js | 15 -- .../thirdparty/cordova/generaldevice.js | 2 + 19 files changed, 255 insertions(+), 246 deletions(-) diff --git a/dashboard-ui/css/chromecast.css b/dashboard-ui/css/chromecast.css index a634670e00..1720c7a4e1 100644 --- a/dashboard-ui/css/chromecast.css +++ b/dashboard-ui/css/chromecast.css @@ -52,7 +52,7 @@ top: 0; } -@media all and (max-width: 1000px) { +@media all and (max-width: 740px) { .headerSelectedPlayer { display: none; } diff --git a/dashboard-ui/css/librarybrowser.css b/dashboard-ui/css/librarybrowser.css index 6a6c57bbbf..de1e1a1460 100644 --- a/dashboard-ui/css/librarybrowser.css +++ b/dashboard-ui/css/librarybrowser.css @@ -78,7 +78,7 @@ } .homePageSection + .homePageSection { - margin-top: 1em; + margin-top: .25em; } .sectionHeaderButton { diff --git a/dashboard-ui/css/librarymenu.css b/dashboard-ui/css/librarymenu.css index b3d1167eff..ba94ad91a1 100644 --- a/dashboard-ui/css/librarymenu.css +++ b/dashboard-ui/css/librarymenu.css @@ -138,7 +138,7 @@ -webkit-overflow-scrolling: touch; } -.bottomLibraryViewNav { +.bottomSecondaryNav .libraryViewNav { top: initial; bottom: 0; border-top: 1px solid #444; @@ -236,7 +236,7 @@ border-bottom: 5px solid transparent; } -.bottomLibraryViewNav a { +.bottomSecondaryNav .libraryViewNav a { padding: 12px 0 12px; } diff --git a/dashboard-ui/css/mediaplayer.css b/dashboard-ui/css/mediaplayer.css index b0352716a5..acacd002de 100644 --- a/dashboard-ui/css/mediaplayer.css +++ b/dashboard-ui/css/mediaplayer.css @@ -39,7 +39,7 @@ vertical-align: middle; text-align: left; font-size: 15px; - max-width: 170px; + max-width: 150px; } @media (min-width: 800px) { diff --git a/dashboard-ui/scripts/connectlogin.js b/dashboard-ui/scripts/connectlogin.js index 76dc8196d8..333b924999 100644 --- a/dashboard-ui/scripts/connectlogin.js +++ b/dashboard-ui/scripts/connectlogin.js @@ -1,18 +1,13 @@ (function () { - function onLoggedIn() { - - Dashboard.hideModalLoadingMsg(); - Dashboard.navigate('selectserver.html'); - } - function login(page, username, password) { Dashboard.showModalLoadingMsg(); ConnectionManager.loginToConnect(username, password).done(function () { - onLoggedIn(); + Dashboard.hideModalLoadingMsg(); + Dashboard.navigate('selectserver.html'); }).fail(function () { @@ -39,7 +34,7 @@ { var apiClient = result.ApiClient; - Dashboard.onServerChanged(apiClient.serverAddress(), apiClient.getCurrentUserId(), apiClient.accessToken(), apiClient); + Dashboard.onServerChanged(apiClient.getCurrentUserId(), apiClient.accessToken(), apiClient); Dashboard.navigate('index.html'); } break; @@ -50,7 +45,7 @@ break; case MediaBrowser.ConnectionState.ServerSelection: { - onLoggedIn(); + Dashboard.navigate('selectserver.html'); } break; case MediaBrowser.ConnectionState.ConnectSignIn: diff --git a/dashboard-ui/scripts/dashboardpage.js b/dashboard-ui/scripts/dashboardpage.js index 1f203993ae..61bc6730db 100644 --- a/dashboard-ui/scripts/dashboardpage.js +++ b/dashboard-ui/scripts/dashboardpage.js @@ -50,8 +50,10 @@ var apiClient = ApiClient; - $(apiClient).off("websocketmessage", DashboardPage.onWebSocketMessage).off("websocketopen", DashboardPage.onWebSocketConnectionChange).off("websocketerror", DashboardPage.onWebSocketConnectionChange).off("websocketclose", DashboardPage.onWebSocketConnectionChange); - DashboardPage.stopInterval(apiClient); + if (apiClient) { + $(apiClient).off("websocketmessage", DashboardPage.onWebSocketMessage).off("websocketopen", DashboardPage.onWebSocketConnectionChange).off("websocketerror", DashboardPage.onWebSocketConnectionChange).off("websocketclose", DashboardPage.onWebSocketConnectionChange); + DashboardPage.stopInterval(apiClient); + } if (DashboardPage.sessionUpdateTimer) { clearInterval(DashboardPage.sessionUpdateTimer); @@ -1280,7 +1282,11 @@ $(document).on('pagebeforeshowready', "#dashboardPage", DashboardPage.onPageShow createList(this); } - startListening(ApiClient); + var apiClient = ApiClient; + + if (apiClient) { + startListening(apiClient); + } return this; }; diff --git a/dashboard-ui/scripts/librarylist.js b/dashboard-ui/scripts/librarylist.js index ac3583c8c3..8dc85960ba 100644 --- a/dashboard-ui/scripts/librarylist.js +++ b/dashboard-ui/scripts/librarylist.js @@ -807,7 +807,7 @@ var innerElem = $('.cardOverlayTarget', elem); var dataElement = elem; - while (!dataElement.getAttribute('data-itemid')) { + while (dataElement && !dataElement.getAttribute('data-itemid')) { dataElement = dataElement.parentNode; } diff --git a/dashboard-ui/scripts/librarymenu.js b/dashboard-ui/scripts/librarymenu.js index e6e2d41d08..69b9af38af 100644 --- a/dashboard-ui/scripts/librarymenu.js +++ b/dashboard-ui/scripts/librarymenu.js @@ -257,6 +257,7 @@ } var requiresLibraryMenuRefresh = false; + var requiresViewMenuRefresh = false; function getLibraryMenu(user) { @@ -460,7 +461,7 @@ function updateContextText(page) { - var name = page.getAttribute('data-contextname'); + var name = $(page)[0].getAttribute('data-contextname'); if (name) { @@ -490,21 +491,17 @@ } } - $(document).on('pageinit', ".page", function () { + function buildViewMenuBar(page) { - var page = this; + if ($(page).hasClass('standalonePage')) { + $('.viewMenuBar').remove(); + return; + } - $('.libraryViewNav', page).wrapInner('
'); + if (requiresViewMenuRefresh) { + $('.viewMenuBar').remove(); + } - $('.libraryViewNav a', page).each(function () { - - this.innerHTML = '' + this.innerHTML + ''; - - }); - - }).on('pagebeforeshowready', ".page:not(.standalonePage)", function () { - - var page = this; var viewMenuBar = $('.viewMenuBar'); if (!$('.viewMenuBar').length) { @@ -517,17 +514,56 @@ updateLibraryNavLinks(page); updateContextText(page); + requiresViewMenuRefresh = false; }); } else { updateContextText(page); updateLibraryNavLinks(page); updateViewMenuBarHeadroom(page, viewMenuBar); + requiresViewMenuRefresh = false; } + } + + $(document).on('pageinit', ".page", function () { + + var page = this; + + $('.libraryViewNav', page).wrapInner('
'); + + $('.libraryViewNav a', page).each(function () { + + this.innerHTML = '' + this.innerHTML + ''; + + }); + + }).on('pagebeforeshowready', ".page", function () { + + var page = this; + buildViewMenuBar(page); + var jpage = $(page); - if (jpage.hasClass('libraryPage')) { + var isLibraryPage = jpage.hasClass('libraryPage'); + + if (isLibraryPage) { $(document.body).addClass('libraryDocument').removeClass('dashboardDocument'); + + if (AppInfo.enableBottomTabs) { + $(page).addClass('noSecondaryNavPage'); + + $(function () { + + $('.footer').addClass('footerOverBottomTabs'); + }); + + } else { + + $('.libraryViewNav', page).each(function () { + + initHeadRoom(this); + }); + } } else if (jpage.hasClass('type-interior')) { $(document.body).addClass('dashboardDocument').removeClass('libraryDocument'); @@ -535,27 +571,6 @@ $(document.body).removeClass('dashboardDocument').removeClass('libraryDocument'); } - }).on('pagebeforeshowready', ".libraryPage", function () { - - var page = this; - - if (AppInfo.enableBottomTabs) { - $('.libraryViewNav', page).addClass('bottomLibraryViewNav'); - $(page).addClass('noSecondaryNavPage'); - - $(function () { - - $('.footer').addClass('footerOverBottomTabs'); - }); - - } else { - - $('.libraryViewNav', page).each(function () { - - initHeadRoom(this); - }); - } - }).on('pageshow', ".libraryPage", function () { var page = this; @@ -601,8 +616,8 @@ initializeApiClient(apiClient); }).on('localusersignedin localusersignedout', function () { - $('.viewMenuBar').remove(); requiresLibraryMenuRefresh = true; + requiresViewMenuRefresh = true; }); }); diff --git a/dashboard-ui/scripts/loginpage.js b/dashboard-ui/scripts/loginpage.js index 632be937c8..7685d0a090 100644 --- a/dashboard-ui/scripts/loginpage.js +++ b/dashboard-ui/scripts/loginpage.js @@ -105,12 +105,12 @@ var newUrl; if (user.Policy.IsAdministrator && !serverId) { - newUrl = "dashboard.html?u=" + user.Id + '&t=' + result.AccessToken; + newUrl = "dashboard.html"; } else { - newUrl = "index.html?u=" + user.Id + '&t=' + result.AccessToken; + newUrl = "index.html"; } - Dashboard.onServerChanged(apiClient.serverAddress(), user.Id, result.AccessToken, apiClient); + Dashboard.onServerChanged(user.Id, result.AccessToken, apiClient); Dashboard.navigate(newUrl); }).fail(function () { diff --git a/dashboard-ui/scripts/selectserver.js b/dashboard-ui/scripts/selectserver.js index 96b256c596..b284a80213 100644 --- a/dashboard-ui/scripts/selectserver.js +++ b/dashboard-ui/scripts/selectserver.js @@ -14,7 +14,7 @@ case MediaBrowser.ConnectionState.SignedIn: { - Dashboard.onServerChanged(apiClient.serverAddress(), apiClient.getCurrentUserId(), apiClient.accessToken(), apiClient); + Dashboard.onServerChanged(apiClient.getCurrentUserId(), apiClient.accessToken(), apiClient); Dashboard.navigate('index.html'); } break; @@ -22,7 +22,7 @@ { if (Dashboard.isRunningInCordova()) { - Dashboard.onServerChanged(apiClient.serverAddress(), null, null, apiClient); + Dashboard.onServerChanged(null, null, apiClient); Dashboard.navigate('login.html?serverid=' + result.Servers[0].Id); } else { showServerConnectionFailure(); diff --git a/dashboard-ui/scripts/site.js b/dashboard-ui/scripts/site.js index 847a6a33a7..d42cec7bff 100644 --- a/dashboard-ui/scripts/site.js +++ b/dashboard-ui/scripts/site.js @@ -104,13 +104,6 @@ var Dashboard = { } }, - onApiClientServerAddressChanged: function () { - - var apiClient = this; - - Dashboard.serverAddress(apiClient.serverAddress()); - }, - getCurrentUser: function () { if (!Dashboard.getUserPromise) { @@ -130,45 +123,37 @@ var Dashboard = { } }, - getAccessToken: function () { + serverAddress: function () { - return store.getItem('token'); - }, + if (Dashboard.isConnectMode()) { + var apiClient = window.ApiClient; - serverAddress: function (val) { + if (apiClient) { + return apiClient.serverAddress(); + } - if (val != null) { - - console.log('Setting server address to: ' + val); - - store.setItem('serverAddress', val); + return null; } - var address = store.getItem('serverAddress'); + // Try to get the server address from the browser url + // This will preserve protocol, hostname, port and subdirectory + var urlLower = getWindowUrl().toLowerCase(); + var index = urlLower.indexOf('/web'); + if (index == -1) { + index = urlLower.indexOf('/dashboard'); + } - if (!address && !Dashboard.isConnectMode()) { + if (index != -1) { + return urlLower.substring(0, index); + } - // Try to get the server address from the browser url - // This will preserve protocol, hostname, port and subdirectory - var urlLower = getWindowUrl().toLowerCase(); - var index = urlLower.indexOf('/web'); - if (index == -1) { - index = urlLower.indexOf('/dashboard'); - } + // If the above failed, just piece it together manually + var loc = window.location; - if (index != -1) { - address = urlLower.substring(0, index); - return address; - } + var address = loc.protocol + '//' + loc.hostname; - // If the above failed, just piece it together manually - var loc = window.location; - - address = loc.protocol + '//' + loc.hostname; - - if (loc.port) { - address += ':' + loc.port; - } + if (loc.port) { + address += ':' + loc.port; } return address; @@ -176,38 +161,26 @@ var Dashboard = { getCurrentUserId: function () { - var autoLoginUserId = getParameterByName('u'); - - var storedUserId = store.getItem("userId"); - - if (autoLoginUserId && autoLoginUserId != storedUserId) { - - var token = getParameterByName('t'); - Dashboard.setCurrentUser(autoLoginUserId, token); - } - - return autoLoginUserId || storedUserId; - }, - - setCurrentUser: function (userId, token) { - - store.setItem("userId", userId); - store.setItem("token", token); - var apiClient = window.ApiClient; if (apiClient) { - apiClient.setCurrentUserId(userId, token); + return apiClient.getCurrentUserId(); } + + return null; + }, + + onServerChanged: function (userId, accessToken, apiClient) { + + apiClient = apiClient || window.ApiClient; + + window.ApiClient = apiClient; + Dashboard.getUserPromise = null; }, logout: function (logoutWithServer, forceReload) { - store.removeItem("userId"); - store.removeItem("token"); - store.removeItem("serverAddress"); - function onLogoutDone() { var loginPage; @@ -594,9 +567,11 @@ var Dashboard = { refreshSystemInfoFromServer: function () { - if (Dashboard.getAccessToken()) { - if (AppInfo.enableFooterNotifications) { - ApiClient.getSystemInfo().done(function (info) { + var apiClient = ApiClient; + + if (apiClient.accessToken()) { + if (apiClient.enableFooterNotifications) { + apiClient.getSystemInfo().done(function (info) { Dashboard.updateSystemInfo(info); }); @@ -1040,14 +1015,14 @@ var Dashboard = { Dashboard.updateSystemInfo(msg.Data); } else if (msg.MessageType === "UserUpdated" || msg.MessageType === "UserConfigurationUpdated") { - Dashboard.validateCurrentUser(); - var user = msg.Data; if (user.Id == Dashboard.getCurrentUserId()) { + Dashboard.validateCurrentUser(); $('.currentUsername').html(user.Name); } + } else if (msg.MessageType === "PackageInstallationCompleted") { Dashboard.getCurrentUser().done(function (currentUser) { @@ -1076,7 +1051,7 @@ var Dashboard = { } }); } - else if (msg.MessageType === "PackageInstalling") { + else if (msg.MessaapiclientcgeType === "PackageInstalling") { Dashboard.getCurrentUser().done(function (currentUser) { if (currentUser.Policy.IsAdministrator) { @@ -1492,18 +1467,8 @@ var Dashboard = { return deferred.promise(); }, - onServerChanged: function (serverAddress, userId, accessToken, apiClient) { + ready: function (fn) { - window.ApiClient = apiClient; - if (Dashboard.isConnectMode()) { - Dashboard.serverAddress(serverAddress); - } - - Dashboard.setCurrentUser(userId, accessToken); - }, - - ready: function(fn) { - if (Dashboard.initPromiseDone) { fn(); return; @@ -1593,8 +1558,7 @@ var AppInfo = {}; $(apiClient).off('.dashboard') .on("websocketmessage.dashboard", Dashboard.onWebSocketMessageReceived) - .on('requestfail.dashboard', Dashboard.onRequestFail) - .on('serveraddresschanged.dashboard', Dashboard.onApiClientServerAddressChanged); + .on('requestfail.dashboard', Dashboard.onRequestFail); } function createConnectionManager(appInfo) { @@ -1605,41 +1569,36 @@ var AppInfo = {}; window.ConnectionManager = new MediaBrowser.ConnectionManager(Logger, credentialProvider, appInfo.appName, appInfo.appVersion, appInfo.deviceName, appInfo.deviceId, capabilities); + $(ConnectionManager).on('apiclientcreated', function (e, apiClient) { + + initializeApiClient(apiClient); + }); + + var lastApiClient = ConnectionManager.getLastUsedApiClient(); + if (Dashboard.isConnectMode()) { - $(ConnectionManager).on('apiclientcreated', function (e, apiClient) { - - initializeApiClient(apiClient); - }); - if (!Dashboard.isServerlessPage()) { - if (Dashboard.serverAddress() && Dashboard.getCurrentUserId() && Dashboard.getAccessToken()) { + if (lastApiClient && lastApiClient.serverAddress() && lastApiClient.getCurrentUserId() && lastApiClient.accessToken()) { - window.ApiClient = new MediaBrowser.ApiClient(Logger, Dashboard.serverAddress(), appInfo.appName, appInfo.appVersion, appInfo.deviceName, appInfo.deviceId); + window.ApiClient = lastApiClient; - ApiClient.setCurrentUserId(Dashboard.getCurrentUserId(), Dashboard.getAccessToken()); + initializeApiClient(lastApiClient); - initializeApiClient(ApiClient); + //ConnectionManager.addApiClient(lastApiClient, true).fail(Dashboard.logout); - ConnectionManager.addApiClient(ApiClient, true).fail(Dashboard.logout); - - } else { - - Dashboard.logout(); - return; } } } else { - window.ApiClient = new MediaBrowser.ApiClient(Logger, Dashboard.serverAddress(), appInfo.appName, appInfo.appVersion, appInfo.deviceName, appInfo.deviceId); + if (!lastApiClient) { + lastApiClient = new MediaBrowser.ApiClient(Logger, Dashboard.serverAddress(), appInfo.appName, appInfo.appVersion, appInfo.deviceName, appInfo.deviceId); + ConnectionManager.addApiClient(lastApiClient); + } - ApiClient.setCurrentUserId(Dashboard.getCurrentUserId(), Dashboard.getAccessToken()); - - initializeApiClient(ApiClient); - - ConnectionManager.addApiClient(ApiClient); + window.ApiClient = lastApiClient; } if (window.ApiClient) { @@ -1835,7 +1794,7 @@ var AppInfo = {}; } } - function init(deferred, appName, deviceId, deviceName) { + function init(deferred, appName, deviceId, deviceName, resolveOnReady) { requirejs.config({ map: { @@ -1857,9 +1816,17 @@ var AppInfo = {}; createConnectionManager(appInfo); - Dashboard.initPromiseDone = true; - deferred.resolve(); - $(onDocumentReady); + if (!resolveOnReady) { + Dashboard.initPromiseDone = true; + deferred.resolve(); + } + $(function () { + onDocumentReady(); + if (resolveOnReady) { + Dashboard.initPromiseDone = true; + deferred.resolve(); + } + }); } function initCordovaWithDeviceId(deferred, deviceId) { @@ -1867,7 +1834,7 @@ var AppInfo = {}; requirejs(['thirdparty/cordova/imagestore.js']); } - init(deferred, "Emby Mobile", deviceId, device.model); + init(deferred, "Emby Mobile", deviceId, device.model, true); } function initCordova(deferred) { @@ -1974,29 +1941,27 @@ $(document).on('pagecreate', ".page", function () { Dashboard.firePageEvent(page, 'pagebeforeshowready'); } -}).on('pagebeforeshow', ".page", function () { +}).on('pagebeforeshowready', ".page", function () { var page = $(this); var apiClient = window.ApiClient; - if (Dashboard.getAccessToken() && Dashboard.getCurrentUserId()) { + if (apiClient && apiClient.accessToken() && Dashboard.getCurrentUserId()) { - if (apiClient) { - Dashboard.getCurrentUser().done(function (user) { + Dashboard.getCurrentUser().done(function (user) { - var isSettingsPage = page.hasClass('type-interior'); + var isSettingsPage = page.hasClass('type-interior'); - if (!user.Policy.IsAdministrator && isSettingsPage) { - window.location.replace("index.html"); - return; - } + if (!user.Policy.IsAdministrator && isSettingsPage) { + window.location.replace("index.html"); + return; + } - if (isSettingsPage) { - Dashboard.ensureToolsMenu(page, user); - } - }); - } + if (isSettingsPage) { + Dashboard.ensureToolsMenu(page, user); + } + }); Dashboard.ensureHeader(page); Dashboard.ensurePageTitle(page); diff --git a/dashboard-ui/scripts/supporterkeypage.js b/dashboard-ui/scripts/supporterkeypage.js index 8464880244..58f5422521 100644 --- a/dashboard-ui/scripts/supporterkeypage.js +++ b/dashboard-ui/scripts/supporterkeypage.js @@ -120,7 +120,7 @@ }; -$(document).on('pageshow', "#supporterKeyPage", SupporterKeyPage.onPageShow); +$(document).on('pageshowready', "#supporterKeyPage", SupporterKeyPage.onPageShow); (function () { @@ -255,14 +255,19 @@ $(document).on('pageshow', "#supporterKeyPage", SupporterKeyPage.onPageShow); } - $(document).on('pageinit', "#supporterKeyPage", function () { + $(document).on('pageinitdepends', "#supporterKeyPage", function () { var page = this; $('#btnAddConnectUser', page).on('click', function () { showAddUserForm(page); }); - }).on('pageshow', "#supporterKeyPage", function () { + $('#supporterKeyForm').on('submit', SupporterKeyPage.updateSupporterKey); + $('#lostKeyForm').on('submit', SupporterKeyPage.retrieveSupporterKey); + $('#linkKeysForm').on('submit', SupporterKeyPage.linkSupporterKeys); + $('.popupAddUserForm').on('submit', SupporterKeyPage.onAddConnectUserSubmit).on('submit', SupporterKeyPage.onAddConnectUserSubmit); + + }).on('pageshowready', "#supporterKeyPage", function () { var page = this; loadConnectSupporters(page); diff --git a/dashboard-ui/scripts/sync.js b/dashboard-ui/scripts/sync.js index bd7cd1f057..5b7c26efde 100644 --- a/dashboard-ui/scripts/sync.js +++ b/dashboard-ui/scripts/sync.js @@ -387,7 +387,7 @@ }); } - $(document).on('pageinit', ".libraryPage", function () { + $(document).on('pageinitdepends', ".libraryPage", function () { var page = this; @@ -396,7 +396,7 @@ onCategorySyncButtonClick(page, this); }); - }).on('pagebeforeshow', ".libraryPage", function () { + }).on('pagebeforeshowready', ".libraryPage", function () { var page = this; diff --git a/dashboard-ui/supporterkey.html b/dashboard-ui/supporterkey.html index 66683a1a46..6d2eeca26f 100644 --- a/dashboard-ui/supporterkey.html +++ b/dashboard-ui/supporterkey.html @@ -4,7 +4,7 @@ ${TitleSupport} -
+
@@ -132,13 +132,6 @@
- -
diff --git a/dashboard-ui/thirdparty/apiclient/apiclient.js b/dashboard-ui/thirdparty/apiclient/apiclient.js index ed2f9eed4b..068dee9f59 100644 --- a/dashboard-ui/thirdparty/apiclient/apiclient.js +++ b/dashboard-ui/thirdparty/apiclient/apiclient.js @@ -7,7 +7,7 @@ /** * Creates a new api client instance * @param {String} serverAddress - * @param {String} clientName + * @param {String} clientName s * @param {String} applicationVersion */ globalScope.MediaBrowser.ApiClient = function (logger, serverAddress, clientName, applicationVersion, deviceName, deviceId) { @@ -23,10 +23,8 @@ logger.log('ApiClient deviceId: ' + deviceId); var self = this; - var currentUserId; - var accessToken; var webSocket; - var serverInfo; + var serverInfo = {}; self.enableAppStorePolicy = false; @@ -37,6 +35,10 @@ if (val != null) { + if (val.toLowerCase().indexOf('http') != 0) { + throw new Error('Invalid url: ' + val); + } + var changed = val != serverAddress; serverAddress = val; @@ -81,18 +83,11 @@ */ self.getCurrentUserId = function () { - return currentUserId; + return serverInfo.UserId; }; self.accessToken = function () { - return accessToken; - }; - - self.setCurrentUserId = function (userId, token) { - - currentUserId = userId; - currentUserPromise = null; - accessToken = token; + return serverInfo.AccessToken; }; self.deviceName = function () { @@ -104,13 +99,14 @@ }; self.clearAuthenticationInfo = function () { - accessToken = null; - currentUserId = null; + self.setAuthenticationInfo(null, null); }; self.setAuthenticationInfo = function (accessKey, userId) { - accessToken = accessKey; - currentUserId = userId; + currentUserPromise = null; + + serverInfo.AccessToken = accessKey; + serverInfo.UserId = userId; }; self.encodeName = function (name) { @@ -156,8 +152,10 @@ var auth = 'MediaBrowser Client="' + clientName + '", Device="' + deviceName + '", DeviceId="' + deviceId + '", Version="' + applicationVersion + '"'; - if (currentUserId) { - auth += ', UserId="' + currentUserId + '"'; + var userId = serverInfo.UserId; + + if (userId) { + auth += ', UserId="' + userId + '"'; } request.headers = { @@ -165,12 +163,14 @@ }; } + var accessToken = serverInfo.AccessToken; + if (accessToken) { request.headers['X-MediaBrowser-Token'] = accessToken; } } - if (!self.enableAutomaticNetwork || !self.serverInfo() || self.connectionMode == null) { + if (!self.enableAutomaticNetwork || self.connectionMode == null) { logger.log('Requesting url without automatic networking: ' + request.url); return AjaxApi.ajax(request).fail(onRequestFail); } @@ -332,6 +332,10 @@ var url = serverAddress; + if (!url) { + throw new Error("serverAddress is yet not set"); + } + if (name.charAt(0) != '/') { url += '/'; } @@ -347,6 +351,18 @@ self.enableAutomaticNetworking = function (server, connectionMode, serverUrl) { + if (server == null) { + throw new Error('server cannot be null'); + } + + if (connectionMode == null) { + throw new Error('connectionMode cannot be null'); + } + + if (!serverUrl) { + throw new Error('serverUrl cannot be null or empty'); + } + logger.log('Begin enableAutomaticNetworking'); self.serverInfo(server); @@ -363,6 +379,8 @@ self.openWebSocket = function () { + var accessToken = serverInfo.AccessToken; + if (!accessToken) { throw new Error("Cannot open web socket without access token."); } @@ -405,7 +423,7 @@ }; function onWebSocketMessage(msg) { - + if (msg.MessageType === "UserDeleted") { currentUserPromise = null; } @@ -557,10 +575,10 @@ self.closeWebSocket(); var done = function () { - self.setCurrentUserId(null, null); + self.setAuthenticationInfo(null, null); }; - if (accessToken) { + if (serverInfo.AccessToken) { var url = self.getUrl("Sessions/Logout"); return self.ajax({ diff --git a/dashboard-ui/thirdparty/apiclient/connectionmanager.js b/dashboard-ui/thirdparty/apiclient/connectionmanager.js index a5e94f1fe5..88aa267a71 100644 --- a/dashboard-ui/thirdparty/apiclient/connectionmanager.js +++ b/dashboard-ui/thirdparty/apiclient/connectionmanager.js @@ -24,6 +24,7 @@ var self = this; var apiClients = []; + var defaultTimeout = 15000; function mergeServers(list1, list2) { @@ -73,7 +74,7 @@ url: url, dataType: "json", - timeout: timeout || 15000 + timeout: timeout || defaultTimeout }); } @@ -100,10 +101,33 @@ return credentialProvider.credentials().ConnectAccessToken; }; - self.addApiClient = function (apiClient, enableAutomaticNetworking) { + self.getLastUsedApiClient = function() { + + var servers = credentialProvider.credentials().servers; + + servers.sort(function (a, b) { + return b.DateLastAccessed - a.DateLastAccessed; + }); + + if (!servers.length) { + return null; + } + + var server = servers[0]; + + return getOrAddApiClient(server, server.LastConnectionMode); + }; + + self.addApiClient = function (apiClient) { apiClients.push(apiClient); + Events.on(apiClient, 'authenticated', function (e, result) { + onAuthenticated(this, result, {}, true); + }); + + Events.trigger(self, 'apiclientcreated', [apiClient]); + return apiClient.getPublicSystemInfo().done(function (systemInfo) { var server = credentialProvider.credentials().servers.filter(function (s) { @@ -115,13 +139,7 @@ updateServerInfo(server, systemInfo); apiClient.serverInfo(server); - Events.trigger(self, 'apiclientcreated', [apiClient]); - - if (enableAutomaticNetworking) { - self.connectToServer(server); - } }); - }; function onConnectUserSignIn(user) { @@ -151,7 +169,7 @@ Events.trigger(self, 'apiclientcreated', [apiClient]); } - if (server.AccessToken) { + if (server.AccessToken && server.UserId) { apiClient.setAuthenticationInfo(server.AccessToken, server.UserId); } @@ -799,7 +817,7 @@ var address = self.getServerAddress(server, mode); var enableRetry = false; var skipTest = false; - var timeout = 15000; + var timeout = defaultTimeout; if (mode == MediaBrowser.ConnectionMode.Local) { @@ -807,7 +825,7 @@ skipTest = true; } enableRetry = true; - timeout = 5000; + timeout = 7000; } else if (mode == MediaBrowser.ConnectionMode.Manual) { @@ -924,7 +942,7 @@ case MediaBrowser.ConnectionMode.Remote: return server.RemoteAddress; default: - throw new Error("Unexpected ConnectionMode"); + return server.ManualAddress || server.LocalAddress || server.RemoteAddress; } }; @@ -934,6 +952,10 @@ address = "http://" + address; } + // Seeing failures in iOS when protocol isn't lowercase + address = address.replace('Http:', 'http:'); + address = address.replace('Https:', 'https:'); + return address; } @@ -953,7 +975,7 @@ resolveWithFailure(deferred); } - tryConnect(address, 15000).done(function (publicInfo) { + tryConnect(address, defaultTimeout).done(function (publicInfo) { logger.log('connectToAddress ' + address + ' succeeded'); diff --git a/dashboard-ui/thirdparty/cordova/chromecast.js b/dashboard-ui/thirdparty/cordova/chromecast.js index a4aa19189f..88396b104a 100644 --- a/dashboard-ui/thirdparty/cordova/chromecast.js +++ b/dashboard-ui/thirdparty/cordova/chromecast.js @@ -580,10 +580,13 @@ $(MediaController).on('playerchange', function (e, newPlayer, newTarget) { - if (newPlayer.name != PlayerName || newTarget.id != currentPairedDeviceId) { - if (currentWebAppSession) { - currentWebAppSession.disconnect(); - onDisconnected(); + if (currentPairedDeviceId) { + if (newTarget.id != currentPairedDeviceId) { + if (currentWebAppSession) { + console.log('Disconnecting from chromecast'); + currentWebAppSession.disconnect(); + onDisconnected(); + } } } }); diff --git a/dashboard-ui/thirdparty/cordova/connectsdk.js b/dashboard-ui/thirdparty/cordova/connectsdk.js index 9a85b5f5f9..349967ea47 100644 --- a/dashboard-ui/thirdparty/cordova/connectsdk.js +++ b/dashboard-ui/thirdparty/cordova/connectsdk.js @@ -1,16 +1,5 @@ (function () { - - function onDeviceFound(e) { - - console.log('device found'); - } - - function onDeviceLost(e) { - - console.log('device lost'); - } - function initSdk() { var manager = ConnectSDK.discoveryManager; @@ -23,9 +12,6 @@ // new ConnectSDK.CapabilityFilter(["MediaPlayer.Display.Video", "MediaControl.Pause"]) //]); - manager.addListener('devicefound', onDeviceFound); - manager.addListener('devicelost', onDeviceLost); - manager.startDiscovery(); requirejs(['thirdparty/cordova/chromecast', 'thirdparty/cordova/generaldevice']); @@ -33,5 +19,4 @@ Dashboard.ready(initSdk); - })(); \ No newline at end of file diff --git a/dashboard-ui/thirdparty/cordova/generaldevice.js b/dashboard-ui/thirdparty/cordova/generaldevice.js index a6d8ca0e00..f0331d15a0 100644 --- a/dashboard-ui/thirdparty/cordova/generaldevice.js +++ b/dashboard-ui/thirdparty/cordova/generaldevice.js @@ -481,6 +481,7 @@ function onDisconnected(device) { if (currentDevice && device.getId() == currentDevice.getId()) { + currentDevice = null; MediaController.removeActiveTarget(device.getId()); } } @@ -552,6 +553,7 @@ if (currentDevice && newTarget.id != currentDevice.getId()) { MediaController.removeActiveTarget(currentDevice.getId()); + currentDevice = null; } }); }