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

377 lines
11 KiB
JavaScript
Raw Normal View History

2016-06-18 10:26:42 -07:00
define(['paper-icon-button-light'], function () {
2014-10-25 11:32:58 -07:00
2016-06-18 10:26:42 -07:00
function updatePageStyle(page) {
2014-10-25 11:32:58 -07:00
2016-06-18 10:26:42 -07:00
if (getParameterByName('showuser') == '1') {
page.classList.add('libraryPage');
page.classList.add('noSecondaryNavPage');
page.classList.remove('standalonePage');
} else {
page.classList.add('standalonePage');
page.classList.remove('noSecondaryNavPage');
page.classList.remove('libraryPage');
}
2014-10-25 11:32:58 -07:00
}
function showServerConnectionFailure() {
2016-02-21 15:17:38 -07:00
Dashboard.alert({
message: Globalize.translate("MessageUnableToConnectToServer"),
title: Globalize.translate("HeaderConnectionFailure")
});
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
2016-06-25 18:33:16 -07:00
html += '<div class="listItem serverItem" data-id="' + server.Id + '">';
2014-10-25 11:32:58 -07:00
2016-06-25 18:33:16 -07:00
html += '<button type="button" is="emby-button" class="fab mini autoSize blue lnkServer" item-icon><i class="md-icon">wifi</i></button>';
2014-10-25 11:32:58 -07:00
2016-06-25 18:33:16 -07:00
html += '<div class="listItemBody lnkServer">';
2015-08-29 17:40:52 -07:00
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-09-22 18:22: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>';
2016-06-25 18:33:16 -07:00
html += '</div>';
2014-11-04 05:41:12 -07:00
2015-08-29 17:40:52 -07:00
if (server.Id) {
2016-06-18 22:26:52 -07:00
html += '<button is="paper-icon-button-light" class="btnServerMenu autoSize"><i class="md-icon">' + AppInfo.moreIcon.replace('-', '_') + '</i></button>';
2015-05-05 16:15:47 -07:00
}
2014-11-04 05:41:12 -07:00
2016-06-25 18:33:16 -07:00
html += '</div>';
2014-11-04 05:41:12 -07:00
return html;
}
function renderServers(page, servers) {
if (servers.length) {
2016-06-18 10:26:42 -07:00
page.querySelector('.noServersMessage').classList.add('hide');
page.querySelector('.serverList').classList.remove('hide');
2014-11-04 05:41:12 -07:00
} else {
2016-06-18 10:26:42 -07:00
page.querySelector('.serverList').classList.add('hide');
page.querySelector('.noServersMessage').classList.remove('hide');
2014-11-04 05:41:12 -07:00
}
var html = '';
html += servers.map(getServerHtml).join('');
2014-10-25 11:32:58 -07:00
2016-06-18 10:26:42 -07:00
page.querySelector('.serverList').innerHTML = html;
}
2014-10-25 11:32:58 -07:00
2016-06-18 10:26:42 -07:00
function showGeneralError() {
2014-10-25 11:32:58 -07:00
2016-06-18 10:26:42 -07:00
Dashboard.hideModalLoadingMsg();
Dashboard.alert({
message: Globalize.translate('DefaultErrorMessage')
});
}
2015-05-05 16:15:47 -07:00
2016-06-18 10:26:42 -07:00
function parentWithClass(elem, className) {
2015-05-05 16:15:47 -07:00
2016-06-18 10:26:42 -07:00
while (!elem.classList || !elem.classList.contains(className)) {
elem = elem.parentNode;
2014-10-25 11:32:58 -07:00
2016-06-18 10:26:42 -07:00
if (!elem) {
return null;
}
}
2014-11-04 05:41:12 -07:00
2016-06-18 10:26:42 -07:00
return elem;
2014-11-04 05:41:12 -07:00
}
2016-06-18 10:26:42 -07:00
return function (view, params) {
2014-11-04 05:41:12 -07:00
2016-06-18 10:26:42 -07:00
var cachedServers;
2014-11-04 05:41:12 -07:00
2016-06-18 10:26:42 -07:00
function connectToServer(page, server) {
2014-11-04 05:41:12 -07:00
2016-06-18 10:26:42 -07:00
Dashboard.showLoadingMsg();
2014-11-04 05:41:12 -07:00
2016-06-18 10:26:42 -07:00
ConnectionManager.connectToServer(server).then(function (result) {
2014-11-04 05:41:12 -07:00
2016-06-18 10:26:42 -07:00
Dashboard.hideLoadingMsg();
2014-11-04 05:41:12 -07:00
2016-06-18 10:26:42 -07:00
var apiClient = result.ApiClient;
2014-11-04 05:41:12 -07:00
2016-06-18 10:26:42 -07:00
switch (result.State) {
2014-11-04 05:41:12 -07:00
2016-06-18 10:26:42 -07:00
case MediaBrowser.ConnectionState.SignedIn:
{
Dashboard.onServerChanged(apiClient.getCurrentUserId(), apiClient.accessToken(), apiClient);
Dashboard.navigate('home.html');
}
break;
case MediaBrowser.ConnectionState.ServerSignIn:
{
Dashboard.onServerChanged(null, null, apiClient);
Dashboard.navigate('login.html?serverid=' + result.Servers[0].Id);
}
break;
case MediaBrowser.ConnectionState.ServerUpdateNeeded:
{
Dashboard.alert(alert({
2014-11-04 05:41:12 -07:00
2016-06-18 10:26:42 -07:00
text: Globalize.translate('core#ServerUpdateNeeded', 'https://emby.media'),
html: Globalize.translate('core#ServerUpdateNeeded', '<a href="https://emby.media">https://emby.media</a>')
2014-11-04 05:41:12 -07:00
2016-06-18 10:26:42 -07:00
}));
}
break;
default:
showServerConnectionFailure();
break;
}
2014-11-04 05:41:12 -07:00
2016-06-18 10:26:42 -07:00
});
}
2014-11-04 05:41:12 -07:00
2016-06-18 10:26:42 -07:00
function acceptInvitation(page, id) {
2014-11-04 05:41:12 -07:00
2016-06-18 10:26:42 -07:00
Dashboard.showModalLoadingMsg();
2015-06-06 19:51:20 -07:00
2016-06-18 10:26:42 -07:00
// Add/Update connect info
ConnectionManager.acceptServer(id).then(function () {
2014-11-04 05:41:12 -07:00
2016-06-18 10:26:42 -07:00
Dashboard.hideModalLoadingMsg();
loadPage(page);
2014-11-04 05:41:12 -07:00
2016-06-18 10:26:42 -07:00
}, function () {
2014-11-04 05:41:12 -07:00
2016-06-18 10:26:42 -07:00
showGeneralError();
});
}
2014-11-04 05:41:12 -07:00
2016-06-18 10:26:42 -07:00
function deleteServer(page, serverId) {
2014-11-18 19:45:12 -07:00
2016-06-18 10:26:42 -07:00
Dashboard.showModalLoadingMsg();
2014-11-18 19:45:12 -07:00
2016-06-18 10:26:42 -07:00
// Add/Update connect info
ConnectionManager.deleteServer(serverId).then(function () {
2014-11-18 19:45:12 -07:00
2016-06-18 10:26:42 -07:00
Dashboard.hideModalLoadingMsg();
2015-06-06 19:51:20 -07:00
2016-06-18 10:26:42 -07:00
loadPage(page);
2014-11-18 19:45:12 -07:00
2016-06-18 10:26:42 -07:00
}, function () {
2014-11-18 19:45:12 -07:00
2016-06-18 10:26:42 -07:00
showGeneralError();
2014-11-18 19:45:12 -07:00
2016-06-18 10:26:42 -07:00
});
}
2014-11-18 19:45:12 -07:00
2016-06-18 10:26:42 -07:00
function rejectInvitation(page, id) {
2014-11-04 05:41:12 -07:00
2016-06-18 10:26:42 -07:00
Dashboard.showModalLoadingMsg();
2014-11-04 05:41:12 -07:00
2016-06-18 10:26:42 -07:00
// Add/Update connect info
ConnectionManager.rejectServer(id).then(function () {
2014-11-04 05:41:12 -07:00
2016-06-18 10:26:42 -07:00
Dashboard.hideModalLoadingMsg();
2014-11-04 05:41:12 -07:00
2016-06-18 10:26:42 -07:00
loadPage(page);
2014-11-04 05:41:12 -07:00
2016-06-18 10:26:42 -07:00
}, function () {
2014-11-04 05:41:12 -07:00
2016-06-18 10:26:42 -07:00
showGeneralError();
2014-11-04 05:41:12 -07:00
2015-06-22 08:43:19 -07:00
});
2016-06-18 10:26:42 -07:00
}
2014-11-04 05:41:12 -07:00
2016-06-18 10:26:42 -07:00
function showServerMenu(elem) {
2014-11-04 05:41:12 -07:00
2016-06-18 10:26:42 -07:00
var card = parentWithClass(elem, 'serverItem');
var page = parentWithClass(elem, 'page');
var serverId = card.getAttribute('data-id');
2014-11-04 05:41:12 -07:00
2016-06-18 10:26:42 -07:00
var menuItems = [];
2014-11-04 05:41:12 -07:00
2016-06-18 10:26:42 -07:00
menuItems.push({
name: Globalize.translate('ButtonDelete'),
2016-06-18 22:26:52 -07:00
id: 'delete'
2016-06-18 10:26:42 -07:00
});
2014-11-04 05:41:12 -07:00
2016-06-18 10:26:42 -07:00
require(['actionsheet'], function (actionsheet) {
2014-11-04 05:41:12 -07:00
2016-06-18 10:26:42 -07:00
actionsheet.show({
items: menuItems,
positionTo: elem,
callback: function (id) {
2014-11-04 05:41:12 -07:00
2016-06-18 10:26:42 -07:00
switch (id) {
2014-11-04 05:41:12 -07:00
2016-06-18 10:26:42 -07:00
case 'delete':
deleteServer(page, serverId);
break;
default:
break;
}
2015-08-29 17:40:52 -07:00
}
2016-06-18 10:26:42 -07:00
});
2015-08-29 17:40:52 -07:00
});
2016-06-18 10:26:42 -07:00
}
2014-11-04 05:41:12 -07:00
2016-06-18 10:26:42 -07:00
function showPendingInviteMenu(elem) {
2014-11-04 05:41:12 -07:00
2016-06-18 10:26:42 -07:00
var card = parentWithClass(elem, 'inviteItem');
var page = parentWithClass(elem, 'page');
var invitationId = card.getAttribute('data-id');
2014-11-04 05:41:12 -07:00
2016-06-18 10:26:42 -07:00
var menuItems = [];
2014-11-04 05:41:12 -07:00
2016-06-18 10:26:42 -07:00
menuItems.push({
name: Globalize.translate('ButtonAccept'),
2016-06-18 22:26:52 -07:00
id: 'accept'
2016-06-18 10:26:42 -07:00
});
2014-11-04 05:41:12 -07:00
2016-06-18 10:26:42 -07:00
menuItems.push({
name: Globalize.translate('ButtonReject'),
2016-06-18 22:26:52 -07:00
id: 'reject'
2016-06-18 10:26:42 -07:00
});
2014-11-04 05:41:12 -07:00
2016-06-18 10:26:42 -07:00
require(['actionsheet'], function (actionsheet) {
2014-11-04 05:41:12 -07:00
2016-06-18 10:26:42 -07:00
actionsheet.show({
items: menuItems,
positionTo: elem,
callback: function (id) {
2015-05-19 12:15:40 -07:00
2016-06-18 10:26:42 -07:00
switch (id) {
2014-11-04 05:41:12 -07:00
2016-06-18 10:26:42 -07:00
case 'accept':
acceptInvitation(page, invitationId);
break;
case 'reject':
rejectInvitation(page, invitationId);
break;
default:
break;
}
}
});
2014-11-04 05:41:12 -07:00
2016-06-18 10:26:42 -07:00
});
}
2014-11-04 05:41:12 -07:00
2016-06-18 10:26:42 -07:00
function getPendingInviteHtml(invite) {
2014-11-04 05:41:12 -07:00
2016-06-18 10:26:42 -07:00
var html = '';
2014-11-04 05:41:12 -07:00
2016-06-25 18:33:16 -07:00
html += '<div class="listItem inviteItem" data-id="' + invite.Id + '">';
2014-11-04 05:41:12 -07:00
2016-06-25 18:33:16 -07:00
html += '<button type="button" is="emby-button" class="fab mini autoSize blue lnkServer" item-icon><i class="md-icon">wifi</i></button>';
2014-11-04 05:41:12 -07:00
2016-06-25 18:33:16 -07:00
html += '<div class="listItemBody">';
2014-11-04 05:41:12 -07:00
2016-06-18 10:26:42 -07:00
html += '<div>';
html += invite.Name;
html += '</div>';
2014-11-04 05:41:12 -07:00
2016-06-25 18:33:16 -07:00
html += '</div>';
2014-11-04 05:41:12 -07:00
2016-06-18 22:26:52 -07:00
html += '<button is="paper-icon-button-light" class="btnInviteMenu autoSize"><i class="md-icon">' + AppInfo.moreIcon.replace('-','_') + '</i></button>';
2014-11-04 05:41:12 -07:00
2016-06-25 18:33:16 -07:00
html += '</div>';
2015-05-05 08:24:47 -07:00
2016-06-18 10:26:42 -07:00
return html;
}
2015-05-05 08:24:47 -07:00
2016-06-18 10:26:42 -07:00
function renderInvitations(page, list) {
2015-05-05 08:24:47 -07:00
2016-06-18 10:26:42 -07:00
if (list.length) {
page.querySelector('.invitationSection').classList.remove('hide');
} else {
page.querySelector('.invitationSection').classList.add('hide');
}
2015-05-05 08:24:47 -07:00
2016-06-18 10:26:42 -07:00
var html = list.map(getPendingInviteHtml).join('');
2014-11-04 05:41:12 -07:00
2016-06-18 10:26:42 -07:00
page.querySelector('.invitationList').innerHTML = html;
}
2014-10-25 11:32:58 -07:00
2016-06-18 10:26:42 -07:00
function loadInvitations(page) {
2014-10-25 11:32:58 -07:00
2016-06-18 10:26:42 -07:00
if (ConnectionManager.isLoggedIntoConnect()) {
2014-10-25 11:32:58 -07:00
2016-06-18 10:26:42 -07:00
ConnectionManager.getUserInvitations().then(function (list) {
2014-10-25 11:32:58 -07:00
2016-06-18 10:26:42 -07:00
renderInvitations(page, list);
2015-05-05 16:15:47 -07:00
2016-06-18 10:26:42 -07:00
});
2014-10-25 11:32:58 -07:00
2016-06-18 10:26:42 -07:00
} else {
2014-11-04 05:41:12 -07:00
2016-06-18 10:26:42 -07:00
renderInvitations(page, []);
}
2015-05-19 12:15:40 -07:00
}
2014-10-25 11:32:58 -07:00
2016-06-18 10:26:42 -07:00
function loadPage(page) {
2015-05-19 12:15:40 -07:00
2016-06-18 10:26:42 -07:00
Dashboard.showLoadingMsg();
ConnectionManager.getAvailableServers().then(function (servers) {
servers = servers.slice(0);
cachedServers = servers;
renderServers(page, servers);
Dashboard.hideLoadingMsg();
});
loadInvitations(page);
if (ConnectionManager.isLoggedIntoConnect()) {
page.querySelector('.connectLogin').classList.add('hide');
} else {
page.querySelector('.connectLogin').classList.remove('hide');
}
2015-05-12 21:55:19 -07:00
}
2015-05-18 15:23:03 -07:00
2016-06-18 10:26:42 -07:00
view.querySelector('.invitationList').addEventListener('click', function (e) {
2015-05-18 15:23:03 -07:00
2016-06-18 10:26:42 -07:00
var btnInviteMenu = parentWithClass(e.target, 'btnInviteMenu');
if (btnInviteMenu) {
showPendingInviteMenu(btnInviteMenu);
}
});
view.querySelector('.serverList').addEventListener('click', function (e) {
2015-05-12 21:55:19 -07:00
2016-06-18 10:26:42 -07:00
var lnkServer = parentWithClass(e.target, 'lnkServer');
if (lnkServer) {
var item = parentWithClass(lnkServer, 'serverItem');
var id = item.getAttribute('data-id');
2014-10-25 11:32:58 -07:00
2016-06-18 10:26:42 -07:00
var server = cachedServers.filter(function (s) {
return s.Id == id;
})[0];
2014-10-25 11:32:58 -07:00
2016-06-18 22:26:52 -07:00
connectToServer(view, server);
2016-06-18 10:26:42 -07:00
}
var btnServerMenu = parentWithClass(e.target, 'btnServerMenu');
if (btnServerMenu) {
showServerMenu(btnServerMenu);
}
});
2014-10-25 11:32:58 -07:00
2016-06-18 10:26:42 -07:00
view.addEventListener('viewbeforeshow', function () {
updatePageStyle(this);
});
view.addEventListener('viewshow', function () {
loadPage(this);
});
};
});