jellyfin-web/dashboard-ui/apiclient/credentials.js

110 lines
3.2 KiB
JavaScript
Raw Normal View History

2015-01-16 13:54:37 -07:00
(function (globalScope, JSON) {
2014-10-23 21:54:35 -07:00
2014-10-27 14:45:50 -07:00
if (!globalScope.MediaBrowser) {
globalScope.MediaBrowser = {};
}
2014-10-23 21:54:35 -07:00
2015-06-13 07:46:59 -07:00
globalScope.MediaBrowser.CredentialProvider = function (key) {
2014-10-23 21:54:35 -07:00
var self = this;
2015-06-08 14:32:20 -07:00
var credentials = null;
2015-06-13 07:46:59 -07:00
key = key || 'servercredentials3';
2014-10-23 21:54:35 -07:00
function ensure() {
2015-06-08 14:32:20 -07:00
if (!credentials) {
var json = appStorage.getItem(key) || '{}';
2015-05-28 19:53:05 -07:00
2015-06-26 20:27:38 -07:00
Logger.log('credentials initialized with: ' + json);
2015-06-08 14:32:20 -07:00
credentials = JSON.parse(json);
2015-07-24 14:44:25 -07:00
credentials.Servers = credentials.Servers || [];
2015-06-08 14:32:20 -07:00
}
2014-10-23 21:54:35 -07:00
}
function get() {
ensure();
return credentials;
}
function set(data) {
2015-02-16 13:56:57 -07:00
2014-12-03 22:24:41 -07:00
if (data) {
credentials = data;
2015-05-20 10:29:26 -07:00
appStorage.setItem(key, JSON.stringify(data));
2014-12-03 22:24:41 -07:00
} else {
self.clear();
}
2015-05-28 16:37:43 -07:00
Events.trigger(self, 'credentialsupdated');
2014-10-23 21:54:35 -07:00
}
2014-10-27 14:45:50 -07:00
self.clear = function () {
2014-10-25 11:32:58 -07:00
credentials = null;
2015-05-20 10:29:26 -07:00
appStorage.removeItem(key);
2014-10-25 11:32:58 -07:00
};
2014-10-23 21:54:35 -07:00
self.credentials = function (data) {
if (data) {
set(data);
}
return get();
};
2014-10-27 14:45:50 -07:00
self.addOrUpdateServer = function (list, server) {
2014-10-23 21:54:35 -07:00
2015-05-24 11:33:28 -07:00
if (!server.Id) {
throw new Error('Server.Id cannot be null or empty');
}
2014-10-27 14:45:50 -07:00
var existing = list.filter(function (s) {
2014-10-23 21:54:35 -07:00
return s.Id == server.Id;
})[0];
2014-10-27 14:45:50 -07:00
if (existing) {
2015-02-16 13:56:57 -07:00
2014-10-23 21:54:35 -07:00
// Merge the data
2015-02-16 13:56:57 -07:00
existing.DateLastAccessed = Math.max(existing.DateLastAccessed || 0, server.DateLastAccessed || 0);
2014-10-23 21:54:35 -07:00
2015-06-08 14:32:20 -07:00
existing.UserLinkType = server.UserLinkType;
2014-10-27 14:45:50 -07:00
if (server.AccessToken) {
2014-10-23 21:54:35 -07:00
existing.AccessToken = server.AccessToken;
existing.UserId = server.UserId;
}
2014-10-27 14:45:50 -07:00
if (server.ExchangeToken) {
2014-10-23 21:54:35 -07:00
existing.ExchangeToken = server.ExchangeToken;
}
2014-10-27 14:45:50 -07:00
if (server.RemoteAddress) {
2014-10-23 21:54:35 -07:00
existing.RemoteAddress = server.RemoteAddress;
}
2015-06-04 13:27:46 -07:00
if (server.ManualAddress) {
existing.ManualAddress = server.ManualAddress;
}
2014-10-27 14:45:50 -07:00
if (server.LocalAddress) {
2014-10-23 21:54:35 -07:00
existing.LocalAddress = server.LocalAddress;
}
2014-10-27 14:45:50 -07:00
if (server.Name) {
2014-10-23 21:54:35 -07:00
existing.Name = server.Name;
}
2014-10-27 14:45:50 -07:00
if (server.WakeOnLanInfos && server.WakeOnLanInfos.length) {
2014-10-23 21:54:35 -07:00
existing.WakeOnLanInfos = server.WakeOnLanInfos;
}
2015-05-24 11:33:28 -07:00
if (server.LastConnectionMode != null) {
existing.LastConnectionMode = server.LastConnectionMode;
}
2015-05-26 10:48:05 -07:00
existing.DateLastLocalConnection = Math.max(existing.DateLastLocalConnection || 0, server.DateLastLocalConnection || 0);
2015-05-24 11:33:28 -07:00
return existing;
2014-10-23 21:54:35 -07:00
}
else {
list.push(server);
2015-05-24 11:33:28 -07:00
return server;
2014-10-23 21:54:35 -07:00
}
};
};
2015-01-16 13:54:37 -07:00
})(window, window.JSON);