mirror of
https://github.com/GNS3/gns3-web-ui.git
synced 2025-01-18 18:56:26 +00:00
NodesDataSource
This commit is contained in:
parent
65b491e54e
commit
6aa7070196
63
src/app/cartography/shared/datasources/datasource.spec.ts
Normal file
63
src/app/cartography/shared/datasources/datasource.spec.ts
Normal file
@ -0,0 +1,63 @@
|
||||
import {DataSource} from "./datasource";
|
||||
|
||||
class TestDataSource extends DataSource<string> {
|
||||
protected findIndex(item: string) {
|
||||
return this.data.findIndex((i: string) => i === item);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
describe('TestDataSource', () => {
|
||||
let dataSource: TestDataSource;
|
||||
let data: string[];
|
||||
|
||||
beforeEach(() => {
|
||||
dataSource = new TestDataSource();
|
||||
dataSource.connect().subscribe((updated: string[]) => {
|
||||
data = updated;
|
||||
});
|
||||
});
|
||||
|
||||
describe('Item can be added', () => {
|
||||
beforeEach(() => {
|
||||
dataSource.add("test");
|
||||
});
|
||||
|
||||
it('item should be in data', () => {
|
||||
expect(data).toEqual(["test"]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Items can be set', () => {
|
||||
beforeEach(() => {
|
||||
dataSource.set(["test", "test2"]);
|
||||
});
|
||||
|
||||
it('items should be in data', () => {
|
||||
expect(data).toEqual(["test", "test2"]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Items can be removed', () => {
|
||||
beforeEach(() => {
|
||||
dataSource.set(["test", "test2"]);
|
||||
dataSource.remove("test");
|
||||
});
|
||||
|
||||
it('item should not be in data', () => {
|
||||
expect(data).toEqual(["test2"]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Item can be updated', () => {
|
||||
beforeEach(() => {
|
||||
dataSource.set(["test", "test2"]);
|
||||
dataSource.update("test");
|
||||
});
|
||||
|
||||
it('item should be updated', () => {
|
||||
expect(data).toEqual(["test", "test2"]);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
38
src/app/cartography/shared/datasources/datasource.ts
Normal file
38
src/app/cartography/shared/datasources/datasource.ts
Normal file
@ -0,0 +1,38 @@
|
||||
import {BehaviorSubject} from "rxjs/BehaviorSubject";
|
||||
|
||||
export abstract class DataSource<T> {
|
||||
protected data: T[] = [];
|
||||
protected dataChange: BehaviorSubject<T[]> = new BehaviorSubject<T[]>([]);
|
||||
|
||||
public add(item: T) {
|
||||
this.data.push(item);
|
||||
this.dataChange.next(this.data);
|
||||
}
|
||||
|
||||
public set(data: T[]) {
|
||||
this.data = data;
|
||||
this.dataChange.next(this.data);
|
||||
}
|
||||
|
||||
public update(item: T) {
|
||||
const index = this.findIndex(item);
|
||||
if (index >= 0) {
|
||||
this.data[index] = item;
|
||||
this.dataChange.next(this.data);
|
||||
}
|
||||
}
|
||||
|
||||
public remove(item: T) {
|
||||
const index = this.findIndex(item);
|
||||
if (index >= 0) {
|
||||
this.data.splice(index, 1);
|
||||
this.dataChange.next(this.data);
|
||||
}
|
||||
}
|
||||
|
||||
public connect() {
|
||||
return this.dataChange;
|
||||
}
|
||||
|
||||
protected abstract findIndex(item: T): number;
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
import {NodesDataSource} from "./nodes-datasource";
|
||||
import {Node} from "../models/node.model";
|
||||
|
||||
|
||||
describe('NodesDataSource', () => {
|
||||
let dataSource: NodesDataSource;
|
||||
let data: Node[];
|
||||
|
||||
beforeEach(() => {
|
||||
dataSource = new NodesDataSource();
|
||||
dataSource.connect().subscribe((nodes: Node[]) => {
|
||||
data = nodes;
|
||||
});
|
||||
});
|
||||
|
||||
describe('Node can be updated', () => {
|
||||
beforeEach(() => {
|
||||
const node = new Node();
|
||||
node.node_id = "1";
|
||||
node.name = "Node 1";
|
||||
dataSource.add(node);
|
||||
|
||||
node.name = "Node 2";
|
||||
dataSource.update(node);
|
||||
});
|
||||
|
||||
it('name should change', () => {
|
||||
expect(data[1].name).toEqual("1");
|
||||
expect(data[0].name).toEqual("Node 2");
|
||||
});
|
||||
});
|
||||
|
||||
});
|
@ -0,0 +1,9 @@
|
||||
import {Node} from "../models/node.model";
|
||||
import {DataSource} from "./datasource";
|
||||
|
||||
|
||||
export class NodesDataSource extends DataSource<Node> {
|
||||
protected findIndex(node: Node) {
|
||||
return this.data.findIndex((n: Node) => n.node_id === node.node_id);
|
||||
}
|
||||
}
|
13
src/app/cartography/shared/managers/selection-manager.ts
Normal file
13
src/app/cartography/shared/managers/selection-manager.ts
Normal file
@ -0,0 +1,13 @@
|
||||
import {Node} from "../models/node.model";
|
||||
|
||||
export class SelectionManager {
|
||||
private selectedNodes: Node[] = [];
|
||||
|
||||
constructor() {}
|
||||
|
||||
public setSelectedNodes(nodes: Node[]) {
|
||||
this.selectedNodes = nodes;
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user