From 749c5d543dd8201b805b1d32026852ece51506df Mon Sep 17 00:00:00 2001 From: Punam Dahiya Date: Wed, 25 Nov 2015 16:06:46 -0800 Subject: [PATCH] Music TV Demo - Handle keydown in player view --- apps/music/js/app.js | 18 +++++++++++++++--- apps/music/js/endpoint.js | 10 +++++++--- apps/music/views/home-tv/view.js | 6 +++--- apps/music/views/player-tv/view.js | 23 +++++++++++++++++++++++ 4 files changed, 48 insertions(+), 9 deletions(-) diff --git a/apps/music/js/app.js b/apps/music/js/app.js index 4d08706fc7c8..3b6b9e6a4c99 100644 --- a/apps/music/js/app.js +++ b/apps/music/js/app.js @@ -1,4 +1,4 @@ -/* exported onSearchOpen, onSearchClose */ +/* exported onSearchOpen, onSearchClose, navigateBack */ /* global SERVICE_WORKERS, bridge */ 'use strict'; @@ -28,8 +28,8 @@ const VIEWS = { ALBUMS: {TAB: 'albums', URL: '/views/albums/index.html'}, ARTIST_DETAIL: {TAB: 'artists', URL: '/views/artist-detail/index.html'}, ARTISTS: {TAB: 'artists', URL: '/views/artists/index.html'}, - HOME: {TAB: 'home', URL: '/views/home/index.html'}, - PLAYER: {TAB: 'home', URL: '/views/player/index.html'}, + HOME: {TAB: 'home', URL: '/views/home-tv/index.html'}, + PLAYER: {TAB: 'home', URL: '/views/player-tv/index.html'}, PLAYLIST_DETAIL: {TAB: 'playlists', URL: '/views/playlist-detail/index.html'}, PLAYLISTS: {TAB: 'playlists', URL: '/views/playlists/index.html'}, SONGS: {TAB: 'songs', URL: '/views/songs/index.html'} @@ -292,6 +292,18 @@ function navigateToURL(url, replaceRoot) { window.history.pushState(null, null, url); } +function navigateBack() { + var isPlayerView = viewStack.activeView && + viewStack.activeView.url === VIEWS.PLAYER.URL; + + if (viewStack.views.length > 1) { + // Don't destroy the popped view if it is the "Player" view. + viewStack.popView(!isPlayerView); + window.history.back(); + return; + } +} + function updateOverlays() { if (emptyOverlay) { diff --git a/apps/music/js/endpoint.js b/apps/music/js/endpoint.js index 22f4db4be202..0ed9964edd28 100644 --- a/apps/music/js/endpoint.js +++ b/apps/music/js/endpoint.js @@ -1,6 +1,6 @@ /* global AlbumArtCache, AudioMetadata, Database, LazyLoader, NFCShare, - PlaybackQueue, Remote, bridge, navigateToURL, onSearchOpen, - onSearchClose */ + PlaybackQueue, Remote, bridge, navigateToURL, navigateBack, + onSearchOpen, onSearchClose */ 'use strict'; var audio = null; @@ -466,7 +466,11 @@ function getDatabaseStatus() { } function navigate(url) { - navigateToURL(url); + if(url) { + navigateToURL(url); + } else { + navigateBack(); + } } function searchOpen() { diff --git a/apps/music/views/home-tv/view.js b/apps/music/views/home-tv/view.js index 49f733c37790..1e62d9b310d6 100644 --- a/apps/music/views/home-tv/view.js +++ b/apps/music/views/home-tv/view.js @@ -24,6 +24,9 @@ var HomeView = View.extend(function HomeView() { var selectedIndex = [].indexOf.call(elements, selectedElement); switch (evt.key) { + case 'Escape': + this.client.method('navigate', '/player-tv'); + break; case 'ArrowUp': selectedIndex = clamp(0, elements.length - 1, selectedIndex - 3); break; @@ -41,9 +44,6 @@ var HomeView = View.extend(function HomeView() { this.client.method('navigate', selectedElement.getAttribute('href')); break; } - - console.log('keydown == ', evt); - selectedElement.classList.remove('selected'); selectedElement = elements[selectedIndex]; diff --git a/apps/music/views/player-tv/view.js b/apps/music/views/player-tv/view.js index 1bfe198b79ec..45646168f5d2 100644 --- a/apps/music/views/player-tv/view.js +++ b/apps/music/views/player-tv/view.js @@ -45,6 +45,29 @@ var PlayerView = View.extend(function PlayerView() { this.seekBar.elapsedTime = elapsedTime; }); + window.addEventListener('keydown', (evt) => { + switch (evt.key) { + case 'Escape': + // Goes back to music home screen + this.client.method('navigate'); + break; + case 'ArrowLeft': + this.previous(); + break; + case 'ArrowRight': + this.next(); + break; + case 'Enter': + if (this.controls.paused) { + this.play(); + } + else { + this.pause(); + } + break; + } + }); + this.update(); });