(function () {
function connectToServer(page, server) {
Dashboard.showLoadingMsg();
ConnectionManager.connectToServer(server).done(function (result) {
Dashboard.hideLoadingMsg();
switch (result.State) {
case MediaBrowser.ConnectionState.SignedIn:
{
var apiClient = result.ApiClient;
Dashboard.serverAddress(apiClient.serverAddress());
Dashboard.setCurrentUser(apiClient.getCurrentUserId(), apiClient.accessToken());
window.location = 'index.html';
}
break;
default:
showServerConnectionFailure();
break;
}
});
}
function showServerConnectionFailure() {
// Need the timeout because jquery mobile will not show a popup while another is in process of closing
setTimeout(function () {
Dashboard.alert({
message: Globalize.translate("MessageUnableToConnectToServer"),
title: Globalize.translate("HeaderConnectionFailure")
});
}, 300);
}
function getServerHtml(server) {
var html = '';
var cssClass = "card homePageSquareCard bottomPaddedCard";
html += "
";
html += '
';
html += '
";
html += '";
// cardBox
html += "
";
// card
html += "
";
return html;
}
function renderServers(page, servers) {
if (servers.length) {
$('.noServersMessage', page).hide();
} else {
$('.noServersMessage', page).show();
}
var html = '';
html += servers.map(getServerHtml).join('');
var elem = $('.serverList', page).html(html).trigger('create');
$('.lnkServer', elem).on('click', function () {
var id = this.getAttribute('data-serverid');
var server = servers.filter(function (s) {
return s.Id == id;
})[0];
connectToServer(page, server);
});
$('.btnServerMenu', elem).on('click', function () {
showServerMenu(this);
});
}
function showGeneralError() {
// Need the timeout because jquery mobile will not show a popup if there's currently already one in the process of closing
setTimeout(function () {
Dashboard.hideLoadingMsg();
Dashboard.alert({
message: Globalize.translate('DefaultErrorMessage')
});
}, 300);
}
function acceptInvitation(page, id) {
Dashboard.showLoadingMsg();
// Add/Update connect info
ConnectionManager.acceptServer(id).done(function () {
Dashboard.hideLoadingMsg();
loadPage(page);
}).fail(function () {
showGeneralError();
});
}
function deleteServer(page, id) {
Dashboard.showLoadingMsg();
// Add/Update connect info
ConnectionManager.deleteServer(id).done(function () {
Dashboard.hideLoadingMsg();
loadPage(page);
}).fail(function () {
showGeneralError();
});
}
function rejectInvitation(page, id) {
Dashboard.showLoadingMsg();
// Add/Update connect info
ConnectionManager.rejectServer(id).done(function () {
Dashboard.hideLoadingMsg();
loadPage(page);
}).fail(function () {
showGeneralError();
});
}
function showServerMenu(elem) {
var card = $(elem).parents('.card');
var page = $(elem).parents('.page');
var id = card.attr('data-id');
var connectserverid = card.attr('data-connectserverid');
$('.serverMenu', page).popup("close").remove();
var html = '';
page.append(html);
var flyout = $('.serverMenu', page).popup({ positionTo: elem || "window" }).trigger('create').popup("open").on("popupafterclose", function () {
$(this).off("popupafterclose").remove();
});
$('.btnDelete', flyout).on('click', function () {
deleteServer(page, this.getAttribute('data-connectserverid'));
$('.serverMenu', page).popup("close").remove();
});
}
function showPendingInviteMenu(elem) {
var card = $(elem).parents('.card');
var page = $(elem).parents('.page');
var id = card.attr('data-id');
$('.inviteMenu', page).popup("close").remove();
var html = '';
page.append(html);
var flyout = $('.inviteMenu', page).popup({ positionTo: elem || "window" }).trigger('create').popup("open").on("popupafterclose", function () {
$(this).off("popupafterclose").remove();
});
$('.btnAccept', flyout).on('click', function () {
acceptInvitation(page, this.getAttribute('data-id'));
$('.inviteMenu', page).popup("close").remove();
});
$('.btnReject', flyout).on('click', function () {
rejectInvitation(page, this.getAttribute('data-id'));
$('.inviteMenu', page).popup("close").remove();
});
}
function getPendingInviteHtml(invite) {
var html = '';
var cssClass = "card homePageSquareCard alternateHover bottomPaddedCard";
html += "";
html += '
';
html += '
";
html += '";
// cardBox
html += "
";
// card
html += "
";
return html;
}
function renderInvitations(page, list) {
if (list.length) {
$('.invitationSection', page).show();
} else {
$('.invitationSection', page).hide();
}
var html = list.map(getPendingInviteHtml).join('');
var elem = $('.invitationList', page).html(html).trigger('create');
$('.btnInviteMenu', elem).on('click', function () {
showPendingInviteMenu(this);
});
}
function loadInvitations(page) {
ConnectionManager.getUserInvitations().done(function (list) {
renderInvitations(page, list);
});
}
function loadPage(page) {
Dashboard.showLoadingMsg();
ConnectionManager.getServers().done(function (servers) {
renderServers(page, servers);
Dashboard.hideLoadingMsg();
});
loadInvitations(page);
}
$(document).on('pageshow', "#selectServerPage", function () {
var page = this;
loadPage(page);
});
window.SelectServerPage = {
onServerAddressEntrySubmit: function () {
Dashboard.showLoadingMsg();
var form = this;
var page = $(form).parents('.page');
// Disable default form submission
return false;
}
};
})();