jellyfin-web/dashboard-ui/scripts/nowplayingbar.js

744 lines
22 KiB
JavaScript
Raw Normal View History

2017-01-03 12:37:10 -07:00
define(['datetime', 'userdataButtons', 'itemHelper', 'events', 'browser', 'imageLoader', 'playbackManager', 'nowPlayingHelper', 'apphost', 'dom', 'connectionManager', 'paper-icon-button-light'], function (datetime, userdataButtons, itemHelper, events, browser, imageLoader, playbackManager, nowPlayingHelper, appHost, dom, connectionManager) {
2016-10-22 22:11:46 -07:00
'use strict';
var currentPlayer;
2017-01-02 23:51:36 -07:00
var currentPlayerSupportedCommands = [];
var currentTimeElement;
var nowPlayingImageElement;
var nowPlayingTextElement;
var nowPlayingUserData;
var muteButton;
var volumeSlider;
2016-06-24 10:30:29 -07:00
var volumeSliderContainer;
2017-01-05 13:21:07 -07:00
var playPauseButtons;
var positionSlider;
2015-07-26 14:02:23 -07:00
var toggleRepeatButton;
2016-05-07 10:47:41 -07:00
var toggleRepeatButtonIcon;
2017-01-02 23:51:36 -07:00
var lastUpdateTime = 0;
var lastPlayerState = {};
var isEnabled;
var currentRuntimeTicks = 0;
function getNowPlayingBarHtml() {
var html = '';
2017-01-02 23:51:36 -07:00
html += '<div class="nowPlayingBar hide nowPlayingBar-hidden">';
2014-06-21 22:52:31 -07:00
2016-08-01 13:06:30 -07:00
html += '<div class="nowPlayingBarTop">';
2016-06-13 12:02:48 -07:00
html += '<div class="nowPlayingBarPositionContainer sliderContainer">';
2017-01-04 22:22:32 -07:00
html += '<input type="range" is="emby-slider" pin step=".01" min="0" max="100" value="0" class="nowPlayingBarPositionSlider"/>';
2015-06-27 12:53:36 -07:00
html += '</div>';
html += '<div class="nowPlayingBarInfoContainer">';
2015-05-01 11:37:01 -07:00
html += '<div class="nowPlayingImage"></div>';
2015-06-27 12:53:36 -07:00
html += '<div class="nowPlayingBarText"></div>';
html += '</div>';
2015-05-01 11:37:01 -07:00
2015-06-17 18:41:22 -07:00
// The onclicks are needed due to the return false above
2015-06-27 12:53:36 -07:00
html += '<div class="nowPlayingBarCenter">';
2015-05-01 11:37:01 -07:00
2016-06-14 20:12:32 -07:00
html += '<button is="paper-icon-button-light" class="previousTrackButton mediaButton autoSize"><i class="md-icon">skip_previous</i></button>';
2014-06-21 22:52:31 -07:00
2017-01-05 13:21:07 -07:00
html += '<button is="paper-icon-button-light" class="playPauseButton mediaButton autoSize"><i class="md-icon">pause</i></button>';
2014-06-24 14:45:21 -07:00
2016-06-14 20:12:32 -07:00
html += '<button is="paper-icon-button-light" class="stopButton mediaButton autoSize"><i class="md-icon">stop</i></button>';
html += '<button is="paper-icon-button-light" class="nextTrackButton mediaButton autoSize"><i class="md-icon">skip_next</i></button>';
2015-06-27 12:53:36 -07:00
html += '<div class="nowPlayingBarCurrentTime"></div>';
html += '</div>';
2015-06-27 12:53:36 -07:00
html += '<div class="nowPlayingBarRight">';
2016-06-14 20:12:32 -07:00
html += '<button is="paper-icon-button-light" class="muteButton mediaButton autoSize"><i class="md-icon">volume_up</i></button>';
2016-06-24 10:30:29 -07:00
html += '<div class="sliderContainer nowPlayingBarVolumeSliderContainer hide" style="width:100px;vertical-align:middle;display:inline-flex;">';
2016-06-13 12:02:48 -07:00
html += '<input type="range" is="emby-slider" pin step="1" min="0" max="100" value="0" class="nowPlayingBarVolumeSlider"/>';
html += '</div>';
2016-06-14 20:12:32 -07:00
html += '<button is="paper-icon-button-light" class="toggleRepeatButton mediaButton autoSize"><i class="md-icon">repeat</i></button>';
2015-07-26 14:02:23 -07:00
html += '<div class="nowPlayingBarUserDataButtons">';
html += '</div>';
2017-01-05 13:21:07 -07:00
html += '<button is="paper-icon-button-light" class="playPauseButton mediaButton autoSize"><i class="md-icon">pause</i></button>';
2017-01-17 11:00:19 -07:00
html += '<button is="paper-icon-button-light" class="remoteControlButton mediaButton autoSize"><i class="md-icon">playlist_play</i></button>';
2015-06-27 12:53:36 -07:00
2016-08-01 13:06:30 -07:00
html += '</div>';
2015-06-27 12:53:36 -07:00
html += '</div>';
html += '</div>';
return html;
}
2017-01-02 23:51:36 -07:00
function onSlideDownComplete() {
this.classList.add('hide');
}
2015-12-14 08:43:03 -07:00
function slideDown(elem) {
2017-01-02 23:51:36 -07:00
requestAnimationFrame(function () {
2015-12-14 08:43:03 -07:00
2017-01-02 23:51:36 -07:00
// trigger reflow
void elem.offsetWidth;
2015-12-14 08:43:03 -07:00
2017-01-02 23:51:36 -07:00
elem.classList.add('nowPlayingBar-hidden');
2015-12-14 08:43:03 -07:00
2017-01-02 23:51:36 -07:00
dom.addEventListener(elem, dom.whichTransitionEvent(), onSlideDownComplete, {
once: true
});
2015-12-14 08:43:03 -07:00
});
}
function slideUp(elem) {
elem.classList.remove('hide');
2017-01-02 23:51:36 -07:00
dom.removeEventListener(elem, dom.whichTransitionEvent(), onSlideDownComplete, {
once: true
});
2016-08-01 13:06:30 -07:00
requestAnimationFrame(function () {
2017-01-02 23:51:36 -07:00
// trigger reflow
void elem.offsetWidth;
elem.classList.remove('nowPlayingBar-hidden');
2016-08-01 13:06:30 -07:00
});
}
function slideUpToFullScreen(elem) {
if (!elem.classList.contains('hide')) {
return;
}
elem.classList.remove('hide');
2016-08-06 07:07:44 -07:00
if (!browser.animate || browser.slow) {
2015-12-14 08:43:03 -07:00
return;
2015-12-27 11:47:48 -07:00
}
2015-12-14 08:43:03 -07:00
requestAnimationFrame(function () {
var keyframes = [
2016-08-01 13:06:30 -07:00
{ transform: 'none', offset: 0 },
{ transform: 'translateY(-100%)', offset: 1 }];
2015-12-14 08:43:03 -07:00
var timing = { duration: 200, iterations: 1, fill: 'both', easing: 'ease-out' };
elem.animate(keyframes, timing);
});
}
2017-01-05 13:21:07 -07:00
function onPlayPauseClick() {
playbackManager.playPause(currentPlayer);
2016-06-01 23:08:47 -07:00
}
function bindEvents(elem) {
2016-02-06 14:11:30 -07:00
currentTimeElement = elem.querySelector('.nowPlayingBarCurrentTime');
2016-02-05 10:04:46 -07:00
nowPlayingImageElement = elem.querySelector('.nowPlayingImage');
2016-02-06 14:11:30 -07:00
nowPlayingTextElement = elem.querySelector('.nowPlayingBarText');
nowPlayingUserData = elem.querySelector('.nowPlayingBarUserDataButtons');
2016-06-01 23:08:47 -07:00
muteButton = elem.querySelector('.muteButton');
muteButton.addEventListener('click', function () {
if (currentPlayer) {
2017-01-09 13:27:39 -07:00
playbackManager.toggleMute(currentPlayer);
}
});
2016-06-01 23:08:47 -07:00
elem.querySelector('.stopButton').addEventListener('click', function () {
if (currentPlayer) {
2017-01-02 23:51:36 -07:00
playbackManager.stop(currentPlayer);
}
});
2016-06-01 23:08:47 -07:00
var i, length;
2017-01-05 13:21:07 -07:00
playPauseButtons = elem.querySelectorAll('.playPauseButton');
for (i = 0, length = playPauseButtons.length; i < length; i++) {
playPauseButtons[i].addEventListener('click', onPlayPauseClick);
2016-06-01 23:08:47 -07:00
}
2016-06-01 23:08:47 -07:00
elem.querySelector('.nextTrackButton').addEventListener('click', function () {
if (currentPlayer) {
2017-01-02 23:51:36 -07:00
playbackManager.nextTrack(currentPlayer);
}
});
2016-06-01 23:08:47 -07:00
elem.querySelector('.previousTrackButton').addEventListener('click', function () {
if (currentPlayer) {
2017-01-02 23:51:36 -07:00
playbackManager.previousTrack(currentPlayer);
}
});
elem.querySelector('.remoteControlButton').addEventListener('click', function () {
2015-07-03 04:51:45 -07:00
showRemoteControl();
});
2016-06-01 23:08:47 -07:00
toggleRepeatButton = elem.querySelector('.toggleRepeatButton');
toggleRepeatButton.addEventListener('click', function () {
2015-07-26 14:02:23 -07:00
2015-07-30 07:34:46 -07:00
if (currentPlayer) {
var state = lastPlayerState || {};
2015-07-26 14:02:23 -07:00
switch ((state.PlayState || {}).RepeatMode) {
case 'RepeatAll':
2017-01-02 23:51:36 -07:00
playbackManager.setRepeatMode('RepeatOne', currentPlayer);
2015-07-26 14:02:23 -07:00
break;
case 'RepeatOne':
2017-01-02 23:51:36 -07:00
playbackManager.setRepeatMode('RepeatNone', currentPlayer);
2015-07-26 14:02:23 -07:00
break;
2015-07-30 07:34:46 -07:00
default:
2017-01-02 23:51:36 -07:00
playbackManager.setRepeatMode('RepeatAll', currentPlayer);
2015-07-30 07:34:46 -07:00
break;
2015-07-26 14:02:23 -07:00
}
}
2016-06-01 23:08:47 -07:00
});
2015-07-26 14:02:23 -07:00
2016-06-14 20:12:32 -07:00
toggleRepeatButtonIcon = toggleRepeatButton.querySelector('i');
2016-05-07 10:47:41 -07:00
2016-06-24 10:30:29 -07:00
volumeSlider = elem.querySelector('.nowPlayingBarVolumeSlider');
volumeSliderContainer = elem.querySelector('.nowPlayingBarVolumeSliderContainer');
2016-06-01 23:08:47 -07:00
2017-01-02 23:51:36 -07:00
if (appHost.supports('physicalvolumecontrol')) {
2016-06-24 10:30:29 -07:00
volumeSliderContainer.classList.add('hide');
} else {
volumeSliderContainer.classList.remove('hide');
}
2016-06-24 10:30:29 -07:00
volumeSlider.addEventListener('change', function () {
2016-06-24 10:30:29 -07:00
if (currentPlayer) {
currentPlayer.setVolume(this.value);
}
2016-06-24 10:30:29 -07:00
});
2016-06-24 10:30:29 -07:00
positionSlider = elem.querySelector('.nowPlayingBarPositionSlider');
positionSlider.addEventListener('change', function () {
2017-01-02 23:51:36 -07:00
if (currentPlayer) {
2014-04-22 10:25:54 -07:00
2016-06-24 10:30:29 -07:00
var newPercent = parseFloat(this.value);
2015-06-27 12:53:36 -07:00
2017-01-02 23:51:36 -07:00
playbackManager.seekPercent(newPercent, currentPlayer);
2016-06-24 10:30:29 -07:00
}
2015-06-27 12:53:36 -07:00
2016-06-24 10:30:29 -07:00
});
2015-06-27 12:53:36 -07:00
2016-06-24 10:30:29 -07:00
positionSlider.getBubbleText = function (value) {
2015-06-27 12:53:36 -07:00
2016-06-24 10:30:29 -07:00
var state = lastPlayerState;
2015-06-27 12:53:36 -07:00
2017-01-02 23:51:36 -07:00
if (!state || !state.NowPlayingItem || !currentRuntimeTicks) {
2016-06-24 10:30:29 -07:00
return '--:--';
}
2017-01-02 23:51:36 -07:00
var ticks = currentRuntimeTicks;
2016-06-24 10:30:29 -07:00
ticks /= 100;
ticks *= value;
2015-06-27 12:53:36 -07:00
2016-06-24 10:30:29 -07:00
return datetime.getDisplayRunningTime(ticks);
};
}
2016-05-11 07:36:28 -07:00
function showRemoteControl(tabIndex) {
2016-05-11 07:36:28 -07:00
if (tabIndex) {
Dashboard.navigate('nowplaying.html?tab=' + tabIndex);
} else {
Dashboard.navigate('nowplaying.html');
}
}
2015-12-14 08:43:03 -07:00
var nowPlayingBarElement;
function getNowPlayingBar() {
if (nowPlayingBarElement) {
return Promise.resolve(nowPlayingBarElement);
}
return new Promise(function (resolve, reject) {
2016-08-22 13:47:16 -07:00
require(['appfooter-shared', 'itemShortcuts', 'css!css/nowplayingbar.css', 'emby-slider'], function (appfooter, itemShortcuts) {
2016-08-22 13:47:16 -07:00
var parentContainer = appfooter.element;
nowPlayingBarElement = parentContainer.querySelector('.nowPlayingBar');
2015-12-14 08:43:03 -07:00
if (nowPlayingBarElement) {
resolve(nowPlayingBarElement);
return;
}
2016-08-31 12:17:11 -07:00
parentContainer.insertAdjacentHTML('afterbegin', getNowPlayingBarHtml());
2016-08-22 13:47:16 -07:00
nowPlayingBarElement = parentContainer.querySelector('.nowPlayingBar');
2015-06-27 16:18:09 -07:00
2016-08-06 07:07:44 -07:00
if (browser.safari && browser.slow) {
2015-12-14 08:43:03 -07:00
// Not handled well here. The wrong elements receive events, bar doesn't update quickly enough, etc.
nowPlayingBarElement.classList.add('noMediaProgress');
}
itemShortcuts.on(nowPlayingBarElement);
2015-12-14 08:43:03 -07:00
bindEvents(nowPlayingBarElement);
resolve(nowPlayingBarElement);
});
});
}
2015-03-17 21:09:31 -07:00
2014-04-19 10:43:12 -07:00
function showButton(button) {
2016-06-01 23:08:47 -07:00
button.classList.remove('hide');
2014-04-19 10:43:12 -07:00
}
2015-03-17 21:09:31 -07:00
2014-04-19 10:43:12 -07:00
function hideButton(button) {
2016-06-01 23:08:47 -07:00
button.classList.add('hide');
2014-04-19 10:43:12 -07:00
}
2017-01-02 23:51:36 -07:00
function updatePlayPauseState(isPaused) {
2015-12-14 08:43:03 -07:00
2016-06-01 23:08:47 -07:00
var i, length;
2017-01-06 12:37:57 -07:00
if (playPauseButtons) {
if (isPaused) {
2017-01-06 12:37:57 -07:00
for (i = 0, length = playPauseButtons.length; i < length; i++) {
playPauseButtons[i].querySelector('i').innerHTML = 'play_arrow';
}
2017-01-06 12:37:57 -07:00
} else {
for (i = 0, length = playPauseButtons.length; i < length; i++) {
playPauseButtons[i].querySelector('i').innerHTML = 'pause';
}
2016-06-01 23:08:47 -07:00
}
}
2017-01-02 23:51:36 -07:00
}
2017-01-02 23:51:36 -07:00
function updatePlayerStateInternal(event, state) {
2017-01-02 23:51:36 -07:00
showNowPlayingBar();
2017-01-02 23:51:36 -07:00
lastPlayerState = state;
2017-01-02 23:51:36 -07:00
var playerInfo = playbackManager.getPlayerInfo();
2017-01-02 23:51:36 -07:00
var playState = state.PlayState || {};
2017-01-02 23:51:36 -07:00
updatePlayPauseState(playState.IsPaused);
2014-04-30 08:07:02 -07:00
2017-01-02 23:51:36 -07:00
var supportedCommands = playerInfo.supportedCommands;
currentPlayerSupportedCommands = supportedCommands;
2015-07-11 17:33:50 -07:00
2017-01-02 23:51:36 -07:00
if (supportedCommands.indexOf('SetRepeatMode') == -1) {
toggleRepeatButton.classList.add('hide');
} else {
toggleRepeatButton.classList.remove('hide');
}
2017-01-02 23:51:36 -07:00
if (playState.RepeatMode == 'RepeatAll') {
toggleRepeatButtonIcon.innerHTML = "repeat";
toggleRepeatButton.classList.add('repeatActive');
}
else if (playState.RepeatMode == 'RepeatOne') {
toggleRepeatButtonIcon.innerHTML = "repeat_one";
toggleRepeatButton.classList.add('repeatActive');
} else {
toggleRepeatButtonIcon.innerHTML = "repeat";
toggleRepeatButton.classList.remove('repeatActive');
}
2017-01-02 23:51:36 -07:00
updatePlayerVolumeState(playState.IsMuted, playState.VolumeLevel);
2017-01-02 23:51:36 -07:00
if (positionSlider && !positionSlider.dragging) {
positionSlider.disabled = !playState.CanSeek;
}
2017-01-02 23:51:36 -07:00
var nowPlayingItem = state.NowPlayingItem || {};
updateTimeDisplay(playState.PositionTicks, nowPlayingItem.RunTimeTicks);
updateNowPlayingInfo(state);
}
2017-01-02 23:51:36 -07:00
function updateTimeDisplay(positionTicks, runtimeTicks) {
// See bindEvents for why this is necessary
if (positionSlider && !positionSlider.dragging) {
if (runtimeTicks) {
2015-05-21 13:53:14 -07:00
2017-01-02 23:51:36 -07:00
var pct = positionTicks / runtimeTicks;
pct *= 100;
2015-01-03 12:38:22 -07:00
2017-01-02 23:51:36 -07:00
positionSlider.value = pct;
} else {
positionSlider.value = 0;
}
}
2017-01-05 13:21:07 -07:00
if (currentTimeElement) {
2017-01-11 10:52:22 -07:00
2017-01-05 13:21:07 -07:00
var timeText = positionTicks == null ? '--:--' : datetime.getDisplayRunningTime(positionTicks);
2017-01-02 23:51:36 -07:00
2017-01-05 13:21:07 -07:00
if (runtimeTicks) {
timeText += " / " + datetime.getDisplayRunningTime(runtimeTicks);
}
2017-01-02 23:51:36 -07:00
2017-01-05 13:21:07 -07:00
currentTimeElement.innerHTML = timeText;
}
2017-01-02 23:51:36 -07:00
}
function updatePlayerVolumeState(isMuted, volumeLevel) {
var supportedCommands = currentPlayerSupportedCommands;
2015-01-03 12:38:22 -07:00
2015-05-21 13:53:14 -07:00
var showMuteButton = true;
var showVolumeSlider = true;
2015-01-03 12:38:22 -07:00
2017-01-09 13:27:39 -07:00
if (supportedCommands.indexOf('ToggleMute') == -1) {
2015-05-21 13:53:14 -07:00
showMuteButton = false;
}
2017-01-02 23:51:36 -07:00
if (isMuted) {
2017-01-09 13:27:39 -07:00
muteButton.querySelector('i').innerHTML = '&#xE04F;';
2015-01-03 12:38:22 -07:00
} else {
2017-01-09 13:27:39 -07:00
muteButton.querySelector('i').innerHTML = '&#xE050;';
2015-05-21 13:53:14 -07:00
}
if (supportedCommands.indexOf('SetVolume') == -1) {
showVolumeSlider = false;
}
2017-01-02 23:51:36 -07:00
if (currentPlayer.isLocalPlayer && appHost.supports('physicalvolumecontrol')) {
2015-05-21 13:53:14 -07:00
showMuteButton = false;
showVolumeSlider = false;
}
if (showMuteButton) {
2015-01-03 12:38:22 -07:00
showButton(muteButton);
2015-05-21 13:53:14 -07:00
} else {
hideButton(muteButton);
}
2015-07-11 17:33:50 -07:00
// See bindEvents for why this is necessary
if (volumeSlider) {
2015-12-14 08:43:03 -07:00
if (showVolumeSlider) {
2016-06-24 10:30:29 -07:00
volumeSliderContainer.classList.remove('hide');
2015-12-14 08:43:03 -07:00
} else {
2016-06-24 10:30:29 -07:00
volumeSliderContainer.classList.add('hide');
2015-12-14 08:43:03 -07:00
}
2015-05-21 13:53:14 -07:00
2015-07-11 17:33:50 -07:00
if (!volumeSlider.dragging) {
2017-01-02 23:51:36 -07:00
volumeSlider.value = volumeLevel || 0;
2015-07-11 17:33:50 -07:00
}
2015-01-03 12:38:22 -07:00
}
}
function getTextActionButton(item, text) {
if (!text) {
text = itemHelper.getDisplayName(item);
}
var html = '<button data-id="' + item.Id + '" data-type="' + item.Type + '" data-mediatype="' + item.MediaType + '" data-channelid="' + item.ChannelId + '" data-isfolder="' + item.IsFolder + '" type="button" class="itemAction textActionButton" data-action="link">';
html += text;
html += '</button>';
return html;
}
2017-01-03 12:37:10 -07:00
function seriesImageUrl(item, options) {
2017-01-09 10:05:34 -07:00
if (!item) {
throw new Error('item cannot be null!');
}
2017-01-03 12:37:10 -07:00
if (item.Type !== 'Episode') {
return null;
}
2017-01-03 12:37:10 -07:00
options = options || {};
options.type = options.type || "Primary";
if (options.type === 'Primary') {
if (item.SeriesPrimaryImageTag) {
options.tag = item.SeriesPrimaryImageTag;
return connectionManager.getApiClient(item.ServerId).getScaledImageUrl(item.SeriesId, options);
}
2017-01-03 12:37:10 -07:00
}
2017-01-03 12:37:10 -07:00
if (options.type === 'Thumb') {
2015-06-26 20:27:38 -07:00
2017-01-03 12:37:10 -07:00
if (item.SeriesThumbImageTag) {
2017-01-03 12:37:10 -07:00
options.tag = item.SeriesThumbImageTag;
2015-03-17 21:09:31 -07:00
2017-01-03 12:37:10 -07:00
return connectionManager.getApiClient(item.ServerId).getScaledImageUrl(item.SeriesId, options);
}
if (item.ParentThumbImageTag) {
2017-01-03 12:37:10 -07:00
options.tag = item.ParentThumbImageTag;
2017-01-03 12:37:10 -07:00
return connectionManager.getApiClient(item.ServerId).getScaledImageUrl(item.ParentThumbItemId, options);
}
}
2017-01-03 12:37:10 -07:00
return null;
}
2017-01-03 12:37:10 -07:00
function imageUrl(item, options) {
2017-01-09 10:05:34 -07:00
if (!item) {
throw new Error('item cannot be null!');
}
2017-01-03 12:37:10 -07:00
options = options || {};
options.type = options.type || "Primary";
2017-01-03 12:37:10 -07:00
if (item.ImageTags && item.ImageTags[options.type]) {
options.tag = item.ImageTags[options.type];
2017-01-09 13:27:39 -07:00
return connectionManager.getApiClient(item.ServerId).getScaledImageUrl(item.PrimaryImageItemId || item.Id, options);
}
2017-01-03 12:37:10 -07:00
2017-01-07 22:52:51 -07:00
if (item.AlbumId && item.AlbumPrimaryImageTag) {
2017-01-03 12:37:10 -07:00
2017-01-07 22:52:51 -07:00
options.tag = item.AlbumPrimaryImageTag;
return connectionManager.getApiClient(item.ServerId).getScaledImageUrl(item.AlbumId, options);
}
2017-01-03 12:37:10 -07:00
return null;
}
var currentImgUrl;
function updateNowPlayingInfo(state) {
2017-01-09 10:05:34 -07:00
var nowPlayingItem = state.NowPlayingItem;
nowPlayingTextElement.innerHTML = nowPlayingItem ? nowPlayingHelper.getNowPlayingNames(nowPlayingItem).map(function (nowPlayingName) {
2017-01-03 12:37:10 -07:00
if (nowPlayingName.item) {
return '<div>' + getTextActionButton(nowPlayingName.item, nowPlayingName.text) + '</div>';
}
return '<div>' + nowPlayingName.text + '</div>';
2017-01-09 10:05:34 -07:00
}).join('') : '';
2017-01-03 12:37:10 -07:00
var imgHeight = 70;
2017-01-09 10:05:34 -07:00
var url = nowPlayingItem ? (seriesImageUrl(nowPlayingItem, {
2017-01-03 12:37:10 -07:00
height: imgHeight
}) || imageUrl(nowPlayingItem, {
height: imgHeight
2017-01-09 10:05:34 -07:00
})) : null;
2017-01-03 12:37:10 -07:00
2017-01-14 01:03:46 -07:00
if (url !== currentImgUrl) {
currentImgUrl = url;
2017-01-14 01:03:46 -07:00
imageLoader.lazyImage(nowPlayingImageElement, url);
}
2017-01-10 13:17:29 -07:00
if (nowPlayingItem.Id) {
2017-01-14 01:03:46 -07:00
ApiClient.getItem(Dashboard.getCurrentUserId(), nowPlayingItem.Id).then(function(item) {
2016-09-26 22:13:56 -07:00
userdataButtons.fill({
2016-07-17 23:45:29 -07:00
item: item,
2016-09-26 22:13:56 -07:00
includePlayed: false,
element: nowPlayingUserData
2016-07-17 23:45:29 -07:00
});
});
2017-01-14 01:03:46 -07:00
} else {
userdataButtons.destroy({
element: nowPlayingUserData
});
}
}
function onPlaybackStart(e, state) {
2015-12-23 10:46:01 -07:00
console.log('nowplaying event: ' + e.type);
2014-04-19 10:43:12 -07:00
var player = this;
onStateChanged.call(player, e, state);
}
function showNowPlayingBar() {
2015-12-14 08:43:03 -07:00
getNowPlayingBar().then(slideUp);
}
function hideNowPlayingBar() {
2017-01-02 23:51:36 -07:00
isEnabled = false;
// Use a timeout to prevent the bar from hiding and showing quickly
// in the event of a stop->play command
// Don't call getNowPlayingBar here because we don't want to end up creating it just to hide it
2015-06-28 07:45:21 -07:00
var elem = document.getElementsByClassName('nowPlayingBar')[0];
if (elem) {
2017-01-11 10:52:22 -07:00
// If it's not currently visible, don't bother with the animation
// transitionend events not firing in mobile chrome/safari when hidden
if (document.body.classList.contains('hiddenNowPlayingBar')) {
dom.removeEventListener(elem, dom.whichTransitionEvent(), onSlideDownComplete, {
once: true
});
elem.classList.add('hide');
elem.classList.add('nowPlayingBar-hidden');
} else {
slideDown(elem);
}
2015-06-28 07:45:21 -07:00
}
}
2017-01-11 21:27:01 -07:00
function onPlaybackStopped(e, stopInfo) {
2015-12-23 10:46:01 -07:00
console.log('nowplaying event: ' + e.type);
var player = this;
2017-01-11 21:27:01 -07:00
if (stopInfo.nextMediaType !== 'Audio') {
hideNowPlayingBar();
}
}
2017-01-02 23:51:36 -07:00
function onPlayPauseStateChanged(e) {
if (!isEnabled) {
return;
}
var player = this;
updatePlayPauseState(player.paused());
}
function onStateChanged(event, state) {
2015-12-23 10:46:01 -07:00
//console.log('nowplaying event: ' + e.type);
var player = this;
2017-01-02 23:51:36 -07:00
if (!state.NowPlayingItem) {
hideNowPlayingBar();
return;
}
if (player.isLocalPlayer && state.NowPlayingItem && state.NowPlayingItem.MediaType == 'Video') {
hideNowPlayingBar();
return;
}
isEnabled = true;
if (nowPlayingBarElement) {
updatePlayerStateInternal(event, state);
return;
}
2017-01-02 23:51:36 -07:00
getNowPlayingBar().then(function () {
updatePlayerStateInternal(event, state);
});
}
function onTimeUpdate(e) {
if (!isEnabled) {
return;
}
// Try to avoid hammering the document with changes
var now = new Date().getTime();
if ((now - lastUpdateTime) < 700) {
return;
}
lastUpdateTime = now;
var player = this;
currentRuntimeTicks = playbackManager.duration(player);
updateTimeDisplay(playbackManager.currentTime(player), currentRuntimeTicks);
}
function releaseCurrentPlayer() {
2017-01-02 23:51:36 -07:00
var player = currentPlayer;
2017-01-02 23:51:36 -07:00
if (player) {
events.off(player, 'playbackstart', onPlaybackStart);
2017-01-05 13:21:07 -07:00
events.off(player, 'statechange', onPlaybackStart);
events.off(player, 'repeatmodechange', onPlaybackStart);
2017-01-02 23:51:36 -07:00
events.off(player, 'playbackstop', onPlaybackStopped);
events.off(player, 'volumechange', onVolumeChanged);
events.off(player, 'pause', onPlayPauseStateChanged);
events.off(player, 'playing', onPlayPauseStateChanged);
events.off(player, 'timeupdate', onTimeUpdate);
2015-06-29 11:45:42 -07:00
currentPlayer = null;
hideNowPlayingBar();
}
}
2015-01-03 12:38:22 -07:00
function onVolumeChanged(e) {
2017-01-02 23:51:36 -07:00
if (!isEnabled) {
return;
}
2015-03-17 21:09:31 -07:00
2017-01-02 23:51:36 -07:00
var player = this;
2015-01-03 12:38:22 -07:00
2017-01-02 23:51:36 -07:00
updatePlayerVolumeState(player.isMuted(), player.getVolume());
2015-01-03 12:38:22 -07:00
}
function bindToPlayer(player) {
2017-01-02 23:51:36 -07:00
if (player === currentPlayer) {
return;
}
releaseCurrentPlayer();
currentPlayer = player;
2017-01-02 23:51:36 -07:00
if (!player) {
return;
}
2017-01-02 23:51:36 -07:00
playbackManager.getPlayerState(player).then(function (state) {
2015-03-17 21:09:31 -07:00
onStateChanged.call(player, { type: 'init' }, state);
});
2016-08-01 13:06:30 -07:00
events.on(player, 'playbackstart', onPlaybackStart);
2017-01-05 13:21:07 -07:00
events.on(player, 'statechange', onPlaybackStart);
// TODO: Replace this with smaller changes on repeatmodechange.
// For now go cheap and just refresh the entire component
events.on(player, 'repeatmodechange', onPlaybackStart);
2016-08-01 13:06:30 -07:00
events.on(player, 'playbackstop', onPlaybackStopped);
events.on(player, 'volumechange', onVolumeChanged);
2017-01-02 23:51:36 -07:00
events.on(player, 'pause', onPlayPauseStateChanged);
events.on(player, 'playing', onPlayPauseStateChanged);
events.on(player, 'timeupdate', onTimeUpdate);
}
2017-01-02 23:51:36 -07:00
events.on(playbackManager, 'playerchange', function () {
bindToPlayer(playbackManager.getCurrentPlayer());
});
2017-01-02 23:51:36 -07:00
bindToPlayer(playbackManager.getCurrentPlayer());
2015-12-14 08:43:03 -07:00
});