(function (window, document, $) {
function showMenu(page, item, context, sessionsPromise, usersPromise) {
var html = '
';
html += '
';
html += '
Remote Control
';
html += '
';
html += '
';
html += '
';
html += '
';
$(document.body).append(html);
var popup = $('#remoteControlFlyout').popup({ history: false }).trigger('create').popup("open").on("popupafterclose", function () {
$(this).off("popupafterclose").remove();
});
$('#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: context
});
}
else if (command == "Play") {
promise = ApiClient.sendPlayCommand(sessionIds[0], {
ItemIds: [item.Id].join(','),
PlayCommand: 'PlayNow'
});
}
promise.done(function () {
$('#remoteControlFlyout').popup("close");
});
return false;
});
var elem = $('.sessionsPopupContent');
$.when(sessionsPromise, usersPromise).done(function (response1, response2) {
var deviceId = ApiClient.deviceId();
var sessions = response1[0].filter(function (s) {
return s.DeviceId != deviceId;
});
renderSessions(sessions, response2[0], elem);
});
}
function renderSessions(sessions, users, elem) {
if (!sessions.length) {
elem.html('
There are currently no available media browser sessions to control.
');
$('#remoteControlFlyout').popup("reposition", {});
return;
}
var html = '';
html += '
';
html += '';
html += '';
html += '
';
html += '
Send To Device
';
html += '
';
html += '
';
html += '
';
html += '
';
html += '
Client
';
html += '
Device
';
html += '
User
';
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 += '
';
var user = null;
if (session.UserId) {
user = users.filter(function (u) {
return u.Id == session.UserId;
})[0];
}
html += user ? user.Name : ' ';
html += '
';
html += '
';
}
html += '';
html += '
';
html += ' ';
html += '
';
elem.html(html).trigger('create');
$('.checkboxCell', elem).html('');
$('#remoteControlFlyout').popup("reposition", {});
}
function remoteControl() {
var self = this;
self.showMenu = function (page, item, context) {
showMenu(page, item, context, ApiClient.getSessions({ SupportsRemoteControl: true }), ApiClient.getUsers());
};
}
window.RemoteControl = new remoteControl();
})(window, document, jQuery);