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

749 lines
20 KiB
JavaScript
Raw Normal View History

2014-04-27 15:51:07 -07:00
(function (window, document, $, setTimeout, clearTimeout) {
var currentPlayer;
2014-04-28 08:05:28 -07:00
var lastPlayerState;
2014-04-30 08:07:02 -07:00
var isPositionSliderActive;
2014-04-27 15:51:07 -07:00
2014-05-12 11:04:25 -07:00
function populateChapters(elem, chapters, itemId, runtimeTicks) {
var html = '';
for (var i = 0, length = chapters.length; i < length; i++) {
var chapter = chapters[i];
html += '<div data-positionticks="' + chapter.StartPositionTicks + '" class="posterItem backdropPosterItem chapterPosterItem">';
var imgUrl;
if (chapter.ImageTag) {
imgUrl = ApiClient.getScaledImageUrl(itemId, {
width: 240,
2014-05-12 11:04:25 -07:00
tag: chapter.ImageTag,
type: "Chapter",
index: i
});
} else {
imgUrl = "css/images/items/list/chapter.png";
}
var dataSrc = ' data-src="' + imgUrl + '"';
2015-03-06 20:53:31 -07:00
// TODO: This markup needs to be converted to the newer card layout pattern
2014-05-12 11:04:25 -07:00
html += '<div class="posterItemImage lazy"' + dataSrc + '>';
2015-03-06 20:53:31 -07:00
html += '<div class="posterItemTextOverlay" style="position:absolute;bottom:0;left:0;right:0;">';
2014-05-12 11:04:25 -07:00
if (chapter.Name) {
html += "<div class='posterItemText'>";
html += chapter.Name;
html += "</div>";
}
html += "<div class='posterItemProgress posterItemText'>";
var pct = 100 * (chapter.StartPositionTicks / runtimeTicks);
2015-03-06 20:53:31 -07:00
html += '<progress class="itemProgressBar" min="0" max="100" value="' + pct + '" style="opacity:.8;width:100%;"></progress>';
2014-05-12 11:04:25 -07:00
html += "</div>";
html += "</div>";
html += "</div>";
html += "</div>";
}
2015-03-06 20:53:31 -07:00
elem.html(html).trigger('create').lazyChildren();
2014-05-12 11:04:25 -07:00
}
function selectCurrentChapter(elem, positionTicks) {
var elems = $('.chapterPosterItem', elem).removeClass('currentChapter');
var matches = elems.get().filter(function (i) {
var ticks = i.getAttribute('data-positionticks');
return positionTicks >= ticks;
});
var chapterElem = matches[matches.length - 1];
$(chapterElem).addClass('currentChapter');
chapterElem.scrollIntoView();
elem[0].scrollLeft += 50;
}
function showChapterMenu(page, item, currentPositionTicks) {
$('.chapterMenuOverlay', page).show();
var elem = $('.chapterMenu', page).show();
if (item.Id == elem.attr('data-itemid')) {
selectCurrentChapter(elem, currentPositionTicks);
return;
}
var innerElem = $('.chapterMenuInner', elem);
populateChapters(innerElem, item.Chapters, item.Id, item.RunTimeTicks);
elem.attr('data-itemid', item.Id);
selectCurrentChapter(elem, currentPositionTicks);
}
function hideChapterMenu(page) {
$('.chapterMenuOverlay', page).hide();
$('.chapterMenu', page).hide();
}
2014-05-10 10:28:03 -07:00
function showAudioMenu(page, item, currentIndex) {
2014-05-07 22:04:39 -07:00
var streams = (item.MediaStreams || []).filter(function (i) {
return i.Type == 'Audio';
});
var elem = $('#popupAudioTrackMenu', page);
2014-05-30 12:23:56 -07:00
var html = '<ul data-role="listview" data-inset="true" style="min-width: 210px;"><li data-role="list-divider">' + Globalize.translate('HeaderSelectAudio') + '</li>';
2014-05-07 22:04:39 -07:00
html += streams.map(function (s) {
2014-05-10 10:28:03 -07:00
var streamHtml = '<li><a data-index="' + s.Index + '" href="#" class="lnkTrackOption">';
streamHtml += '<h3>';
2014-05-12 11:04:25 -07:00
2014-05-10 10:28:03 -07:00
if (s.Index == currentIndex) {
streamHtml += '<img src="css/images/checkmarkgreen.png" style="width:18px;border-radius:3px;margin-right:.5em;vertical-align:top;" />';
}
2014-05-07 22:04:39 -07:00
streamHtml += (s.Codec || '').toUpperCase();
if (s.Profile) {
streamHtml += ' ' + s.Profile;
}
2014-05-08 13:09:53 -07:00
streamHtml += '</h3><p>';
2014-05-07 22:04:39 -07:00
var extras = [];
if (s.Language) {
extras.push(s.Language);
}
if (s.Layout) {
extras.push(s.Layout);
}
else if (s.Channels) {
extras.push(s.Channels + ' ch');
}
if (s.BitRate) {
extras.push((parseInt(s.BitRate / 1000)) + ' kbps');
}
streamHtml += extras.join(' - ');
2014-05-08 13:09:53 -07:00
streamHtml += '</p></a></li>';
2014-05-07 22:04:39 -07:00
return streamHtml;
}).join('');
html += '</ul>';
2014-05-10 10:28:03 -07:00
$('.trackList', elem).html(html).trigger('create');
2014-05-07 22:04:39 -07:00
elem.popup('open');
}
2014-05-10 10:28:03 -07:00
function showSubtitleMenu(page, item, currentIndex) {
2014-05-07 22:04:39 -07:00
2014-05-10 10:28:03 -07:00
var currentStreamImage = '<img src="css/images/checkmarkgreen.png" style="width:18px;border-radius:3px;margin-right:.5em;vertical-align:top;" />';
2014-05-12 11:04:25 -07:00
2014-05-07 22:04:39 -07:00
var streams = (item.MediaStreams || []).filter(function (i) {
return i.Type == 'Subtitle';
});
var elem = $('#popupSubtitleTrackMenu', page);
2014-05-30 12:23:56 -07:00
var html = '<ul data-role="listview" data-inset="true" style="min-width: 210px;"><li data-role="list-divider">' + Globalize.translate('HeaderSelectSubtitles') + '</li>';
2014-05-10 10:28:03 -07:00
html += '<li><a href="#" data-index="-1" class="lnkTrackOption"><h3>';
if (currentIndex == null) {
html += currentStreamImage;
}
2014-05-07 22:04:39 -07:00
2014-05-10 10:28:03 -07:00
html += 'Off';
html += '</h3></a></li>';
2014-05-07 22:04:39 -07:00
html += streams.map(function (s) {
2014-05-10 10:28:03 -07:00
var streamHtml = '<li><a data-index="' + s.Index + '" href="#" class="lnkTrackOption">';
streamHtml += '<h3>';
if (s.Index == currentIndex) {
streamHtml += currentStreamImage;
}
2014-05-07 22:04:39 -07:00
2014-05-30 12:23:56 -07:00
streamHtml += (s.Language || Globalize.translate('LabelUnknownLanguage'));
2014-05-07 22:04:39 -07:00
if (s.IsDefault && s.IsForced) {
2014-05-30 12:23:56 -07:00
streamHtml += ' ' + Globalize.translate('LabelDefaultForcedStream');
2014-05-07 22:04:39 -07:00
}
else if (s.IsDefault) {
2014-05-30 12:23:56 -07:00
streamHtml += ' ' + Globalize.translate('LabelDefaultStream');
2014-05-07 22:04:39 -07:00
}
else if (s.IsForced) {
2014-05-30 12:23:56 -07:00
streamHtml += ' ' + Globalize.translate('LabelForcedStream');
2014-05-07 22:04:39 -07:00
}
2014-05-08 13:09:53 -07:00
streamHtml += '</h3><p>';
2014-05-07 22:04:39 -07:00
streamHtml += (s.Codec || '').toUpperCase();
2014-05-08 13:09:53 -07:00
streamHtml += '</p></a></li>';
2014-05-07 22:04:39 -07:00
return streamHtml;
}).join('');
2014-05-10 10:28:03 -07:00
html += '</ul>';
$('.trackList', elem).html(html).trigger('create');
2014-05-07 22:04:39 -07:00
elem.popup('open');
}
2014-04-27 15:51:07 -07:00
function bindEvents(page) {
2014-05-08 13:09:53 -07:00
$('.tabButton', page).on('click', function () {
2014-04-27 15:51:07 -07:00
2014-05-08 13:09:53 -07:00
var elem = $('.' + this.getAttribute('data-tab'), page);
2014-04-27 15:51:07 -07:00
elem.siblings('.tabContent').hide();
elem.show();
2014-05-08 13:09:53 -07:00
$('.tabButton', page).removeClass('ui-btn-active');
$(this).addClass('ui-btn-active');
2014-04-27 15:51:07 -07:00
});
2014-05-12 11:04:25 -07:00
$('.chapterMenuOverlay', page).on('click', function () {
hideChapterMenu(page);
});
$('.chapterMenu', page).on('click', '.chapterPosterItem', function () {
if (currentPlayer) {
2015-03-04 13:37:35 -07:00
var ticks = this.getAttribute('data-positionticks') || '0';
2014-05-12 11:04:25 -07:00
currentPlayer.seek(parseInt(ticks));
}
hideChapterMenu(page);
});
2014-04-28 08:05:28 -07:00
$('.btnCommand,.btnToggleFullscreen', page).on('click', function () {
2014-04-27 15:51:07 -07:00
2014-05-07 22:04:39 -07:00
if (currentPlayer) {
MediaController.sendCommand({
Name: this.getAttribute('data-command')
}, currentPlayer);
}
});
$('#popupAudioTrackMenu', page).on('click', '.lnkTrackOption', function () {
if (currentPlayer && lastPlayerState) {
var index = this.getAttribute('data-index');
currentPlayer.setAudioStreamIndex(parseInt(index));
$('#popupAudioTrackMenu', page).popup('close');
}
});
$('#popupSubtitleTrackMenu', page).on('click', '.lnkTrackOption', function () {
if (currentPlayer && lastPlayerState) {
var index = this.getAttribute('data-index');
currentPlayer.setSubtitleStreamIndex(parseInt(index));
$('#popupSubtitleTrackMenu', page).popup('close');
}
});
$('.btnAudioTracks', page).on('click', function () {
2014-05-10 10:28:03 -07:00
if (currentPlayer && lastPlayerState && lastPlayerState.PlayState) {
var currentIndex = lastPlayerState.PlayState.AudioStreamIndex;
showAudioMenu(page, lastPlayerState.NowPlayingItem, currentIndex);
2014-05-07 22:04:39 -07:00
}
});
$('.btnSubtitles', page).on('click', function () {
2014-05-10 10:28:03 -07:00
if (currentPlayer && lastPlayerState && lastPlayerState.PlayState) {
var currentIndex = lastPlayerState.PlayState.SubtitleStreamIndex;
showSubtitleMenu(page, lastPlayerState.NowPlayingItem, currentIndex);
2014-05-07 22:04:39 -07:00
}
});
$('.btnChapters', page).on('click', function () {
if (currentPlayer && lastPlayerState) {
2014-05-12 11:04:25 -07:00
var currentPositionTicks = lastPlayerState.PlayState.PositionTicks;
showChapterMenu(page, lastPlayerState.NowPlayingItem, currentPositionTicks);
2014-05-07 22:04:39 -07:00
}
2014-04-27 15:51:07 -07:00
});
2014-04-28 08:05:28 -07:00
$('.btnStop', page).on('click', function () {
2014-05-07 22:04:39 -07:00
if (currentPlayer) {
currentPlayer.stop();
}
2014-04-28 08:05:28 -07:00
});
$('.btnPlay', page).on('click', function () {
2014-05-07 22:04:39 -07:00
if (currentPlayer) {
currentPlayer.unpause();
}
2014-04-28 08:05:28 -07:00
});
$('.btnPause', page).on('click', function () {
2014-05-07 22:04:39 -07:00
if (currentPlayer) {
currentPlayer.pause();
}
2014-04-28 08:05:28 -07:00
});
$('.btnNextTrack', page).on('click', function () {
2014-05-07 22:04:39 -07:00
if (currentPlayer) {
currentPlayer.nextTrack();
}
2014-04-28 08:05:28 -07:00
});
$('.btnPreviousTrack', page).on('click', function () {
2014-05-07 22:04:39 -07:00
if (currentPlayer) {
currentPlayer.previousTrack();
}
2014-04-28 08:05:28 -07:00
});
2014-04-30 08:07:02 -07:00
$('.positionSlider', page).on('slidestart', function () {
isPositionSliderActive = true;
}).on('slidestop', function () {
isPositionSliderActive = false;
if (currentPlayer && lastPlayerState) {
var newPercent = parseFloat(this.value);
var newPositionTicks = (newPercent / 100) * lastPlayerState.NowPlayingItem.RunTimeTicks;
currentPlayer.seek(Math.floor(newPositionTicks));
}
});
2015-03-17 21:09:31 -07:00
$(page).on('swipedown', function () {
document.title = new Date().getTime();
history.back();
});
2015-03-18 09:56:14 -07:00
$(page).on('click', '.lnkPlayFromIndex', function () {
var index = parseInt(this.getAttribute('data-index'));
MediaController.currentPlaylistIndex(index);
loadPlaylist(page);
}).on('click', '.lnkRemoveFromPlaylist', function () {
var index = parseInt(this.getAttribute('data-index'));
MediaController.removeFromPlaylist(index);
loadPlaylist(page);
});
2014-04-27 15:51:07 -07:00
}
function onPlaybackStart(e, state) {
var player = this;
player.beginPlayerUpdates();
onStateChanged.call(player, e, state);
}
function onPlaybackStopped(e, state) {
var player = this;
player.endPlayerUpdates();
2014-04-28 08:05:28 -07:00
2014-04-30 20:24:55 -07:00
onStateChanged.call(player, e, {});
2014-04-27 15:51:07 -07:00
}
function onStateChanged(e, state) {
2014-04-28 08:05:28 -07:00
updatePlayerState($.mobile.activePage, state);
}
function showButton(button) {
button.removeClass('hide');
}
function hideButton(button) {
button.addClass('hide');
}
2014-05-07 22:04:39 -07:00
function hasStreams(item, type) {
return item && item.MediaStreams && item.MediaStreams.filter(function (i) {
return i.Type == type;
}).length > 0;
}
2014-04-28 08:05:28 -07:00
function updatePlayerState(page, state) {
lastPlayerState = state;
var item = state.NowPlayingItem;
var playerInfo = MediaController.getPlayerInfo();
var supportedCommands = playerInfo.supportedCommands;
var playState = state.PlayState || {};
2014-04-28 08:05:28 -07:00
$('.btnToggleFullscreen', page).buttonEnabled(item && item.MediaType == 'Video' && supportedCommands.indexOf('ToggleFullscreen') != -1);
2014-05-07 22:04:39 -07:00
$('.btnAudioTracks', page).buttonEnabled(hasStreams(item, 'Audio') && supportedCommands.indexOf('SetAudioStreamIndex') != -1);
$('.btnSubtitles', page).buttonEnabled(hasStreams(item, 'Subtitle') && supportedCommands.indexOf('SetSubtitleStreamIndex') != -1);
2014-05-12 11:04:25 -07:00
if (item && item.Chapters && item.Chapters.length && playState.CanSeek) {
2014-05-12 11:04:25 -07:00
$('.btnChapters', page).buttonEnabled(true);
} else {
$('.btnChapters', page).buttonEnabled(false);
hideChapterMenu(page);
}
2014-04-28 08:05:28 -07:00
$('.sendMessageElement', page).buttonEnabled(supportedCommands.indexOf('DisplayMessage') != -1);
2014-05-09 12:43:06 -07:00
$('.typeTextElement', page).buttonEnabled(supportedCommands.indexOf('SendString') != -1);
2014-04-28 08:05:28 -07:00
$('.btnStop', page).buttonEnabled(item != null);
$('.btnNextTrack', page).buttonEnabled(item != null);
$('.btnPreviousTrack', page).buttonEnabled(item != null);
2014-05-07 22:04:39 -07:00
2014-04-28 08:05:28 -07:00
var btnPause = $('.btnPause', page).buttonEnabled(item != null);
var btnPlay = $('.btnPlay', page).buttonEnabled(item != null);
2014-05-07 22:04:39 -07:00
2014-04-28 08:05:28 -07:00
if (playState.IsPaused) {
hideButton(btnPause);
showButton(btnPlay);
} else {
showButton(btnPause);
hideButton(btnPlay);
}
2014-04-30 08:07:02 -07:00
if (!isPositionSliderActive) {
var positionSlider = $('.positionSlider', page);
if (item && item.RunTimeTicks) {
var pct = playState.PositionTicks / item.RunTimeTicks;
pct *= 100;
positionSlider.val(pct);
} else {
positionSlider.val(0);
}
2014-05-07 22:04:39 -07:00
2014-04-30 08:07:02 -07:00
if (playState.CanSeek) {
positionSlider.slider("enable");
} else {
positionSlider.slider("disable");
}
positionSlider.slider('refresh');
}
if (playState.PositionTicks == null) {
$('.positionTime', page).html('--:--');
} else {
$('.positionTime', page).html(Dashboard.getDisplayTime(playState.PositionTicks));
}
if (item && item.RunTimeTicks != null) {
$('.runtime', page).html(Dashboard.getDisplayTime(item.RunTimeTicks));
} else {
$('.runtime', page).html('--:--');
}
if (item && item.MediaType == 'Video') {
$('.videoButton', page).css('visibility', 'visible');
} else {
$('.videoButton', page).css('visibility', 'hidden');
}
2014-05-07 22:04:39 -07:00
2014-04-30 08:07:02 -07:00
updateNowPlayingInfo(page, state);
}
var currentImgUrl;
function updateNowPlayingInfo(page, state) {
var item = state.NowPlayingItem;
$('.itemName', page).html(item ? MediaPlayer.getNowPlayingNameHtml(state) : '');
var url;
if (!item) {
}
else if (item.PrimaryImageTag) {
url = ApiClient.getScaledImageUrl(item.PrimaryImageItemId, {
2014-04-30 08:07:02 -07:00
type: "Primary",
height: 300,
2014-04-30 08:07:02 -07:00
tag: item.PrimaryImageTag
});
}
else if (item.BackdropImageTag) {
url = ApiClient.getScaledImageUrl(item.BackdropItemId, {
2014-04-30 08:07:02 -07:00
type: "Backdrop",
height: 300,
2014-04-30 08:07:02 -07:00
tag: item.BackdropImageTag,
index: 0
});
} else if (item.ThumbImageTag) {
url = ApiClient.getScaledImageUrl(item.ThumbImageItemId, {
2014-04-30 08:07:02 -07:00
type: "Thumb",
height: 300,
2014-04-30 08:07:02 -07:00
tag: item.ThumbImageTag
});
}
if (url == currentImgUrl) {
return;
}
2014-04-30 20:24:55 -07:00
setImageUrl(page, url);
}
2014-05-07 22:04:39 -07:00
2014-04-30 20:24:55 -07:00
function setImageUrl(page, url) {
2014-04-30 08:07:02 -07:00
currentImgUrl = url;
$('.nowPlayingPageImage', page).html(url ? '<img src="' + url + '" />' : '');
2014-04-27 15:51:07 -07:00
}
function updateSupportedCommands(page, commands) {
$('.btnCommand', page).each(function () {
$(this).buttonEnabled(commands.indexOf(this.getAttribute('data-command')) != -1);
});
}
function releaseCurrentPlayer() {
if (currentPlayer) {
$(currentPlayer).off('.nowplayingpage');
currentPlayer.endPlayerUpdates();
currentPlayer = null;
}
}
function bindToPlayer(page, player) {
releaseCurrentPlayer();
currentPlayer = player;
player.getPlayerState().done(function (state) {
2014-04-28 20:56:20 -07:00
if (state.NowPlayingItem) {
2014-04-27 15:51:07 -07:00
player.beginPlayerUpdates();
}
onStateChanged.call(player, { type: 'init' }, state);
});
$(player).on('playbackstart.nowplayingpage', onPlaybackStart)
.on('playbackstop.nowplayingpage', onPlaybackStopped)
.on('volumechange.nowplayingpage', onStateChanged)
.on('playstatechange.nowplayingpage', onStateChanged)
.on('positionchange.nowplayingpage', onStateChanged);
var playerInfo = MediaController.getPlayerInfo();
var supportedCommands = playerInfo.supportedCommands;
updateSupportedCommands(page, supportedCommands);
}
2015-03-17 21:09:31 -07:00
function showIntro() {
2015-04-30 20:00:29 -07:00
2015-05-08 12:10:53 -07:00
var expected = '2';
if (store.getItem('remotecontrolswipedown') != expected) {
2015-03-17 21:09:31 -07:00
Dashboard.alert({
message: Globalize.translate('MessageSwipeDownOnRemoteControl'),
title: Globalize.translate('HeaderAlert')
});
2015-05-08 12:10:53 -07:00
store.setItem('remotecontrolswipedown', expected);
2015-03-17 21:09:31 -07:00
}
}
2015-03-18 09:56:14 -07:00
function loadPlaylist(page) {
2015-04-30 20:00:29 -07:00
var html = '';
2015-03-18 09:56:14 -07:00
2015-04-30 20:00:29 -07:00
//ApiClient.getItems(Dashboard.getCurrentUserId(), {
// SortBy: "SortName",
// SortOrder: "Ascending",
// IncludeItemTypes: "Audio",
// Recursive: true,
// Fields: "PrimaryImageAspectRatio,SortName,MediaSourceCount,IsUnidentified,SyncInfo",
// StartIndex: 0,
// ImageTypeLimit: 1,
// EnableImageTypes: "Primary,Backdrop,Banner,Thumb",
// Limit: 100
//}).done(function (result) {
// html += LibraryBrowser.getListViewHtml({
// items: result.Items,
// smallIcon: true,
// defaultAction: 'setplaylistindex'
// });
// $(".playlist", page).html(html).trigger('create').lazyChildren();
//});
html += LibraryBrowser.getListViewHtml({
items: MediaController.playlist(),
smallIcon: true,
defaultAction: 'play'
2015-03-18 09:56:14 -07:00
});
2015-04-30 20:00:29 -07:00
$(".playlist", page).html(html).trigger('create').lazyChildren();
2015-03-18 09:56:14 -07:00
}
2014-04-27 15:51:07 -07:00
$(document).on('pageinit', "#nowPlayingPage", function () {
var page = this;
bindEvents(page);
2014-04-28 20:56:20 -07:00
}).on('pageshow', "#nowPlayingPage", function () {
var page = this;
2015-03-18 09:56:14 -07:00
var tab = getParameterByName('tab');
if (tab) {
$('.tabButton' + tab, page).trigger('click');
} else {
$('.tabButton:first', page).trigger('click');
}
2014-04-28 20:56:20 -07:00
2014-04-27 15:51:07 -07:00
$(function () {
$(MediaController).on('playerchange.nowplayingpage', function () {
bindToPlayer(page, MediaController.getCurrentPlayer());
});
bindToPlayer(page, MediaController.getCurrentPlayer());
});
2015-03-17 21:09:31 -07:00
showIntro();
2015-03-18 09:56:14 -07:00
loadPlaylist(page);
2015-03-17 21:09:31 -07:00
2014-04-27 15:51:07 -07:00
}).on('pagehide', "#nowPlayingPage", function () {
releaseCurrentPlayer();
$(MediaController).off('playerchange.nowplayingpage');
2014-04-28 08:05:28 -07:00
lastPlayerState = null;
2014-04-27 15:51:07 -07:00
});
window.NowPlayingPage = {
onMessageSubmit: function () {
var form = this;
MediaController.sendCommand({
Name: 'DisplayMessage',
Arguments: {
Header: $('#txtMessageTitle', form).val(),
Text: $('#txtMessageText', form).val()
}
}, currentPlayer);
$('input', form).val('');
Dashboard.alert('Message sent.');
2014-05-09 12:43:06 -07:00
return false;
},
2014-05-12 11:04:25 -07:00
onSendStringSubmit: function () {
2014-05-09 12:43:06 -07:00
var form = this;
MediaController.sendCommand({
Name: 'SendString',
Arguments: {
String: $('#txtTypeText', form).val()
}
}, currentPlayer);
$('input', form).val('');
Dashboard.alert('Text sent.');
return false;
}
};
2014-04-27 15:51:07 -07:00
})(window, document, jQuery, setTimeout, clearTimeout);