Skip to content

Commit

Permalink
Do not expect the id field in DataSet to always be "id" (visjs#1293)
Browse files Browse the repository at this point in the history
The DataSet allows a configurable "fieldId" argument which by default
is the value "id". The checks for DataSet/DataView interfaces should
use this id property name and not expect a fixed hardcoded value "id".

Co-authored-by: Yotam Berkowitz <[email protected]>
  • Loading branch information
smee and yotamberk authored Nov 25, 2021
1 parent 06f621b commit 327fd3d
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 17 deletions.
10 changes: 5 additions & 5 deletions lib/timeline/Graph2d.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import moment from '../module/moment';
import util, { typeCoerceDataSet } from '../util';
import { DataSet, DataView, isDataViewLike } from 'vis-data/esnext';
import util, { typeCoerceDataSet, isDataViewLike } from '../util';
import { DataSet, DataView } from 'vis-data/esnext';
import Range from './Range';
import Core from './Core';
import TimeAxis from './component/TimeAxis';
Expand All @@ -26,7 +26,7 @@ import Configurator from '../shared/Configurator';
*/
function Graph2d (container, items, groups, options) {
// if the third element is options, the forth is groups (optionally);
if (!(Array.isArray(groups) || isDataViewLike("id", groups)) && groups instanceof Object) {
if (!(Array.isArray(groups) || isDataViewLike(groups)) && groups instanceof Object) {
var forthArgument = options;
options = groups;
groups = forthArgument;
Expand Down Expand Up @@ -198,7 +198,7 @@ Graph2d.prototype.setItems = function(items) {
if (!items) {
newDataSet = null;
}
else if (isDataViewLike("id", items)) {
else if (isDataViewLike(items)) {
newDataSet = typeCoerceDataSet(items);
}
else {
Expand Down Expand Up @@ -236,7 +236,7 @@ Graph2d.prototype.setGroups = function(groups) {
if (!groups) {
newDataSet = null;
}
else if (isDataViewLike("id", groups)) {
else if (isDataViewLike(groups)) {
newDataSet = groups;
}
else {
Expand Down
8 changes: 4 additions & 4 deletions lib/timeline/Timeline.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import moment from '../module/moment';
import util, { typeCoerceDataSet } from '../util';
import { DataSet, DataView, isDataViewLike } from 'vis-data/esnext';
import util, { typeCoerceDataSet, isDataViewLike } from '../util';
import { DataSet, DataView } from 'vis-data/esnext';
import Range from './Range';
import Core from './Core';
import TimeAxis from './component/TimeAxis';
Expand Down Expand Up @@ -37,7 +37,7 @@ export default class Timeline extends Core {
}

// if the third element is options, the forth is groups (optionally);
if (!(Array.isArray(groups) || isDataViewLike("id", groups)) && groups instanceof Object) {
if (!(Array.isArray(groups) || isDataViewLike(groups)) && groups instanceof Object) {
const forthArgument = options;
options = groups;
groups = forthArgument;
Expand Down Expand Up @@ -317,7 +317,7 @@ export default class Timeline extends Core {
if (!items) {
newDataSet = null;
}
else if (isDataViewLike("id", items)) {
else if (isDataViewLike(items)) {
newDataSet = typeCoerceDataSet(items);
}
else {
Expand Down
8 changes: 4 additions & 4 deletions lib/timeline/component/ItemSet.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Hammer from '../../module/hammer';
import util, { typeCoerceDataSet, randomUUID } from '../../util';
import { DataSet, DataView, isDataViewLike } from 'vis-data/esnext';
import util, { typeCoerceDataSet, randomUUID, isDataViewLike } from '../../util';
import { DataSet, DataView } from 'vis-data/esnext';
import TimeStep from '../TimeStep';
import Component from './Component';
import Group from './Group';
Expand Down Expand Up @@ -1000,7 +1000,7 @@ class ItemSet extends Component {
if (!items) {
this.itemsData = null;
}
else if (isDataViewLike("id", items)) {
else if (isDataViewLike(items)) {
this.itemsData = typeCoerceDataSet(items);
}
else {
Expand Down Expand Up @@ -1071,7 +1071,7 @@ class ItemSet extends Component {
if (!groups) {
this.groupsData = null;
}
else if (isDataViewLike("id", groups)) {
else if (isDataViewLike(groups)) {
this.groupsData = groups;
}
else {
Expand Down
8 changes: 4 additions & 4 deletions lib/timeline/component/LineGraph.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import util, { typeCoerceDataSet, randomUUID } from '../../util';
import util, { typeCoerceDataSet, randomUUID, isDataViewLike } from '../../util';
import * as DOMutil from '../../DOMutil';
import { DataSet, DataView, isDataViewLike } from 'vis-data/esnext';
import { DataSet, DataView } from 'vis-data/esnext';
import Component from './Component';
import DataAxis from './DataAxis';
import GraphGroup from './GraphGroup';
Expand Down Expand Up @@ -251,7 +251,7 @@ LineGraph.prototype.setItems = function (items) {
if (!items) {
this.itemsData = null;
}
else if (isDataViewLike("id", items)) {
else if (isDataViewLike(items)) {
this.itemsData = typeCoerceDataSet(items);
}
else {
Expand Down Expand Up @@ -312,7 +312,7 @@ LineGraph.prototype.setGroups = function (groups) {
if (!groups) {
this.groupsData = null;
}
else if (isDataViewLike("id", groups)) {
else if (isDataViewLike(groups)) {
this.groupsData = groups;
}
else {
Expand Down
15 changes: 15 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,26 @@ export * from "vis-util/esnext";
import * as util from "vis-util/esnext";
import { getType, isNumber, isString } from "vis-util/esnext";
import { DataSet, createNewDataPipeFrom } from "vis-data/esnext";
import {isDataViewLike as isDataViewLikeUpstream} from "vis-data/esnext";

import moment from "moment";
import xssFilter from 'xss';

export { v4 as randomUUID } from "uuid";
/**
* Test if an object implements the DataView interface from vis-data.
* Uses the idProp property instead of expecting a hardcoded id field "id".
*/
export function isDataViewLike(obj) {
if(!obj) {
return false;
}
let idProp = obj._idProp;
if(!idProp) {
return false;
}
return isDataViewLikeUpstream(idProp, obj);
}

// parse ASP.Net Date pattern,
// for example '/Date(1198908717056)/' or '/Date(1198908717056-0700)/'
Expand Down
14 changes: 14 additions & 0 deletions test/Timeline.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import assert from 'assert'
import jsdom_global from 'jsdom-global'
import { DataSet } from'vis-data/esnext'
import Timeline from'../lib/timeline/Timeline'
Expand Down Expand Up @@ -50,4 +51,17 @@ describe('Timeline', () => {

timeline.setItems(null);
});
it("setItems(with custom ID property) should work", function() {
const timeline = new Timeline(document.createElement("div"), []);
const events = [
{start: new Date(), fooid: 1},
{start: new Date(), fooid: 2}
]
const dataSet = new DataSet(events,{fieldId:"fooid"});
timeline.setItems(dataSet);
timeline.setSelection([2], {animation: false});
const selectedIds = timeline.getSelection();
assert( selectedIds.length === 1 )
assert( dataSet.get(selectedIds[0]).fooid === 2 )
});
});

0 comments on commit 327fd3d

Please sign in to comment.