(function () { var serverList = []; function connectToServer(page, server) { Dashboard.showLoadingMsg(); ConnectionManager.connectToServer(server).done(function (result) { Dashboard.hideLoadingMsg(); var apiClient = result.ApiClient; switch (result.State) { case MediaBrowser.ConnectionState.SignedIn: { Dashboard.onServerChanged(apiClient.getCurrentUserId(), apiClient.accessToken(), apiClient); Dashboard.navigate('index.html'); } break; case MediaBrowser.ConnectionState.ServerSignIn: { Dashboard.onServerChanged(null, null, apiClient); Dashboard.navigate('login.html?serverid=' + result.Servers[0].Id); } 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 squareCard bottomPaddedCard"; html += "
"; html += '
'; html += '
'; html += '
'; var href = server.href || "#"; html += ''; var imgUrl = server.Id == 'connect' ? 'css/images/logo536.png' : ''; if (imgUrl) { html += '"; html += '
'; if (server.showOptions !== false) { html += '
'; html += ''; html += "
"; } html += '
'; html += server.Name; html += "
"; html += '
'; html += ' '; html += "
"; // cardFooter html += "
"; // cardBox html += "
"; // card html += "
"; return html; } function renderServers(page, servers) { serverList = 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'); if (id != 'new' && id != 'connect') { 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.hideModalLoadingMsg(); Dashboard.alert({ message: Globalize.translate('DefaultErrorMessage') }); }, 300); } function acceptInvitation(page, id) { Dashboard.showModalLoadingMsg(); // Add/Update connect info ConnectionManager.acceptServer(id).done(function () { Dashboard.hideModalLoadingMsg(); loadPage(page); }).fail(function () { showGeneralError(); }); } function deleteServer(page, id) { Dashboard.showModalLoadingMsg(); // Add/Update connect info ConnectionManager.deleteServer(id).done(function () { Dashboard.hideModalLoadingMsg(); // Just re-render the servers without discovering them again // If we re-discover then the one they deleted may just come back var newServerList = serverList.filter(function(s){ return s.Id != id; }); renderServers(page, newServerList); }).fail(function () { showGeneralError(); }); } function rejectInvitation(page, id) { Dashboard.showModalLoadingMsg(); // Add/Update connect info ConnectionManager.rejectServer(id).done(function () { Dashboard.hideModalLoadingMsg(); 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 = '
'; html += ''; 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 = '
'; html += ''; 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 squareCard alternateHover bottomPaddedCard"; html += "
"; html += '
'; html += '
'; html += '
'; var href = "#"; html += ''; html += '
'; html += ''; html += "
"; // cardContent html += "
"; // cardScalable html += "
"; html += '
'; html += '
'; html += ''; html += "
"; html += '
'; html += invite.Name; html += "
"; html += '
'; html += ' '; html += "
"; // cardFooter 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) { if (ConnectionManager.isLoggedIntoConnect()) { ConnectionManager.getUserInvitations().done(function (list) { renderInvitations(page, list); }); } else { renderInvitations(page, []); } } function loadPage(page) { Dashboard.showLoadingMsg(); Backdrops.setDefault(page); ConnectionManager.getAvailableServers().done(function (servers) { servers = servers.slice(0); if (AppInfo.isNativeApp) { servers.push({ Name: Globalize.translate('ButtonNewServer'), Id: 'new', showOptions: false, href: 'connectlogin.html?mode=manualserver' }); } renderServers(page, servers); Dashboard.hideLoadingMsg(); }); loadInvitations(page); if (ConnectionManager.isLoggedIntoConnect()) { $('.connectLogin', page).hide(); } else { $('.connectLogin', page).show(); } } function updatePageStyle(page) { if (ConnectionManager.isLoggedIntoConnect()) { $(page).addClass('libraryPage').addClass('noSecondaryNavPage').removeClass('standalonePage'); } else { $(page).removeClass('libraryPage').removeClass('noSecondaryNavPage').addClass('standalonePage'); } } $(document).on('pageinitdepends pagebeforeshowready', "#selectServerPage", function () { var page = this; updatePageStyle(page); }).on('pageshowready', "#selectServerPage", function () { var page = this; loadPage(page); }); })();