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

416 lines
11 KiB
JavaScript
Raw Normal View History

2014-10-25 11:32:58 -07:00
(function () {
2015-08-03 09:54:01 -07:00
function connectToServer(page, server) {
2014-10-25 11:32:58 -07:00
Dashboard.showLoadingMsg();
ConnectionManager.connectToServer(server).done(function (result) {
Dashboard.hideLoadingMsg();
2015-05-18 15:23:03 -07:00
var apiClient = result.ApiClient;
2014-10-25 11:32:58 -07:00
switch (result.State) {
case MediaBrowser.ConnectionState.SignedIn:
{
2015-05-20 09:28:55 -07:00
Dashboard.onServerChanged(apiClient.getCurrentUserId(), apiClient.accessToken(), apiClient);
2015-05-17 18:27:48 -07:00
Dashboard.navigate('index.html');
2014-10-25 11:32:58 -07:00
}
break;
2015-05-05 08:24:47 -07:00
case MediaBrowser.ConnectionState.ServerSignIn:
{
2015-05-21 13:53:14 -07:00
Dashboard.onServerChanged(null, null, apiClient);
Dashboard.navigate('login.html?serverid=' + result.Servers[0].Id);
2015-05-05 08:24:47 -07:00
}
break;
2014-10-25 11:32:58 -07:00
default:
2014-12-03 22:24:41 -07:00
showServerConnectionFailure();
2014-10-25 11:32:58 -07:00
break;
}
});
}
function showServerConnectionFailure() {
2014-11-28 10:41:47 -07:00
// 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);
2014-10-25 11:32:58 -07:00
}
2014-11-04 05:41:12 -07:00
function getServerHtml(server) {
2014-10-25 11:32:58 -07:00
2014-11-04 05:41:12 -07:00
var html = '';
2014-10-25 11:32:58 -07:00
2015-01-04 07:18:28 -07:00
var cssClass = "card squareCard bottomPaddedCard";
2014-10-25 11:32:58 -07:00
2014-11-18 19:45:12 -07:00
html += "<div data-id='" + server.Id + "' data-connectserverid='" + (server.ConnectServerId || '') + "' class='" + cssClass + "'>";
2014-10-25 11:32:58 -07:00
2015-01-11 13:31:09 -07:00
html += '<div class="cardBox visualCardBox">';
2014-11-04 05:41:12 -07:00
html += '<div class="cardScalable">';
2014-10-25 11:32:58 -07:00
2014-11-04 05:41:12 -07:00
html += '<div class="cardPadder"></div>';
2014-10-25 11:32:58 -07:00
2015-05-19 12:15:40 -07:00
var href = server.href || "#";
2014-11-04 05:41:12 -07:00
html += '<a class="cardContent lnkServer" data-serverid="' + server.Id + '" href="' + href + '">';
2014-10-25 11:32:58 -07:00
2015-05-05 16:15:47 -07:00
var imgUrl = server.Id == 'connect' ? 'css/images/logo536.png' : '';
2014-10-25 11:32:58 -07:00
2015-05-05 16:15:47 -07:00
if (imgUrl) {
html += '<div class="cardImage" style="background-image:url(\'' + imgUrl + '\');">';
} else {
html += '<div class="cardImage" style="text-align:center;">';
2015-05-12 07:53:35 -07:00
var icon = server.Id == 'new' ? 'plus-circle' : 'server';
2015-06-23 15:13:06 -07:00
html += '<i class="fa fa-' + icon + '" style="color:#fff;vertical-align:middle;font-size:100px;margin-top:40px;"></i>';
2015-05-05 16:15:47 -07:00
}
2014-10-25 11:32:58 -07:00
2014-11-04 05:41:12 -07:00
html += "</div>";
2014-10-25 11:32:58 -07:00
2015-05-05 16:15:47 -07:00
// cardContent'
2014-11-04 05:41:12 -07:00
html += "</a>";
// cardScalable
html += "</div>";
2015-05-19 12:15:40 -07:00
html += '<div class="cardFooter outerCardFooter">';
2014-11-04 05:41:12 -07:00
2015-05-05 16:15:47 -07:00
if (server.showOptions !== false) {
2015-06-23 21:38:46 -07:00
html += '<div class="cardText" style="text-align:right; float:right;padding:0;">';
2015-07-14 09:39:34 -07:00
html += '<paper-icon-button icon="' + AppInfo.moreIcon + '" class="btnServerMenu"></paper-icon-button>';
2015-05-05 16:15:47 -07:00
html += "</div>";
}
2014-11-04 05:41:12 -07:00
2015-05-19 12:15:40 -07:00
html += '<div class="cardText">';
2014-11-04 05:41:12 -07:00
html += server.Name;
html += "</div>";
2015-05-19 12:15:40 -07:00
html += '<div class="cardText">';
html += '&nbsp;';
html += "</div>";
2014-11-04 05:41:12 -07:00
// cardFooter
html += "</div>";
// cardBox
html += "</div>";
// card
html += "</div>";
return html;
}
function renderServers(page, servers) {
if (servers.length) {
$('.noServersMessage', page).hide();
} else {
$('.noServersMessage', page).show();
}
var html = '';
html += servers.map(getServerHtml).join('');
2014-10-25 11:32:58 -07:00
var elem = $('.serverList', page).html(html).trigger('create');
$('.lnkServer', elem).on('click', function () {
var id = this.getAttribute('data-serverid');
2015-05-05 16:15:47 -07:00
2015-05-19 12:15:40 -07:00
if (id != 'new' && id != 'connect') {
2015-05-05 16:15:47 -07:00
2015-05-19 12:15:40 -07:00
var server = servers.filter(function (s) {
return s.Id == id;
})[0];
2015-05-05 16:15:47 -07:00
2015-05-19 12:15:40 -07:00
connectToServer(page, server);
}
2014-10-25 11:32:58 -07:00
});
2014-11-04 05:41:12 -07:00
$('.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 () {
2015-05-12 21:55:19 -07:00
Dashboard.hideModalLoadingMsg();
2014-11-04 05:41:12 -07:00
Dashboard.alert({
message: Globalize.translate('DefaultErrorMessage')
});
}, 300);
}
function acceptInvitation(page, id) {
2015-05-12 21:55:19 -07:00
Dashboard.showModalLoadingMsg();
2014-11-04 05:41:12 -07:00
// Add/Update connect info
ConnectionManager.acceptServer(id).done(function () {
2015-05-12 21:55:19 -07:00
Dashboard.hideModalLoadingMsg();
2014-11-04 05:41:12 -07:00
loadPage(page);
}).fail(function () {
showGeneralError();
});
}
2014-11-18 19:45:12 -07:00
function deleteServer(page, id) {
2014-11-04 05:41:12 -07:00
2015-05-12 21:55:19 -07:00
Dashboard.showModalLoadingMsg();
2014-11-04 05:41:12 -07:00
// Add/Update connect info
ConnectionManager.deleteServer(id).done(function () {
2015-05-12 21:55:19 -07:00
Dashboard.hideModalLoadingMsg();
2015-06-06 19:51:20 -07:00
2015-08-03 09:54:01 -07:00
loadPage(page);
2014-11-04 05:41:12 -07:00
}).fail(function () {
showGeneralError();
});
}
2014-11-18 19:45:12 -07:00
function rejectInvitation(page, id) {
2015-05-12 21:55:19 -07:00
Dashboard.showModalLoadingMsg();
2014-11-18 19:45:12 -07:00
// Add/Update connect info
ConnectionManager.rejectServer(id).done(function () {
2015-05-12 21:55:19 -07:00
Dashboard.hideModalLoadingMsg();
2015-06-06 19:51:20 -07:00
2014-11-18 19:45:12 -07:00
loadPage(page);
}).fail(function () {
showGeneralError();
});
}
2014-11-04 05:41:12 -07:00
function showServerMenu(elem) {
var card = $(elem).parents('.card');
var page = $(elem).parents('.page');
2015-06-22 08:43:19 -07:00
var serverId = card.attr('data-id');
2014-11-18 19:45:12 -07:00
var connectserverid = card.attr('data-connectserverid');
2014-11-04 05:41:12 -07:00
2015-06-22 08:43:19 -07:00
var menuItems = [];
2014-11-04 05:41:12 -07:00
2015-06-22 08:43:19 -07:00
menuItems.push({
name: Globalize.translate('ButtonDelete'),
id: 'delete',
ironIcon: 'delete'
});
2014-11-04 05:41:12 -07:00
2015-06-22 08:43:19 -07:00
require(['actionsheet'], function () {
2014-11-04 05:41:12 -07:00
2015-06-22 08:43:19 -07:00
ActionSheetElement.show({
items: menuItems,
positionTo: elem,
callback: function (id) {
2014-11-04 05:41:12 -07:00
2015-06-22 08:43:19 -07:00
switch (id) {
2014-11-04 05:41:12 -07:00
2015-06-22 08:43:19 -07:00
case 'delete':
deleteServer(page, connectserverid);
break;
default:
break;
}
}
});
2014-11-04 05:41:12 -07:00
});
}
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 = '<div data-role="popup" class="inviteMenu" data-theme="a">';
html += '<ul data-role="listview" style="min-width: 180px;">';
html += '<li data-role="list-divider">' + Globalize.translate('HeaderMenu') + '</li>';
html += '<li><a href="#" class="btnAccept" data-id="' + id + '">' + Globalize.translate('ButtonAccept') + '</a></li>';
html += '<li><a href="#" class="btnReject" data-id="' + id + '">' + Globalize.translate('ButtonReject') + '</a></li>';
html += '</ul>';
html += '</div>';
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 = '';
2015-01-04 07:18:28 -07:00
var cssClass = "card squareCard alternateHover bottomPaddedCard";
2014-11-04 05:41:12 -07:00
html += "<div data-id='" + invite.Id + "' class='" + cssClass + "'>";
2015-01-11 13:31:09 -07:00
html += '<div class="cardBox visualCardBox">';
2014-11-04 05:41:12 -07:00
html += '<div class="cardScalable">';
html += '<div class="cardPadder"></div>';
var href = "#";
html += '<a class="cardContent" href="' + href + '">';
2015-05-05 16:15:47 -07:00
html += '<div class="cardImage" style="text-align:center;">';
2015-06-23 15:13:06 -07:00
html += '<i class="fa fa-globe" style="color:#fff;vertical-align:middle;font-size:100px;margin-top:40px;"></i>';
2014-11-04 05:41:12 -07:00
html += "</div>";
// cardContent
html += "</a>";
// cardScalable
html += "</div>";
2015-05-19 12:15:40 -07:00
html += '<div class="cardFooter outerCardFooter">';
2014-11-04 05:41:12 -07:00
2015-06-23 21:38:46 -07:00
html += '<div class="cardText" style="text-align:right; float:right;padding:0;">';
2015-07-14 09:39:34 -07:00
html += '<paper-icon-button icon="' + AppInfo.moreIcon + '" class="btnInviteMenu"></paper-icon-button>';
2014-11-04 05:41:12 -07:00
html += "</div>";
2015-05-19 12:15:40 -07:00
html += '<div class="cardText">';
2014-11-04 05:41:12 -07:00
html += invite.Name;
html += "</div>";
2015-05-19 12:15:40 -07:00
html += '<div class="cardText">';
html += '&nbsp;';
html += "</div>";
2014-11-04 05:41:12 -07:00
// cardFooter
html += "</div>";
// cardBox
html += "</div>";
// card
html += "</div>";
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) {
2015-05-05 08:24:47 -07:00
if (ConnectionManager.isLoggedIntoConnect()) {
2014-11-04 05:41:12 -07:00
2015-05-05 08:24:47 -07:00
ConnectionManager.getUserInvitations().done(function (list) {
renderInvitations(page, list);
});
} else {
renderInvitations(page, []);
}
2014-11-04 05:41:12 -07:00
2014-10-25 11:32:58 -07:00
}
function loadPage(page) {
Dashboard.showLoadingMsg();
2015-05-12 21:55:19 -07:00
Backdrops.setDefault(page);
ConnectionManager.getAvailableServers().done(function (servers) {
2014-10-25 11:32:58 -07:00
2015-05-05 16:15:47 -07:00
servers = servers.slice(0);
2014-10-25 11:32:58 -07:00
renderServers(page, servers);
Dashboard.hideLoadingMsg();
});
2014-11-04 05:41:12 -07:00
loadInvitations(page);
2015-05-19 12:15:40 -07:00
if (ConnectionManager.isLoggedIntoConnect()) {
$('.connectLogin', page).hide();
} else {
$('.connectLogin', page).show();
}
2014-10-25 11:32:58 -07:00
}
2015-05-18 15:23:03 -07:00
function updatePageStyle(page) {
2015-05-19 12:15:40 -07:00
2015-05-12 21:55:19 -07:00
if (ConnectionManager.isLoggedIntoConnect()) {
2015-05-18 15:23:03 -07:00
$(page).addClass('libraryPage').addClass('noSecondaryNavPage').removeClass('standalonePage');
2015-05-12 21:55:19 -07:00
} else {
2015-05-18 15:23:03 -07:00
$(page).removeClass('libraryPage').removeClass('noSecondaryNavPage').addClass('standalonePage');
2015-05-12 21:55:19 -07:00
}
2015-05-18 15:23:03 -07:00
}
2015-05-19 12:15:40 -07:00
$(document).on('pageinitdepends pagebeforeshowready', "#selectServerPage", function () {
2015-05-18 15:23:03 -07:00
var page = this;
updatePageStyle(page);
2015-05-12 21:55:19 -07:00
2015-05-19 12:15:40 -07:00
}).on('pageshowready', "#selectServerPage", function () {
2014-10-25 11:32:58 -07:00
var page = this;
loadPage(page);
});
})();