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

85 lines
2.3 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
2014-10-27 14:45:50 -07:00
globalScope.MediaBrowser.CredentialProvider = function () {
2014-10-23 21:54:35 -07:00
var self = this;
var credentials;
2014-11-18 19:45:12 -07:00
var key = 'servercredentials3';
2014-10-23 21:54:35 -07:00
function ensure() {
2014-11-18 19:45:12 -07:00
credentials = credentials || JSON.parse(store.getItem(key) || '{}');
2014-10-23 21:54:35 -07:00
credentials.servers = credentials.servers || [];
}
function get() {
ensure();
return credentials;
}
function set(data) {
2014-12-03 22:24:41 -07:00
if (data) {
credentials = data;
store.setItem(key, JSON.stringify(data));
} else {
self.clear();
}
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;
2014-11-18 19:45:12 -07:00
store.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
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) {
2014-10-23 21:54:35 -07:00
// Merge the data
existing.DateLastAccessed = Math.max(existing.DateLastAccessed || 0, server.DateLastAccessed || 0, new Date().getTime());
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;
}
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;
}
}
else {
list.push(server);
}
};
};
2015-01-16 13:54:37 -07:00
})(window, window.JSON);