From 6aa7070196977529af8cbc4315b1241db924682c Mon Sep 17 00:00:00 2001 From: ziajka Date: Mon, 19 Mar 2018 13:23:39 +0100 Subject: [PATCH] NodesDataSource --- .../shared/datasources/datasource.spec.ts | 63 +++++++++++++++++++ .../shared/datasources/datasource.ts | 38 +++++++++++ .../datasources/nodes-datasource.spec.ts | 33 ++++++++++ .../shared/datasources/nodes-datasource.ts | 9 +++ .../shared/managers/selection-manager.ts | 13 ++++ 5 files changed, 156 insertions(+) create mode 100644 src/app/cartography/shared/datasources/datasource.spec.ts create mode 100644 src/app/cartography/shared/datasources/datasource.ts create mode 100644 src/app/cartography/shared/datasources/nodes-datasource.spec.ts create mode 100644 src/app/cartography/shared/datasources/nodes-datasource.ts create mode 100644 src/app/cartography/shared/managers/selection-manager.ts diff --git a/src/app/cartography/shared/datasources/datasource.spec.ts b/src/app/cartography/shared/datasources/datasource.spec.ts new file mode 100644 index 00000000..1ee9d4b1 --- /dev/null +++ b/src/app/cartography/shared/datasources/datasource.spec.ts @@ -0,0 +1,63 @@ +import {DataSource} from "./datasource"; + +class TestDataSource extends DataSource { + 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"]); + }); + }); + +}); diff --git a/src/app/cartography/shared/datasources/datasource.ts b/src/app/cartography/shared/datasources/datasource.ts new file mode 100644 index 00000000..5348eb65 --- /dev/null +++ b/src/app/cartography/shared/datasources/datasource.ts @@ -0,0 +1,38 @@ +import {BehaviorSubject} from "rxjs/BehaviorSubject"; + +export abstract class DataSource { + protected data: T[] = []; + protected dataChange: BehaviorSubject = new BehaviorSubject([]); + + 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; +} diff --git a/src/app/cartography/shared/datasources/nodes-datasource.spec.ts b/src/app/cartography/shared/datasources/nodes-datasource.spec.ts new file mode 100644 index 00000000..0b1d2c78 --- /dev/null +++ b/src/app/cartography/shared/datasources/nodes-datasource.spec.ts @@ -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"); + }); + }); + +}); diff --git a/src/app/cartography/shared/datasources/nodes-datasource.ts b/src/app/cartography/shared/datasources/nodes-datasource.ts new file mode 100644 index 00000000..4df37084 --- /dev/null +++ b/src/app/cartography/shared/datasources/nodes-datasource.ts @@ -0,0 +1,9 @@ +import {Node} from "../models/node.model"; +import {DataSource} from "./datasource"; + + +export class NodesDataSource extends DataSource { + protected findIndex(node: Node) { + return this.data.findIndex((n: Node) => n.node_id === node.node_id); + } +} diff --git a/src/app/cartography/shared/managers/selection-manager.ts b/src/app/cartography/shared/managers/selection-manager.ts new file mode 100644 index 00000000..e964ba1b --- /dev/null +++ b/src/app/cartography/shared/managers/selection-manager.ts @@ -0,0 +1,13 @@ +import {Node} from "../models/node.model"; + +export class SelectionManager { + private selectedNodes: Node[] = []; + + constructor() {} + + public setSelectedNodes(nodes: Node[]) { + this.selectedNodes = nodes; + } + + +}