Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
hpinkos committed Mar 19, 2018
1 parent 0374722 commit 5f5c5a5
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 16 deletions.
43 changes: 39 additions & 4 deletions Source/DataSources/DataSourceDisplay.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,21 @@ define([

this._dataSourceCollection = dataSourceCollection;
this._scene = scene;
this._primitives = scene.primitives.add(new PrimitiveCollection());
this._groundPrimitives = scene.primitives.add(new PrimitiveCollection());
this._visualizersCallback = defaultValue(options.visualizersCallback, DataSourceDisplay.defaultVisualizersCallback);

var primitivesAdded = false;
var primitives = new PrimitiveCollection();
var groundPrimitives = new PrimitiveCollection();

if (dataSourceCollection.length > 0) {
scene.primitives.add(primitives);
scene.groundPrimitives.add(groundPrimitives);
primitivesAdded = true;
}

this._primitives = primitives;
this._groundPrimitives = groundPrimitives;

for (var i = 0, len = dataSourceCollection.length; i < len; i++) {
this._onDataSourceAdded(dataSourceCollection, dataSourceCollection.get(i));
}
Expand All @@ -83,6 +94,25 @@ define([
this._onDataSourceAdded(undefined, defaultDataSource);
this._defaultDataSource = defaultDataSource;

var removeDefaultDataSoureListener;
var removeDataSourceCollectionListener;
if (!primitivesAdded) {
var that = this;
var addPrimitives = function() {
scene.primitives.add(primitives);
scene.groundPrimitives.add(groundPrimitives);
removeDefaultDataSoureListener();
removeDataSourceCollectionListener();
that._removeDefaultDataSoureListener = undefined;
that._removeDataSourceCollectionListener = undefined;
};
removeDefaultDataSoureListener = defaultDataSource.entities.collectionChanged.addEventListener(addPrimitives);
removeDataSourceCollectionListener = dataSourceCollection.dataSourceAdded.addEventListener(addPrimitives);
}

this._removeDefaultDataSoureListener = removeDefaultDataSoureListener;
this._removeDataSourceCollectionListener = removeDataSourceCollectionListener;

this._ready = false;
}

Expand Down Expand Up @@ -190,8 +220,13 @@ define([
}
this._onDataSourceRemoved(undefined, this._defaultDataSource);

this._scene.primitives.remove(this._primitives);
this._scene.groundPrimitives.remove(this._groundPrimitives);
if (defined(this._removeDefaultDataSoureListener)) {
this._removeDefaultDataSoureListener();
this._removeDataSourceCollectionListener();
} else {
this._scene.primitives.remove(this._primitives);
this._scene.groundPrimitives.remove(this._groundPrimitives);
}

return destroyObject(this);
};
Expand Down
110 changes: 98 additions & 12 deletions Specs/DataSources/DataSourceDisplaySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ defineSuite([
expect(display._groundPrimitives.length).toBe(1);
});

it('re-orders primitives on dataSource moved', function() {
it('raises primitives on dataSource raise', function() {
var source1 = new MockDataSource();
var source2 = new MockDataSource();
var source3 = new MockDataSource();
Expand All @@ -432,32 +432,118 @@ defineSuite([
dataSourceCollection.add(source2);
dataSourceCollection.add(source3);

expect(display._primitives.get(1)).toBe(source1._primitives);
expect(display._primitives.get(2)).toBe(source2._primitives);
expect(display._primitives.get(3)).toBe(source3._primitives);

dataSourceCollection.raise(source1);

expect(display._primitives.get(1)).toBe(source2._primitives);
expect(display._primitives.get(2)).toBe(source1._primitives);
expect(display._primitives.get(3)).toBe(source3._primitives);
});

it('lowers primitives on dataSource lower', function() {
var source1 = new MockDataSource();
var source2 = new MockDataSource();
var source3 = new MockDataSource();

display = new DataSourceDisplay({
scene : scene,
dataSourceCollection : dataSourceCollection,
visualizersCallback : visualizersCallback
});

dataSourceCollection.add(source1);
dataSourceCollection.add(source2);
dataSourceCollection.add(source3);

dataSourceCollection.lower(source1);
dataSourceCollection.lower(source3);

expect(display._primitives.get(1)).toBe(source1._primitives);
expect(display._primitives.get(2)).toBe(source2._primitives);
expect(display._primitives.get(3)).toBe(source3._primitives);
expect(display._primitives.get(2)).toBe(source3._primitives);
expect(display._primitives.get(3)).toBe(source2._primitives);
});

it('raises primitives to top on dataSource raiseToTop', function() {
var source1 = new MockDataSource();
var source2 = new MockDataSource();
var source3 = new MockDataSource();

display = new DataSourceDisplay({
scene : scene,
dataSourceCollection : dataSourceCollection,
visualizersCallback : visualizersCallback
});

dataSourceCollection.add(source1);
dataSourceCollection.add(source2);
dataSourceCollection.add(source3);

dataSourceCollection.raiseToTop(source1);

expect(display._primitives.get(1)).toBe(source2._primitives);
expect(display._primitives.get(2)).toBe(source3._primitives);
expect(display._primitives.get(3)).toBe(source1._primitives);
});

dataSourceCollection.lowerToBottom(source1);
it('lowers primitives to bottom on dataSource lowerToBottom', function() {
var source1 = new MockDataSource();
var source2 = new MockDataSource();
var source3 = new MockDataSource();

expect(display._primitives.get(1)).toBe(source1._primitives);
expect(display._primitives.get(2)).toBe(source2._primitives);
expect(display._primitives.get(3)).toBe(source3._primitives);
display = new DataSourceDisplay({
scene : scene,
dataSourceCollection : dataSourceCollection,
visualizersCallback : visualizersCallback
});

dataSourceCollection.add(source1);
dataSourceCollection.add(source2);
dataSourceCollection.add(source3);

dataSourceCollection.lowerToBottom(source3);

expect(display._primitives.get(1)).toBe(source3._primitives);
expect(display._primitives.get(2)).toBe(source1._primitives);
expect(display._primitives.get(3)).toBe(source2._primitives);
});

it('adds primitives to scene when dataSource is added to the collection', function() {
display = new DataSourceDisplay({
scene : scene,
dataSourceCollection : dataSourceCollection,
visualizersCallback : visualizersCallback
});
expect(scene.primitives.contains(display._primitives)).toBe(false);
expect(scene.groundPrimitives.contains(display._groundPrimitives)).toBe(false);

dataSourceCollection.add(new MockDataSource());

expect(scene.primitives.contains(display._primitives)).toBe(true);
expect(scene.groundPrimitives.contains(display._groundPrimitives)).toBe(true);
});

it('adds primitives to scene if dataSourceCollection is not empty', function() {
dataSourceCollection.add(new MockDataSource());
display = new DataSourceDisplay({
scene : scene,
dataSourceCollection : dataSourceCollection,
visualizersCallback : visualizersCallback
});

expect(scene.primitives.contains(display._primitives)).toBe(true);
expect(scene.groundPrimitives.contains(display._groundPrimitives)).toBe(true);
});

it('adds primitives to the scene when entities are added to the default dataSource', function() {
display = new DataSourceDisplay({
scene : scene,
dataSourceCollection : dataSourceCollection,
visualizersCallback : visualizersCallback
});
expect(scene.primitives.contains(display._primitives)).toBe(false);
expect(scene.groundPrimitives.contains(display._groundPrimitives)).toBe(false);

display.defaultDataSource.entities.add(new Entity());

expect(scene.primitives.contains(display._primitives)).toBe(true);
expect(scene.groundPrimitives.contains(display._groundPrimitives)).toBe(true);
});
}, 'WebGL');

0 comments on commit 5f5c5a5

Please sign in to comment.