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

846 lines
25 KiB
JavaScript
Raw Normal View History

2014-08-25 19:30:52 -07:00
(function ($, window, store) {
2014-03-29 08:40:32 -07:00
2014-08-25 19:30:52 -07:00
function setMirrorModeEnabled(enabled) {
2014-08-31 12:15:33 -07:00
var val = enabled ? '1' : '0';
2014-08-25 19:30:52 -07:00
store.setItem('displaymirror--' + Dashboard.getCurrentUserId(), val);
2014-08-25 19:30:52 -07:00
}
function isMirrorModeEnabled() {
2014-08-31 12:15:33 -07:00
return (store.getItem('displaymirror--' + Dashboard.getCurrentUserId()) || '') != '0';
2014-08-25 19:30:52 -07:00
}
2014-04-15 19:17:48 -07:00
2014-08-25 19:30:52 -07:00
var currentDisplayInfo;
2014-04-13 10:27:13 -07:00
function mirrorItem(info) {
var item = info.item;
2014-04-15 19:17:48 -07:00
2014-04-13 10:27:13 -07:00
MediaController.getCurrentPlayer().displayContent({
2014-08-21 08:55:35 -07:00
ItemName: item.Name,
ItemId: item.Id,
ItemType: item.Type,
Context: info.context
2014-04-13 10:27:13 -07:00
});
}
2014-08-25 19:30:52 -07:00
function mirrorIfEnabled(info) {
if (isMirrorModeEnabled()) {
var player = MediaController.getPlayerInfo();
if (!player.isLocalPlayer && player.supportedCommands.indexOf('DisplayContent') != -1) {
mirrorItem(info);
}
}
}
function monitorPlayer(player) {
$(player).on('playbackstart.mediacontroller', function (e, state) {
2014-04-30 20:24:55 -07:00
var info = {
QueueableMediaTypes: state.NowPlayingItem.MediaType,
ItemId: state.NowPlayingItem.Id,
NowPlayingItem: state.NowPlayingItem
};
info = $.extend(info, state.PlayState);
2014-04-30 20:24:55 -07:00
ApiClient.reportPlaybackStart(info);
2014-04-30 20:24:55 -07:00
}).on('playbackstop.mediacontroller', function (e, state) {
2015-03-29 11:38:18 -07:00
var stopInfo = {
itemId: state.NowPlayingItem.Id,
mediaSourceId: state.PlayState.MediaSourceId,
positionTicks: state.PlayState.PositionTicks
2015-03-29 11:38:18 -07:00
};
if (state.PlayState.LiveStreamId) {
stopInfo.LiveStreamId = state.PlayState.LiveStreamId;
}
2015-04-01 14:55:50 -07:00
if (state.PlayState.PlaySessionId) {
stopInfo.PlaySessionId = state.PlayState.PlaySessionId;
}
2015-03-29 11:38:18 -07:00
ApiClient.reportPlaybackStopped(stopInfo);
});
}
2014-03-29 08:40:32 -07:00
function mediaController() {
var self = this;
var currentPlayer;
2014-03-29 09:58:49 -07:00
var currentTargetInfo;
2014-03-29 08:40:32 -07:00
var players = [];
2014-05-30 12:23:56 -07:00
2014-05-04 20:05:46 -07:00
var keys = new bindKeys(self);
2015-03-16 22:58:29 -07:00
$(window).on("keydown", keys.keyBinding).on("keypress keyup", keys.keyPrevent);
2014-03-29 08:40:32 -07:00
self.registerPlayer = function (player) {
players.push(player);
if (player.isLocalPlayer) {
monitorPlayer(player);
}
2014-03-29 09:58:49 -07:00
};
self.getPlayerInfo = function () {
return {
name: currentPlayer.name,
isLocalPlayer: currentPlayer.isLocalPlayer,
2014-03-29 11:20:42 -07:00
id: currentTargetInfo.id,
deviceName: currentTargetInfo.deviceName,
2014-04-14 20:54:52 -07:00
playableMediaTypes: currentTargetInfo.playableMediaTypes,
supportedCommands: currentTargetInfo.supportedCommands
2014-03-29 09:58:49 -07:00
};
};
2015-05-18 15:23:03 -07:00
function triggerPlayerChange(newPlayer, newTarget) {
2015-05-22 12:16:14 -07:00
2015-05-18 15:23:03 -07:00
$(self).trigger('playerchange', [newPlayer, newTarget]);
}
2014-03-29 09:58:49 -07:00
self.setActivePlayer = function (player, targetInfo) {
if (typeof (player) === 'string') {
player = players.filter(function (p) {
return p.name == player;
})[0];
}
if (!player) {
throw new Error('null player');
}
2014-03-29 08:40:32 -07:00
2014-03-29 09:58:49 -07:00
currentPlayer = player;
2015-05-18 09:40:20 -07:00
currentTargetInfo = targetInfo;
2014-03-29 09:58:49 -07:00
2014-08-17 20:00:37 -07:00
console.log('Active player: ' + JSON.stringify(currentTargetInfo));
2015-05-18 15:23:03 -07:00
triggerPlayerChange(player, targetInfo);
2014-03-29 09:58:49 -07:00
};
2015-05-27 22:51:48 -07:00
var currentPairingId = null;
2015-05-18 09:40:20 -07:00
self.trySetActivePlayer = function (player, targetInfo) {
if (typeof (player) === 'string') {
player = players.filter(function (p) {
return p.name == player;
})[0];
}
if (!player) {
throw new Error('null player');
}
2015-05-27 22:51:48 -07:00
if (currentPairingId == targetInfo.id) {
return;
}
currentPairingId = targetInfo.id;
2015-05-22 12:16:14 -07:00
player.tryPair(targetInfo).done(function () {
2015-05-18 09:40:20 -07:00
currentPlayer = player;
currentTargetInfo = targetInfo;
console.log('Active player: ' + JSON.stringify(currentTargetInfo));
2015-05-18 15:23:03 -07:00
triggerPlayerChange(player, targetInfo);
2015-05-18 09:40:20 -07:00
});
};
self.setDefaultPlayerActive = function () {
2015-05-18 09:40:20 -07:00
var player = self.getDefaultPlayer();
var target = player.getTargets()[0];
self.setActivePlayer(player, target);
};
self.removeActivePlayer = function (name) {
if (self.getPlayerInfo().name == name) {
2014-04-06 10:53:23 -07:00
self.setDefaultPlayerActive();
}
};
2015-05-19 12:15:40 -07:00
self.removeActiveTarget = function (id) {
if (self.getPlayerInfo().id == id) {
self.setDefaultPlayerActive();
}
};
2015-05-22 12:16:14 -07:00
self.getPlayers = function () {
2015-05-08 20:48:43 -07:00
return players;
};
2014-03-29 09:58:49 -07:00
self.getTargets = function () {
var deferred = $.Deferred();
var promises = players.map(function (p) {
return p.getTargets();
});
$.when.apply($, promises).done(function () {
var targets = [];
for (var i = 0; i < arguments.length; i++) {
var subTargets = arguments[i];
for (var j = 0; j < subTargets.length; j++) {
targets.push(subTargets[j]);
}
}
targets = targets.sort(function (a, b) {
var aVal = a.isLocalPlayer ? 0 : 1;
var bVal = b.isLocalPlayer ? 0 : 1;
aVal = aVal.toString() + a.name;
bVal = bVal.toString() + b.name;
return aVal.localeCompare(bVal);
});
2014-03-29 09:58:49 -07:00
deferred.resolveWith(null, [targets]);
});
return deferred.promise();
2014-03-29 08:40:32 -07:00
};
2015-05-22 12:16:14 -07:00
function doWithPlaybackValidation(fn) {
requirejs(["scripts/registrationservices"], function () {
RegistrationServices.validateFeature('playback').done(fn);
});
}
2014-03-29 08:40:32 -07:00
self.play = function (options) {
2015-05-25 10:32:22 -07:00
doWithPlaybackValidation(function () {
2015-05-22 12:16:14 -07:00
if (typeof (options) === 'string') {
options = { ids: [options] };
}
2014-03-29 08:40:32 -07:00
2015-05-22 12:16:14 -07:00
currentPlayer.play(options);
});
2014-03-29 08:40:32 -07:00
};
self.shuffle = function (id) {
2014-03-29 09:58:49 -07:00
2015-05-22 12:16:14 -07:00
doWithPlaybackValidation(function () {
currentPlayer.shuffle(id);
});
2014-03-29 08:40:32 -07:00
};
self.instantMix = function (id) {
2015-05-22 12:16:14 -07:00
doWithPlaybackValidation(function () {
currentPlayer.instantMix(id);
});
2014-03-29 08:40:32 -07:00
};
self.queue = function (options) {
if (typeof (options) === 'string') {
options = { ids: [options] };
}
currentPlayer.queue(options);
};
self.queueNext = function (options) {
if (typeof (options) === 'string') {
options = { ids: [options] };
}
currentPlayer.queueNext(options);
};
self.canPlay = function (item) {
2015-03-25 11:29:21 -07:00
return self.canPlayByAttributes(item.Type, item.MediaType, item.PlayAccess, item.LocationType);
2014-07-19 21:46:29 -07:00
};
2015-03-25 11:29:21 -07:00
self.canPlayByAttributes = function (itemType, mediaType, playAccess, locationType) {
2014-07-19 21:46:29 -07:00
if (playAccess != 'Full') {
2014-03-29 08:40:32 -07:00
return false;
}
2015-03-25 11:29:21 -07:00
if (locationType == "Virtual") {
2014-03-29 08:40:32 -07:00
return false;
}
2015-02-04 20:01:37 -07:00
if (itemType == "Program") {
return false;
}
if (itemType == "MusicGenre" || itemType == "Season" || itemType == "Series" || itemType == "BoxSet" || itemType == "MusicAlbum" || itemType == "MusicArtist" || itemType == "Playlist") {
2014-03-29 08:40:32 -07:00
return true;
}
2014-07-19 21:46:29 -07:00
return self.getPlayerInfo().playableMediaTypes.indexOf(mediaType) != -1;
2014-03-29 08:40:32 -07:00
};
2014-07-15 12:16:16 -07:00
self.canQueueMediaType = function (mediaType, itemType) {
if (itemType == 'MusicAlbum' || itemType == 'MusicArtist' || itemType == 'MusicGenre') {
mediaType = 'Audio';
}
2014-03-29 08:40:32 -07:00
return currentPlayer.canQueueMediaType(mediaType);
};
self.getLocalPlayer = function () {
2014-03-29 09:58:49 -07:00
2014-03-29 08:40:32 -07:00
return currentPlayer.isLocalPlayer ?
2014-03-29 09:58:49 -07:00
2014-03-29 08:40:32 -07:00
currentPlayer :
2014-03-29 09:58:49 -07:00
2014-03-29 08:40:32 -07:00
players.filter(function (p) {
return p.isLocalPlayer;
})[0];
};
2014-04-06 10:53:23 -07:00
self.getDefaultPlayer = function () {
return currentPlayer.isLocalPlayer ?
currentPlayer :
players.filter(function (p) {
return p.isDefaultPlayer;
})[0];
};
2014-04-09 16:58:09 -07:00
self.getCurrentPlayer = function () {
return currentPlayer;
};
2014-04-09 16:58:09 -07:00
self.pause = function () {
currentPlayer.pause();
};
self.stop = function () {
currentPlayer.stop();
};
self.unpause = function () {
currentPlayer.unpause();
};
self.seek = function (position) {
currentPlayer.seek(position);
2014-04-09 16:58:09 -07:00
};
2014-04-09 21:12:04 -07:00
self.currentPlaylistIndex = function (i) {
currentPlayer.currentPlaylistIndex(i);
2014-04-09 21:12:04 -07:00
};
self.removeFromPlaylist = function (i) {
currentPlayer.removeFromPlaylist(i);
};
self.nextTrack = function () {
currentPlayer.nextTrack();
};
self.previousTrack = function () {
currentPlayer.previousTrack();
};
self.mute = function () {
currentPlayer.mute();
};
2014-04-15 20:49:49 -07:00
self.unMute = function () {
currentPlayer.unMute();
};
self.toggleMute = function () {
currentPlayer.toggleMute();
};
self.volumeDown = function () {
currentPlayer.volumeDown();
};
self.volumeUp = function () {
currentPlayer.volumeUp();
};
2014-06-21 22:52:31 -07:00
self.playlist = function () {
return currentPlayer.playlist || [];
};
2014-04-30 20:24:55 -07:00
self.sendCommand = function (cmd, player) {
player = player || self.getLocalPlayer();
// Full list
// https://github.com/MediaBrowser/MediaBrowser/blob/master/MediaBrowser.Model/Session/GeneralCommand.cs#L23
console.log('MediaController received command: ' + cmd.Name);
switch (cmd.Name) {
case 'VolumeUp':
player.volumeUp();
break;
case 'VolumeDown':
player.volumeDown();
break;
case 'Mute':
player.mute();
break;
case 'Unmute':
player.unMute();
break;
case 'ToggleMute':
player.toggleMute();
break;
case 'SetVolume':
player.setVolume(cmd.Arguments.Volume);
break;
case 'SetAudioStreamIndex':
2014-05-07 22:04:39 -07:00
player.setAudioStreamIndex(parseInt(cmd.Arguments.Index));
2014-04-30 20:24:55 -07:00
break;
case 'SetSubtitleStreamIndex':
2014-05-07 22:04:39 -07:00
player.setSubtitleStreamIndex(parseInt(cmd.Arguments.Index));
2014-04-30 20:24:55 -07:00
break;
case 'ToggleFullscreen':
player.toggleFullscreen();
break;
default:
{
if (player.isLocalPlayer) {
// Not player-related
Dashboard.processGeneralCommand(cmd);
} else {
player.sendCommand(cmd);
}
break;
}
}
};
2015-05-08 20:48:43 -07:00
// TOOD: This doesn't really belong here
2015-05-13 10:53:26 -07:00
self.getNowPlayingNameHtml = function (nowPlayingItem, includeNonNameInfo) {
2015-05-08 20:48:43 -07:00
var topText = nowPlayingItem.Name;
if (nowPlayingItem.MediaType == 'Video') {
if (nowPlayingItem.IndexNumber != null) {
topText = nowPlayingItem.IndexNumber + " - " + topText;
}
if (nowPlayingItem.ParentIndexNumber != null) {
topText = nowPlayingItem.ParentIndexNumber + "." + topText;
}
}
var bottomText = '';
if (nowPlayingItem.Artists && nowPlayingItem.Artists.length) {
bottomText = topText;
topText = nowPlayingItem.Artists[0];
}
else if (nowPlayingItem.SeriesName || nowPlayingItem.Album) {
bottomText = topText;
topText = nowPlayingItem.SeriesName || nowPlayingItem.Album;
}
2015-05-13 10:53:26 -07:00
else if (nowPlayingItem.ProductionYear && includeNonNameInfo !== false) {
2015-05-08 20:48:43 -07:00
bottomText = nowPlayingItem.ProductionYear;
}
return bottomText ? topText + '<br/>' + bottomText : topText;
};
2015-05-19 12:15:40 -07:00
2015-05-22 12:16:14 -07:00
self.showPlaybackInfoErrorMessage = function (errorCode) {
2015-05-19 12:15:40 -07:00
// This timeout is messy, but if jqm is in the act of hiding a popup, it will not show a new one
// If we're coming from the popup play menu, this will be a problem
2015-05-22 12:16:14 -07:00
setTimeout(function () {
2015-05-19 12:15:40 -07:00
Dashboard.alert({
message: Globalize.translate('MessagePlaybackError' + errorCode),
title: Globalize.translate('HeaderPlaybackError')
});
}, 300);
};
2015-05-25 10:32:22 -07:00
self.getPlaybackInfo = function (itemId, deviceProfile, startPosition, mediaSource, audioStreamIndex, subtitleStreamIndex, liveStreamId) {
var postData = {
DeviceProfile: deviceProfile
};
var query = {
UserId: Dashboard.getCurrentUserId(),
StartTimeTicks: startPosition || 0
};
if (audioStreamIndex != null) {
query.AudioStreamIndex = audioStreamIndex;
}
if (subtitleStreamIndex != null) {
query.SubtitleStreamIndex = subtitleStreamIndex;
}
if (mediaSource) {
query.MediaSourceId = mediaSource.Id;
}
if (liveStreamId) {
query.LiveStreamId = liveStreamId;
}
return ApiClient.ajax({
url: ApiClient.getUrl('Items/' + itemId + '/PlaybackInfo', query),
type: 'POST',
data: JSON.stringify(postData),
contentType: "application/json",
dataType: "json"
});
}
self.getLiveStream = function (itemId, playSessionId, deviceProfile, startPosition, mediaSource, audioStreamIndex, subtitleStreamIndex) {
var postData = {
DeviceProfile: deviceProfile,
OpenToken: mediaSource.OpenToken
};
var query = {
UserId: Dashboard.getCurrentUserId(),
StartTimeTicks: startPosition || 0,
ItemId: itemId,
PlaySessionId: playSessionId
};
if (audioStreamIndex != null) {
query.AudioStreamIndex = audioStreamIndex;
}
if (subtitleStreamIndex != null) {
query.SubtitleStreamIndex = subtitleStreamIndex;
}
return ApiClient.ajax({
url: ApiClient.getUrl('LiveStreams/Open', query),
type: 'POST',
data: JSON.stringify(postData),
contentType: "application/json",
dataType: "json"
});
};
self.supportsDirectPlay = function (mediaSource) {
if (mediaSource.SupportsDirectPlay && mediaSource.Protocol == 'Http' && !mediaSource.RequiredHttpHeaders.length) {
// TODO: Need to verify the host is going to be reachable
return true;
}
2015-05-29 16:51:33 -07:00
if (mediaSource.SupportsDirectPlay && mediaSource.Protocol == 'File') {
return FileSystem.fileExists(mediaSource.Path);
}
2015-05-25 10:32:22 -07:00
return false;
};
2014-03-29 08:40:32 -07:00
}
window.MediaController = new mediaController();
2014-03-29 11:20:42 -07:00
function onWebSocketMessageReceived(e, msg) {
2014-03-29 08:40:32 -07:00
var localPlayer;
2014-03-29 08:40:32 -07:00
if (msg.MessageType === "Play") {
localPlayer = MediaController.getLocalPlayer();
2014-03-29 08:40:32 -07:00
if (msg.Data.PlayCommand == "PlayNext") {
localPlayer.queueNext({ ids: msg.Data.ItemIds });
}
else if (msg.Data.PlayCommand == "PlayLast") {
localPlayer.queue({ ids: msg.Data.ItemIds });
}
else {
localPlayer.play({ ids: msg.Data.ItemIds, startPositionTicks: msg.Data.StartPositionTicks });
}
}
2014-04-06 10:53:23 -07:00
else if (msg.MessageType === "ServerShuttingDown") {
MediaController.setDefaultPlayerActive();
}
else if (msg.MessageType === "ServerRestarting") {
MediaController.setDefaultPlayerActive();
}
2014-03-29 08:40:32 -07:00
else if (msg.MessageType === "Playstate") {
localPlayer = MediaController.getLocalPlayer();
2014-03-29 08:40:32 -07:00
if (msg.Data.Command === 'Stop') {
localPlayer.stop();
}
else if (msg.Data.Command === 'Pause') {
localPlayer.pause();
}
else if (msg.Data.Command === 'Unpause') {
localPlayer.unpause();
}
else if (msg.Data.Command === 'Seek') {
localPlayer.seek(msg.Data.SeekPositionTicks);
}
else if (msg.Data.Command === 'NextTrack') {
localPlayer.nextTrack();
}
else if (msg.Data.Command === 'PreviousTrack') {
localPlayer.previousTrack();
}
}
2014-03-31 14:04:22 -07:00
else if (msg.MessageType === "GeneralCommand") {
var cmd = msg.Data;
localPlayer = MediaController.getLocalPlayer();
2014-04-30 20:24:55 -07:00
MediaController.sendCommand(cmd, localPlayer);
2014-03-31 14:04:22 -07:00
}
2014-03-29 08:40:32 -07:00
}
2014-10-25 11:32:58 -07:00
function initializeApiClient(apiClient) {
2015-05-19 12:15:40 -07:00
$(apiClient).off("websocketmessage", onWebSocketMessageReceived).on("websocketmessage", onWebSocketMessageReceived);
2014-10-25 11:32:58 -07:00
}
2015-05-19 12:15:40 -07:00
Dashboard.ready(function () {
2014-10-25 11:32:58 -07:00
2015-05-19 12:15:40 -07:00
if (window.ApiClient) {
initializeApiClient(window.ApiClient);
}
$(ConnectionManager).on('apiclientcreated', function (e, apiClient) {
initializeApiClient(apiClient);
});
2014-10-25 11:32:58 -07:00
});
2014-03-29 08:40:32 -07:00
2014-03-29 09:58:49 -07:00
function getTargetsHtml(targets) {
var playerInfo = MediaController.getPlayerInfo();
var html = '';
2014-04-13 10:27:13 -07:00
html += '<form>';
2014-07-19 21:46:29 -07:00
html += '<h3>' + Globalize.translate('HeaderSelectPlayer') + '</h3>';
2014-03-29 09:58:49 -07:00
html += '<fieldset data-role="controlgroup" data-mini="true">';
2014-04-15 19:17:48 -07:00
var checkedHtml;
2014-03-29 09:58:49 -07:00
for (var i = 0, length = targets.length; i < length; i++) {
var target = targets[i];
var id = 'radioPlayerTarget' + i;
2014-03-29 11:20:42 -07:00
var isChecked = target.id == playerInfo.id;
2014-04-15 19:17:48 -07:00
checkedHtml = isChecked ? ' checked="checked"' : '';
2014-03-29 09:58:49 -07:00
2014-04-13 10:27:13 -07:00
var mirror = (!target.isLocalPlayer && target.supportedCommands.indexOf('DisplayContent') != -1) ? 'true' : 'false';
2014-08-16 22:38:13 -07:00
html += '<input type="radio" class="radioSelectPlayerTarget" name="radioSelectPlayerTarget" data-mirror="' + mirror + '" data-commands="' + target.supportedCommands.join(',') + '" data-mediatypes="' + target.playableMediaTypes.join(',') + '" data-playername="' + target.playerName + '" data-targetid="' + target.id + '" data-targetname="' + target.name + '" data-devicename="' + (target.deviceName || '') + '" id="' + id + '" value="' + target.id + '"' + checkedHtml + '>';
2014-03-29 11:20:42 -07:00
html += '<label for="' + id + '" style="font-weight:normal;">' + target.name;
2014-08-19 15:28:35 -07:00
if (target.appName && target.appName != target.name) {
2014-07-19 21:46:29 -07:00
html += '<br/><span>' + target.appName + '</span>';
2014-03-29 11:20:42 -07:00
}
html += '</label>';
2014-03-29 09:58:49 -07:00
}
html += '</fieldset>';
2014-05-30 12:23:56 -07:00
html += '<p class="fieldDescription">' + Globalize.translate('LabelAllPlaysSentToPlayer') + '</p>';
2014-08-25 19:30:52 -07:00
checkedHtml = isMirrorModeEnabled() ? ' checked="checked"' : '';
2014-04-21 09:02:30 -07:00
html += '<div style="margin-top:1.5em;" class="fldMirrorMode"><label for="chkEnableMirrorMode">Enable display mirroring</label><input type="checkbox" class="chkEnableMirrorMode" id="chkEnableMirrorMode" data-mini="true"' + checkedHtml + ' /></div>';
2014-04-13 10:27:13 -07:00
html += '</form>';
2014-03-29 09:58:49 -07:00
return html;
}
function showPlayerSelection(page) {
2014-03-29 09:58:49 -07:00
var promise = MediaController.getTargets();
2014-03-31 14:04:22 -07:00
var html = '<div data-role="panel" data-position="right" data-display="overlay" data-position-fixed="true" id="playerSelectionPanel" data-theme="a">';
2014-03-29 09:58:49 -07:00
html += '<div class="players"></div>';
2014-06-21 22:52:31 -07:00
html += '<br/>';
2015-03-17 21:09:31 -07:00
html += '<p><a href="nowplaying.html" data-role="button" data-icon="remote" data-transition="slideup">' + Globalize.translate('ButtonRemoteControl') + '</a></p>';
2014-06-21 22:52:31 -07:00
2014-03-29 09:58:49 -07:00
html += '</div>';
$(document.body).append(html);
2014-07-22 09:36:34 -07:00
var elem = $('#playerSelectionPanel').panel({}).trigger('create').panel("open").on("panelclose", function () {
2014-03-29 09:58:49 -07:00
2014-07-22 09:36:34 -07:00
$(this).off("panelclose").remove();
2014-03-29 09:58:49 -07:00
});
promise.done(function (targets) {
$('.players', elem).html(getTargetsHtml(targets)).trigger('create');
$('.chkEnableMirrorMode', elem).on('change', function () {
2014-08-25 19:30:52 -07:00
setMirrorModeEnabled(this.checked);
2014-04-15 19:17:48 -07:00
2014-04-13 10:27:13 -07:00
if (this.checked && currentDisplayInfo) {
mirrorItem(currentDisplayInfo);
}
});
2015-05-18 15:23:03 -07:00
$('.radioSelectPlayerTarget', elem).off('change').on('change', function () {
2014-03-29 09:58:49 -07:00
2014-04-13 10:27:13 -07:00
var supportsMirror = this.getAttribute('data-mirror') == 'true';
if (supportsMirror) {
$('.fldMirrorMode', elem).show();
} else {
$('.fldMirrorMode', elem).hide();
}
2014-03-29 09:58:49 -07:00
var playerName = this.getAttribute('data-playername');
var targetId = this.getAttribute('data-targetid');
var targetName = this.getAttribute('data-targetname');
2014-06-21 22:52:31 -07:00
var deviceName = this.getAttribute('data-deviceName');
2014-03-29 11:20:42 -07:00
var playableMediaTypes = this.getAttribute('data-mediatypes').split(',');
2014-04-14 20:54:52 -07:00
var supportedCommands = this.getAttribute('data-commands').split(',');
2014-03-29 09:58:49 -07:00
2015-05-18 09:40:20 -07:00
MediaController.trySetActivePlayer(playerName, {
2014-03-29 09:58:49 -07:00
id: targetId,
2014-03-29 11:20:42 -07:00
name: targetName,
2014-04-14 20:54:52 -07:00
playableMediaTypes: playableMediaTypes,
2014-06-21 22:52:31 -07:00
supportedCommands: supportedCommands,
deviceName: deviceName
2014-03-29 09:58:49 -07:00
});
2014-08-25 19:30:52 -07:00
if (currentDisplayInfo) {
mirrorIfEnabled(currentDisplayInfo);
}
2014-03-29 09:58:49 -07:00
});
2015-05-18 15:23:03 -07:00
if ($('.radioSelectPlayerTarget:checked', elem).attr('data-mirror') == 'true') {
$('.fldMirrorMode', elem).show();
} else {
$('.fldMirrorMode', elem).hide();
}
2014-03-29 09:58:49 -07:00
});
}
2014-05-04 20:05:46 -07:00
function bindKeys(controller) {
var self = this;
var keyResult = {};
2014-05-30 12:23:56 -07:00
self.keyBinding = function (e) {
2014-05-04 20:05:46 -07:00
if (bypass()) return;
console.log("keyCode", e.keyCode);
if (keyResult[e.keyCode]) {
e.preventDefault();
keyResult[e.keyCode](e);
}
};
2014-05-30 12:23:56 -07:00
self.keyPrevent = function (e) {
2014-05-04 20:05:46 -07:00
if (bypass()) return;
var codes = [32, 38, 40, 37, 39, 81, 77, 65, 84, 83, 70];
if (codes.indexOf(e.keyCode) != -1) {
e.preventDefault();
}
};
2014-05-30 12:23:56 -07:00
keyResult[32] = function () { // spacebar
2014-05-04 20:05:46 -07:00
var player = controller.getCurrentPlayer();
2014-05-30 12:23:56 -07:00
2014-05-04 20:05:46 -07:00
player.getPlayerState().done(function (result) {
var state = result;
if (state.NowPlayingItem && state.PlayState) {
if (state.PlayState.IsPaused) {
player.unpause();
} else {
player.pause();
}
}
});
};
2014-05-30 12:23:56 -07:00
var bypass = function () {
2014-05-04 20:05:46 -07:00
// Get active elem to see what type it is
var active = document.activeElement;
var type = active.type || active.tagName.toLowerCase();
return (type === "text" || type === "select" || type === "textarea" || type == "password");
2014-05-04 20:05:46 -07:00
};
}
2014-05-21 20:35:18 -07:00
$(document).on('headercreated', function () {
2014-03-29 09:58:49 -07:00
2015-05-29 18:07:54 -07:00
$('.btnCast').off('.mediacontroller').on('click.mediacontroller', function () {
2014-03-29 09:58:49 -07:00
2014-05-01 19:54:33 -07:00
showPlayerSelection($.mobile.activePage);
2014-03-29 09:58:49 -07:00
});
2015-05-19 12:15:40 -07:00
}).on('pagebeforeshow', ".page", function () {
2014-04-13 10:27:13 -07:00
var page = this;
currentDisplayInfo = null;
}).on('displayingitem', ".libraryPage", function (e, info) {
var page = this;
currentDisplayInfo = info;
2014-08-25 19:30:52 -07:00
mirrorIfEnabled(info);
2014-04-13 10:27:13 -07:00
});
2015-05-20 10:29:26 -07:00
})(jQuery, window, window.appStorage);