Allow layer source to be set

This commit is contained in:
Tim Schaub
2014-10-21 22:32:25 -06:00
parent 773ac433ce
commit 88030dbce7
3 changed files with 93 additions and 8 deletions

View File

@@ -5,7 +5,7 @@ goog.require('goog.events');
goog.require('goog.events.EventType');
goog.require('goog.object');
goog.require('ol.layer.Base');
goog.require('ol.source.Source');
goog.require('ol.layer.LayerProperty');
@@ -33,13 +33,11 @@ ol.layer.Layer = function(options) {
/**
* @private
* @type {ol.source.Source}
* @type {goog.events.Key}
*/
this.source_ = options.source;
goog.events.listen(this.source_, goog.events.EventType.CHANGE,
this.handleSourceChange_, false, this);
this.sourceChangeKey_ = null;
this.setSource(options.source);
};
goog.inherits(ol.layer.Layer, ol.layer.Base);
@@ -79,12 +77,20 @@ ol.layer.Layer.prototype.getLayerStatesArray = function(opt_states) {
/**
* Get the layer source.
* @return {ol.source.Source} Source.
* @observable
* @api stable
*/
ol.layer.Layer.prototype.getSource = function() {
return this.source_;
var source = this.get(ol.layer.LayerProperty.SOURCE);
return goog.isDef(source) ?
/** @type {ol.source.Source} */ (source) : null;
};
goog.exportProperty(
ol.layer.Layer.prototype,
'getSource',
ol.layer.Layer.prototype.getSource);
/**
@@ -101,3 +107,25 @@ ol.layer.Layer.prototype.getSourceState = function() {
ol.layer.Layer.prototype.handleSourceChange_ = function() {
this.changed();
};
/**
* Set the layer source.
* @param {ol.source.Source} source The layer source.
* @observable
* @api stable
*/
ol.layer.Layer.prototype.setSource = function(source) {
if (!goog.isNull(this.sourceChangeKey_)) {
goog.events.unlistenByKey(this.sourceChangeKey_);
this.sourceChangeKey_ = null;
}
this.sourceChangeKey_ = goog.events.listen(source,
goog.events.EventType.CHANGE, this.handleSourceChange_, false, this);
this.set(ol.layer.LayerProperty.SOURCE, source);
this.changed();
};
goog.exportProperty(
ol.layer.Layer.prototype,
'setSource',
ol.layer.Layer.prototype.setSource);

View File

@@ -21,7 +21,8 @@ ol.layer.LayerProperty = {
VISIBLE: 'visible',
EXTENT: 'extent',
MAX_RESOLUTION: 'maxResolution',
MIN_RESOLUTION: 'minResolution'
MIN_RESOLUTION: 'minResolution',
SOURCE: 'source'
};

View File

@@ -243,6 +243,62 @@ describe('ol.layer.Layer', function() {
});
describe('#getSource', function() {
it('gets the layer source', function() {
var source = new ol.source.Source({projection: ol.proj.get('EPSG:4326')});
var layer = new ol.layer.Layer({source: source});
expect(layer.getSource()).to.be(source);
});
});
describe('#setSource()', function() {
var projection = ol.proj.get('EPSG:4326');
it('sets the layer source', function() {
var layer = new ol.layer.Layer({
source: new ol.source.Source({projection: projection})
});
var source = new ol.source.Source({projection: projection});
layer.setSource(source);
expect(layer.getSource()).to.be(source);
});
it('calls changed', function() {
var layer = new ol.layer.Layer({
source: new ol.source.Source({projection: projection})
});
sinon.spy(layer, 'changed');
var source = new ol.source.Source({projection: projection});
layer.setSource(source);
expect(layer.changed.calledOnce).to.be(true);
});
it('sets up event listeners', function() {
sinon.spy(ol.layer.Layer.prototype, 'handleSourceChange_');
var first = new ol.source.Source({projection: projection});
var layer = new ol.layer.Layer({source: first});
first.setState(ol.source.State.READY);
expect(layer.handleSourceChange_.calledOnce).to.be(true);
var second = new ol.source.Source({projection: projection});
layer.setSource(second);
expect(layer.handleSourceChange_.calledOnce).to.be(true);
second.setState(ol.source.State.READY);
expect(layer.handleSourceChange_.callCount).to.be(2);
// remove spy
ol.layer.Layer.prototype.handleSourceChange_.restore();
});
});
describe('#setBrightness', function() {
var layer;