mirror of
https://github.com/GNS3/gns3-web-ui.git
synced 2025-06-19 23:33:43 +00:00
Updating angular to 12, removing indexed db
This commit is contained in:
@ -225,7 +225,6 @@ import { ExternalSoftwareDefinitionService } from './services/external-software-
|
||||
import { Gns3vmService } from './services/gns3vm.service';
|
||||
import { GoogleAnalyticsService } from './services/google-analytics.service';
|
||||
import { HttpServer, ServerErrorHandler } from './services/http-server.service';
|
||||
import { IndexedDbService } from './services/indexed-db.service';
|
||||
import { InfoService } from './services/info.service';
|
||||
import { InstalledSoftwareService } from './services/installed-software.service';
|
||||
import { IosConfigurationService } from './services/ios-configuration.service';
|
||||
@ -488,7 +487,6 @@ import { RotationValidator } from './validators/rotation-validator';
|
||||
NodeService,
|
||||
LinkService,
|
||||
DrawingService,
|
||||
IndexedDbService,
|
||||
HttpServer,
|
||||
SnapshotService,
|
||||
ProgressDialogService,
|
||||
|
@ -1,20 +0,0 @@
|
||||
import { inject, TestBed } from '@angular/core/testing';
|
||||
import { IndexedDbService } from './indexed-db.service';
|
||||
|
||||
describe('IndexedDbService', () => {
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({
|
||||
providers: [IndexedDbService],
|
||||
});
|
||||
});
|
||||
|
||||
it('should be created', inject([IndexedDbService], (service: IndexedDbService) => {
|
||||
expect(service).toBeTruthy();
|
||||
}));
|
||||
|
||||
it('should get AngularIndexedDB', inject([IndexedDbService], (service: IndexedDbService) => {
|
||||
const indexeddb = service.get();
|
||||
expect(indexeddb.dbWrapper.dbName).toEqual(IndexedDbService.DATABASE);
|
||||
expect(indexeddb.dbWrapper.dbVersion).toEqual(IndexedDbService.VERSION);
|
||||
}));
|
||||
});
|
@ -1,18 +0,0 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { AngularIndexedDB } from 'angular2-indexeddb';
|
||||
|
||||
@Injectable()
|
||||
export class IndexedDbService {
|
||||
static VERSION = 1;
|
||||
static DATABASE = 'gns3-web-ui';
|
||||
|
||||
private db: AngularIndexedDB;
|
||||
|
||||
constructor() {
|
||||
this.db = new AngularIndexedDB(IndexedDbService.DATABASE, IndexedDbService.VERSION);
|
||||
}
|
||||
|
||||
public get() {
|
||||
return this.db;
|
||||
}
|
||||
}
|
@ -1,179 +0,0 @@
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
import { AngularIndexedDB } from 'angular2-indexeddb';
|
||||
import { Server } from '../models/server';
|
||||
import { HttpServer, ServerErrorHandler } from '../services/http-server.service';
|
||||
import { IndexedDbService } from './indexed-db.service';
|
||||
import { ServerService } from './server.service';
|
||||
|
||||
import Spy = jasmine.Spy;
|
||||
|
||||
export class MockedServerService {
|
||||
public servers: Server[] = [];
|
||||
|
||||
public create(server: Server) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.servers.push(server);
|
||||
resolve(server);
|
||||
});
|
||||
}
|
||||
|
||||
public get(server_id: number) {
|
||||
const server = new Server();
|
||||
server.id = server_id;
|
||||
return Promise.resolve(server);
|
||||
}
|
||||
|
||||
public getLocalServer(hostname: string, port: number) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const server = new Server();
|
||||
server.id = 99;
|
||||
resolve(server);
|
||||
});
|
||||
}
|
||||
|
||||
public findAll() {
|
||||
return new Promise((resolve, reject) => {
|
||||
resolve(this.servers);
|
||||
});
|
||||
}
|
||||
|
||||
public getServerUrl(server: Server) {
|
||||
return `${server.host}:${server.port}`;
|
||||
}
|
||||
}
|
||||
|
||||
describe('ServerService', () => {
|
||||
let indexedDbService: IndexedDbService;
|
||||
let db: AngularIndexedDB;
|
||||
let service: ServerService;
|
||||
let openDatabaseSpy: Spy;
|
||||
let httpServer = new HttpServer({} as HttpClient, {} as ServerErrorHandler);
|
||||
|
||||
beforeEach(() => {
|
||||
indexedDbService = new IndexedDbService();
|
||||
|
||||
db = indexedDbService.get();
|
||||
|
||||
openDatabaseSpy = spyOn(db, 'openDatabase').and.returnValue(Promise.resolve(true));
|
||||
|
||||
TestBed.configureTestingModule({
|
||||
providers: [
|
||||
ServerService,
|
||||
{ provide: IndexedDbService, useValue: indexedDbService },
|
||||
{ provide: HttpServer, useValue: httpServer },
|
||||
],
|
||||
});
|
||||
|
||||
service = TestBed.get(ServerService);
|
||||
});
|
||||
|
||||
it('should be created and create database', () => {
|
||||
expect(service).toBeTruthy();
|
||||
expect(db.openDatabase).toHaveBeenCalled();
|
||||
expect(openDatabaseSpy.calls.first().args[0]).toEqual(1);
|
||||
|
||||
const evnt = {
|
||||
currentTarget: {
|
||||
result: {
|
||||
createObjectStore: function () {},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
spyOn(evnt.currentTarget.result, 'createObjectStore');
|
||||
|
||||
const upgradeCallback = openDatabaseSpy.calls.first().args[1];
|
||||
upgradeCallback(evnt);
|
||||
|
||||
expect(evnt.currentTarget.result.createObjectStore).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
describe('operations on records', () => {
|
||||
let record: any;
|
||||
|
||||
beforeEach(() => {
|
||||
record = new Server();
|
||||
record.name = 'test';
|
||||
});
|
||||
|
||||
it('should create an object', (done) => {
|
||||
const created = new Server();
|
||||
created.id = 22;
|
||||
|
||||
spyOn(db, 'add').and.returnValue(Promise.resolve(created));
|
||||
|
||||
service.create(record).then((result) => {
|
||||
expect(db.add).toHaveBeenCalledWith('servers', record);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should update an object', (done) => {
|
||||
spyOn(db, 'update').and.returnValue(Promise.resolve(record));
|
||||
|
||||
service.update(record).then((result) => {
|
||||
expect(db.update).toHaveBeenCalledWith('servers', record);
|
||||
expect(result).toEqual(record);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should delete an object', (done) => {
|
||||
record.id = 88;
|
||||
|
||||
spyOn(db, 'delete').and.returnValue(Promise.resolve());
|
||||
|
||||
service.delete(record).then(() => {
|
||||
expect(db.delete).toHaveBeenCalledWith('servers', record.id);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should call findAll', (done) => {
|
||||
spyOn(db, 'getAll').and.returnValue(Promise.resolve([]));
|
||||
|
||||
service.findAll().then((result) => {
|
||||
expect(result).toEqual([]);
|
||||
expect(db.getAll).toHaveBeenCalledWith('servers');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should create local server when missing', (done) => {
|
||||
spyOn(db, 'getAll').and.returnValue(Promise.resolve([]));
|
||||
spyOn(service, 'create').and.returnValue(Promise.resolve(new Server()));
|
||||
|
||||
const expectedServer = new Server();
|
||||
expectedServer.name = 'local';
|
||||
expectedServer.host = 'hostname';
|
||||
expectedServer.port = 9999;
|
||||
expectedServer.location = 'bundled';
|
||||
expectedServer.protocol = 'http:';
|
||||
|
||||
service.getLocalServer('hostname', 9999).then(() => {
|
||||
expect(service.create).toHaveBeenCalledWith(expectedServer);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should update local server when found', (done) => {
|
||||
const server = new Server();
|
||||
server.name = 'local';
|
||||
server.host = 'hostname';
|
||||
server.port = 9999;
|
||||
server.location = 'bundled';
|
||||
|
||||
spyOn(db, 'getAll').and.returnValue(Promise.resolve([server]));
|
||||
spyOn(service, 'update').and.returnValue(Promise.resolve(new Server()));
|
||||
|
||||
service.getLocalServer('hostname-2', 11111).then(() => {
|
||||
server.host = 'hostname-2';
|
||||
server.port = 11111;
|
||||
|
||||
expect(service.update).toHaveBeenCalledWith(server);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
@ -2,146 +2,88 @@ import { Injectable } from '@angular/core';
|
||||
import { Observable, Subject } from 'rxjs';
|
||||
import { Server, ServerProtocol } from '../models/server';
|
||||
import { HttpServer } from './http-server.service';
|
||||
import { IndexedDbService } from './indexed-db.service';
|
||||
|
||||
@Injectable()
|
||||
export class ServerService {
|
||||
private tablename = 'servers';
|
||||
private ready: Promise<any>;
|
||||
private isIncognitoMode: boolean = false;
|
||||
private serverIdsInIncognitoMode: string[] = [];
|
||||
private serverIds: string[] = [];
|
||||
public serviceInitialized: Subject<boolean> = new Subject<boolean>();
|
||||
public isServiceInitialized: boolean;
|
||||
|
||||
constructor(private indexedDbService: IndexedDbService, private httpServer: HttpServer) {
|
||||
this.ready = this.indexedDbService
|
||||
.get()
|
||||
.openDatabase(1, (evt) => {
|
||||
evt.currentTarget.result.createObjectStore(this.tablename, { keyPath: 'id', autoIncrement: true });
|
||||
})
|
||||
.then(() => {
|
||||
this.indexedDbService
|
||||
.get()
|
||||
.getAll(this.tablename)
|
||||
.then(() => {})
|
||||
.catch(() => {
|
||||
this.isIncognitoMode = true;
|
||||
});
|
||||
})
|
||||
.catch(() => {
|
||||
this.isIncognitoMode = true;
|
||||
})
|
||||
.finally(() => {
|
||||
this.isServiceInitialized = true;
|
||||
this.serviceInitialized.next(true);
|
||||
});
|
||||
constructor(private httpServer: HttpServer) {
|
||||
this.serverIds = this.getServerIds();
|
||||
this.isServiceInitialized = true;
|
||||
this.serviceInitialized.next(this.isServiceInitialized);
|
||||
}
|
||||
|
||||
public tryToCreateDb() {
|
||||
let promise = new Promise((resolve) => {
|
||||
this.indexedDbService
|
||||
.get()
|
||||
.openDatabase(1, (evt) => {
|
||||
evt.currentTarget.result.createObjectStore(this.tablename, { keyPath: 'id', autoIncrement: true });
|
||||
})
|
||||
.then(() => {})
|
||||
.catch(() => {
|
||||
this.isIncognitoMode = true;
|
||||
});
|
||||
getServerIds() : string[]{
|
||||
let str = localStorage.getItem("serverIds");
|
||||
if (str?.length > 0) {
|
||||
return str.split(",");
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
updateServerIds() {
|
||||
localStorage.removeItem("serverIds");
|
||||
localStorage.setItem("serverIds", this.serverIds.toString());
|
||||
}
|
||||
|
||||
public get(id: number): Promise<Server> {
|
||||
let server: Server = JSON.parse(localStorage.getItem(`server-${id}`));
|
||||
let promise = new Promise<Server>((resolve) => {
|
||||
resolve(server);
|
||||
});
|
||||
return promise;
|
||||
}
|
||||
|
||||
public get(id: number): Promise<Server> {
|
||||
if (this.isIncognitoMode) {
|
||||
let server: Server = JSON.parse(localStorage.getItem(`server-${id}`));
|
||||
let promise = new Promise<Server>((resolve) => {
|
||||
resolve(server);
|
||||
});
|
||||
return promise;
|
||||
}
|
||||
|
||||
return this.onReady(() => this.indexedDbService.get().getByKey(this.tablename, id)) as Promise<Server>;
|
||||
}
|
||||
|
||||
public create(server: Server) {
|
||||
if (this.isIncognitoMode) {
|
||||
server.id = this.serverIdsInIncognitoMode.length + 1;
|
||||
localStorage.setItem(`server-${server.id}`, JSON.stringify(server));
|
||||
this.serverIdsInIncognitoMode.push(`server-${server.id}`);
|
||||
server.id = this.serverIds.length + 1;
|
||||
localStorage.setItem(`server-${server.id}`, JSON.stringify(server));
|
||||
|
||||
let promise = new Promise<Server>((resolve) => {
|
||||
resolve(server);
|
||||
});
|
||||
return promise;
|
||||
}
|
||||
this.serverIds.push(`server-${server.id}`);
|
||||
this.updateServerIds();
|
||||
|
||||
return this.onReady(() => {
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
this.indexedDbService
|
||||
.get()
|
||||
.add(this.tablename, server)
|
||||
.then((added) => {
|
||||
server.id = added.key;
|
||||
resolve(server);
|
||||
}, reject);
|
||||
});
|
||||
return promise;
|
||||
let promise = new Promise<Server>((resolve) => {
|
||||
resolve(server);
|
||||
});
|
||||
return promise;
|
||||
}
|
||||
|
||||
public update(server: Server) {
|
||||
if (this.isIncognitoMode) {
|
||||
localStorage.removeItem(`server-${server.id}`);
|
||||
localStorage.setItem(`server-${server.id}`, JSON.stringify(server));
|
||||
localStorage.removeItem(`server-${server.id}`);
|
||||
localStorage.setItem(`server-${server.id}`, JSON.stringify(server));
|
||||
|
||||
let promise = new Promise<Server>((resolve) => {
|
||||
resolve(server);
|
||||
});
|
||||
return promise;
|
||||
}
|
||||
|
||||
return this.onReady(() => {
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
this.indexedDbService
|
||||
.get()
|
||||
.update(this.tablename, server)
|
||||
.then((updated) => {
|
||||
resolve(server);
|
||||
}, reject);
|
||||
});
|
||||
return promise;
|
||||
let promise = new Promise<Server>((resolve) => {
|
||||
resolve(server);
|
||||
});
|
||||
return promise;
|
||||
}
|
||||
|
||||
public findAll() {
|
||||
if (this.isIncognitoMode) {
|
||||
let promise = new Promise<Server[]>((resolve) => {
|
||||
let servers: Server[] = [];
|
||||
this.serverIdsInIncognitoMode.forEach((n) => {
|
||||
let server: Server = JSON.parse(localStorage.getItem(n));
|
||||
servers.push(server);
|
||||
});
|
||||
resolve(servers);
|
||||
});
|
||||
return promise;
|
||||
}
|
||||
let promise = new Promise<Server[]>((resolve) => {
|
||||
let servers: Server[] = [];
|
||||
this.serverIds.forEach((n) => {
|
||||
console.log(n);
|
||||
|
||||
return this.onReady(() => this.indexedDbService.get().getAll(this.tablename)) as Promise<Server[]>;
|
||||
let server: Server = JSON.parse(localStorage.getItem(n));
|
||||
console.log(server);
|
||||
|
||||
servers.push(server);
|
||||
});
|
||||
resolve(servers);
|
||||
});
|
||||
return promise;
|
||||
}
|
||||
|
||||
public delete(server: Server) {
|
||||
if (this.isIncognitoMode) {
|
||||
localStorage.removeItem(`server-${server.id}`);
|
||||
this.serverIdsInIncognitoMode = this.serverIdsInIncognitoMode.filter((n) => n !== `server-${server.id}`);
|
||||
localStorage.removeItem(`server-${server.id}`);
|
||||
this.serverIds = this.serverIds.filter((n) => n !== `server-${server.id}`);
|
||||
this.updateServerIds();
|
||||
|
||||
let promise = new Promise((resolve) => {
|
||||
resolve(server.id);
|
||||
});
|
||||
return promise;
|
||||
}
|
||||
|
||||
return this.onReady(() => this.indexedDbService.get().delete(this.tablename, server.id));
|
||||
let promise = new Promise((resolve) => {
|
||||
resolve(server.id);
|
||||
});
|
||||
return promise;
|
||||
}
|
||||
|
||||
public getServerUrl(server: Server) {
|
||||
@ -179,25 +121,4 @@ export class ServerService {
|
||||
|
||||
return promise;
|
||||
}
|
||||
|
||||
protected onReady(query) {
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
this.ready.then(
|
||||
() => {
|
||||
query().then(
|
||||
(result) => {
|
||||
resolve(result);
|
||||
},
|
||||
(error) => {
|
||||
reject(error);
|
||||
}
|
||||
);
|
||||
},
|
||||
(error) => {
|
||||
reject(error);
|
||||
}
|
||||
);
|
||||
});
|
||||
return promise;
|
||||
}
|
||||
}
|
||||
|
@ -16,4 +16,4 @@ export const environment = {
|
||||
* This import should be commented out in production mode because it will have a negative impact
|
||||
* on performance if an error is thrown.
|
||||
*/
|
||||
// import 'zone.js/dist/zone-error'; // Included with Angular CLI.
|
||||
// import 'zone.js/plugins/zone-error'; // Included with Angular CLI.
|
||||
|
@ -72,7 +72,7 @@
|
||||
/***************************************************************************************************
|
||||
* Zone JS is required by default for Angular itself.
|
||||
*/
|
||||
import 'zone.js/dist/zone'; // Included with Angular CLI.
|
||||
import 'zone.js'; // Included with Angular CLI.
|
||||
|
||||
/***************************************************************************************************
|
||||
* APPLICATION IMPORTS
|
||||
|
@ -1,4 +1,4 @@
|
||||
import 'zone.js/dist/zone-testing';
|
||||
import 'zone.js/testing';
|
||||
import { getTestBed } from '@angular/core/testing';
|
||||
import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing';
|
||||
|
||||
|
@ -17,7 +17,6 @@
|
||||
"polyfills.ts"
|
||||
],
|
||||
"include": [
|
||||
// "../src/**/*",
|
||||
"../node_modules/angular2-indexeddb/*"
|
||||
// "../src/**/*"
|
||||
]
|
||||
}
|
||||
|
@ -15,7 +15,6 @@
|
||||
],
|
||||
"include": [
|
||||
"**/*.spec.ts",
|
||||
"**/*.d.ts",
|
||||
"../node_modules/angular2-indexeddb/*"
|
||||
"**/*.d.ts"
|
||||
]
|
||||
}
|
||||
|
Reference in New Issue
Block a user