(function (window, document, $) {
function sendPlayFolderCommand(item, sessionId, popup) {
ApiClient.getItems(Dashboard.getCurrentUserId(), {
ParentId: item.Id,
Filters: "IsNotFolder",
SortBy: "SortName",
Recursive: true,
Limit: 100
}).done(function (result) {
ApiClient.sendPlayCommand(sessionId, {
ItemIds: result.Items.map(function (i) {
return i.Id;
}).join(','),
PlayCommand: $('#fldPlayCommand', popup).val()
});
popup.popup("close");
});
}
function showMenu(options, sessionsPromise) {
var playFromRendered;
var trailersRendered;
var specialFeaturesRendered;
var themeVideosRendered;
var themeSongsRendered;
var item = options.item;
var html = '
';
html += '
';
html += '
Remote Control
';
html += '
';
html += '
';
html += '
';
$(document.body).append(html);
var popup = $('#remoteControlFlyout').popup({ history: false, tolerance: 0, corners: false }).trigger('create').popup("open").on("popupafterclose", function () {
if (ApiClient.isWebSocketOpen()) {
ApiClient.sendWebSocketMessage("SessionsStop");
}
$(ApiClient).off("websocketmessage.remotecontrol");
$(this).off("popupafterclose").remove();
});
popup.on('click', '.trSession', function () {
$('input', this).checked(true);
}).on('click', '.trSelectPlayTime', function () {
$('input', this).checked(true);
}).on('click', '.trItem', function () {
$('input', this).checked(true);
});
$('#sendToForm', popup).on('submit', function () {
var checkboxes = $('.chkClient', popup);
if (!checkboxes.length) {
$('#remoteControlFlyout').popup("close");
return false;
}
checkboxes = $('.chkClient:checked', popup);
if (!checkboxes.length) {
Dashboard.alert('Please select a device to control.');
return false;
}
var sessionIds = [];
checkboxes.parents('.trSession').each(function () {
sessionIds.push(this.getAttribute('data-sessionid'));
});
var command = $('#selectCommand', popup).val();
var promise;
if (command == "Browse") {
promise = ApiClient.sendBrowseCommand(sessionIds[0], {
ItemId: item.Id,
ItemName: item.Name,
ItemType: item.Type,
Context: options.context
});
}
else if (command == "Play") {
if (item.IsFolder) {
sendPlayFolderCommand(item, sessionIds[0], popup);
return false;
}
promise = ApiClient.sendPlayCommand(sessionIds[0], {
ItemIds: [item.Id].join(','),
PlayCommand: $('#fldPlayCommand', popup).val()
});
}
else if (command == "PlayFromChapter") {
var checkedChapter = $('.chkSelectPlayTime:checked', popup);
var ticks = checkedChapter.length ? checkedChapter.parents('.trSelectPlayTime').attr('data-ticks') : 0;
promise = ApiClient.sendPlayCommand(sessionIds[0], {
ItemIds: [item.Id].join(','),
PlayCommand: $('#fldPlayCommand', popup).val(),
StartPositionTicks: ticks
});
}
else if (command == "Resume") {
promise = ApiClient.sendPlayCommand(sessionIds[0], {
ItemIds: [item.Id].join(','),
PlayCommand: 'PlayNow',
StartPositionTicks: item.UserData.PlaybackPositionTicks
});
}
else if (command == "Trailer" || command == "SpecialFeature" || command == "ThemeSong" || command == "ThemeVideo") {
var id = $('.chkSelectItem:checked', popup).parents('.trItem').attr('data-id');
if (!id) {
Dashboard.alert('Please select an item.');
return false;
}
promise = ApiClient.sendPlayCommand(sessionIds[0], {
ItemIds: [id].join(','),
PlayCommand: $('#fldPlayCommand', popup).val()
});
}
promise.done(function () {
popup.popup("close");
});
return false;
});
var elem = $('.sessionsPopupContent');
sessionsPromise.done(function (sessions) {
var deviceId = ApiClient.deviceId();
sessions = sessions.filter(function (s) {
return s.DeviceId != deviceId;
});
renderSessions(sessions, options, elem);
if (ApiClient.isWebSocketOpen()) {
ApiClient.sendWebSocketMessage("SessionsStart", "1500,1500");
$(ApiClient).on("websocketmessage.remotecontrol", function (e, msg) {
if (msg.MessageType === "Sessions") {
refreshSessions(msg.Data, elem);
}
});
}
$('#selectCommand', popup).on('change', function () {
var playFromMenu = $('.playFromMenu', popup).hide();
var trailersElem = $('.trailers', popup).hide();
var specialFeaturesElem = $('.specialFeatures', popup).hide();
var themeSongsElem = $('.themeSongs', popup).hide();
var themeVideosElem = $('.themeVideos', popup).hide();
var playButtonContainer = $('#playButtonContainer', popup).hide();
var queueButtonContainer = $('#queueButtonContainer', popup).hide();
var okButtonContainer = $('#okButtonContainer', popup).hide();
var value = this.value;
if (value == "Browse") {
okButtonContainer.show();
}
else if (value == "Play") {
playButtonContainer.show();
queueButtonContainer.show();
}
else if (value == "Resume") {
playButtonContainer.show();
}
else if (value == "PlayFromChapter" && item.Chapters && item.Chapters.length) {
playFromMenu.show();
playButtonContainer.show();
if (!playFromRendered) {
playFromRendered = true;
renderPlayFromOptions(playFromMenu, item);
}
popup.popup("reposition", { tolerance: 0 });
}
else if (value == "Trailer") {
trailersElem.show();
playButtonContainer.show();
queueButtonContainer.show();
if (!trailersRendered) {
trailersRendered = true;
ApiClient.getLocalTrailers(Dashboard.getCurrentUserId(), item.Id).done(function (trailers) {
renderVideos(trailersElem, trailers, 'Trailers');
popup.popup("reposition", { tolerance: 0 });
});
}
}
else if (value == "SpecialFeature") {
specialFeaturesElem.show();
playButtonContainer.show();
queueButtonContainer.show();
if (!specialFeaturesRendered) {
specialFeaturesRendered = true;
ApiClient.getSpecialFeatures(Dashboard.getCurrentUserId(), item.Id).done(function (videos) {
renderVideos(specialFeaturesElem, videos, 'Special Features');
popup.popup("reposition", { tolerance: 0 });
});
}
}
else if (value == "ThemeSong") {
themeSongsElem.show();
playButtonContainer.show();
queueButtonContainer.show();
if (!themeSongsRendered) {
themeSongsRendered = true;
ApiClient.getThemeSongs(Dashboard.getCurrentUserId(), item.Id).done(function (result) {
renderVideos(themeSongsElem, result.Items, 'Theme Songs');
$('#remoteControlFlyout').popup("reposition", { tolerance: 0 });
});
}
}
else if (value == "ThemeVideo") {
themeVideosElem.show();
playButtonContainer.show();
queueButtonContainer.show();
if (!themeVideosRendered) {
themeVideosRendered = true;
ApiClient.getThemeVideos(Dashboard.getCurrentUserId(), item.Id).done(function (result) {
renderVideos(themeVideosElem, result.Items, 'Theme Videos');
popup.popup("reposition", { tolerance: 0 });
});
}
}
});
});
}
function renderPlayFromOptions(elem, item) {
var html = '';
html += 'Play from scene
';
html += '';
elem.html(html);
$('.tdSelectPlayTime', elem).html('');
$('.chkSelectPlayTime:first', elem).checked(true);
}
function renderSessions(sessions, options, elem) {
if (!sessions.length) {
elem.html('There are currently no available media browser sessions to control.
');
$('#remoteControlFlyout').popup("reposition", {});
return;
}
var item = options.item;
var html = '';
html += '';
html += '';
html += '';
html += '
';
html += '';
html += '';
html += '';
html += '';
html += '';
html += 'Select Device
';
html += '';
html += '';
html += ' | ';
html += 'Client | ';
html += 'Device | ';
html += 'User | ';
html += 'Now Playing | ';
html += 'Time | ';
html += '
';
html += '';
for (var i = 0, length = sessions.length; i < length; i++) {
var session = sessions[i];
html += '';
html += ' | ';
html += '' + session.Client + ' | ';
html += '' + session.DeviceName + ' | ';
html += '';
html += session.UserName || '';
html += ' | ';
if (session.NowPlayingItem) {
html += '';
html += session.NowPlayingItem ? session.NowPlayingItem.Name : '';
html += ' | ';
html += '';
html += getSessionNowPlayingTime(session);
html += ' | ';
} else {
html += ' | ';
html += ' | ';
}
html += '
';
}
html += '';
html += '
';
html += '';
elem.html(html).trigger('create');
$('.tdSelectSession', elem).html('');
$('.chkClient:first', elem).checked(true);
$('#remoteControlFlyout').popup("reposition", { tolerance: 0 });
}
function getSessionNowPlayingTime(session) {
var html = '';
if (session.NowPlayingItem) {
html += Dashboard.getDisplayTime(session.NowPlayingPositionTicks || 0);
if (session.NowPlayingItem.RunTimeTicks) {
html += " / ";
html += Dashboard.getDisplayTime(session.NowPlayingItem.RunTimeTicks);
}
}
return html;
}
function refreshSessions(sessions, elem) {
for (var i = 0, length = sessions.length; i < length; i++) {
var session = sessions[i];
var sessionElem = $('.trSession[data-sessionid=' + session.Id + ']', elem);
$('.tdUserName', sessionElem).html(session.UserName || '');
$('.tdNowPlayingTime', sessionElem).html(getSessionNowPlayingTime(session));
$('.tdNowPlayingName', sessionElem).html(session.NowPlayingItem ? session.NowPlayingItem.Name : '');
}
}
function renderVideos(elem, videos, header) {
var html = '';
html += '' + header + '
';
html += '';
elem.html(html);
$('.tdSelectItem', elem).html('');
$('.chkSelectItem:first', elem).checked(true);
}
function remoteControl() {
var self = this;
self.showMenu = function (options) {
showMenu(options, ApiClient.getSessions({ SupportsRemoteControl: true }));
};
}
window.RemoteControl = new remoteControl();
})(window, document, jQuery);