From 6c5a958af09f32884b59406ac32d982abcc58150 Mon Sep 17 00:00:00 2001 From: Tomina Date: Wed, 14 Oct 2020 22:11:36 +0200 Subject: [PATCH] feat(data): allow custom DataSet/View implementations (#1108) This tests that the methods and properties are implemented instead of checking the prototype. Thanks to this it is now possible to use a different DataSet or DataView without extending the Vis one. This also removes some interoperability issues of the standalone build when used toghether with other Vis family libraries. --- lib/network/modules/EdgesHandler.js | 6 +++--- lib/network/modules/NodesHandler.js | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/network/modules/EdgesHandler.js b/lib/network/modules/EdgesHandler.js index 78556630c5..4938468acd 100644 --- a/lib/network/modules/EdgesHandler.js +++ b/lib/network/modules/EdgesHandler.js @@ -1,5 +1,5 @@ import { deepExtend, forEach } from "vis-util/esnext"; -import { DataSet, DataView } from "vis-data/esnext"; +import { DataSet, isDataViewLike } from "vis-data/esnext"; import Edge from "./components/Edge"; /** @@ -248,13 +248,13 @@ class EdgesHandler { * Load edges by reading the data table * * @param {Array | DataSet | DataView} edges The data containing the edges. - * @param {boolean} [doNotEmit=false] + * @param {boolean} [doNotEmit=false] - Suppress data changed event. * @private */ setData(edges, doNotEmit = false) { const oldEdgesData = this.body.data.edges; - if (edges instanceof DataSet || edges instanceof DataView) { + if (isDataViewLike("id", edges)) { this.body.data.edges = edges; } else if (Array.isArray(edges)) { this.body.data.edges = new DataSet(); diff --git a/lib/network/modules/NodesHandler.js b/lib/network/modules/NodesHandler.js index 2d5e4b8d64..96d75d9072 100644 --- a/lib/network/modules/NodesHandler.js +++ b/lib/network/modules/NodesHandler.js @@ -1,5 +1,5 @@ import { bridgeObject, forEach } from "vis-util/esnext"; -import { DataSet, DataView } from "vis-data/esnext"; +import { DataSet, isDataViewLike } from "vis-data/esnext"; import Node from "./components/Node"; /** @@ -245,13 +245,13 @@ class NodesHandler { * Set a data set with nodes for the network * * @param {Array | DataSet | DataView} nodes The data containing the nodes. - * @param {boolean} [doNotEmit=false] + * @param {boolean} [doNotEmit=false] - Suppress data changed event. * @private */ setData(nodes, doNotEmit = false) { const oldNodesData = this.body.data.nodes; - if (nodes instanceof DataSet || nodes instanceof DataView) { + if (isDataViewLike("id", nodes)) { this.body.data.nodes = nodes; } else if (Array.isArray(nodes)) { this.body.data.nodes = new DataSet();