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

357 lines
8.9 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-08-29 17:40:52 -07:00
html += '<paper-icon-item class="serverItem" data-id="' + server.Id + '">';
2014-10-25 11:32:58 -07:00
2015-08-29 17:40:52 -07:00
html += '<paper-fab class="listAvatar blue lnkServer" icon="wifi" item-icon></paper-fab>';
2014-10-25 11:32:58 -07:00
2015-08-29 17:40:52 -07:00
html += '<paper-item-body class="lnkServer" two-line>';
html += '<a class="clearLink" href="#">';
2014-10-25 11:32:58 -07:00
2015-08-29 17:40:52 -07:00
html += '<div>';
html += server.Name;
html += '</div>';
2014-11-04 05:41:12 -07:00
2015-08-29 17:40:52 -07:00
html += '<div secondary>';
html += MediaBrowser.ServerInfo.getServerAddress(server, server.LastConnectionMode);
html += '</div>';
2014-11-04 05:41:12 -07:00
2015-08-29 17:40:52 -07:00
html += '</a>';
html += '</paper-item-body>';
2014-11-04 05:41:12 -07:00
2015-08-29 17:40:52 -07:00
if (server.Id) {
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
}
2014-11-04 05:41:12 -07:00
2015-08-29 17:40:52 -07:00
html += '</paper-icon-item>';
2014-11-04 05:41:12 -07:00
return html;
}
function renderServers(page, servers) {
if (servers.length) {
$('.noServersMessage', page).hide();
2015-08-29 17:40:52 -07:00
$('.serverList', page).show();
2014-11-04 05:41:12 -07:00
} else {
$('.noServersMessage', page).show();
2015-08-29 17:40:52 -07:00
$('.serverList', page).hide();
2014-11-04 05:41:12 -07:00
}
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 () {
2015-08-29 17:40:52 -07:00
var item = $(this).parents('.serverItem')[0];
var id = item.getAttribute('data-id');
2015-05-05 16:15:47 -07:00
2015-08-29 17:40:52 -07:00
var server = servers.filter(function (s) {
return s.Id == id;
})[0];
2015-05-05 16:15:47 -07:00
2015-08-29 17:40:52 -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();
});
}
2015-08-29 17:40:52 -07:00
function deleteServer(page, serverId) {
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
2015-08-29 17:40:52 -07:00
ConnectionManager.deleteServer(serverId).done(function () {
2014-11-04 05:41:12 -07:00
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) {
2015-08-29 17:40:52 -07:00
var card = $(elem).parents('.serverItem');
2014-11-04 05:41:12 -07:00
var page = $(elem).parents('.page');
2015-06-22 08:43:19 -07:00
var serverId = card.attr('data-id');
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':
2015-08-29 17:40:52 -07:00
deleteServer(page, serverId);
2015-06-22 08:43:19 -07:00
break;
default:
break;
}
}
});
2014-11-04 05:41:12 -07:00
});
}
function showPendingInviteMenu(elem) {
2015-08-29 17:40:52 -07:00
var card = $(elem).parents('.inviteItem');
2014-11-04 05:41:12 -07:00
var page = $(elem).parents('.page');
2015-08-29 17:40:52 -07:00
var invitationId = card.attr('data-id');
2014-11-04 05:41:12 -07:00
2015-08-29 17:40:52 -07:00
var menuItems = [];
2014-11-04 05:41:12 -07:00
2015-08-29 17:40:52 -07:00
menuItems.push({
name: Globalize.translate('ButtonAccept'),
id: 'accept',
ironIcon: 'add'
});
2014-11-04 05:41:12 -07:00
2015-08-29 17:40:52 -07:00
menuItems.push({
name: Globalize.translate('ButtonReject'),
id: 'reject',
ironIcon: 'cancel'
});
2014-11-04 05:41:12 -07:00
2015-08-29 17:40:52 -07:00
require(['actionsheet'], function () {
2014-11-04 05:41:12 -07:00
2015-08-29 17:40:52 -07:00
ActionSheetElement.show({
items: menuItems,
positionTo: elem,
callback: function (id) {
2014-11-04 05:41:12 -07:00
2015-08-29 17:40:52 -07:00
switch (id) {
2014-11-04 05:41:12 -07:00
2015-08-29 17:40:52 -07:00
case 'accept':
acceptInvitation(page, invitationId);
break;
case 'reject':
rejectInvitation(page, invitationId);
break;
default:
break;
}
}
});
2014-11-04 05:41:12 -07:00
});
}
function getPendingInviteHtml(invite) {
var html = '';
2015-08-29 17:40:52 -07:00
html += '<paper-icon-item class="inviteItem" data-id="' + invite.Id + '">';
2014-11-04 05:41:12 -07:00
2015-08-29 17:40:52 -07:00
html += '<paper-fab class="listAvatar blue lnkServer" icon="wifi" item-icon></paper-fab>';
2014-11-04 05:41:12 -07:00
2015-08-29 17:40:52 -07:00
html += '<paper-item-body two-line>';
2014-11-04 05:41:12 -07:00
2015-08-29 17:40:52 -07:00
html += '<div>';
2014-11-04 05:41:12 -07:00
html += invite.Name;
2015-08-29 17:40:52 -07:00
html += '</div>';
2015-05-19 12:15:40 -07:00
2015-08-29 17:40:52 -07:00
html += '</paper-item-body>';
2014-11-04 05:41:12 -07:00
2015-08-29 17:40:52 -07:00
html += '<paper-icon-button icon="' + AppInfo.moreIcon + '" class="btnInviteMenu"></paper-icon-button>';
2014-11-04 05:41:12 -07:00
2015-08-29 17:40:52 -07:00
html += '</paper-icon-item>';
2014-11-04 05:41:12 -07:00
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();
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-09-01 07:01:59 -07:00
$(document).on('pageinit pagebeforeshow', "#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);
});
})();